babylon.triPlanarMaterial.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON {
  3. class TriPlanarMaterialDefines extends MaterialDefines {
  4. public DIFFUSEX = false;
  5. public DIFFUSEY = false;
  6. public DIFFUSEZ = false;
  7. public BUMPX = false;
  8. public BUMPY = false;
  9. public BUMPZ = false;
  10. public CLIPPLANE = false;
  11. public CLIPPLANE2 = false;
  12. public CLIPPLANE3 = false;
  13. public CLIPPLANE4 = false;
  14. public ALPHATEST = false;
  15. public DEPTHPREPASS = false;
  16. public POINTSIZE = false;
  17. public FOG = false;
  18. public SPECULARTERM = false;
  19. public NORMAL = false;
  20. public VERTEXCOLOR = false;
  21. public VERTEXALPHA = false;
  22. public NUM_BONE_INFLUENCERS = 0;
  23. public BonesPerMesh = 0;
  24. public INSTANCES = false;
  25. constructor() {
  26. super();
  27. this.rebuild();
  28. }
  29. }
  30. export class TriPlanarMaterial extends PushMaterial {
  31. @serializeAsTexture()
  32. public mixTexture: BaseTexture;
  33. @serializeAsTexture("diffuseTextureX")
  34. private _diffuseTextureX: BaseTexture;
  35. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  36. public diffuseTextureX: BaseTexture;
  37. @serializeAsTexture("diffuseTexturY")
  38. private _diffuseTextureY: BaseTexture;
  39. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  40. public diffuseTextureY: BaseTexture;
  41. @serializeAsTexture("diffuseTextureZ")
  42. private _diffuseTextureZ: BaseTexture;
  43. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  44. public diffuseTextureZ: BaseTexture;
  45. @serializeAsTexture("normalTextureX")
  46. private _normalTextureX: BaseTexture;
  47. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  48. public normalTextureX: BaseTexture;
  49. @serializeAsTexture("normalTextureY")
  50. private _normalTextureY: BaseTexture;
  51. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  52. public normalTextureY: BaseTexture;
  53. @serializeAsTexture("normalTextureZ")
  54. private _normalTextureZ: BaseTexture;
  55. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  56. public normalTextureZ: BaseTexture;
  57. @serialize()
  58. public tileSize: number = 1;
  59. @serializeAsColor3()
  60. public diffuseColor = new Color3(1, 1, 1);
  61. @serializeAsColor3()
  62. public specularColor = new Color3(0.2, 0.2, 0.2);
  63. @serialize()
  64. public specularPower = 64;
  65. @serialize("disableLighting")
  66. private _disableLighting = false;
  67. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  68. public disableLighting: boolean;
  69. @serialize("maxSimultaneousLights")
  70. private _maxSimultaneousLights = 4;
  71. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  72. public maxSimultaneousLights: number;
  73. private _renderId: number;
  74. constructor(name: string, scene: Scene) {
  75. super(name, scene);
  76. }
  77. public needAlphaBlending(): boolean {
  78. return (this.alpha < 1.0);
  79. }
  80. public needAlphaTesting(): boolean {
  81. return false;
  82. }
  83. public getAlphaTestTexture(): Nullable<BaseTexture> {
  84. return null;
  85. }
  86. // Methods
  87. public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances?: boolean): boolean {
  88. if (this.isFrozen) {
  89. if (this._wasPreviouslyReady && subMesh.effect) {
  90. return true;
  91. }
  92. }
  93. if (!subMesh._materialDefines) {
  94. subMesh._materialDefines = new TriPlanarMaterialDefines();
  95. }
  96. var defines = <TriPlanarMaterialDefines>subMesh._materialDefines;
  97. var scene = this.getScene();
  98. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  99. if (this._renderId === scene.getRenderId()) {
  100. return true;
  101. }
  102. }
  103. var engine = scene.getEngine();
  104. // Textures
  105. if (defines._areTexturesDirty) {
  106. if (scene.texturesEnabled) {
  107. if (StandardMaterial.DiffuseTextureEnabled) {
  108. var textures = [this.diffuseTextureX, this.diffuseTextureY, this.diffuseTextureZ];
  109. var textureDefines = ["DIFFUSEX", "DIFFUSEY", "DIFFUSEZ"];
  110. for (var i = 0; i < textures.length; i++) {
  111. if (textures[i]) {
  112. if (!textures[i].isReady()) {
  113. return false;
  114. } else {
  115. (<any>defines)[textureDefines[i]] = true;
  116. }
  117. }
  118. }
  119. }
  120. if (StandardMaterial.BumpTextureEnabled) {
  121. var textures = [this.normalTextureX, this.normalTextureY, this.normalTextureZ];
  122. var textureDefines = ["BUMPX", "BUMPY", "BUMPZ"];
  123. for (var i = 0; i < textures.length; i++) {
  124. if (textures[i]) {
  125. if (!textures[i].isReady()) {
  126. return false;
  127. } else {
  128. (<any>defines)[textureDefines[i]] = true;
  129. }
  130. }
  131. }
  132. }
  133. }
  134. }
  135. // Misc.
  136. MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, this._shouldTurnAlphaTestOn(mesh), defines);
  137. // Lights
  138. defines._needNormals = MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, false, this._maxSimultaneousLights, this._disableLighting);
  139. // Values that need to be evaluated on every frame
  140. MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances ? true : false);
  141. // Attribs
  142. MaterialHelper.PrepareDefinesForAttributes(mesh, defines, true, true);
  143. // Get correct effect
  144. if (defines.isDirty) {
  145. defines.markAsProcessed();
  146. scene.resetCachedMaterial();
  147. // Fallbacks
  148. var fallbacks = new EffectFallbacks();
  149. if (defines.FOG) {
  150. fallbacks.addFallback(1, "FOG");
  151. }
  152. MaterialHelper.HandleFallbacksForShadows(defines, fallbacks, this.maxSimultaneousLights);
  153. if (defines.NUM_BONE_INFLUENCERS > 0) {
  154. fallbacks.addCPUSkinningFallback(0, mesh);
  155. }
  156. //Attributes
  157. var attribs = [VertexBuffer.PositionKind];
  158. if (defines.NORMAL) {
  159. attribs.push(VertexBuffer.NormalKind);
  160. }
  161. if (defines.VERTEXCOLOR) {
  162. attribs.push(VertexBuffer.ColorKind);
  163. }
  164. MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  165. MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  166. // Legacy browser patch
  167. var shaderName = "triplanar";
  168. var join = defines.toString();
  169. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor", "vSpecularColor",
  170. "vFogInfos", "vFogColor", "pointSize",
  171. "mBones",
  172. "vClipPlane", "vClipPlane2", "vClipPlane3", "vClipPlane4",
  173. "tileSize"
  174. ];
  175. var samplers = ["diffuseSamplerX", "diffuseSamplerY", "diffuseSamplerZ",
  176. "normalSamplerX", "normalSamplerY", "normalSamplerZ"
  177. ];
  178. var uniformBuffers = new Array<string>();
  179. MaterialHelper.PrepareUniformsAndSamplersList(<EffectCreationOptions>{
  180. uniformsNames: uniforms,
  181. uniformBuffersNames: uniformBuffers,
  182. samplers: samplers,
  183. defines: defines,
  184. maxSimultaneousLights: this.maxSimultaneousLights
  185. });
  186. subMesh.setEffect(scene.getEngine().createEffect(shaderName,
  187. <EffectCreationOptions>{
  188. attributes: attribs,
  189. uniformsNames: uniforms,
  190. uniformBuffersNames: uniformBuffers,
  191. samplers: samplers,
  192. defines: join,
  193. fallbacks: fallbacks,
  194. onCompiled: this.onCompiled,
  195. onError: this.onError,
  196. indexParameters: { maxSimultaneousLights: this.maxSimultaneousLights }
  197. }, engine), defines);
  198. }
  199. if (!subMesh.effect || !subMesh.effect.isReady()) {
  200. return false;
  201. }
  202. this._renderId = scene.getRenderId();
  203. this._wasPreviouslyReady = true;
  204. return true;
  205. }
  206. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  207. var scene = this.getScene();
  208. var defines = <TriPlanarMaterialDefines>subMesh._materialDefines;
  209. if (!defines) {
  210. return;
  211. }
  212. var effect = subMesh.effect;
  213. if (!effect) {
  214. return;
  215. }
  216. this._activeEffect = effect;
  217. // Matrices
  218. this.bindOnlyWorldMatrix(world);
  219. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  220. // Bones
  221. MaterialHelper.BindBonesParameters(mesh, this._activeEffect);
  222. this._activeEffect.setFloat("tileSize", this.tileSize);
  223. if (scene.getCachedMaterial() !== this) {
  224. // Textures
  225. if (this.diffuseTextureX) {
  226. this._activeEffect.setTexture("diffuseSamplerX", this.diffuseTextureX);
  227. }
  228. if (this.diffuseTextureY) {
  229. this._activeEffect.setTexture("diffuseSamplerY", this.diffuseTextureY);
  230. }
  231. if (this.diffuseTextureZ) {
  232. this._activeEffect.setTexture("diffuseSamplerZ", this.diffuseTextureZ);
  233. }
  234. if (this.normalTextureX) {
  235. this._activeEffect.setTexture("normalSamplerX", this.normalTextureX);
  236. }
  237. if (this.normalTextureY) {
  238. this._activeEffect.setTexture("normalSamplerY", this.normalTextureY);
  239. }
  240. if (this.normalTextureZ) {
  241. this._activeEffect.setTexture("normalSamplerZ", this.normalTextureZ);
  242. }
  243. // Clip plane
  244. MaterialHelper.BindClipPlane(this._activeEffect, scene);
  245. // Point size
  246. if (this.pointsCloud) {
  247. this._activeEffect.setFloat("pointSize", this.pointSize);
  248. }
  249. MaterialHelper.BindEyePosition(effect, scene);
  250. }
  251. this._activeEffect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  252. if (defines.SPECULARTERM) {
  253. this._activeEffect.setColor4("vSpecularColor", this.specularColor, this.specularPower);
  254. }
  255. if (scene.lightsEnabled && !this.disableLighting) {
  256. MaterialHelper.BindLights(scene, mesh, this._activeEffect, defines, this.maxSimultaneousLights);
  257. }
  258. // View
  259. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  260. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  261. }
  262. // Fog
  263. MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  264. this._afterBind(mesh, this._activeEffect);
  265. }
  266. public getAnimatables(): IAnimatable[] {
  267. var results = [];
  268. if (this.mixTexture && this.mixTexture.animations && this.mixTexture.animations.length > 0) {
  269. results.push(this.mixTexture);
  270. }
  271. return results;
  272. }
  273. public getActiveTextures(): BaseTexture[] {
  274. var activeTextures = super.getActiveTextures();
  275. if (this._diffuseTextureX) {
  276. activeTextures.push(this._diffuseTextureX);
  277. }
  278. if (this._diffuseTextureY) {
  279. activeTextures.push(this._diffuseTextureY);
  280. }
  281. if (this._diffuseTextureZ) {
  282. activeTextures.push(this._diffuseTextureZ);
  283. }
  284. if (this._normalTextureX) {
  285. activeTextures.push(this._normalTextureX);
  286. }
  287. if (this._normalTextureY) {
  288. activeTextures.push(this._normalTextureY);
  289. }
  290. if (this._normalTextureZ) {
  291. activeTextures.push(this._normalTextureZ);
  292. }
  293. return activeTextures;
  294. }
  295. public hasTexture(texture: BaseTexture): boolean {
  296. if (super.hasTexture(texture)) {
  297. return true;
  298. }
  299. if (this._diffuseTextureX === texture) {
  300. return true;
  301. }
  302. if (this._diffuseTextureY === texture) {
  303. return true;
  304. }
  305. if (this._diffuseTextureZ === texture) {
  306. return true;
  307. }
  308. if (this._normalTextureX === texture) {
  309. return true;
  310. }
  311. if (this._normalTextureY === texture) {
  312. return true;
  313. }
  314. if (this._normalTextureZ === texture) {
  315. return true;
  316. }
  317. return false;
  318. }
  319. public dispose(forceDisposeEffect?: boolean): void {
  320. if (this.mixTexture) {
  321. this.mixTexture.dispose();
  322. }
  323. super.dispose(forceDisposeEffect);
  324. }
  325. public clone(name: string): TriPlanarMaterial {
  326. return SerializationHelper.Clone(() => new TriPlanarMaterial(name, this.getScene()), this);
  327. }
  328. public serialize(): any {
  329. var serializationObject = SerializationHelper.Serialize(this);
  330. serializationObject.customType = "BABYLON.TriPlanarMaterial";
  331. return serializationObject;
  332. }
  333. public getClassName(): string {
  334. return "TriPlanarMaterial";
  335. }
  336. // Statics
  337. public static Parse(source: any, scene: Scene, rootUrl: string): TriPlanarMaterial {
  338. return SerializationHelper.Parse(() => new TriPlanarMaterial(source.name, scene), source, scene, rootUrl);
  339. }
  340. }
  341. }