babylon.shadowGenerator.ts 8.8 KB

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