babylon.triPlanarMaterial.ts 16 KB

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