babylon.simpleMaterial.ts 13 KB

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