babylon.shadowGenerator.js 11 KB

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