babylon.triPlanarMaterial.ts 15 KB

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