babylon.shadowGenerator.js 8.3 KB

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