normalMaterial.ts 12 KB

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