babylon.shadowGenerator.js 11 KB

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