normalMaterial.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 { 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 "./normal.fragment";
  19. import "./normal.vertex";
  20. import { EffectFallbacks } from 'babylonjs/Materials/effectFallbacks';
  21. class NormalMaterialDefines extends MaterialDefines {
  22. public DIFFUSE = false;
  23. public CLIPPLANE = false;
  24. public CLIPPLANE2 = false;
  25. public CLIPPLANE3 = false;
  26. public CLIPPLANE4 = false;
  27. public ALPHATEST = false;
  28. public DEPTHPREPASS = false;
  29. public POINTSIZE = false;
  30. public FOG = false;
  31. public LIGHT0 = false;
  32. public LIGHT1 = false;
  33. public LIGHT2 = false;
  34. public LIGHT3 = false;
  35. public SPOTLIGHT0 = false;
  36. public SPOTLIGHT1 = false;
  37. public SPOTLIGHT2 = false;
  38. public SPOTLIGHT3 = false;
  39. public HEMILIGHT0 = false;
  40. public HEMILIGHT1 = false;
  41. public HEMILIGHT2 = false;
  42. public HEMILIGHT3 = false;
  43. public DIRLIGHT0 = false;
  44. public DIRLIGHT1 = false;
  45. public DIRLIGHT2 = false;
  46. public DIRLIGHT3 = false;
  47. public POINTLIGHT0 = false;
  48. public POINTLIGHT1 = false;
  49. public POINTLIGHT2 = false;
  50. public POINTLIGHT3 = false;
  51. public SHADOW0 = false;
  52. public SHADOW1 = false;
  53. public SHADOW2 = false;
  54. public SHADOW3 = false;
  55. public SHADOWS = false;
  56. public SHADOWESM0 = false;
  57. public SHADOWESM1 = false;
  58. public SHADOWESM2 = false;
  59. public SHADOWESM3 = false;
  60. public SHADOWPOISSON0 = false;
  61. public SHADOWPOISSON1 = false;
  62. public SHADOWPOISSON2 = false;
  63. public SHADOWPOISSON3 = false;
  64. public SHADOWPCF0 = false;
  65. public SHADOWPCF1 = false;
  66. public SHADOWPCF2 = false;
  67. public SHADOWPCF3 = false;
  68. public SHADOWPCSS0 = false;
  69. public SHADOWPCSS1 = false;
  70. public SHADOWPCSS2 = false;
  71. public SHADOWPCSS3 = false;
  72. public NORMAL = false;
  73. public UV1 = false;
  74. public UV2 = false;
  75. public NUM_BONE_INFLUENCERS = 0;
  76. public BonesPerMesh = 0;
  77. public INSTANCES = false;
  78. public LIGHTING = false;
  79. constructor() {
  80. super();
  81. this.rebuild();
  82. }
  83. }
  84. export class NormalMaterial extends PushMaterial {
  85. @serializeAsTexture("diffuseTexture")
  86. private _diffuseTexture: BaseTexture;
  87. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  88. public diffuseTexture: BaseTexture;
  89. @serializeAsColor3()
  90. public diffuseColor = new Color3(1, 1, 1);
  91. @serialize("disableLighting")
  92. private _disableLighting = false;
  93. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  94. public disableLighting: boolean;
  95. @serialize("maxSimultaneousLights")
  96. private _maxSimultaneousLights = 4;
  97. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  98. public maxSimultaneousLights: number;
  99. private _renderId: number;
  100. constructor(name: string, scene: Scene) {
  101. super(name, scene);
  102. }
  103. public needAlphaBlending(): boolean {
  104. return (this.alpha < 1.0);
  105. }
  106. public needAlphaBlendingForMesh(mesh: AbstractMesh): boolean {
  107. return this.needAlphaBlending() || (mesh.visibility < 1.0);
  108. }
  109. public needAlphaTesting(): boolean {
  110. return false;
  111. }
  112. public getAlphaTestTexture(): Nullable<BaseTexture> {
  113. return null;
  114. }
  115. // Methods
  116. public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances?: boolean): boolean {
  117. if (this.isFrozen) {
  118. if (this._wasPreviouslyReady && subMesh.effect) {
  119. return true;
  120. }
  121. }
  122. if (!subMesh._materialDefines) {
  123. subMesh._materialDefines = new NormalMaterialDefines();
  124. }
  125. var defines = <NormalMaterialDefines>subMesh._materialDefines;
  126. var scene = this.getScene();
  127. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  128. if (this._renderId === scene.getRenderId()) {
  129. return true;
  130. }
  131. }
  132. var engine = scene.getEngine();
  133. // Textures
  134. if (defines._areTexturesDirty) {
  135. defines._needUVs = false;
  136. if (scene.texturesEnabled) {
  137. if (this._diffuseTexture && MaterialFlags.DiffuseTextureEnabled) {
  138. if (!this._diffuseTexture.isReady()) {
  139. return false;
  140. } else {
  141. defines._needUVs = true;
  142. defines.DIFFUSE = true;
  143. }
  144. }
  145. }
  146. }
  147. // Misc.
  148. MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, this._shouldTurnAlphaTestOn(mesh), defines);
  149. // Lights
  150. defines._needNormals = true;
  151. MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, false, this._maxSimultaneousLights, this._disableLighting);
  152. // Values that need to be evaluated on every frame
  153. MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances ? true : false);
  154. defines.LIGHTING = !this._disableLighting;
  155. // Attribs
  156. MaterialHelper.PrepareDefinesForAttributes(mesh, defines, true, true);
  157. // Get correct effect
  158. if (defines.isDirty) {
  159. defines.markAsProcessed();
  160. scene.resetCachedMaterial();
  161. // Fallbacks
  162. var fallbacks = new EffectFallbacks();
  163. if (defines.FOG) {
  164. fallbacks.addFallback(1, "FOG");
  165. }
  166. MaterialHelper.HandleFallbacksForShadows(defines, fallbacks);
  167. if (defines.NUM_BONE_INFLUENCERS > 0) {
  168. fallbacks.addCPUSkinningFallback(0, mesh);
  169. }
  170. //Attributes
  171. var attribs = [VertexBuffer.PositionKind];
  172. if (defines.NORMAL) {
  173. attribs.push(VertexBuffer.NormalKind);
  174. }
  175. if (defines.UV1) {
  176. attribs.push(VertexBuffer.UVKind);
  177. }
  178. if (defines.UV2) {
  179. attribs.push(VertexBuffer.UV2Kind);
  180. }
  181. MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  182. MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  183. var shaderName = "normal";
  184. var join = defines.toString();
  185. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  186. "vFogInfos", "vFogColor", "pointSize",
  187. "vDiffuseInfos",
  188. "mBones",
  189. "vClipPlane", "vClipPlane2", "vClipPlane3", "vClipPlane4", "diffuseMatrix"
  190. ];
  191. var samplers = ["diffuseSampler"];
  192. var uniformBuffers = new Array<string>();
  193. MaterialHelper.PrepareUniformsAndSamplersList(<EffectCreationOptions>{
  194. uniformsNames: uniforms,
  195. uniformBuffersNames: uniformBuffers,
  196. samplers: samplers,
  197. defines: defines,
  198. maxSimultaneousLights: 4
  199. });
  200. subMesh.setEffect(scene.getEngine().createEffect(shaderName,
  201. <EffectCreationOptions>{
  202. attributes: attribs,
  203. uniformsNames: uniforms,
  204. uniformBuffersNames: uniformBuffers,
  205. samplers: samplers,
  206. defines: join,
  207. fallbacks: fallbacks,
  208. onCompiled: this.onCompiled,
  209. onError: this.onError,
  210. indexParameters: { maxSimultaneousLights: 4 }
  211. }, engine), defines);
  212. }
  213. if (!subMesh.effect || !subMesh.effect.isReady()) {
  214. return false;
  215. }
  216. this._renderId = scene.getRenderId();
  217. this._wasPreviouslyReady = true;
  218. return true;
  219. }
  220. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  221. var scene = this.getScene();
  222. var defines = <NormalMaterialDefines>subMesh._materialDefines;
  223. if (!defines) {
  224. return;
  225. }
  226. var effect = subMesh.effect;
  227. if (!effect) {
  228. return;
  229. }
  230. this._activeEffect = effect;
  231. // Matrices
  232. this.bindOnlyWorldMatrix(world);
  233. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  234. // Bones
  235. MaterialHelper.BindBonesParameters(mesh, this._activeEffect);
  236. if (this._mustRebind(scene, effect)) {
  237. // Textures
  238. if (this.diffuseTexture && MaterialFlags.DiffuseTextureEnabled) {
  239. this._activeEffect.setTexture("diffuseSampler", this.diffuseTexture);
  240. this._activeEffect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  241. this._activeEffect.setMatrix("diffuseMatrix", this.diffuseTexture.getTextureMatrix());
  242. }
  243. // Clip plane
  244. MaterialHelper.BindClipPlane(this._activeEffect, scene);
  245. // Point size
  246. if (this.pointsCloud) {
  247. this._activeEffect.setFloat("pointSize", this.pointSize);
  248. }
  249. MaterialHelper.BindEyePosition(effect, scene);
  250. }
  251. this._activeEffect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  252. // Lights
  253. if (scene.lightsEnabled && !this.disableLighting) {
  254. MaterialHelper.BindLights(scene, mesh, this._activeEffect, defines);
  255. }
  256. // View
  257. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  258. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  259. }
  260. // Fog
  261. MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  262. this._afterBind(mesh, this._activeEffect);
  263. }
  264. public getAnimatables(): IAnimatable[] {
  265. var results = [];
  266. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  267. results.push(this.diffuseTexture);
  268. }
  269. return results;
  270. }
  271. public getActiveTextures(): BaseTexture[] {
  272. var activeTextures = super.getActiveTextures();
  273. if (this._diffuseTexture) {
  274. activeTextures.push(this._diffuseTexture);
  275. }
  276. return activeTextures;
  277. }
  278. public hasTexture(texture: BaseTexture): boolean {
  279. if (super.hasTexture(texture)) {
  280. return true;
  281. }
  282. if (this.diffuseTexture === texture) {
  283. return true;
  284. }
  285. return false;
  286. }
  287. public dispose(forceDisposeEffect?: boolean): void {
  288. if (this.diffuseTexture) {
  289. this.diffuseTexture.dispose();
  290. }
  291. super.dispose(forceDisposeEffect);
  292. }
  293. public clone(name: string): NormalMaterial {
  294. return SerializationHelper.Clone(() => new NormalMaterial(name, this.getScene()), this);
  295. }
  296. public serialize(): any {
  297. var serializationObject = SerializationHelper.Serialize(this);
  298. serializationObject.customType = "BABYLON.NormalMaterial";
  299. return serializationObject;
  300. }
  301. public getClassName(): string {
  302. return "NormalMaterial";
  303. }
  304. // Statics
  305. public static Parse(source: any, scene: Scene, rootUrl: string): NormalMaterial {
  306. return SerializationHelper.Parse(() => new NormalMaterial(source.name, scene), source, scene, rootUrl);
  307. }
  308. }
  309. _TypeStore.RegisteredTypes["BABYLON.NormalMaterial"] = NormalMaterial;