babylon.shadowGenerator.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. var effectiveRender = (useBones: boolean, mesh: AbstractMesh, renderMesh:Mesh, subMesh: SubMesh): void => {
  28. // World
  29. var world = mesh.getWorldMatrix();
  30. if (useBones) {
  31. this._effect.setMatrix("world", world);
  32. } else {
  33. world.multiplyToRef(this.getTransformMatrix(), this._worldViewProjection);
  34. this._effect.setMatrix("worldViewProjection", this._worldViewProjection);
  35. }
  36. // Draw
  37. renderMesh._draw(subMesh, true);
  38. }
  39. // Custom render function
  40. var renderSubMesh = (subMesh: SubMesh): void => {
  41. var mesh = subMesh.getRenderingMesh();
  42. var scene = this._scene;
  43. var engine = scene.getEngine();
  44. if (this.isReady(mesh)) {
  45. // Managing instances
  46. var batch = mesh._getInstancesRenderList();
  47. if (batch.mustReturn) {
  48. return;
  49. }
  50. engine.enableEffect(this._effect);
  51. mesh._bind(subMesh, this._effect, false);
  52. // Alpha test
  53. if (mesh.material && mesh.material.needAlphaTesting()) {
  54. var alphaTexture = mesh.material.getAlphaTestTexture();
  55. this._effect.setTexture("diffuseSampler", alphaTexture);
  56. this._effect.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
  57. }
  58. // Bones
  59. var useBones = mesh.skeleton && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind);
  60. if (useBones) {
  61. this._effect.setMatrix("viewProjection", this.getTransformMatrix());
  62. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  63. }
  64. if (batch.renderSelf) {
  65. effectiveRender(useBones, mesh, mesh, subMesh);
  66. }
  67. if (batch.visibleInstances) {
  68. for (var instanceIndex = 0; instanceIndex < batch.visibleInstances.length; instanceIndex++) {
  69. var instance = batch.visibleInstances[instanceIndex];
  70. effectiveRender(useBones, instance, mesh, subMesh);
  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): 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);
  117. }
  118. // Get correct effect
  119. var join = defines.join("\n");
  120. if (this._cachedDefines != join) {
  121. this._cachedDefines = join;
  122. this._effect = this._scene.getEngine().createEffect("shadowMap",
  123. attribs,
  124. ["world", "mBones", "viewProjection", "worldViewProjection", "diffuseMatrix"],
  125. ["diffuseSampler"], join);
  126. }
  127. return this._effect.isReady();
  128. }
  129. public getShadowMap(): RenderTargetTexture {
  130. return this._shadowMap;
  131. }
  132. public getLight() : DirectionalLight{
  133. return this._light;
  134. }
  135. // Methods
  136. public getTransformMatrix(): Matrix {
  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. public getDarkness(): number {
  153. return this._darkness;
  154. }
  155. public setDarkness(darkness: number): void {
  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. public setTransparencyShadow(hasShadow: boolean): void {
  164. this._transparencyShadow = hasShadow;
  165. }
  166. public dispose(): void {
  167. this._shadowMap.dispose();
  168. }
  169. }
  170. }