babylon.cellMaterial.ts 11 KB

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