babylon.simpleMaterial.ts 14 KB

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