babylon.normalMaterial.ts 12 KB

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