babylon.shadowGenerator.js 21 KB

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