babylon.shadowGenerator.js 16 KB

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