babylon.simpleMaterial.ts 12 KB

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