babylon.shadowGenerator.js 8.7 KB

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