cellMaterial.ts 11 KB

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