babylon.cellMaterial.js 17 KB

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