cellMaterial.ts 12 KB

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