babylon.shadowGenerator.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var ShadowGenerator = (function () {
  4. function ShadowGenerator(mapSize, light) {
  5. var _this = this;
  6. // Members
  7. this.useVarianceShadowMap = true;
  8. this._darkness = 0;
  9. this._transparencyShadow = false;
  10. this._viewMatrix = BABYLON.Matrix.Zero();
  11. this._projectionMatrix = BABYLON.Matrix.Zero();
  12. this._transformMatrix = BABYLON.Matrix.Zero();
  13. this._worldViewProjection = BABYLON.Matrix.Zero();
  14. this._light = light;
  15. this._scene = light.getScene();
  16. light._shadowGenerator = this;
  17. // Render target
  18. this._shadowMap = new BABYLON.RenderTargetTexture(light.name + "_shadowMap", mapSize, this._scene, false);
  19. this._shadowMap.wrapU = BABYLON.Texture.CLAMP_ADDRESSMODE;
  20. this._shadowMap.wrapV = BABYLON.Texture.CLAMP_ADDRESSMODE;
  21. this._shadowMap.renderParticles = false;
  22. // Custom render function
  23. var renderSubMesh = function (subMesh) {
  24. var mesh = subMesh.getRenderingMesh();
  25. var scene = _this._scene;
  26. var engine = scene.getEngine();
  27. // Managing instances
  28. var batch = mesh._getInstancesRenderList();
  29. if (batch.mustReturn) {
  30. return;
  31. }
  32. var hardwareInstancedRendering = (engine.getCaps().instancedArrays !== null) && (batch.visibleInstances !== null);
  33. if (_this.isReady(mesh, hardwareInstancedRendering)) {
  34. engine.enableEffect(_this._effect);
  35. mesh._bind(subMesh, _this._effect, false);
  36. _this._effect.setMatrix("viewProjection", _this.getTransformMatrix());
  37. // Alpha test
  38. if (mesh.material && mesh.material.needAlphaTesting()) {
  39. var alphaTexture = mesh.material.getAlphaTestTexture();
  40. _this._effect.setTexture("diffuseSampler", alphaTexture);
  41. _this._effect.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
  42. }
  43. // Bones
  44. var useBones = mesh.skeleton && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind);
  45. if (useBones) {
  46. _this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  47. }
  48. if (hardwareInstancedRendering) {
  49. mesh._renderWithInstances(subMesh, false, batch, _this._effect, engine);
  50. } else {
  51. if (batch.renderSelf) {
  52. _this._effect.setMatrix("world", mesh.getWorldMatrix());
  53. // Draw
  54. mesh._draw(subMesh, true);
  55. }
  56. if (batch.visibleInstances) {
  57. for (var instanceIndex = 0; instanceIndex < batch.visibleInstances.length; instanceIndex++) {
  58. var instance = batch.visibleInstances[instanceIndex];
  59. _this._effect.setMatrix("world", instance.getWorldMatrix());
  60. // Draw
  61. mesh._draw(subMesh, true);
  62. }
  63. }
  64. }
  65. } else {
  66. // Need to reset refresh rate of the shadowMap
  67. _this._shadowMap.resetRefreshCounter();
  68. }
  69. };
  70. this._shadowMap.customRenderFunction = function (opaqueSubMeshes, alphaTestSubMeshes, transparentSubMeshes) {
  71. var index;
  72. for (index = 0; index < opaqueSubMeshes.length; index++) {
  73. renderSubMesh(opaqueSubMeshes.data[index]);
  74. }
  75. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  76. renderSubMesh(alphaTestSubMeshes.data[index]);
  77. }
  78. if (_this._transparencyShadow) {
  79. for (index = 0; index < transparentSubMeshes.length; index++) {
  80. renderSubMesh(transparentSubMeshes.data[index]);
  81. }
  82. }
  83. };
  84. }
  85. ShadowGenerator.prototype.isReady = function (mesh, useInstances) {
  86. var defines = [];
  87. if (this.useVarianceShadowMap) {
  88. defines.push("#define VSM");
  89. }
  90. var attribs = [BABYLON.VertexBuffer.PositionKind];
  91. // Alpha test
  92. if (mesh.material && mesh.material.needAlphaTesting()) {
  93. defines.push("#define ALPHATEST");
  94. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  95. attribs.push(BABYLON.VertexBuffer.UVKind);
  96. defines.push("#define UV1");
  97. }
  98. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  99. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  100. defines.push("#define UV2");
  101. }
  102. }
  103. // Bones
  104. if (mesh.skeleton && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
  105. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  106. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  107. defines.push("#define BONES");
  108. defines.push("#define BonesPerMesh " + mesh.skeleton.bones.length);
  109. }
  110. // Instances
  111. if (useInstances) {
  112. defines.push("#define INSTANCES");
  113. attribs.push("world");
  114. }
  115. // Get correct effect
  116. var join = defines.join("\n");
  117. if (this._cachedDefines != join) {
  118. this._cachedDefines = join;
  119. this._effect = this._scene.getEngine().createEffect("shadowMap", attribs, ["world", "mBones", "viewProjection", "diffuseMatrix"], ["diffuseSampler"], join);
  120. }
  121. return this._effect.isReady();
  122. };
  123. ShadowGenerator.prototype.getShadowMap = function () {
  124. return this._shadowMap;
  125. };
  126. ShadowGenerator.prototype.getLight = function () {
  127. return this._light;
  128. };
  129. // Methods
  130. ShadowGenerator.prototype.getTransformMatrix = function () {
  131. var lightPosition = this._light.position;
  132. var lightDirection = this._light.direction;
  133. if (this._light._computeTransformedPosition()) {
  134. lightPosition = this._light._transformedPosition;
  135. }
  136. if (!this._cachedPosition || !this._cachedDirection || !lightPosition.equals(this._cachedPosition) || !lightDirection.equals(this._cachedDirection)) {
  137. this._cachedPosition = lightPosition.clone();
  138. this._cachedDirection = lightDirection.clone();
  139. var activeCamera = this._scene.activeCamera;
  140. BABYLON.Matrix.LookAtLHToRef(lightPosition, this._light.position.add(lightDirection), BABYLON.Vector3.Up(), this._viewMatrix);
  141. BABYLON.Matrix.PerspectiveFovLHToRef(Math.PI / 2.0, 1.0, activeCamera.minZ, activeCamera.maxZ, this._projectionMatrix);
  142. this._viewMatrix.multiplyToRef(this._projectionMatrix, this._transformMatrix);
  143. }
  144. return this._transformMatrix;
  145. };
  146. ShadowGenerator.prototype.getDarkness = function () {
  147. return this._darkness;
  148. };
  149. ShadowGenerator.prototype.setDarkness = function (darkness) {
  150. if (darkness >= 1.0)
  151. this._darkness = 1.0;
  152. else if (darkness <= 0.0)
  153. this._darkness = 0.0;
  154. else
  155. this._darkness = darkness;
  156. };
  157. ShadowGenerator.prototype.setTransparencyShadow = function (hasShadow) {
  158. this._transparencyShadow = hasShadow;
  159. };
  160. ShadowGenerator.prototype.dispose = function () {
  161. this._shadowMap.dispose();
  162. };
  163. return ShadowGenerator;
  164. })();
  165. BABYLON.ShadowGenerator = ShadowGenerator;
  166. })(BABYLON || (BABYLON = {}));
  167. //# sourceMappingURL=babylon.shadowGenerator.js.map