babylon.shadowGenerator.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. // Custom render function
  23. var renderSubMesh = function (subMesh) {
  24. var mesh = subMesh.getRenderingMesh();
  25. var scene = _this._scene;
  26. var engine = scene.getEngine();
  27. // Managing instances
  28. var batch = mesh._getInstancesRenderList();
  29. if (batch.mustReturn) {
  30. return;
  31. }
  32. var hardwareInstancedRendering = (engine.getCaps().instancedArrays !== null) && (batch.visibleInstances !== null);
  33. if (_this.isReady(mesh, hardwareInstancedRendering)) {
  34. engine.enableEffect(_this._effect);
  35. mesh._bind(subMesh, _this._effect, false);
  36. _this._effect.setMatrix("viewProjection", _this.getTransformMatrix());
  37. // Alpha test
  38. if (mesh.material && mesh.material.needAlphaTesting()) {
  39. var alphaTexture = mesh.material.getAlphaTestTexture();
  40. _this._effect.setTexture("diffuseSampler", alphaTexture);
  41. _this._effect.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
  42. }
  43. // Bones
  44. var useBones = mesh.skeleton && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind);
  45. if (useBones) {
  46. _this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  47. }
  48. if (hardwareInstancedRendering) {
  49. mesh._renderWithInstances(subMesh, false, batch, _this._effect, engine);
  50. } else {
  51. if (batch.renderSelf) {
  52. _this._effect.setMatrix("world", mesh.getWorldMatrix());
  53. // Draw
  54. mesh._draw(subMesh, true);
  55. }
  56. if (batch.visibleInstances) {
  57. for (var instanceIndex = 0; instanceIndex < batch.visibleInstances.length; instanceIndex++) {
  58. var instance = batch.visibleInstances[instanceIndex];
  59. _this._effect.setMatrix("world", instance.getWorldMatrix());
  60. // Draw
  61. mesh._draw(subMesh, true);
  62. }
  63. }
  64. }
  65. } else {
  66. // Need to reset refresh rate of the shadowMap
  67. _this._shadowMap.resetRefreshCounter();
  68. }
  69. };
  70. this._shadowMap.customRenderFunction = function (opaqueSubMeshes, alphaTestSubMeshes, transparentSubMeshes) {
  71. var index;
  72. for (index = 0; index < opaqueSubMeshes.length; index++) {
  73. renderSubMesh(opaqueSubMeshes.data[index]);
  74. }
  75. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  76. renderSubMesh(alphaTestSubMeshes.data[index]);
  77. }
  78. if (_this._transparencyShadow) {
  79. for (index = 0; index < transparentSubMeshes.length; index++) {
  80. renderSubMesh(transparentSubMeshes.data[index]);
  81. }
  82. }
  83. };
  84. }
  85. ShadowGenerator.prototype.isReady = function (mesh, useInstances) {
  86. var defines = [];
  87. if (this.useVarianceShadowMap) {
  88. defines.push("#define VSM");
  89. }
  90. var attribs = [BABYLON.VertexBuffer.PositionKind];
  91. // Alpha test
  92. if (mesh.material && mesh.material.needAlphaTesting()) {
  93. defines.push("#define ALPHATEST");
  94. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  95. attribs.push(BABYLON.VertexBuffer.UVKind);
  96. defines.push("#define UV1");
  97. }
  98. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  99. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  100. defines.push("#define UV2");
  101. }
  102. }
  103. // Bones
  104. if (mesh.skeleton && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
  105. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  106. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  107. defines.push("#define BONES");
  108. defines.push("#define BonesPerMesh " + mesh.skeleton.bones.length);
  109. }
  110. // Instances
  111. if (useInstances) {
  112. defines.push("#define INSTANCES");
  113. attribs.push("world0");
  114. attribs.push("world1");
  115. attribs.push("world2");
  116. attribs.push("world3");
  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", attribs, ["world", "mBones", "viewProjection", "diffuseMatrix"], ["diffuseSampler"], join);
  123. }
  124. return this._effect.isReady();
  125. };
  126. ShadowGenerator.prototype.getShadowMap = function () {
  127. return this._shadowMap;
  128. };
  129. ShadowGenerator.prototype.getLight = function () {
  130. return this._light;
  131. };
  132. // Methods
  133. ShadowGenerator.prototype.getTransformMatrix = function () {
  134. var lightPosition = this._light.position;
  135. var lightDirection = this._light.direction;
  136. if (this._light._computeTransformedPosition()) {
  137. lightPosition = this._light._transformedPosition;
  138. }
  139. if (!this._cachedPosition || !this._cachedDirection || !lightPosition.equals(this._cachedPosition) || !lightDirection.equals(this._cachedDirection)) {
  140. this._cachedPosition = lightPosition.clone();
  141. this._cachedDirection = lightDirection.clone();
  142. var activeCamera = this._scene.activeCamera;
  143. BABYLON.Matrix.LookAtLHToRef(lightPosition, this._light.position.add(lightDirection), BABYLON.Vector3.Up(), this._viewMatrix);
  144. BABYLON.Matrix.PerspectiveFovLHToRef(Math.PI / 2.0, 1.0, activeCamera.minZ, activeCamera.maxZ, this._projectionMatrix);
  145. this._viewMatrix.multiplyToRef(this._projectionMatrix, this._transformMatrix);
  146. }
  147. return this._transformMatrix;
  148. };
  149. ShadowGenerator.prototype.getDarkness = function () {
  150. return this._darkness;
  151. };
  152. ShadowGenerator.prototype.setDarkness = function (darkness) {
  153. if (darkness >= 1.0)
  154. this._darkness = 1.0;
  155. else if (darkness <= 0.0)
  156. this._darkness = 0.0;
  157. else
  158. this._darkness = darkness;
  159. };
  160. ShadowGenerator.prototype.setTransparencyShadow = function (hasShadow) {
  161. this._transparencyShadow = hasShadow;
  162. };
  163. ShadowGenerator.prototype.dispose = function () {
  164. this._shadowMap.dispose();
  165. };
  166. return ShadowGenerator;
  167. })();
  168. BABYLON.ShadowGenerator = ShadowGenerator;
  169. })(BABYLON || (BABYLON = {}));
  170. //# sourceMappingURL=babylon.shadowGenerator.js.map