simpleMaterial.ts 11 KB

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