babylon.triPlanarMaterial.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON {
  3. var maxSimultaneousLights = 4;
  4. class TriPlanarMaterialDefines extends MaterialDefines {
  5. public DIFFUSEX = false;
  6. public DIFFUSEY = false;
  7. public DIFFUSEZ = false;
  8. public BUMPX = false;
  9. public BUMPY = false;
  10. public BUMPZ = false;
  11. public CLIPPLANE = false;
  12. public ALPHATEST = 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 Material {
  28. @serializeAsTexture()
  29. public mixTexture: BaseTexture;
  30. @serializeAsTexture()
  31. public diffuseTextureX: Texture;
  32. @serializeAsTexture()
  33. public diffuseTextureY: Texture;
  34. @serializeAsTexture()
  35. public diffuseTextureZ: Texture;
  36. @serializeAsTexture()
  37. public normalTextureX: Texture;
  38. @serializeAsTexture()
  39. public normalTextureY: Texture;
  40. @serializeAsTexture()
  41. public normalTextureZ: Texture;
  42. @serialize()
  43. public tileSize: number = 1;
  44. @serializeAsColor3()
  45. public diffuseColor = new Color3(1, 1, 1);
  46. @serializeAsColor3()
  47. public specularColor = new Color3(0.2, 0.2, 0.2);
  48. @serialize()
  49. public specularPower = 64;
  50. @serialize()
  51. public disableLighting = false;
  52. @serialize()
  53. public maxSimultaneousLights = 4;
  54. private _worldViewProjectionMatrix = Matrix.Zero();
  55. private _renderId: number;
  56. private _defines = new TriPlanarMaterialDefines();
  57. private _cachedDefines = new TriPlanarMaterialDefines();
  58. constructor(name: string, scene: Scene) {
  59. super(name, scene);
  60. this._cachedDefines.BonesPerMesh = -1;
  61. }
  62. public needAlphaBlending(): boolean {
  63. return (this.alpha < 1.0);
  64. }
  65. public needAlphaTesting(): boolean {
  66. return false;
  67. }
  68. public getAlphaTestTexture(): BaseTexture {
  69. return null;
  70. }
  71. // Methods
  72. private _checkCache(scene: Scene, mesh?: AbstractMesh, useInstances?: boolean): boolean {
  73. if (!mesh) {
  74. return true;
  75. }
  76. if (this._defines.INSTANCES !== useInstances) {
  77. return false;
  78. }
  79. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  80. return true;
  81. }
  82. return false;
  83. }
  84. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  85. if (this.checkReadyOnlyOnce) {
  86. if (this._wasPreviouslyReady) {
  87. return true;
  88. }
  89. }
  90. var scene = this.getScene();
  91. if (!this.checkReadyOnEveryCall) {
  92. if (this._renderId === scene.getRenderId()) {
  93. if (this._checkCache(scene, mesh, useInstances)) {
  94. return true;
  95. }
  96. }
  97. }
  98. var engine = scene.getEngine();
  99. var needNormals = false;
  100. this._defines.reset();
  101. // Textures
  102. if (scene.texturesEnabled) {
  103. if (StandardMaterial.DiffuseTextureEnabled) {
  104. var textures = [this.diffuseTextureX, this.diffuseTextureY, this.diffuseTextureZ];
  105. var textureDefines = ["DIFFUSEX", "DIFFUSEY", "DIFFUSEZ"];
  106. for (var i=0; i < textures.length; i++) {
  107. if (textures[i]) {
  108. if (!textures[i].isReady()) {
  109. return false;
  110. } else {
  111. this._defines[textureDefines[i]] = true;
  112. }
  113. }
  114. }
  115. }
  116. if (StandardMaterial.BumpTextureEnabled) {
  117. var textures = [this.normalTextureX, this.normalTextureY, this.normalTextureZ];
  118. var textureDefines = ["BUMPX", "BUMPY", "BUMPZ"];
  119. for (var i=0; i < textures.length; i++) {
  120. if (textures[i]) {
  121. if (!textures[i].isReady()) {
  122. return false;
  123. } else {
  124. this._defines[textureDefines[i]] = true;
  125. }
  126. }
  127. }
  128. }
  129. }
  130. // Effect
  131. if (scene.clipPlane) {
  132. this._defines.CLIPPLANE = true;
  133. }
  134. if (engine.getAlphaTesting()) {
  135. this._defines.ALPHATEST = true;
  136. }
  137. // Point size
  138. if (this.pointsCloud || scene.forcePointsCloud) {
  139. this._defines.POINTSIZE = true;
  140. }
  141. // Fog
  142. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled) {
  143. this._defines.FOG = true;
  144. }
  145. // Lights
  146. if (scene.lightsEnabled && !this.disableLighting) {
  147. needNormals = MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines, this.maxSimultaneousLights);
  148. }
  149. // Attribs
  150. if (mesh) {
  151. if (needNormals && mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  152. this._defines.NORMAL = true;
  153. }
  154. if (mesh.useVertexColors && mesh.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  155. this._defines.VERTEXCOLOR = true;
  156. if (mesh.hasVertexAlpha) {
  157. this._defines.VERTEXALPHA = true;
  158. }
  159. }
  160. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  161. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  162. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  163. }
  164. // Instances
  165. if (useInstances) {
  166. this._defines.INSTANCES = true;
  167. }
  168. }
  169. // Get correct effect
  170. if (!this._defines.isEqual(this._cachedDefines)) {
  171. this._defines.cloneTo(this._cachedDefines);
  172. scene.resetCachedMaterial();
  173. // Fallbacks
  174. var fallbacks = new EffectFallbacks();
  175. if (this._defines.FOG) {
  176. fallbacks.addFallback(1, "FOG");
  177. }
  178. MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks, this.maxSimultaneousLights);
  179. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  180. fallbacks.addCPUSkinningFallback(0, mesh);
  181. }
  182. //Attributes
  183. var attribs = [VertexBuffer.PositionKind];
  184. if (this._defines.NORMAL) {
  185. attribs.push(VertexBuffer.NormalKind);
  186. }
  187. if (this._defines.VERTEXCOLOR) {
  188. attribs.push(VertexBuffer.ColorKind);
  189. }
  190. MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  191. MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  192. // Legacy browser patch
  193. var shaderName = "triplanar";
  194. var join = this._defines.toString();
  195. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor", "vSpecularColor",
  196. "vFogInfos", "vFogColor", "pointSize",
  197. "mBones",
  198. "vClipPlane",
  199. "tileSize"
  200. ];
  201. var samplers = ["diffuseSamplerX", "diffuseSamplerY", "diffuseSamplerZ",
  202. "normalSamplerX", "normalSamplerY", "normalSamplerZ"
  203. ];
  204. MaterialHelper.PrepareUniformsAndSamplersList(uniforms, samplers, this._defines, this.maxSimultaneousLights);
  205. this._effect = scene.getEngine().createEffect(shaderName,
  206. attribs, uniforms, samplers,
  207. join, fallbacks, this.onCompiled, this.onError, { maxSimultaneousLights: this.maxSimultaneousLights });
  208. }
  209. if (!this._effect.isReady()) {
  210. return false;
  211. }
  212. this._renderId = scene.getRenderId();
  213. this._wasPreviouslyReady = true;
  214. if (mesh) {
  215. if (!mesh._materialDefines) {
  216. mesh._materialDefines = new TriPlanarMaterialDefines();
  217. }
  218. this._defines.cloneTo(mesh._materialDefines);
  219. }
  220. return true;
  221. }
  222. public bindOnlyWorldMatrix(world: Matrix): void {
  223. this._effect.setMatrix("world", world);
  224. }
  225. public bind(world: Matrix, mesh?: Mesh): void {
  226. var scene = this.getScene();
  227. // Matrices
  228. this.bindOnlyWorldMatrix(world);
  229. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  230. // Bones
  231. MaterialHelper.BindBonesParameters(mesh, this._effect);
  232. this._effect.setFloat("tileSize", this.tileSize);
  233. if (scene.getCachedMaterial() !== this) {
  234. // Textures
  235. if (this.diffuseTextureX) {
  236. this._effect.setTexture("diffuseSamplerX", this.diffuseTextureX);
  237. }
  238. if (this.diffuseTextureY) {
  239. this._effect.setTexture("diffuseSamplerY", this.diffuseTextureY);
  240. }
  241. if (this.diffuseTextureZ) {
  242. this._effect.setTexture("diffuseSamplerZ", this.diffuseTextureZ);
  243. }
  244. if (this.normalTextureX) {
  245. this._effect.setTexture("normalSamplerX", this.normalTextureX);
  246. }
  247. if (this.normalTextureY) {
  248. this._effect.setTexture("normalSamplerY", this.normalTextureY);
  249. }
  250. if (this.normalTextureZ) {
  251. this._effect.setTexture("normalSamplerZ", this.normalTextureZ);
  252. }
  253. // Clip plane
  254. MaterialHelper.BindClipPlane(this._effect, scene);
  255. // Point size
  256. if (this.pointsCloud) {
  257. this._effect.setFloat("pointSize", this.pointSize);
  258. }
  259. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  260. }
  261. this._effect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  262. if (this._defines.SPECULARTERM) {
  263. this._effect.setColor4("vSpecularColor", this.specularColor, this.specularPower);
  264. }
  265. if (scene.lightsEnabled && !this.disableLighting) {
  266. MaterialHelper.BindLights(scene, mesh, this._effect, this._defines, this.maxSimultaneousLights);
  267. }
  268. // View
  269. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  270. this._effect.setMatrix("view", scene.getViewMatrix());
  271. }
  272. // Fog
  273. MaterialHelper.BindFogParameters(scene, mesh, this._effect);
  274. super.bind(world, mesh);
  275. }
  276. public getAnimatables(): IAnimatable[] {
  277. var results = [];
  278. if (this.mixTexture && this.mixTexture.animations && this.mixTexture.animations.length > 0) {
  279. results.push(this.mixTexture);
  280. }
  281. return results;
  282. }
  283. public dispose(forceDisposeEffect?: boolean): void {
  284. if (this.mixTexture) {
  285. this.mixTexture.dispose();
  286. }
  287. super.dispose(forceDisposeEffect);
  288. }
  289. public clone(name: string): TriPlanarMaterial {
  290. return SerializationHelper.Clone(() => new TriPlanarMaterial(name, this.getScene()), this);
  291. }
  292. public serialize(): any {
  293. var serializationObject = SerializationHelper.Serialize(this);
  294. serializationObject.customType = "BABYLON.TriPlanarMaterial";
  295. return serializationObject;
  296. }
  297. // Statics
  298. public static Parse(source: any, scene: Scene, rootUrl: string): TriPlanarMaterial {
  299. return SerializationHelper.Parse(() => new TriPlanarMaterial(source.name, scene), source, scene, rootUrl);
  300. }
  301. }
  302. }