babylon.shadowGenerator.js 9.6 KB

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