triPlanarMaterial.ts 15 KB

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