babylon.simpleMaterial.ts 9.9 KB

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