babylon.shadowGenerator.js 20 KB

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