babylon.shadowGenerator.ts 9.9 KB

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