babylon.cellMaterial.ts 12 KB

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