babylon.triPlanarMaterial.ts 15 KB

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