simpleMaterial.ts 12 KB

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