babylon.simpleMaterial.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 DEPTHPREPASS = 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 PushMaterial {
  24. @serializeAsTexture("diffuseTexture")
  25. private _diffuseTexture: BaseTexture;
  26. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  27. public diffuseTexture: BaseTexture;
  28. @serializeAsColor3("diffuseColor")
  29. public diffuseColor = new Color3(1, 1, 1);
  30. @serialize("disableLighting")
  31. private _disableLighting = false;
  32. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  33. public disableLighting: boolean;
  34. @serialize("maxSimultaneousLights")
  35. private _maxSimultaneousLights = 4;
  36. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  37. public maxSimultaneousLights: number;
  38. private _worldViewProjectionMatrix = Matrix.Zero();
  39. private _scaledDiffuse = new Color3();
  40. private _renderId: number;
  41. constructor(name: string, scene: Scene) {
  42. super(name, scene);
  43. }
  44. public needAlphaBlending(): boolean {
  45. return (this.alpha < 1.0);
  46. }
  47. public needAlphaTesting(): boolean {
  48. return false;
  49. }
  50. public getAlphaTestTexture(): BaseTexture {
  51. return null;
  52. }
  53. // Methods
  54. public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances?: boolean): boolean {
  55. if (this.isFrozen) {
  56. if (this._wasPreviouslyReady && subMesh.effect) {
  57. return true;
  58. }
  59. }
  60. if (!subMesh._materialDefines) {
  61. subMesh._materialDefines = new SimpleMaterialDefines();
  62. }
  63. var defines = <SimpleMaterialDefines>subMesh._materialDefines;
  64. var scene = this.getScene();
  65. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  66. if (this._renderId === scene.getRenderId()) {
  67. return true;
  68. }
  69. }
  70. var engine = scene.getEngine();
  71. // Textures
  72. if (defines._areTexturesDirty) {
  73. defines._needUVs = false;
  74. if (scene.texturesEnabled) {
  75. if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  76. if (!this._diffuseTexture.isReady()) {
  77. return false;
  78. } else {
  79. defines._needUVs = true;
  80. defines.DIFFUSE = true;
  81. }
  82. }
  83. }
  84. }
  85. // Misc.
  86. MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, defines);
  87. // Lights
  88. defines._needNormals = MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, false, this._maxSimultaneousLights, this._disableLighting);
  89. // Values that need to be evaluated on every frame
  90. MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances);
  91. // Attribs
  92. MaterialHelper.PrepareDefinesForAttributes(mesh, defines, true, true);
  93. // Get correct effect
  94. if (defines.isDirty) {
  95. defines.markAsProcessed();
  96. scene.resetCachedMaterial();
  97. // Fallbacks
  98. var fallbacks = new EffectFallbacks();
  99. if (defines.FOG) {
  100. fallbacks.addFallback(1, "FOG");
  101. }
  102. MaterialHelper.HandleFallbacksForShadows(defines, fallbacks, this.maxSimultaneousLights);
  103. if (defines.NUM_BONE_INFLUENCERS > 0) {
  104. fallbacks.addCPUSkinningFallback(0, mesh);
  105. }
  106. //Attributes
  107. var attribs = [VertexBuffer.PositionKind];
  108. if (defines.NORMAL) {
  109. attribs.push(VertexBuffer.NormalKind);
  110. }
  111. if (defines.UV1) {
  112. attribs.push(VertexBuffer.UVKind);
  113. }
  114. if (defines.UV2) {
  115. attribs.push(VertexBuffer.UV2Kind);
  116. }
  117. if (defines.VERTEXCOLOR) {
  118. attribs.push(VertexBuffer.ColorKind);
  119. }
  120. MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  121. MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  122. var shaderName = "simple";
  123. var join = defines.toString();
  124. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  125. "vFogInfos", "vFogColor", "pointSize",
  126. "vDiffuseInfos",
  127. "mBones",
  128. "vClipPlane", "diffuseMatrix"
  129. ];
  130. var samplers = ["diffuseSampler"];
  131. var uniformBuffers = [];
  132. MaterialHelper.PrepareUniformsAndSamplersList(<EffectCreationOptions>{
  133. uniformsNames: uniforms,
  134. uniformBuffersNames: uniformBuffers,
  135. samplers: samplers,
  136. defines: defines,
  137. maxSimultaneousLights: this.maxSimultaneousLights
  138. });
  139. subMesh.setEffect(scene.getEngine().createEffect(shaderName,
  140. <EffectCreationOptions>{
  141. attributes: attribs,
  142. uniformsNames: uniforms,
  143. uniformBuffersNames: uniformBuffers,
  144. samplers: samplers,
  145. defines: join,
  146. fallbacks: fallbacks,
  147. onCompiled: this.onCompiled,
  148. onError: this.onError,
  149. indexParameters: { maxSimultaneousLights: this._maxSimultaneousLights - 1 }
  150. }, engine), defines);
  151. }
  152. if (!subMesh.effect.isReady()) {
  153. return false;
  154. }
  155. this._renderId = scene.getRenderId();
  156. this._wasPreviouslyReady = true;
  157. return true;
  158. }
  159. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  160. var scene = this.getScene();
  161. var defines = <SimpleMaterialDefines>subMesh._materialDefines;
  162. if (!defines) {
  163. return;
  164. }
  165. var effect = subMesh.effect;
  166. this._activeEffect = effect;
  167. // Matrices
  168. this.bindOnlyWorldMatrix(world);
  169. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  170. // Bones
  171. MaterialHelper.BindBonesParameters(mesh, this._activeEffect);
  172. if (this._mustRebind(scene, effect)) {
  173. // Textures
  174. if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  175. this._activeEffect.setTexture("diffuseSampler", this._diffuseTexture);
  176. this._activeEffect.setFloat2("vDiffuseInfos", this._diffuseTexture.coordinatesIndex, this._diffuseTexture.level);
  177. this._activeEffect.setMatrix("diffuseMatrix", this._diffuseTexture.getTextureMatrix());
  178. }
  179. // Clip plane
  180. MaterialHelper.BindClipPlane(this._activeEffect, scene);
  181. // Point size
  182. if (this.pointsCloud) {
  183. this._activeEffect.setFloat("pointSize", this.pointSize);
  184. }
  185. this._activeEffect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  186. }
  187. this._activeEffect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  188. // Lights
  189. if (scene.lightsEnabled && !this.disableLighting) {
  190. MaterialHelper.BindLights(scene, mesh, this._activeEffect, defines, this.maxSimultaneousLights);
  191. }
  192. // View
  193. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  194. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  195. }
  196. // Fog
  197. MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  198. this._afterBind(mesh, this._activeEffect);
  199. }
  200. public getAnimatables(): IAnimatable[] {
  201. var results = [];
  202. if (this._diffuseTexture && this._diffuseTexture.animations && this._diffuseTexture.animations.length > 0) {
  203. results.push(this._diffuseTexture);
  204. }
  205. return results;
  206. }
  207. public getActiveTextures(): BaseTexture[] {
  208. var activeTextures = super.getActiveTextures();
  209. if (this._diffuseTexture) {
  210. activeTextures.push(this._diffuseTexture);
  211. }
  212. return activeTextures;
  213. }
  214. public hasTexture(texture: BaseTexture): boolean {
  215. if (super.hasTexture(texture)) {
  216. return true;
  217. }
  218. if (this.diffuseTexture === texture) {
  219. return true;
  220. }
  221. return false;
  222. }
  223. public dispose(forceDisposeEffect?: boolean): void {
  224. if (this._diffuseTexture) {
  225. this._diffuseTexture.dispose();
  226. }
  227. super.dispose(forceDisposeEffect);
  228. }
  229. public clone(name: string): SimpleMaterial {
  230. return SerializationHelper.Clone<SimpleMaterial>(() => new SimpleMaterial(name, this.getScene()), this);
  231. }
  232. public serialize(): any {
  233. var serializationObject = SerializationHelper.Serialize(this);
  234. serializationObject.customType = "BABYLON.SimpleMaterial";
  235. return serializationObject;
  236. }
  237. public getClassName(): string {
  238. return "SimpleMaterial";
  239. }
  240. // Statics
  241. public static Parse(source: any, scene: Scene, rootUrl: string): SimpleMaterial {
  242. return SerializationHelper.Parse(() => new SimpleMaterial(source.name, scene), source, scene, rootUrl);
  243. }
  244. }
  245. }