babylon.shadowGenerator.ts 8.5 KB

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