babylon.shadowGenerator.js 8.6 KB

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