babylon.shadowGenerator.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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_NONE;
  8. this.blurScale = 2;
  9. this._blurBoxOffset = 0;
  10. this._darkness = 0;
  11. this._bias = 0.00005;
  12. this._transparencyShadow = false;
  13. this._viewMatrix = BABYLON.Matrix.Zero();
  14. this._projectionMatrix = BABYLON.Matrix.Zero();
  15. this._transformMatrix = BABYLON.Matrix.Zero();
  16. this._worldViewProjection = BABYLON.Matrix.Zero();
  17. this._light = light;
  18. this._scene = light.getScene();
  19. this._mapSize = mapSize;
  20. light._shadowGenerator = this;
  21. // Render target
  22. this._shadowMap = new BABYLON.RenderTargetTexture(light.name + "_shadowMap", mapSize, this._scene, false);
  23. this._shadowMap.wrapU = BABYLON.Texture.CLAMP_ADDRESSMODE;
  24. this._shadowMap.wrapV = BABYLON.Texture.CLAMP_ADDRESSMODE;
  25. this._shadowMap.renderParticles = false;
  26. this._shadowMap.onAfterUnbind = function () {
  27. if (!_this.useBlurVarianceShadowMap) {
  28. return;
  29. }
  30. if (!_this._shadowMap2) {
  31. _this._shadowMap2 = new BABYLON.RenderTargetTexture(light.name + "_shadowMap", mapSize, _this._scene, false);
  32. _this._shadowMap2.wrapU = BABYLON.Texture.CLAMP_ADDRESSMODE;
  33. _this._shadowMap2.wrapV = BABYLON.Texture.CLAMP_ADDRESSMODE;
  34. _this._downSamplePostprocess = new BABYLON.PassPostProcess("downScale", 1.0 / _this.blurScale, null, BABYLON.Texture.NEAREST_SAMPLINGMODE, _this._scene.getEngine());
  35. _this._downSamplePostprocess.onApply = function (effect) {
  36. effect.setTexture("textureSampler", _this._shadowMap);
  37. };
  38. _this.blurBoxOffset = 1;
  39. }
  40. _this._scene.postProcessManager.directRender([_this._downSamplePostprocess, _this._boxBlurPostprocess], _this._shadowMap2.getInternalTexture());
  41. };
  42. // Custom render function
  43. var renderSubMesh = function (subMesh) {
  44. var mesh = subMesh.getRenderingMesh();
  45. var scene = _this._scene;
  46. var engine = scene.getEngine();
  47. // Culling
  48. engine.setState(subMesh.getMaterial().backFaceCulling);
  49. // Managing instances
  50. var batch = mesh._getInstancesRenderList(subMesh._id);
  51. if (batch.mustReturn) {
  52. return;
  53. }
  54. var hardwareInstancedRendering = (engine.getCaps().instancedArrays !== null) && (batch.visibleInstances[subMesh._id] !== null);
  55. if (_this.isReady(subMesh, hardwareInstancedRendering)) {
  56. engine.enableEffect(_this._effect);
  57. mesh._bind(subMesh, _this._effect, BABYLON.Material.TriangleFillMode);
  58. var material = subMesh.getMaterial();
  59. _this._effect.setMatrix("viewProjection", _this.getTransformMatrix());
  60. // Alpha test
  61. if (material && material.needAlphaTesting()) {
  62. var alphaTexture = material.getAlphaTestTexture();
  63. _this._effect.setTexture("diffuseSampler", alphaTexture);
  64. _this._effect.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
  65. }
  66. // Bones
  67. if (mesh.useBones) {
  68. _this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  69. }
  70. // Draw
  71. mesh._processRendering(subMesh, _this._effect, BABYLON.Material.TriangleFillMode, batch, hardwareInstancedRendering, function (isInstance, world) { return _this._effect.setMatrix("world", world); });
  72. }
  73. else {
  74. // Need to reset refresh rate of the shadowMap
  75. _this._shadowMap.resetRefreshCounter();
  76. }
  77. };
  78. this._shadowMap.customRenderFunction = function (opaqueSubMeshes, alphaTestSubMeshes, transparentSubMeshes) {
  79. var index;
  80. for (index = 0; index < opaqueSubMeshes.length; index++) {
  81. renderSubMesh(opaqueSubMeshes.data[index]);
  82. }
  83. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  84. renderSubMesh(alphaTestSubMeshes.data[index]);
  85. }
  86. if (_this._transparencyShadow) {
  87. for (index = 0; index < transparentSubMeshes.length; index++) {
  88. renderSubMesh(transparentSubMeshes.data[index]);
  89. }
  90. }
  91. };
  92. this._shadowMap.onClear = function (engine) {
  93. if (_this.useBlurVarianceShadowMap || _this.useVarianceShadowMap) {
  94. engine.clear(new BABYLON.Color4(0, 0, 0, 0), true, true);
  95. }
  96. else {
  97. engine.clear(new BABYLON.Color4(1.0, 1.0, 1.0, 1.0), true, true);
  98. }
  99. };
  100. }
  101. Object.defineProperty(ShadowGenerator, "FILTER_NONE", {
  102. // Static
  103. get: function () {
  104. return ShadowGenerator._FILTER_NONE;
  105. },
  106. enumerable: true,
  107. configurable: true
  108. });
  109. Object.defineProperty(ShadowGenerator, "FILTER_VARIANCESHADOWMAP", {
  110. get: function () {
  111. return ShadowGenerator._FILTER_VARIANCESHADOWMAP;
  112. },
  113. enumerable: true,
  114. configurable: true
  115. });
  116. Object.defineProperty(ShadowGenerator, "FILTER_POISSONSAMPLING", {
  117. get: function () {
  118. return ShadowGenerator._FILTER_POISSONSAMPLING;
  119. },
  120. enumerable: true,
  121. configurable: true
  122. });
  123. Object.defineProperty(ShadowGenerator, "FILTER_BLURVARIANCESHADOWMAP", {
  124. get: function () {
  125. return ShadowGenerator._FILTER_BLURVARIANCESHADOWMAP;
  126. },
  127. enumerable: true,
  128. configurable: true
  129. });
  130. Object.defineProperty(ShadowGenerator.prototype, "blurBoxOffset", {
  131. get: function () {
  132. return this._blurBoxOffset;
  133. },
  134. set: function (value) {
  135. var _this = this;
  136. if (this._blurBoxOffset === value) {
  137. return;
  138. }
  139. this._blurBoxOffset = value;
  140. if (this._boxBlurPostprocess) {
  141. this._boxBlurPostprocess.dispose();
  142. }
  143. this._boxBlurPostprocess = new BABYLON.PostProcess("DepthBoxBlur", "depthBoxBlur", ["screenSize", "boxOffset"], [], 1.0 / this.blurScale, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, this._scene.getEngine(), false, "#define OFFSET " + value);
  144. this._boxBlurPostprocess.onApply = function (effect) {
  145. effect.setFloat2("screenSize", _this._mapSize / _this.blurScale, _this._mapSize / _this.blurScale);
  146. };
  147. },
  148. enumerable: true,
  149. configurable: true
  150. });
  151. Object.defineProperty(ShadowGenerator.prototype, "useVarianceShadowMap", {
  152. get: function () {
  153. return this.filter === ShadowGenerator.FILTER_VARIANCESHADOWMAP && this._light.supportsVSM();
  154. },
  155. set: function (value) {
  156. this.filter = (value ? ShadowGenerator.FILTER_VARIANCESHADOWMAP : ShadowGenerator.FILTER_NONE);
  157. },
  158. enumerable: true,
  159. configurable: true
  160. });
  161. Object.defineProperty(ShadowGenerator.prototype, "usePoissonSampling", {
  162. get: function () {
  163. return this.filter === ShadowGenerator.FILTER_POISSONSAMPLING || (!this._light.supportsVSM() && (this.filter === ShadowGenerator.FILTER_VARIANCESHADOWMAP || this.filter === ShadowGenerator.FILTER_BLURVARIANCESHADOWMAP));
  164. },
  165. set: function (value) {
  166. this.filter = (value ? ShadowGenerator.FILTER_POISSONSAMPLING : ShadowGenerator.FILTER_NONE);
  167. },
  168. enumerable: true,
  169. configurable: true
  170. });
  171. Object.defineProperty(ShadowGenerator.prototype, "useBlurVarianceShadowMap", {
  172. get: function () {
  173. return this.filter === ShadowGenerator.FILTER_BLURVARIANCESHADOWMAP && this._light.supportsVSM();
  174. },
  175. set: function (value) {
  176. this.filter = (value ? ShadowGenerator.FILTER_BLURVARIANCESHADOWMAP : ShadowGenerator.FILTER_NONE);
  177. },
  178. enumerable: true,
  179. configurable: true
  180. });
  181. ShadowGenerator.prototype.isReady = function (subMesh, useInstances) {
  182. var defines = [];
  183. if (this.useVarianceShadowMap || this.useBlurVarianceShadowMap) {
  184. defines.push("#define VSM");
  185. }
  186. var attribs = [BABYLON.VertexBuffer.PositionKind];
  187. var mesh = subMesh.getMesh();
  188. var material = subMesh.getMaterial();
  189. // Alpha test
  190. if (material && material.needAlphaTesting()) {
  191. defines.push("#define ALPHATEST");
  192. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  193. attribs.push(BABYLON.VertexBuffer.UVKind);
  194. defines.push("#define UV1");
  195. }
  196. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  197. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  198. defines.push("#define UV2");
  199. }
  200. }
  201. // Bones
  202. if (mesh.useBones) {
  203. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  204. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  205. defines.push("#define BONES");
  206. defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
  207. }
  208. // Instances
  209. if (useInstances) {
  210. defines.push("#define INSTANCES");
  211. attribs.push("world0");
  212. attribs.push("world1");
  213. attribs.push("world2");
  214. attribs.push("world3");
  215. }
  216. // Get correct effect
  217. var join = defines.join("\n");
  218. if (this._cachedDefines !== join) {
  219. this._cachedDefines = join;
  220. this._effect = this._scene.getEngine().createEffect("shadowMap", attribs, ["world", "mBones", "viewProjection", "diffuseMatrix"], ["diffuseSampler"], join);
  221. }
  222. return this._effect.isReady();
  223. };
  224. ShadowGenerator.prototype.getShadowMap = function () {
  225. return this._shadowMap;
  226. };
  227. ShadowGenerator.prototype.getShadowMapForRendering = function () {
  228. if (this._shadowMap2) {
  229. return this._shadowMap2;
  230. }
  231. return this._shadowMap;
  232. };
  233. ShadowGenerator.prototype.getLight = function () {
  234. return this._light;
  235. };
  236. // Methods
  237. ShadowGenerator.prototype.getTransformMatrix = function () {
  238. var scene = this._scene;
  239. if (this._currentRenderID === scene.getRenderId()) {
  240. return this._transformMatrix;
  241. }
  242. this._currentRenderID = scene.getRenderId();
  243. var lightPosition = this._light.position;
  244. var lightDirection = this._light.direction;
  245. if (this._light.computeTransformedPosition()) {
  246. lightPosition = this._light.transformedPosition;
  247. }
  248. if (this._light.needRefreshPerFrame() || !this._cachedPosition || !this._cachedDirection || !lightPosition.equals(this._cachedPosition) || !lightDirection.equals(this._cachedDirection)) {
  249. this._cachedPosition = lightPosition.clone();
  250. this._cachedDirection = lightDirection.clone();
  251. BABYLON.Matrix.LookAtLHToRef(lightPosition, this._light.position.add(lightDirection), BABYLON.Vector3.Up(), this._viewMatrix);
  252. this._light.setShadowProjectionMatrix(this._projectionMatrix, this._viewMatrix, this.getShadowMap().renderList);
  253. this._viewMatrix.multiplyToRef(this._projectionMatrix, this._transformMatrix);
  254. }
  255. return this._transformMatrix;
  256. };
  257. ShadowGenerator.prototype.getDarkness = function () {
  258. return this._darkness;
  259. };
  260. ShadowGenerator.prototype.setDarkness = function (darkness) {
  261. if (darkness >= 1.0)
  262. this._darkness = 1.0;
  263. else if (darkness <= 0.0)
  264. this._darkness = 0.0;
  265. else
  266. this._darkness = darkness;
  267. };
  268. ShadowGenerator.prototype.getBias = function () {
  269. return this._bias;
  270. };
  271. ShadowGenerator.prototype.setBias = function (bias) {
  272. this._bias = bias;
  273. };
  274. ShadowGenerator.prototype.setTransparencyShadow = function (hasShadow) {
  275. this._transparencyShadow = hasShadow;
  276. };
  277. ShadowGenerator.prototype._packHalf = function (depth) {
  278. var scale = depth * 255.0;
  279. var fract = scale - Math.floor(scale);
  280. return new BABYLON.Vector2(depth - fract / 255.0, fract);
  281. };
  282. ShadowGenerator.prototype.dispose = function () {
  283. this._shadowMap.dispose();
  284. if (this._shadowMap2) {
  285. this._shadowMap2.dispose();
  286. }
  287. if (this._downSamplePostprocess) {
  288. this._downSamplePostprocess.dispose();
  289. }
  290. if (this._boxBlurPostprocess) {
  291. this._boxBlurPostprocess.dispose();
  292. }
  293. };
  294. ShadowGenerator._FILTER_NONE = 0;
  295. ShadowGenerator._FILTER_VARIANCESHADOWMAP = 1;
  296. ShadowGenerator._FILTER_POISSONSAMPLING = 2;
  297. ShadowGenerator._FILTER_BLURVARIANCESHADOWMAP = 3;
  298. return ShadowGenerator;
  299. })();
  300. BABYLON.ShadowGenerator = ShadowGenerator;
  301. })(BABYLON || (BABYLON = {}));
  302. //# sourceMappingURL=babylon.shadowGenerator.js.map