triPlanarMaterial.ts 15 KB

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