babylon.shadowGenerator.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.getMesh();
  30. var world = mesh.getWorldMatrix();
  31. var engine = this._scene.getEngine();
  32. if (this.isReady(mesh)) {
  33. engine.enableEffect(this._effect);
  34. // Bones
  35. if (mesh.skeleton && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
  36. this._effect.setMatrix("world", world);
  37. this._effect.setMatrix("viewProjection", this.getTransformMatrix());
  38. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  39. } else {
  40. world.multiplyToRef(this.getTransformMatrix(), this._worldViewProjection);
  41. this._effect.setMatrix("worldViewProjection", this._worldViewProjection);
  42. }
  43. // Bind and draw
  44. mesh.bindAndDraw(subMesh, this._effect, false);
  45. }
  46. };
  47. this._shadowMap.customRenderFunction = (opaqueSubMeshes: SmartArray, alphaTestSubMeshes: SmartArray, transparentSubMeshes: SmartArray): void => {
  48. var index;
  49. for (index = 0; index < opaqueSubMeshes.length; index++) {
  50. renderSubMesh(opaqueSubMeshes.data[index]);
  51. }
  52. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  53. renderSubMesh(alphaTestSubMeshes.data[index]);
  54. }
  55. if (this._transparencyShadow) {
  56. for (index = 0; index < transparentSubMeshes.length; index++) {
  57. renderSubMesh(transparentSubMeshes.data[index]);
  58. }
  59. }
  60. };
  61. }
  62. public isReady(mesh: Mesh): boolean {
  63. var defines = [];
  64. if (this.useVarianceShadowMap) {
  65. defines.push("#define VSM");
  66. }
  67. var attribs = [BABYLON.VertexBuffer.PositionKind];
  68. if (mesh.skeleton && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
  69. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  70. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  71. defines.push("#define BONES");
  72. defines.push("#define BonesPerMesh " + mesh.skeleton.bones.length);
  73. }
  74. // Get correct effect
  75. var join = defines.join("\n");
  76. if (this._cachedDefines != join) {
  77. this._cachedDefines = join;
  78. this._effect = this._scene.getEngine().createEffect("shadowMap",
  79. attribs,
  80. ["world", "mBones", "viewProjection", "worldViewProjection"],
  81. [], join);
  82. }
  83. return this._effect.isReady();
  84. }
  85. public getShadowMap(): RenderTargetTexture {
  86. return this._shadowMap;
  87. }
  88. public getLight() : DirectionalLight{
  89. return this._light;
  90. }
  91. // Methods
  92. public getTransformMatrix(): Matrix {
  93. var lightPosition = this._light.position;
  94. var lightDirection = this._light.direction;
  95. if (this._light._computeTransformedPosition()) {
  96. lightPosition = this._light._transformedPosition;
  97. }
  98. if (!this._cachedPosition || !this._cachedDirection || !lightPosition.equals(this._cachedPosition) || !lightDirection.equals(this._cachedDirection)) {
  99. this._cachedPosition = lightPosition.clone();
  100. this._cachedDirection = lightDirection.clone();
  101. var activeCamera = this._scene.activeCamera;
  102. BABYLON.Matrix.LookAtLHToRef(lightPosition, this._light.position.add(lightDirection), BABYLON.Vector3.Up(), this._viewMatrix);
  103. BABYLON.Matrix.PerspectiveFovLHToRef(Math.PI / 2.0, 1.0, activeCamera.minZ, activeCamera.maxZ, this._projectionMatrix);
  104. this._viewMatrix.multiplyToRef(this._projectionMatrix, this._transformMatrix);
  105. }
  106. return this._transformMatrix;
  107. }
  108. public getDarkness(): number {
  109. return this._darkness;
  110. }
  111. public setDarkness(darkness: number): void {
  112. if (darkness >= 1.0)
  113. this._darkness = 1.0;
  114. else if (darkness <= 0.0)
  115. this._darkness = 0.0;
  116. else
  117. this._darkness = darkness;
  118. }
  119. public setTransparencyShadow(hasShadow: boolean): void {
  120. this._transparencyShadow = hasShadow;
  121. }
  122. public dispose(): void {
  123. this._shadowMap.dispose();
  124. }
  125. }
  126. }