babylon.simpleMaterial.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 NORMAL = false;
  11. public UV1 = false;
  12. public UV2 = false;
  13. public VERTEXCOLOR = false;
  14. public VERTEXALPHA = false;
  15. public NUM_BONE_INFLUENCERS = 0;
  16. public BonesPerMesh = 0;
  17. public INSTANCES = false;
  18. constructor() {
  19. super();
  20. this.rebuild();
  21. }
  22. }
  23. export class SimpleMaterial extends Material {
  24. @serializeAsTexture()
  25. public diffuseTexture: BaseTexture;
  26. @serializeAsColor3("diffuseColor")
  27. public diffuseColor = new Color3(1, 1, 1);
  28. @serialize()
  29. public disableLighting = false;
  30. @serialize()
  31. public maxSimultaneousLights = 4;
  32. private _worldViewProjectionMatrix = Matrix.Zero();
  33. private _scaledDiffuse = new Color3();
  34. private _renderId: number;
  35. private _defines = new SimpleMaterialDefines();
  36. private _cachedDefines = new SimpleMaterialDefines();
  37. constructor(name: string, scene: Scene) {
  38. super(name, scene);
  39. this._cachedDefines.BonesPerMesh = -1;
  40. }
  41. public needAlphaBlending(): boolean {
  42. return (this.alpha < 1.0);
  43. }
  44. public needAlphaTesting(): boolean {
  45. return false;
  46. }
  47. public getAlphaTestTexture(): BaseTexture {
  48. return null;
  49. }
  50. // Methods
  51. private _checkCache(scene: Scene, mesh?: AbstractMesh, useInstances?: boolean): boolean {
  52. if (!mesh) {
  53. return true;
  54. }
  55. if (this._defines.INSTANCES !== useInstances) {
  56. return false;
  57. }
  58. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  59. return true;
  60. }
  61. return false;
  62. }
  63. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  64. if (this.checkReadyOnlyOnce) {
  65. if (this._wasPreviouslyReady) {
  66. return true;
  67. }
  68. }
  69. var scene = this.getScene();
  70. if (!this.checkReadyOnEveryCall) {
  71. if (this._renderId === scene.getRenderId()) {
  72. if (this._checkCache(scene, mesh, useInstances)) {
  73. return true;
  74. }
  75. }
  76. }
  77. var engine = scene.getEngine();
  78. var needNormals = false;
  79. var needUVs = false;
  80. this._defines.reset();
  81. // Textures
  82. if (scene.texturesEnabled) {
  83. if (this.diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  84. if (!this.diffuseTexture.isReady()) {
  85. return false;
  86. } else {
  87. needUVs = true;
  88. this._defines.DIFFUSE = true;
  89. }
  90. }
  91. }
  92. // Effect
  93. if (scene.clipPlane) {
  94. this._defines.CLIPPLANE = true;
  95. }
  96. if (engine.getAlphaTesting()) {
  97. this._defines.ALPHATEST = true;
  98. }
  99. // Point size
  100. if (this.pointsCloud || scene.forcePointsCloud) {
  101. this._defines.POINTSIZE = true;
  102. }
  103. // Fog
  104. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled) {
  105. this._defines.FOG = true;
  106. }
  107. if (scene.lightsEnabled && !this.disableLighting) {
  108. needNormals = MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines, this.maxSimultaneousLights);
  109. }
  110. // Attribs
  111. if (mesh) {
  112. if (needNormals && mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  113. this._defines.NORMAL = true;
  114. }
  115. if (needUVs) {
  116. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  117. this._defines.UV1 = true;
  118. }
  119. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  120. this._defines.UV2 = true;
  121. }
  122. }
  123. if (mesh.useVertexColors && mesh.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  124. this._defines.VERTEXCOLOR = true;
  125. if (mesh.hasVertexAlpha) {
  126. this._defines.VERTEXALPHA = true;
  127. }
  128. }
  129. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  130. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  131. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  132. }
  133. // Instances
  134. if (useInstances) {
  135. this._defines.INSTANCES = true;
  136. }
  137. }
  138. // Get correct effect
  139. if (!this._defines.isEqual(this._cachedDefines)) {
  140. this._defines.cloneTo(this._cachedDefines);
  141. scene.resetCachedMaterial();
  142. // Fallbacks
  143. var fallbacks = new EffectFallbacks();
  144. if (this._defines.FOG) {
  145. fallbacks.addFallback(1, "FOG");
  146. }
  147. MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks, this.maxSimultaneousLights);
  148. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  149. fallbacks.addCPUSkinningFallback(0, mesh);
  150. }
  151. //Attributes
  152. var attribs = [VertexBuffer.PositionKind];
  153. if (this._defines.NORMAL) {
  154. attribs.push(VertexBuffer.NormalKind);
  155. }
  156. if (this._defines.UV1) {
  157. attribs.push(VertexBuffer.UVKind);
  158. }
  159. if (this._defines.UV2) {
  160. attribs.push(VertexBuffer.UV2Kind);
  161. }
  162. if (this._defines.VERTEXCOLOR) {
  163. attribs.push(VertexBuffer.ColorKind);
  164. }
  165. MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  166. MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  167. var shaderName = "simple";
  168. var join = this._defines.toString();
  169. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  170. "vFogInfos", "vFogColor", "pointSize",
  171. "vDiffuseInfos",
  172. "mBones",
  173. "vClipPlane", "diffuseMatrix", "depthValues"
  174. ];
  175. MaterialHelper.PrepareUniformsListForList(uniforms, this._defines, this.maxSimultaneousLights);
  176. this._effect = scene.getEngine().createEffect(shaderName,
  177. attribs, uniforms,
  178. ["diffuseSampler",
  179. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  180. ],
  181. join, fallbacks, this.onCompiled, this.onError, {maxSimultaneousLights: this.maxSimultaneousLights});
  182. }
  183. if (!this._effect.isReady()) {
  184. return false;
  185. }
  186. this._renderId = scene.getRenderId();
  187. this._wasPreviouslyReady = true;
  188. if (mesh) {
  189. if (!mesh._materialDefines) {
  190. mesh._materialDefines = new SimpleMaterialDefines();
  191. }
  192. this._defines.cloneTo(mesh._materialDefines);
  193. }
  194. return true;
  195. }
  196. public bindOnlyWorldMatrix(world: Matrix): void {
  197. this._effect.setMatrix("world", world);
  198. }
  199. public bind(world: Matrix, mesh?: Mesh): void {
  200. var scene = this.getScene();
  201. // Matrices
  202. this.bindOnlyWorldMatrix(world);
  203. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  204. // Bones
  205. MaterialHelper.BindBonesParameters(mesh, this._effect);
  206. if (scene.getCachedMaterial() !== this) {
  207. // Textures
  208. if (this.diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  209. this._effect.setTexture("diffuseSampler", this.diffuseTexture);
  210. this._effect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  211. this._effect.setMatrix("diffuseMatrix", this.diffuseTexture.getTextureMatrix());
  212. }
  213. // Clip plane
  214. MaterialHelper.BindClipPlane(this._effect, scene);
  215. // Point size
  216. if (this.pointsCloud) {
  217. this._effect.setFloat("pointSize", this.pointSize);
  218. }
  219. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  220. }
  221. this._effect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  222. // Lights
  223. if (scene.lightsEnabled && !this.disableLighting) {
  224. MaterialHelper.BindLights(scene, mesh, this._effect, this._defines, this.maxSimultaneousLights);
  225. }
  226. // View
  227. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  228. this._effect.setMatrix("view", scene.getViewMatrix());
  229. }
  230. // Fog
  231. MaterialHelper.BindFogParameters(scene, mesh, this._effect);
  232. super.bind(world, mesh);
  233. }
  234. public getAnimatables(): IAnimatable[] {
  235. var results = [];
  236. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  237. results.push(this.diffuseTexture);
  238. }
  239. return results;
  240. }
  241. public dispose(forceDisposeEffect?: boolean): void {
  242. if (this.diffuseTexture) {
  243. this.diffuseTexture.dispose();
  244. }
  245. super.dispose(forceDisposeEffect);
  246. }
  247. public clone(name: string): SimpleMaterial {
  248. return SerializationHelper.Clone<SimpleMaterial>(() => new SimpleMaterial(name, this.getScene()), this);
  249. }
  250. public serialize(): any {
  251. var serializationObject = SerializationHelper.Serialize(this);
  252. serializationObject.customType = "BABYLON.SimpleMaterial";
  253. return serializationObject;
  254. }
  255. // Statics
  256. public static Parse(source: any, scene: Scene, rootUrl: string): SimpleMaterial {
  257. return SerializationHelper.Parse(() => new SimpleMaterial(source.name, scene), source, scene, rootUrl);
  258. }
  259. }
  260. }