normalMaterial.ts 12 KB

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