babylon.normalMaterial.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 _worldViewProjectionMatrix = Matrix.Zero();
  72. private _scaledDiffuse = new Color3();
  73. private _renderId: number;
  74. constructor(name: string, scene: Scene) {
  75. super(name, scene);
  76. }
  77. public needAlphaBlending(): boolean {
  78. return (this.alpha < 1.0);
  79. }
  80. public needAlphaTesting(): boolean {
  81. return false;
  82. }
  83. public getAlphaTestTexture(): BaseTexture {
  84. return null;
  85. }
  86. // Methods
  87. public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances?: boolean): boolean {
  88. if (this.isFrozen) {
  89. if (this._wasPreviouslyReady && subMesh.effect) {
  90. return true;
  91. }
  92. }
  93. if (!subMesh._materialDefines) {
  94. subMesh._materialDefines = new NormalMaterialDefines();
  95. }
  96. var defines = <NormalMaterialDefines>subMesh._materialDefines;
  97. var scene = this.getScene();
  98. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  99. if (this._renderId === scene.getRenderId()) {
  100. return true;
  101. }
  102. }
  103. var engine = scene.getEngine();
  104. // Textures
  105. if (defines._areTexturesDirty) {
  106. defines._needUVs = false;
  107. if (scene.texturesEnabled) {
  108. if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  109. if (!this._diffuseTexture.isReady()) {
  110. return false;
  111. } else {
  112. defines._needUVs = true;
  113. defines.DIFFUSE = true;
  114. }
  115. }
  116. }
  117. }
  118. // Misc.
  119. MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, defines);
  120. // Lights
  121. defines._needNormals = MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, false, this._maxSimultaneousLights, this._disableLighting);
  122. // Values that need to be evaluated on every frame
  123. MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances);
  124. // Attribs
  125. MaterialHelper.PrepareDefinesForAttributes(mesh, defines, true, true);
  126. // Get correct effect
  127. if (defines.isDirty) {
  128. defines.markAsProcessed();
  129. scene.resetCachedMaterial();
  130. // Fallbacks
  131. var fallbacks = new EffectFallbacks();
  132. if (defines.FOG) {
  133. fallbacks.addFallback(1, "FOG");
  134. }
  135. MaterialHelper.HandleFallbacksForShadows(defines, fallbacks);
  136. if (defines.NUM_BONE_INFLUENCERS > 0) {
  137. fallbacks.addCPUSkinningFallback(0, mesh);
  138. }
  139. //Attributes
  140. var attribs = [VertexBuffer.PositionKind];
  141. if (defines.NORMAL) {
  142. attribs.push(VertexBuffer.NormalKind);
  143. }
  144. if (defines.UV1) {
  145. attribs.push(VertexBuffer.UVKind);
  146. }
  147. if (defines.UV2) {
  148. attribs.push(VertexBuffer.UV2Kind);
  149. }
  150. if (defines.VERTEXCOLOR) {
  151. attribs.push(VertexBuffer.ColorKind);
  152. }
  153. MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  154. MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  155. var shaderName = "normal";
  156. var join = defines.toString();
  157. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  158. "vFogInfos", "vFogColor", "pointSize",
  159. "vDiffuseInfos",
  160. "mBones",
  161. "vClipPlane", "diffuseMatrix"
  162. ];
  163. var samplers = ["diffuseSampler"];
  164. var uniformBuffers = [];
  165. MaterialHelper.PrepareUniformsAndSamplersList(<EffectCreationOptions>{
  166. uniformsNames: uniforms,
  167. uniformBuffersNames: uniformBuffers,
  168. samplers: samplers,
  169. defines: defines,
  170. maxSimultaneousLights: 4
  171. });
  172. subMesh.setEffect(scene.getEngine().createEffect(shaderName,
  173. <EffectCreationOptions>{
  174. attributes: attribs,
  175. uniformsNames: uniforms,
  176. uniformBuffersNames: uniformBuffers,
  177. samplers: samplers,
  178. defines: join,
  179. fallbacks: fallbacks,
  180. onCompiled: this.onCompiled,
  181. onError: this.onError,
  182. indexParameters: { maxSimultaneousLights: 4 }
  183. }, engine), defines);
  184. }
  185. if (!subMesh.effect.isReady()) {
  186. return false;
  187. }
  188. this._renderId = scene.getRenderId();
  189. this._wasPreviouslyReady = true;
  190. return true;
  191. }
  192. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  193. var scene = this.getScene();
  194. var defines = <NormalMaterialDefines>subMesh._materialDefines;
  195. if (!defines) {
  196. return;
  197. }
  198. var effect = subMesh.effect;
  199. this._activeEffect = effect;
  200. // Matrices
  201. this.bindOnlyWorldMatrix(world);
  202. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  203. // Bones
  204. MaterialHelper.BindBonesParameters(mesh, this._activeEffect);
  205. if (this._mustRebind(scene, effect)) {
  206. // Textures
  207. if (this.diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  208. this._activeEffect.setTexture("diffuseSampler", this.diffuseTexture);
  209. this._activeEffect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  210. this._activeEffect.setMatrix("diffuseMatrix", this.diffuseTexture.getTextureMatrix());
  211. }
  212. // Clip plane
  213. MaterialHelper.BindClipPlane(this._activeEffect, scene);
  214. // Point size
  215. if (this.pointsCloud) {
  216. this._activeEffect.setFloat("pointSize", this.pointSize);
  217. }
  218. this._activeEffect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  219. }
  220. this._activeEffect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  221. // Lights
  222. if (scene.lightsEnabled && !this.disableLighting) {
  223. MaterialHelper.BindLights(scene, mesh, this._activeEffect, defines);
  224. }
  225. // View
  226. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  227. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  228. }
  229. // Fog
  230. MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  231. this._afterBind(mesh, this._activeEffect);
  232. }
  233. public getAnimatables(): IAnimatable[] {
  234. var results = [];
  235. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  236. results.push(this.diffuseTexture);
  237. }
  238. return results;
  239. }
  240. public getActiveTextures(): BaseTexture[] {
  241. var activeTextures = super.getActiveTextures();
  242. if (this._diffuseTexture) {
  243. activeTextures.push(this._diffuseTexture);
  244. }
  245. return activeTextures;
  246. }
  247. public hasTexture(texture: BaseTexture): boolean {
  248. if (super.hasTexture(texture)) {
  249. return true;
  250. }
  251. if (this.diffuseTexture === texture) {
  252. return true;
  253. }
  254. return false;
  255. }
  256. public dispose(forceDisposeEffect?: boolean): void {
  257. if (this.diffuseTexture) {
  258. this.diffuseTexture.dispose();
  259. }
  260. super.dispose(forceDisposeEffect);
  261. }
  262. public clone(name: string): NormalMaterial {
  263. return SerializationHelper.Clone(() => new NormalMaterial(name, this.getScene()), this);
  264. }
  265. public serialize(): any {
  266. var serializationObject = SerializationHelper.Serialize(this);
  267. serializationObject.customType = "BABYLON.NormalMaterial";
  268. return serializationObject;
  269. }
  270. public getClassName(): string {
  271. return "NormalMaterial";
  272. }
  273. // Statics
  274. public static Parse(source: any, scene: Scene, rootUrl: string): NormalMaterial {
  275. return SerializationHelper.Parse(() => new NormalMaterial(source.name, scene), source, scene, rootUrl);
  276. }
  277. }
  278. }