babylon.shadowGenerator.js 10 KB

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