babylon.normalMaterial.ts 12 KB

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