babylon.cellMaterial.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. "use strict";
  2. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  3. var __extends = (this && this.__extends) || (function () {
  4. var extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return function (d, b) {
  8. extendStatics(d, b);
  9. function __() { this.constructor = d; }
  10. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  11. };
  12. })();
  13. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  14. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  15. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  16. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  17. return c > 3 && r && Object.defineProperty(target, key, r), r;
  18. };
  19. var BABYLON;
  20. (function (BABYLON) {
  21. var CellMaterialDefines = /** @class */ (function (_super) {
  22. __extends(CellMaterialDefines, _super);
  23. function CellMaterialDefines() {
  24. var _this = _super.call(this) || this;
  25. _this.DIFFUSE = false;
  26. _this.CLIPPLANE = false;
  27. _this.ALPHATEST = false;
  28. _this.POINTSIZE = false;
  29. _this.FOG = false;
  30. _this.NORMAL = false;
  31. _this.UV1 = false;
  32. _this.UV2 = false;
  33. _this.VERTEXCOLOR = false;
  34. _this.VERTEXALPHA = false;
  35. _this.NUM_BONE_INFLUENCERS = 0;
  36. _this.BonesPerMesh = 0;
  37. _this.INSTANCES = false;
  38. _this.NDOTL = true;
  39. _this.CUSTOMUSERLIGHTING = true;
  40. _this.CELLBASIC = true;
  41. _this.DEPTHPREPASS = false;
  42. _this.rebuild();
  43. return _this;
  44. }
  45. return CellMaterialDefines;
  46. }(BABYLON.MaterialDefines));
  47. var CellMaterial = /** @class */ (function (_super) {
  48. __extends(CellMaterial, _super);
  49. function CellMaterial(name, scene) {
  50. var _this = _super.call(this, name, scene) || this;
  51. _this.diffuseColor = new BABYLON.Color3(1, 1, 1);
  52. _this._computeHighLevel = false;
  53. _this._disableLighting = false;
  54. _this._maxSimultaneousLights = 4;
  55. return _this;
  56. }
  57. CellMaterial.prototype.needAlphaBlending = function () {
  58. return (this.alpha < 1.0);
  59. };
  60. CellMaterial.prototype.needAlphaTesting = function () {
  61. return false;
  62. };
  63. CellMaterial.prototype.getAlphaTestTexture = function () {
  64. return null;
  65. };
  66. // Methods
  67. CellMaterial.prototype.isReadyForSubMesh = function (mesh, subMesh, useInstances) {
  68. if (this.isFrozen) {
  69. if (this._wasPreviouslyReady && subMesh.effect) {
  70. return true;
  71. }
  72. }
  73. if (!subMesh._materialDefines) {
  74. subMesh._materialDefines = new CellMaterialDefines();
  75. }
  76. var defines = subMesh._materialDefines;
  77. var scene = this.getScene();
  78. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  79. if (this._renderId === scene.getRenderId()) {
  80. return true;
  81. }
  82. }
  83. var engine = scene.getEngine();
  84. // Textures
  85. if (defines._areTexturesDirty) {
  86. defines._needUVs = false;
  87. if (scene.texturesEnabled) {
  88. if (this._diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  89. if (!this._diffuseTexture.isReady()) {
  90. return false;
  91. }
  92. else {
  93. defines._needUVs = true;
  94. defines.DIFFUSE = true;
  95. }
  96. }
  97. }
  98. }
  99. // High level
  100. defines.CELLBASIC = !this.computeHighLevel;
  101. // Misc.
  102. BABYLON.MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, this._shouldTurnAlphaTestOn(mesh), defines);
  103. // Lights
  104. defines._needNormals = BABYLON.MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, false, this._maxSimultaneousLights, this._disableLighting);
  105. // Values that need to be evaluated on every frame
  106. BABYLON.MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances ? true : false);
  107. // Attribs
  108. BABYLON.MaterialHelper.PrepareDefinesForAttributes(mesh, defines, true, true);
  109. // Get correct effect
  110. if (defines.isDirty) {
  111. defines.markAsProcessed();
  112. scene.resetCachedMaterial();
  113. // Fallbacks
  114. var fallbacks = new BABYLON.EffectFallbacks();
  115. if (defines.FOG) {
  116. fallbacks.addFallback(1, "FOG");
  117. }
  118. BABYLON.MaterialHelper.HandleFallbacksForShadows(defines, fallbacks, this.maxSimultaneousLights);
  119. if (defines.NUM_BONE_INFLUENCERS > 0) {
  120. fallbacks.addCPUSkinningFallback(0, mesh);
  121. }
  122. //Attributes
  123. var attribs = [BABYLON.VertexBuffer.PositionKind];
  124. if (defines.NORMAL) {
  125. attribs.push(BABYLON.VertexBuffer.NormalKind);
  126. }
  127. if (defines.UV1) {
  128. attribs.push(BABYLON.VertexBuffer.UVKind);
  129. }
  130. if (defines.UV2) {
  131. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  132. }
  133. if (defines.VERTEXCOLOR) {
  134. attribs.push(BABYLON.VertexBuffer.ColorKind);
  135. }
  136. BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  137. BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  138. var shaderName = "cell";
  139. var join = defines.toString();
  140. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  141. "vFogInfos", "vFogColor", "pointSize",
  142. "vDiffuseInfos",
  143. "mBones",
  144. "vClipPlane", "diffuseMatrix"
  145. ];
  146. var samplers = ["diffuseSampler"];
  147. var uniformBuffers = new Array();
  148. BABYLON.MaterialHelper.PrepareUniformsAndSamplersList({
  149. uniformsNames: uniforms,
  150. uniformBuffersNames: uniformBuffers,
  151. samplers: samplers,
  152. defines: defines,
  153. maxSimultaneousLights: this.maxSimultaneousLights
  154. });
  155. subMesh.setEffect(scene.getEngine().createEffect(shaderName, {
  156. attributes: attribs,
  157. uniformsNames: uniforms,
  158. uniformBuffersNames: uniformBuffers,
  159. samplers: samplers,
  160. defines: join,
  161. fallbacks: fallbacks,
  162. onCompiled: this.onCompiled,
  163. onError: this.onError,
  164. indexParameters: { maxSimultaneousLights: this.maxSimultaneousLights - 1 }
  165. }, engine), defines);
  166. }
  167. if (!subMesh.effect || !subMesh.effect.isReady()) {
  168. return false;
  169. }
  170. this._renderId = scene.getRenderId();
  171. this._wasPreviouslyReady = true;
  172. return true;
  173. };
  174. CellMaterial.prototype.bindForSubMesh = function (world, mesh, subMesh) {
  175. var scene = this.getScene();
  176. var defines = subMesh._materialDefines;
  177. if (!defines) {
  178. return;
  179. }
  180. var effect = subMesh.effect;
  181. if (!effect) {
  182. return;
  183. }
  184. this._activeEffect = effect;
  185. // Matrices
  186. this.bindOnlyWorldMatrix(world);
  187. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  188. // Bones
  189. BABYLON.MaterialHelper.BindBonesParameters(mesh, this._activeEffect);
  190. if (this._mustRebind(scene, effect)) {
  191. // Textures
  192. if (this._diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  193. this._activeEffect.setTexture("diffuseSampler", this._diffuseTexture);
  194. this._activeEffect.setFloat2("vDiffuseInfos", this._diffuseTexture.coordinatesIndex, this._diffuseTexture.level);
  195. this._activeEffect.setMatrix("diffuseMatrix", this._diffuseTexture.getTextureMatrix());
  196. }
  197. // Clip plane
  198. BABYLON.MaterialHelper.BindClipPlane(this._activeEffect, scene);
  199. // Point size
  200. if (this.pointsCloud) {
  201. this._activeEffect.setFloat("pointSize", this.pointSize);
  202. }
  203. BABYLON.MaterialHelper.BindEyePosition(effect, scene);
  204. }
  205. this._activeEffect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  206. // Lights
  207. if (scene.lightsEnabled && !this.disableLighting) {
  208. BABYLON.MaterialHelper.BindLights(scene, mesh, this._activeEffect, defines, this._maxSimultaneousLights);
  209. }
  210. // View
  211. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  212. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  213. }
  214. // Fog
  215. BABYLON.MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  216. this._afterBind(mesh, this._activeEffect);
  217. };
  218. CellMaterial.prototype.getAnimatables = function () {
  219. var results = [];
  220. if (this._diffuseTexture && this._diffuseTexture.animations && this._diffuseTexture.animations.length > 0) {
  221. results.push(this._diffuseTexture);
  222. }
  223. return results;
  224. };
  225. CellMaterial.prototype.getActiveTextures = function () {
  226. var activeTextures = _super.prototype.getActiveTextures.call(this);
  227. if (this._diffuseTexture) {
  228. activeTextures.push(this._diffuseTexture);
  229. }
  230. return activeTextures;
  231. };
  232. CellMaterial.prototype.hasTexture = function (texture) {
  233. if (_super.prototype.hasTexture.call(this, texture)) {
  234. return true;
  235. }
  236. return this._diffuseTexture === texture;
  237. };
  238. CellMaterial.prototype.dispose = function (forceDisposeEffect) {
  239. if (this._diffuseTexture) {
  240. this._diffuseTexture.dispose();
  241. }
  242. _super.prototype.dispose.call(this, forceDisposeEffect);
  243. };
  244. CellMaterial.prototype.getClassName = function () {
  245. return "CellMaterial";
  246. };
  247. CellMaterial.prototype.clone = function (name) {
  248. var _this = this;
  249. return BABYLON.SerializationHelper.Clone(function () { return new CellMaterial(name, _this.getScene()); }, this);
  250. };
  251. CellMaterial.prototype.serialize = function () {
  252. var serializationObject = BABYLON.SerializationHelper.Serialize(this);
  253. serializationObject.customType = "BABYLON.CellMaterial";
  254. return serializationObject;
  255. };
  256. // Statics
  257. CellMaterial.Parse = function (source, scene, rootUrl) {
  258. return BABYLON.SerializationHelper.Parse(function () { return new CellMaterial(source.name, scene); }, source, scene, rootUrl);
  259. };
  260. __decorate([
  261. BABYLON.serializeAsTexture("diffuseTexture")
  262. ], CellMaterial.prototype, "_diffuseTexture", void 0);
  263. __decorate([
  264. BABYLON.expandToProperty("_markAllSubMeshesAsTexturesDirty")
  265. ], CellMaterial.prototype, "diffuseTexture", void 0);
  266. __decorate([
  267. BABYLON.serializeAsColor3("diffuse")
  268. ], CellMaterial.prototype, "diffuseColor", void 0);
  269. __decorate([
  270. BABYLON.serialize("computeHighLevel")
  271. ], CellMaterial.prototype, "_computeHighLevel", void 0);
  272. __decorate([
  273. BABYLON.expandToProperty("_markAllSubMeshesAsTexturesDirty")
  274. ], CellMaterial.prototype, "computeHighLevel", void 0);
  275. __decorate([
  276. BABYLON.serialize("disableLighting")
  277. ], CellMaterial.prototype, "_disableLighting", void 0);
  278. __decorate([
  279. BABYLON.expandToProperty("_markAllSubMeshesAsLightsDirty")
  280. ], CellMaterial.prototype, "disableLighting", void 0);
  281. __decorate([
  282. BABYLON.serialize("maxSimultaneousLights")
  283. ], CellMaterial.prototype, "_maxSimultaneousLights", void 0);
  284. __decorate([
  285. BABYLON.expandToProperty("_markAllSubMeshesAsLightsDirty")
  286. ], CellMaterial.prototype, "maxSimultaneousLights", void 0);
  287. return CellMaterial;
  288. }(BABYLON.PushMaterial));
  289. BABYLON.CellMaterial = CellMaterial;
  290. })(BABYLON || (BABYLON = {}));
  291. //# sourceMappingURL=babylon.cellMaterial.js.map
  292. BABYLON.Effect.ShadersStore['cellVertexShader'] = "precision highp float;\n\nattribute vec3 position;\n#ifdef NORMAL\nattribute vec3 normal;\n#endif\n#ifdef UV1\nattribute vec2 uv;\n#endif\n#ifdef UV2\nattribute vec2 uv2;\n#endif\n#ifdef VERTEXCOLOR\nattribute vec4 color;\n#endif\n#include<bonesDeclaration>\n\n#include<instancesDeclaration>\nuniform mat4 view;\nuniform mat4 viewProjection;\n#ifdef DIFFUSE\nvarying vec2 vDiffuseUV;\nuniform mat4 diffuseMatrix;\nuniform vec2 vDiffuseInfos;\n#endif\n#ifdef POINTSIZE\nuniform float pointSize;\n#endif\n\nvarying vec3 vPositionW;\n#ifdef NORMAL\nvarying vec3 vNormalW;\n#endif\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n#include<clipPlaneVertexDeclaration>\n#include<fogVertexDeclaration>\n#include<__decl__lightFragment>[0..maxSimultaneousLights]\nvoid main(void) {\n#include<instancesVertex>\n#include<bonesVertex>\ngl_Position=viewProjection*finalWorld*vec4(position,1.0);\nvec4 worldPos=finalWorld*vec4(position,1.0);\nvPositionW=vec3(worldPos);\n#ifdef NORMAL\nvNormalW=normalize(vec3(finalWorld*vec4(normal,0.0)));\n#endif\n\n#ifndef UV1\nvec2 uv=vec2(0.,0.);\n#endif\n#ifndef UV2\nvec2 uv2=vec2(0.,0.);\n#endif\n#ifdef DIFFUSE\nif (vDiffuseInfos.x == 0.)\n{\nvDiffuseUV=vec2(diffuseMatrix*vec4(uv,1.0,0.0));\n}\nelse\n{\nvDiffuseUV=vec2(diffuseMatrix*vec4(uv2,1.0,0.0));\n}\n#endif\n\n#include<clipPlaneVertex>\n\n#include<fogVertex>\n#include<shadowsVertex>[0..maxSimultaneousLights]\n\n#ifdef VERTEXCOLOR\nvColor=color;\n#endif\n\n#ifdef POINTSIZE\ngl_PointSize=pointSize;\n#endif\n}\n";
  293. BABYLON.Effect.ShadersStore['cellPixelShader'] = "precision highp float;\n\nuniform vec3 vEyePosition;\nuniform vec4 vDiffuseColor;\n\nvarying vec3 vPositionW;\n#ifdef NORMAL\nvarying vec3 vNormalW;\n#endif\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n\n#include<helperFunctions>\n\n#include<__decl__lightFragment>[0..maxSimultaneousLights]\n#include<lightsFragmentFunctions>\n#include<shadowsFragmentFunctions>\n\n#ifdef DIFFUSE\nvarying vec2 vDiffuseUV;\nuniform sampler2D diffuseSampler;\nuniform vec2 vDiffuseInfos;\n#endif\n#include<clipPlaneFragmentDeclaration>\n\n#include<fogFragmentDeclaration>\n\nvec3 computeCustomDiffuseLighting(lightingInfo info,vec3 diffuseBase,float shadow)\n{\ndiffuseBase=info.diffuse*shadow;\n#ifdef CELLBASIC\nfloat level=1.0;\nif (info.ndl<0.5)\nlevel=0.5;\ndiffuseBase.rgb*vec3(level,level,level);\n#else\nfloat ToonThresholds[4];\nToonThresholds[0]=0.95;\nToonThresholds[1]=0.5;\nToonThresholds[2]=0.2;\nToonThresholds[3]=0.03;\nfloat ToonBrightnessLevels[5];\nToonBrightnessLevels[0]=1.0;\nToonBrightnessLevels[1]=0.8;\nToonBrightnessLevels[2]=0.6;\nToonBrightnessLevels[3]=0.35;\nToonBrightnessLevels[4]=0.2;\nif (info.ndl>ToonThresholds[0])\n{\ndiffuseBase.rgb*=ToonBrightnessLevels[0];\n}\nelse if (info.ndl>ToonThresholds[1])\n{\ndiffuseBase.rgb*=ToonBrightnessLevels[1];\n}\nelse if (info.ndl>ToonThresholds[2])\n{\ndiffuseBase.rgb*=ToonBrightnessLevels[2];\n}\nelse if (info.ndl>ToonThresholds[3])\n{\ndiffuseBase.rgb*=ToonBrightnessLevels[3];\n}\nelse\n{\ndiffuseBase.rgb*=ToonBrightnessLevels[4];\n}\n#endif\nreturn max(diffuseBase,vec3(0.2));\n}\nvoid main(void)\n{\n#include<clipPlaneFragment>\nvec3 viewDirectionW=normalize(vEyePosition-vPositionW);\n\nvec4 baseColor=vec4(1.,1.,1.,1.);\nvec3 diffuseColor=vDiffuseColor.rgb;\n\nfloat alpha=vDiffuseColor.a;\n#ifdef DIFFUSE\nbaseColor=texture2D(diffuseSampler,vDiffuseUV);\n#ifdef ALPHATEST\nif (baseColor.a<0.4)\ndiscard;\n#endif\n#include<depthPrePass>\nbaseColor.rgb*=vDiffuseInfos.y;\n#endif\n#ifdef VERTEXCOLOR\nbaseColor.rgb*=vColor.rgb;\n#endif\n\n#ifdef NORMAL\nvec3 normalW=normalize(vNormalW);\n#else\nvec3 normalW=vec3(1.0,1.0,1.0);\n#endif\n\nlightingInfo info;\nvec3 diffuseBase=vec3(0.,0.,0.);\nfloat shadow=1.;\nfloat glossiness=0.;\n#ifdef SPECULARTERM\nvec3 specularBase=vec3(0.,0.,0.);\n#endif \n#include<lightFragment>[0..maxSimultaneousLights]\n#ifdef VERTEXALPHA\nalpha*=vColor.a;\n#endif\nvec3 finalDiffuse=clamp(diffuseBase*diffuseColor,0.0,1.0)*baseColor.rgb;;\n\nvec4 color=vec4(finalDiffuse,alpha);\n#include<fogFragment>\ngl_FragColor=color;\n}";