babylon.shadowGenerator.js 19 KB

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