babylon.shadowGenerator.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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[subMesh._id] !== null);
  65. if (this.isReady(subMesh, hardwareInstancedRendering)) {
  66. engine.enableEffect(this._effect);
  67. mesh._bind(subMesh, this._effect, false);
  68. var material = subMesh.getMaterial();
  69. this._effect.setMatrix("viewProjection", this.getTransformMatrix());
  70. // Alpha test
  71. if (material && material.needAlphaTesting()) {
  72. var alphaTexture = material.getAlphaTestTexture();
  73. this._effect.setTexture("diffuseSampler", alphaTexture);
  74. this._effect.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
  75. }
  76. // Bones
  77. var useBones = mesh.skeleton && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind);
  78. if (useBones) {
  79. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  80. }
  81. if (hardwareInstancedRendering) {
  82. mesh._renderWithInstances(subMesh, false, batch, this._effect, engine);
  83. } else {
  84. if (batch.renderSelf[subMesh._id]) {
  85. this._effect.setMatrix("world", mesh.getWorldMatrix());
  86. // Draw
  87. mesh._draw(subMesh, true);
  88. }
  89. if (batch.visibleInstances[subMesh._id]) {
  90. for (var instanceIndex = 0; instanceIndex < batch.visibleInstances[subMesh._id].length; instanceIndex++) {
  91. var instance = batch.visibleInstances[subMesh._id][instanceIndex];
  92. this._effect.setMatrix("world", instance.getWorldMatrix());
  93. // Draw
  94. mesh._draw(subMesh, true);
  95. }
  96. }
  97. }
  98. } else {
  99. // Need to reset refresh rate of the shadowMap
  100. this._shadowMap.resetRefreshCounter();
  101. }
  102. };
  103. this._shadowMap.customRenderFunction = (opaqueSubMeshes: SmartArray<SubMesh>, alphaTestSubMeshes: SmartArray<SubMesh>, transparentSubMeshes: SmartArray<SubMesh>): void => {
  104. var index;
  105. for (index = 0; index < opaqueSubMeshes.length; index++) {
  106. renderSubMesh(opaqueSubMeshes.data[index]);
  107. }
  108. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  109. renderSubMesh(alphaTestSubMeshes.data[index]);
  110. }
  111. if (this._transparencyShadow) {
  112. for (index = 0; index < transparentSubMeshes.length; index++) {
  113. renderSubMesh(transparentSubMeshes.data[index]);
  114. }
  115. }
  116. };
  117. }
  118. public isReady(subMesh: SubMesh, useInstances: boolean): boolean {
  119. var defines = [];
  120. if (this.useVarianceShadowMap) {
  121. defines.push("#define VSM");
  122. }
  123. var attribs = [BABYLON.VertexBuffer.PositionKind];
  124. var mesh = subMesh.getMesh();
  125. var material = subMesh.getMaterial();
  126. // Alpha test
  127. if (material && material.needAlphaTesting()) {
  128. defines.push("#define ALPHATEST");
  129. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  130. attribs.push(BABYLON.VertexBuffer.UVKind);
  131. defines.push("#define UV1");
  132. }
  133. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  134. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  135. defines.push("#define UV2");
  136. }
  137. }
  138. // Bones
  139. if (mesh.skeleton && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
  140. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  141. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  142. defines.push("#define BONES");
  143. defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
  144. }
  145. // Instances
  146. if (useInstances) {
  147. defines.push("#define INSTANCES");
  148. attribs.push("world0");
  149. attribs.push("world1");
  150. attribs.push("world2");
  151. attribs.push("world3");
  152. }
  153. // Get correct effect
  154. var join = defines.join("\n");
  155. if (this._cachedDefines != join) {
  156. this._cachedDefines = join;
  157. this._effect = this._scene.getEngine().createEffect("shadowMap",
  158. attribs,
  159. ["world", "mBones", "viewProjection", "diffuseMatrix"],
  160. ["diffuseSampler"], join);
  161. }
  162. return this._effect.isReady();
  163. }
  164. public getShadowMap(): RenderTargetTexture {
  165. return this._shadowMap;
  166. }
  167. public getLight(): DirectionalLight {
  168. return this._light;
  169. }
  170. // Methods
  171. public getTransformMatrix(): Matrix {
  172. var lightPosition = this._light.position;
  173. var lightDirection = this._light.direction;
  174. if (this._light._computeTransformedPosition()) {
  175. lightPosition = this._light._transformedPosition;
  176. }
  177. if (!this._cachedPosition || !this._cachedDirection || !lightPosition.equals(this._cachedPosition) || !lightDirection.equals(this._cachedDirection)) {
  178. this._cachedPosition = lightPosition.clone();
  179. this._cachedDirection = lightDirection.clone();
  180. var activeCamera = this._scene.activeCamera;
  181. BABYLON.Matrix.LookAtLHToRef(lightPosition, this._light.position.add(lightDirection), BABYLON.Vector3.Up(), this._viewMatrix);
  182. BABYLON.Matrix.PerspectiveFovLHToRef(Math.PI / 2.0, 1.0, activeCamera.minZ, activeCamera.maxZ, this._projectionMatrix);
  183. this._viewMatrix.multiplyToRef(this._projectionMatrix, this._transformMatrix);
  184. }
  185. return this._transformMatrix;
  186. }
  187. public getDarkness(): number {
  188. return this._darkness;
  189. }
  190. public setDarkness(darkness: number): void {
  191. if (darkness >= 1.0)
  192. this._darkness = 1.0;
  193. else if (darkness <= 0.0)
  194. this._darkness = 0.0;
  195. else
  196. this._darkness = darkness;
  197. }
  198. public setTransparencyShadow(hasShadow: boolean): void {
  199. this._transparencyShadow = hasShadow;
  200. }
  201. public dispose(): void {
  202. this._shadowMap.dispose();
  203. }
  204. }
  205. }