cellMaterial.ts 12 KB

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