babylon.triPlanarMaterial.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 POINTSIZE = false;
  13. public FOG = false;
  14. public SPECULARTERM = false;
  15. public NORMAL = false;
  16. public VERTEXCOLOR = false;
  17. public VERTEXALPHA = false;
  18. public NUM_BONE_INFLUENCERS = 0;
  19. public BonesPerMesh = 0;
  20. public INSTANCES = false;
  21. constructor() {
  22. super();
  23. this.rebuild();
  24. }
  25. }
  26. export class TriPlanarMaterial extends PushMaterial {
  27. @serializeAsTexture()
  28. public mixTexture: BaseTexture;
  29. @serializeAsTexture("diffuseTextureX")
  30. private _diffuseTextureX: BaseTexture;
  31. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  32. public diffuseTextureX: BaseTexture;
  33. @serializeAsTexture("diffuseTexturY")
  34. private _diffuseTextureY: BaseTexture;
  35. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  36. public diffuseTextureY: BaseTexture;
  37. @serializeAsTexture("diffuseTextureZ")
  38. private _diffuseTextureZ: BaseTexture;
  39. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  40. public diffuseTextureZ: BaseTexture;
  41. @serializeAsTexture("normalTextureX")
  42. private _normalTextureX: BaseTexture;
  43. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  44. public normalTextureX: BaseTexture;
  45. @serializeAsTexture("normalTextureY")
  46. private _normalTextureY: BaseTexture;
  47. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  48. public normalTextureY: BaseTexture;
  49. @serializeAsTexture("normalTextureZ")
  50. private _normalTextureZ: BaseTexture;
  51. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  52. public normalTextureZ: BaseTexture;
  53. @serialize()
  54. public tileSize: number = 1;
  55. @serializeAsColor3()
  56. public diffuseColor = new Color3(1, 1, 1);
  57. @serializeAsColor3()
  58. public specularColor = new Color3(0.2, 0.2, 0.2);
  59. @serialize()
  60. public specularPower = 64;
  61. @serialize("disableLighting")
  62. private _disableLighting = false;
  63. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  64. public disableLighting: boolean;
  65. @serialize("maxSimultaneousLights")
  66. private _maxSimultaneousLights = 4;
  67. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  68. public maxSimultaneousLights: number;
  69. private _worldViewProjectionMatrix = Matrix.Zero();
  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. 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. 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 = [];
  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 dispose(forceDisposeEffect?: boolean): void {
  268. if (this.mixTexture) {
  269. this.mixTexture.dispose();
  270. }
  271. super.dispose(forceDisposeEffect);
  272. }
  273. public clone(name: string): TriPlanarMaterial {
  274. return SerializationHelper.Clone(() => new TriPlanarMaterial(name, this.getScene()), this);
  275. }
  276. public serialize(): any {
  277. var serializationObject = SerializationHelper.Serialize(this);
  278. serializationObject.customType = "BABYLON.TriPlanarMaterial";
  279. return serializationObject;
  280. }
  281. // Statics
  282. public static Parse(source: any, scene: Scene, rootUrl: string): TriPlanarMaterial {
  283. return SerializationHelper.Parse(() => new TriPlanarMaterial(source.name, scene), source, scene, rootUrl);
  284. }
  285. }
  286. }