babylon.cellMaterial.js 18 KB

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