normalMaterial.ts 13 KB

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