simpleMaterial.ts 11 KB

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