babylon.normalMaterial.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 SHADOWVSM0 = false;
  35. public SHADOWVSM1 = false;
  36. public SHADOWVSM2 = false;
  37. public SHADOWVSM3 = 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._keys = Object.keys(this);
  53. }
  54. }
  55. export class NormalMaterial extends Material {
  56. public diffuseTexture: BaseTexture;
  57. public diffuseColor = new Color3(1, 1, 1);
  58. public disableLighting = false;
  59. private _worldViewProjectionMatrix = Matrix.Zero();
  60. private _scaledDiffuse = new Color3();
  61. private _renderId: number;
  62. private _defines = new NormalMaterialDefines();
  63. private _cachedDefines = new NormalMaterialDefines();
  64. constructor(name: string, scene: Scene) {
  65. super(name, scene);
  66. this._cachedDefines.BonesPerMesh = -1;
  67. }
  68. public needAlphaBlending(): boolean {
  69. return (this.alpha < 1.0);
  70. }
  71. public needAlphaTesting(): boolean {
  72. return false;
  73. }
  74. public getAlphaTestTexture(): BaseTexture {
  75. return null;
  76. }
  77. // Methods
  78. private _checkCache(scene: Scene, mesh?: AbstractMesh, useInstances?: boolean): boolean {
  79. if (!mesh) {
  80. return true;
  81. }
  82. if (this._defines.INSTANCES !== useInstances) {
  83. return false;
  84. }
  85. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  86. return true;
  87. }
  88. return false;
  89. }
  90. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  91. if (this.checkReadyOnlyOnce) {
  92. if (this._wasPreviouslyReady) {
  93. return true;
  94. }
  95. }
  96. var scene = this.getScene();
  97. if (!this.checkReadyOnEveryCall) {
  98. if (this._renderId === scene.getRenderId()) {
  99. if (this._checkCache(scene, mesh, useInstances)) {
  100. return true;
  101. }
  102. }
  103. }
  104. var engine = scene.getEngine();
  105. var needNormals = false;
  106. var needUVs = false;
  107. this._defines.reset();
  108. // Textures
  109. if (scene.texturesEnabled) {
  110. if (this.diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  111. if (!this.diffuseTexture.isReady()) {
  112. return false;
  113. } else {
  114. needUVs = true;
  115. this._defines.DIFFUSE = true;
  116. }
  117. }
  118. }
  119. // Effect
  120. if (scene.clipPlane) {
  121. this._defines.CLIPPLANE = true;
  122. }
  123. if (engine.getAlphaTesting()) {
  124. this._defines.ALPHATEST = true;
  125. }
  126. // Point size
  127. if (this.pointsCloud || scene.forcePointsCloud) {
  128. this._defines.POINTSIZE = true;
  129. }
  130. // Fog
  131. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled) {
  132. this._defines.FOG = true;
  133. }
  134. var lightIndex = 0;
  135. if (scene.lightsEnabled && !this.disableLighting) {
  136. needNormals = MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines);
  137. }
  138. // Attribs
  139. if (mesh) {
  140. if (needNormals && mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  141. this._defines.NORMAL = true;
  142. }
  143. if (needUVs) {
  144. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  145. this._defines.UV1 = true;
  146. }
  147. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  148. this._defines.UV2 = true;
  149. }
  150. }
  151. if (mesh.useVertexColors && mesh.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  152. this._defines.VERTEXCOLOR = true;
  153. if (mesh.hasVertexAlpha) {
  154. this._defines.VERTEXALPHA = true;
  155. }
  156. }
  157. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  158. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  159. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  160. }
  161. // Instances
  162. if (useInstances) {
  163. this._defines.INSTANCES = true;
  164. }
  165. }
  166. // Get correct effect
  167. if (!this._defines.isEqual(this._cachedDefines)) {
  168. this._defines.cloneTo(this._cachedDefines);
  169. scene.resetCachedMaterial();
  170. // Fallbacks
  171. var fallbacks = new EffectFallbacks();
  172. if (this._defines.FOG) {
  173. fallbacks.addFallback(1, "FOG");
  174. }
  175. MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks);
  176. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  177. fallbacks.addCPUSkinningFallback(0, mesh);
  178. }
  179. //Attributes
  180. var attribs = [VertexBuffer.PositionKind];
  181. if (this._defines.NORMAL) {
  182. attribs.push(VertexBuffer.NormalKind);
  183. }
  184. if (this._defines.UV1) {
  185. attribs.push(VertexBuffer.UVKind);
  186. }
  187. if (this._defines.UV2) {
  188. attribs.push(VertexBuffer.UV2Kind);
  189. }
  190. if (this._defines.VERTEXCOLOR) {
  191. attribs.push(VertexBuffer.ColorKind);
  192. }
  193. MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  194. MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  195. var shaderName = "normal";
  196. var join = this._defines.toString();
  197. this._effect = scene.getEngine().createEffect(shaderName,
  198. attribs,
  199. ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  200. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  201. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  202. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  203. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  204. "vFogInfos", "vFogColor", "pointSize",
  205. "vDiffuseInfos",
  206. "mBones",
  207. "vClipPlane", "diffuseMatrix",
  208. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3", "depthValues"
  209. ],
  210. ["diffuseSampler",
  211. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  212. ],
  213. join, fallbacks, this.onCompiled, this.onError);
  214. }
  215. if (!this._effect.isReady()) {
  216. return false;
  217. }
  218. this._renderId = scene.getRenderId();
  219. this._wasPreviouslyReady = true;
  220. if (mesh) {
  221. if (!mesh._materialDefines) {
  222. mesh._materialDefines = new NormalMaterialDefines();
  223. }
  224. this._defines.cloneTo(mesh._materialDefines);
  225. }
  226. return true;
  227. }
  228. public bindOnlyWorldMatrix(world: Matrix): void {
  229. this._effect.setMatrix("world", world);
  230. }
  231. public bind(world: Matrix, mesh?: Mesh): void {
  232. var scene = this.getScene();
  233. // Matrices
  234. this.bindOnlyWorldMatrix(world);
  235. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  236. // Bones
  237. MaterialHelper.BindBonesParameters(mesh, this._effect);
  238. if (scene.getCachedMaterial() !== this) {
  239. // Textures
  240. if (this.diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  241. this._effect.setTexture("diffuseSampler", this.diffuseTexture);
  242. this._effect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  243. this._effect.setMatrix("diffuseMatrix", this.diffuseTexture.getTextureMatrix());
  244. }
  245. // Clip plane
  246. MaterialHelper.BindClipPlane(this._effect, scene);
  247. // Point size
  248. if (this.pointsCloud) {
  249. this._effect.setFloat("pointSize", this.pointSize);
  250. }
  251. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  252. }
  253. this._effect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  254. // Lights
  255. if (scene.lightsEnabled && !this.disableLighting) {
  256. MaterialHelper.BindLights(scene, mesh, this._effect, this._defines);
  257. }
  258. // View
  259. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  260. this._effect.setMatrix("view", scene.getViewMatrix());
  261. }
  262. // Fog
  263. MaterialHelper.BindFogParameters(scene, mesh, this._effect);
  264. super.bind(world, mesh);
  265. }
  266. public getAnimatables(): IAnimatable[] {
  267. var results = [];
  268. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  269. results.push(this.diffuseTexture);
  270. }
  271. return results;
  272. }
  273. public dispose(forceDisposeEffect?: boolean): void {
  274. if (this.diffuseTexture) {
  275. this.diffuseTexture.dispose();
  276. }
  277. super.dispose(forceDisposeEffect);
  278. }
  279. public clone(name: string): NormalMaterial {
  280. var newMaterial = new NormalMaterial(name, this.getScene());
  281. // Base material
  282. this.copyTo(newMaterial);
  283. // Normal material
  284. if (this.diffuseTexture && this.diffuseTexture.clone) {
  285. newMaterial.diffuseTexture = this.diffuseTexture.clone();
  286. }
  287. newMaterial.diffuseColor = this.diffuseColor.clone();
  288. return newMaterial;
  289. }
  290. public serialize(): any {
  291. var serializationObject = super.serialize();
  292. serializationObject.customType = "BABYLON.NormalMaterial";
  293. serializationObject.diffuseColor = this.diffuseColor.asArray();
  294. serializationObject.disableLighting = this.disableLighting;
  295. if (this.diffuseTexture) {
  296. serializationObject.diffuseTexture = this.diffuseTexture.serialize();
  297. }
  298. return serializationObject;
  299. }
  300. public static Parse(source: any, scene: Scene, rootUrl: string): NormalMaterial {
  301. var material = new NormalMaterial(source.name, scene);
  302. material.diffuseColor = Color3.FromArray(source.diffuseColor);
  303. material.disableLighting = source.disableLighting;
  304. material.alpha = source.alpha;
  305. material.id = source.id;
  306. Tags.AddTagsTo(material, source.tags);
  307. material.backFaceCulling = source.backFaceCulling;
  308. material.wireframe = source.wireframe;
  309. if (source.diffuseTexture) {
  310. material.diffuseTexture = Texture.Parse(source.diffuseTexture, scene, rootUrl);
  311. }
  312. if (source.checkReadyOnlyOnce) {
  313. material.checkReadyOnlyOnce = source.checkReadyOnlyOnce;
  314. }
  315. return material;
  316. }
  317. }
  318. }