babylon.shadowGenerator.js 16 KB

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