cellMaterial.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 { IEffectCreationOptions } 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. import { EffectFallbacks } from 'babylonjs/Materials/effectFallbacks';
  21. class CellMaterialDefines extends MaterialDefines {
  22. public DIFFUSE = false;
  23. public CLIPPLANE = false;
  24. public CLIPPLANE2 = false;
  25. public CLIPPLANE3 = false;
  26. public CLIPPLANE4 = false;
  27. public CLIPPLANE5 = false;
  28. public CLIPPLANE6 = false;
  29. public ALPHATEST = false;
  30. public POINTSIZE = false;
  31. public FOG = false;
  32. public NORMAL = false;
  33. public UV1 = false;
  34. public UV2 = false;
  35. public VERTEXCOLOR = false;
  36. public VERTEXALPHA = false;
  37. public NUM_BONE_INFLUENCERS = 0;
  38. public BonesPerMesh = 0;
  39. public INSTANCES = false;
  40. public NDOTL = true;
  41. public CUSTOMUSERLIGHTING = true;
  42. public CELLBASIC = true;
  43. public DEPTHPREPASS = false;
  44. constructor() {
  45. super();
  46. this.rebuild();
  47. }
  48. }
  49. export class CellMaterial extends PushMaterial {
  50. @serializeAsTexture("diffuseTexture")
  51. private _diffuseTexture: BaseTexture;
  52. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  53. public diffuseTexture: BaseTexture;
  54. @serializeAsColor3("diffuse")
  55. public diffuseColor = new Color3(1, 1, 1);
  56. @serialize("computeHighLevel")
  57. public _computeHighLevel: boolean = false;
  58. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  59. public computeHighLevel: boolean;
  60. @serialize("disableLighting")
  61. private _disableLighting = false;
  62. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  63. public disableLighting: boolean;
  64. @serialize("maxSimultaneousLights")
  65. private _maxSimultaneousLights = 4;
  66. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  67. public maxSimultaneousLights: number;
  68. constructor(name: string, scene: Scene) {
  69. super(name, scene);
  70. }
  71. public needAlphaBlending(): boolean {
  72. return (this.alpha < 1.0);
  73. }
  74. public needAlphaTesting(): boolean {
  75. return false;
  76. }
  77. public getAlphaTestTexture(): Nullable<BaseTexture> {
  78. return null;
  79. }
  80. // Methods
  81. public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances?: boolean): boolean {
  82. if (this.isFrozen) {
  83. if (subMesh.effect && subMesh.effect._wasPreviouslyReady) {
  84. return true;
  85. }
  86. }
  87. if (!subMesh._materialDefines) {
  88. subMesh._materialDefines = new CellMaterialDefines();
  89. }
  90. var defines = <CellMaterialDefines>subMesh._materialDefines;
  91. var scene = this.getScene();
  92. if (this._isReadyForSubMesh(subMesh)) {
  93. return true;
  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", "vClipPlane5", "vClipPlane6", "diffuseMatrix"
  156. ];
  157. var samplers = ["diffuseSampler"];
  158. var uniformBuffers = new Array<string>();
  159. MaterialHelper.PrepareUniformsAndSamplersList(<IEffectCreationOptions>{
  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. <IEffectCreationOptions>{
  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. defines._renderId = scene.getRenderId();
  183. subMesh.effect._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;