babylon.shadowGenerator.ts 8.7 KB

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