babylon.normalMaterial.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 POINTSIZE = false;
  8. public FOG = false;
  9. public LIGHT0 = false;
  10. public LIGHT1 = false;
  11. public LIGHT2 = false;
  12. public LIGHT3 = false;
  13. public SPOTLIGHT0 = false;
  14. public SPOTLIGHT1 = false;
  15. public SPOTLIGHT2 = false;
  16. public SPOTLIGHT3 = false;
  17. public HEMILIGHT0 = false;
  18. public HEMILIGHT1 = false;
  19. public HEMILIGHT2 = false;
  20. public HEMILIGHT3 = false;
  21. public DIRLIGHT0 = false;
  22. public DIRLIGHT1 = false;
  23. public DIRLIGHT2 = false;
  24. public DIRLIGHT3 = false;
  25. public POINTLIGHT0 = false;
  26. public POINTLIGHT1 = false;
  27. public POINTLIGHT2 = false;
  28. public POINTLIGHT3 = false;
  29. public SHADOW0 = false;
  30. public SHADOW1 = false;
  31. public SHADOW2 = false;
  32. public SHADOW3 = false;
  33. public SHADOWS = false;
  34. public SHADOWESM0 = false;
  35. public SHADOWESM1 = false;
  36. public SHADOWESM2 = false;
  37. public SHADOWESM3 = false;
  38. public SHADOWPCF0 = false;
  39. public SHADOWPCF1 = false;
  40. public SHADOWPCF2 = false;
  41. public SHADOWPCF3 = false;
  42. public NORMAL = false;
  43. public UV1 = false;
  44. public UV2 = false;
  45. public VERTEXCOLOR = false;
  46. public VERTEXALPHA = false;
  47. public NUM_BONE_INFLUENCERS = 0;
  48. public BonesPerMesh = 0;
  49. public INSTANCES = false;
  50. constructor() {
  51. super();
  52. this.rebuild();
  53. }
  54. }
  55. export class NormalMaterial extends PushMaterial {
  56. @serializeAsTexture("diffuseTexture")
  57. private _diffuseTexture: BaseTexture;
  58. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  59. public diffuseTexture: BaseTexture;
  60. @serializeAsColor3()
  61. public diffuseColor = new Color3(1, 1, 1);
  62. @serialize("disableLighting")
  63. private _disableLighting = false;
  64. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  65. public disableLighting: boolean;
  66. @serialize("maxSimultaneousLights")
  67. private _maxSimultaneousLights = 4;
  68. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  69. public maxSimultaneousLights: number;
  70. private _worldViewProjectionMatrix = Matrix.Zero();
  71. private _scaledDiffuse = new Color3();
  72. private _renderId: number;
  73. constructor(name: string, scene: Scene) {
  74. super(name, scene);
  75. }
  76. public needAlphaBlending(): boolean {
  77. return (this.alpha < 1.0);
  78. }
  79. public needAlphaTesting(): boolean {
  80. return false;
  81. }
  82. public getAlphaTestTexture(): BaseTexture {
  83. return null;
  84. }
  85. // Methods
  86. public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances?: boolean): boolean {
  87. if (this.isFrozen) {
  88. if (this._wasPreviouslyReady && subMesh.effect) {
  89. return true;
  90. }
  91. }
  92. if (!subMesh._materialDefines) {
  93. subMesh._materialDefines = new NormalMaterialDefines();
  94. }
  95. var defines = <NormalMaterialDefines>subMesh._materialDefines;
  96. var scene = this.getScene();
  97. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  98. if (this._renderId === scene.getRenderId()) {
  99. return true;
  100. }
  101. }
  102. var engine = scene.getEngine();
  103. // Textures
  104. if (defines._areTexturesDirty) {
  105. defines._needUVs = false;
  106. if (scene.texturesEnabled) {
  107. if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  108. if (!this._diffuseTexture.isReady()) {
  109. return false;
  110. } else {
  111. defines._needUVs = true;
  112. defines.DIFFUSE = true;
  113. }
  114. }
  115. }
  116. }
  117. // Misc.
  118. MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, defines);
  119. // Lights
  120. defines._needNormals = MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, false, this._maxSimultaneousLights, this._disableLighting);
  121. // Values that need to be evaluated on every frame
  122. MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances);
  123. // Attribs
  124. MaterialHelper.PrepareDefinesForAttributes(mesh, defines, true, true);
  125. // Get correct effect
  126. if (defines.isDirty) {
  127. defines.markAsProcessed();
  128. scene.resetCachedMaterial();
  129. // Fallbacks
  130. var fallbacks = new EffectFallbacks();
  131. if (defines.FOG) {
  132. fallbacks.addFallback(1, "FOG");
  133. }
  134. MaterialHelper.HandleFallbacksForShadows(defines, fallbacks);
  135. if (defines.NUM_BONE_INFLUENCERS > 0) {
  136. fallbacks.addCPUSkinningFallback(0, mesh);
  137. }
  138. //Attributes
  139. var attribs = [VertexBuffer.PositionKind];
  140. if (defines.NORMAL) {
  141. attribs.push(VertexBuffer.NormalKind);
  142. }
  143. if (defines.UV1) {
  144. attribs.push(VertexBuffer.UVKind);
  145. }
  146. if (defines.UV2) {
  147. attribs.push(VertexBuffer.UV2Kind);
  148. }
  149. if (defines.VERTEXCOLOR) {
  150. attribs.push(VertexBuffer.ColorKind);
  151. }
  152. MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  153. MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  154. var shaderName = "normal";
  155. var join = defines.toString();
  156. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  157. "vFogInfos", "vFogColor", "pointSize",
  158. "vDiffuseInfos",
  159. "mBones",
  160. "vClipPlane", "diffuseMatrix",
  161. "depthValues"
  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 dispose(forceDisposeEffect?: boolean): void {
  241. if (this.diffuseTexture) {
  242. this.diffuseTexture.dispose();
  243. }
  244. super.dispose(forceDisposeEffect);
  245. }
  246. public clone(name: string): NormalMaterial {
  247. return SerializationHelper.Clone(() => new NormalMaterial(name, this.getScene()), this);
  248. }
  249. public serialize(): any {
  250. var serializationObject = SerializationHelper.Serialize(this);
  251. serializationObject.customType = "BABYLON.NormalMaterial";
  252. return serializationObject;
  253. }
  254. // Statics
  255. public static Parse(source: any, scene: Scene, rootUrl: string): NormalMaterial {
  256. return SerializationHelper.Parse(() => new NormalMaterial(source.name, scene), source, scene, rootUrl);
  257. }
  258. }
  259. }