triPlanarMaterial.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. public IMAGEPROCESSINGPOSTPROCESS = false;
  46. constructor() {
  47. super();
  48. this.rebuild();
  49. }
  50. }
  51. export class TriPlanarMaterial extends PushMaterial {
  52. @serializeAsTexture()
  53. public mixTexture: BaseTexture;
  54. @serializeAsTexture("diffuseTextureX")
  55. private _diffuseTextureX: BaseTexture;
  56. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  57. public diffuseTextureX: BaseTexture;
  58. @serializeAsTexture("diffuseTexturY")
  59. private _diffuseTextureY: BaseTexture;
  60. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  61. public diffuseTextureY: BaseTexture;
  62. @serializeAsTexture("diffuseTextureZ")
  63. private _diffuseTextureZ: BaseTexture;
  64. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  65. public diffuseTextureZ: BaseTexture;
  66. @serializeAsTexture("normalTextureX")
  67. private _normalTextureX: BaseTexture;
  68. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  69. public normalTextureX: BaseTexture;
  70. @serializeAsTexture("normalTextureY")
  71. private _normalTextureY: BaseTexture;
  72. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  73. public normalTextureY: BaseTexture;
  74. @serializeAsTexture("normalTextureZ")
  75. private _normalTextureZ: BaseTexture;
  76. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  77. public normalTextureZ: BaseTexture;
  78. @serialize()
  79. public tileSize: number = 1;
  80. @serializeAsColor3()
  81. public diffuseColor = new Color3(1, 1, 1);
  82. @serializeAsColor3()
  83. public specularColor = new Color3(0.2, 0.2, 0.2);
  84. @serialize()
  85. public specularPower = 64;
  86. @serialize("disableLighting")
  87. private _disableLighting = false;
  88. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  89. public disableLighting: boolean;
  90. @serialize("maxSimultaneousLights")
  91. private _maxSimultaneousLights = 4;
  92. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  93. public maxSimultaneousLights: 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 (subMesh.effect && subMesh.effect._wasPreviouslyReady) {
  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._isReadyForSubMesh(subMesh)) {
  119. return true;
  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. defines.IMAGEPROCESSINGPOSTPROCESS = scene.imageProcessingConfiguration.applyByPostProcess;
  175. //Attributes
  176. var attribs = [VertexBuffer.PositionKind];
  177. if (defines.NORMAL) {
  178. attribs.push(VertexBuffer.NormalKind);
  179. }
  180. if (defines.VERTEXCOLOR) {
  181. attribs.push(VertexBuffer.ColorKind);
  182. }
  183. MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  184. MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  185. // Legacy browser patch
  186. var shaderName = "triplanar";
  187. var join = defines.toString();
  188. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor", "vSpecularColor",
  189. "vFogInfos", "vFogColor", "pointSize",
  190. "mBones",
  191. "vClipPlane", "vClipPlane2", "vClipPlane3", "vClipPlane4", "vClipPlane5", "vClipPlane6",
  192. "tileSize"
  193. ];
  194. var samplers = ["diffuseSamplerX", "diffuseSamplerY", "diffuseSamplerZ",
  195. "normalSamplerX", "normalSamplerY", "normalSamplerZ"
  196. ];
  197. var uniformBuffers = new Array<string>();
  198. MaterialHelper.PrepareUniformsAndSamplersList(<IEffectCreationOptions>{
  199. uniformsNames: uniforms,
  200. uniformBuffersNames: uniformBuffers,
  201. samplers: samplers,
  202. defines: defines,
  203. maxSimultaneousLights: this.maxSimultaneousLights
  204. });
  205. subMesh.setEffect(scene.getEngine().createEffect(shaderName,
  206. <IEffectCreationOptions>{
  207. attributes: attribs,
  208. uniformsNames: uniforms,
  209. uniformBuffersNames: uniformBuffers,
  210. samplers: samplers,
  211. defines: join,
  212. fallbacks: fallbacks,
  213. onCompiled: this.onCompiled,
  214. onError: this.onError,
  215. indexParameters: { maxSimultaneousLights: this.maxSimultaneousLights }
  216. }, engine), defines);
  217. }
  218. if (!subMesh.effect || !subMesh.effect.isReady()) {
  219. return false;
  220. }
  221. defines._renderId = scene.getRenderId();
  222. subMesh.effect._wasPreviouslyReady = true;
  223. return true;
  224. }
  225. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  226. var scene = this.getScene();
  227. var defines = <TriPlanarMaterialDefines>subMesh._materialDefines;
  228. if (!defines) {
  229. return;
  230. }
  231. var effect = subMesh.effect;
  232. if (!effect) {
  233. return;
  234. }
  235. this._activeEffect = effect;
  236. // Matrices
  237. this.bindOnlyWorldMatrix(world);
  238. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  239. // Bones
  240. MaterialHelper.BindBonesParameters(mesh, this._activeEffect);
  241. this._activeEffect.setFloat("tileSize", this.tileSize);
  242. if (scene.getCachedMaterial() !== this) {
  243. // Textures
  244. if (this.diffuseTextureX) {
  245. this._activeEffect.setTexture("diffuseSamplerX", this.diffuseTextureX);
  246. }
  247. if (this.diffuseTextureY) {
  248. this._activeEffect.setTexture("diffuseSamplerY", this.diffuseTextureY);
  249. }
  250. if (this.diffuseTextureZ) {
  251. this._activeEffect.setTexture("diffuseSamplerZ", this.diffuseTextureZ);
  252. }
  253. if (this.normalTextureX) {
  254. this._activeEffect.setTexture("normalSamplerX", this.normalTextureX);
  255. }
  256. if (this.normalTextureY) {
  257. this._activeEffect.setTexture("normalSamplerY", this.normalTextureY);
  258. }
  259. if (this.normalTextureZ) {
  260. this._activeEffect.setTexture("normalSamplerZ", this.normalTextureZ);
  261. }
  262. // Clip plane
  263. MaterialHelper.BindClipPlane(this._activeEffect, scene);
  264. // Point size
  265. if (this.pointsCloud) {
  266. this._activeEffect.setFloat("pointSize", this.pointSize);
  267. }
  268. MaterialHelper.BindEyePosition(effect, scene);
  269. }
  270. this._activeEffect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  271. if (defines.SPECULARTERM) {
  272. this._activeEffect.setColor4("vSpecularColor", this.specularColor, this.specularPower);
  273. }
  274. if (scene.lightsEnabled && !this.disableLighting) {
  275. MaterialHelper.BindLights(scene, mesh, this._activeEffect, defines, this.maxSimultaneousLights);
  276. }
  277. // View
  278. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  279. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  280. }
  281. // Fog
  282. MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  283. this._afterBind(mesh, this._activeEffect);
  284. }
  285. public getAnimatables(): IAnimatable[] {
  286. var results = [];
  287. if (this.mixTexture && this.mixTexture.animations && this.mixTexture.animations.length > 0) {
  288. results.push(this.mixTexture);
  289. }
  290. return results;
  291. }
  292. public getActiveTextures(): BaseTexture[] {
  293. var activeTextures = super.getActiveTextures();
  294. if (this._diffuseTextureX) {
  295. activeTextures.push(this._diffuseTextureX);
  296. }
  297. if (this._diffuseTextureY) {
  298. activeTextures.push(this._diffuseTextureY);
  299. }
  300. if (this._diffuseTextureZ) {
  301. activeTextures.push(this._diffuseTextureZ);
  302. }
  303. if (this._normalTextureX) {
  304. activeTextures.push(this._normalTextureX);
  305. }
  306. if (this._normalTextureY) {
  307. activeTextures.push(this._normalTextureY);
  308. }
  309. if (this._normalTextureZ) {
  310. activeTextures.push(this._normalTextureZ);
  311. }
  312. return activeTextures;
  313. }
  314. public hasTexture(texture: BaseTexture): boolean {
  315. if (super.hasTexture(texture)) {
  316. return true;
  317. }
  318. if (this._diffuseTextureX === texture) {
  319. return true;
  320. }
  321. if (this._diffuseTextureY === texture) {
  322. return true;
  323. }
  324. if (this._diffuseTextureZ === texture) {
  325. return true;
  326. }
  327. if (this._normalTextureX === texture) {
  328. return true;
  329. }
  330. if (this._normalTextureY === texture) {
  331. return true;
  332. }
  333. if (this._normalTextureZ === texture) {
  334. return true;
  335. }
  336. return false;
  337. }
  338. public dispose(forceDisposeEffect?: boolean): void {
  339. if (this.mixTexture) {
  340. this.mixTexture.dispose();
  341. }
  342. super.dispose(forceDisposeEffect);
  343. }
  344. public clone(name: string): TriPlanarMaterial {
  345. return SerializationHelper.Clone(() => new TriPlanarMaterial(name, this.getScene()), this);
  346. }
  347. public serialize(): any {
  348. var serializationObject = SerializationHelper.Serialize(this);
  349. serializationObject.customType = "BABYLON.TriPlanarMaterial";
  350. return serializationObject;
  351. }
  352. public getClassName(): string {
  353. return "TriPlanarMaterial";
  354. }
  355. // Statics
  356. public static Parse(source: any, scene: Scene, rootUrl: string): TriPlanarMaterial {
  357. return SerializationHelper.Parse(() => new TriPlanarMaterial(source.name, scene), source, scene, rootUrl);
  358. }
  359. }
  360. _TypeStore.RegisteredTypes["BABYLON.TriPlanarMaterial"] = TriPlanarMaterial;