babylon.normalMaterial.ts 13 KB

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