babylon.triPlanarMaterial.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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 LIGHT0 = false;
  16. public LIGHT1 = false;
  17. public LIGHT2 = false;
  18. public LIGHT3 = false;
  19. public SPOTLIGHT0 = false;
  20. public SPOTLIGHT1 = false;
  21. public SPOTLIGHT2 = false;
  22. public SPOTLIGHT3 = false;
  23. public HEMILIGHT0 = false;
  24. public HEMILIGHT1 = false;
  25. public HEMILIGHT2 = false;
  26. public HEMILIGHT3 = false;
  27. public DIRLIGHT0 = false;
  28. public DIRLIGHT1 = false;
  29. public DIRLIGHT2 = false;
  30. public DIRLIGHT3 = false;
  31. public POINTLIGHT0 = false;
  32. public POINTLIGHT1 = false;
  33. public POINTLIGHT2 = false;
  34. public POINTLIGHT3 = false;
  35. public SHADOW0 = false;
  36. public SHADOW1 = false;
  37. public SHADOW2 = false;
  38. public SHADOW3 = false;
  39. public SHADOWS = false;
  40. public SHADOWVSM0 = false;
  41. public SHADOWVSM1 = false;
  42. public SHADOWVSM2 = false;
  43. public SHADOWVSM3 = false;
  44. public SHADOWPCF0 = false;
  45. public SHADOWPCF1 = false;
  46. public SHADOWPCF2 = false;
  47. public SHADOWPCF3 = false;
  48. public SPECULARTERM = false;
  49. public NORMAL = false;
  50. public VERTEXCOLOR = false;
  51. public VERTEXALPHA = false;
  52. public BONES = false;
  53. public BONES4 = false;
  54. public BonesPerMesh = 0;
  55. public INSTANCES = false;
  56. constructor() {
  57. super();
  58. this._keys = Object.keys(this);
  59. }
  60. }
  61. export class TriPlanarMaterial extends Material {
  62. public mixTexture: BaseTexture;
  63. public diffuseTextureX: Texture;
  64. public diffuseTextureY: Texture;
  65. public diffuseTextureZ: Texture;
  66. public normalTextureX: Texture;
  67. public normalTextureY: Texture;
  68. public normalTextureZ: Texture;
  69. public tileSize: number = 1;
  70. public diffuseColor = new Color3(1, 1, 1);
  71. public specularColor = new Color3(0.2, 0.2, 0.2);
  72. public specularPower = 64;
  73. public disableLighting = false;
  74. private _worldViewProjectionMatrix = Matrix.Zero();
  75. private _scaledDiffuse = new Color3();
  76. private _scaledSpecular = new Color3();
  77. private _renderId: number;
  78. private _defines = new TriPlanarMaterialDefines();
  79. private _cachedDefines = new TriPlanarMaterialDefines();
  80. constructor(name: string, scene: Scene) {
  81. super(name, scene);
  82. this._cachedDefines.BonesPerMesh = -1;
  83. }
  84. public needAlphaBlending(): boolean {
  85. return (this.alpha < 1.0);
  86. }
  87. public needAlphaTesting(): boolean {
  88. return false;
  89. }
  90. public getAlphaTestTexture(): BaseTexture {
  91. return null;
  92. }
  93. // Methods
  94. private _checkCache(scene: Scene, mesh?: AbstractMesh, useInstances?: boolean): boolean {
  95. if (!mesh) {
  96. return true;
  97. }
  98. if (this._defines.INSTANCES !== useInstances) {
  99. return false;
  100. }
  101. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  102. return true;
  103. }
  104. return false;
  105. }
  106. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  107. if (this.checkReadyOnlyOnce) {
  108. if (this._wasPreviouslyReady) {
  109. return true;
  110. }
  111. }
  112. var scene = this.getScene();
  113. if (!this.checkReadyOnEveryCall) {
  114. if (this._renderId === scene.getRenderId()) {
  115. if (this._checkCache(scene, mesh, useInstances)) {
  116. return true;
  117. }
  118. }
  119. }
  120. var engine = scene.getEngine();
  121. var needNormals = false;
  122. this._defines.reset();
  123. // Textures
  124. if (scene.texturesEnabled) {
  125. if (StandardMaterial.DiffuseTextureEnabled) {
  126. var textures = [this.diffuseTextureX, this.diffuseTextureY, this.diffuseTextureZ];
  127. var textureDefines = ["DIFFUSEX", "DIFFUSEY", "DIFFUSEZ"];
  128. for (var i=0; i < textures.length; i++) {
  129. if (textures[i]) {
  130. if (!textures[i].isReady()) {
  131. return false;
  132. } else {
  133. this._defines[textureDefines[i]] = true;
  134. }
  135. }
  136. }
  137. }
  138. if (StandardMaterial.BumpTextureEnabled) {
  139. var textures = [this.normalTextureX, this.normalTextureY, this.normalTextureZ];
  140. var textureDefines = ["BUMPX", "BUMPY", "BUMPZ"];
  141. for (var i=0; i < textures.length; i++) {
  142. if (textures[i]) {
  143. if (!textures[i].isReady()) {
  144. return false;
  145. } else {
  146. this._defines[textureDefines[i]] = true;
  147. }
  148. }
  149. }
  150. }
  151. }
  152. // Effect
  153. if (scene.clipPlane) {
  154. this._defines.CLIPPLANE = true;
  155. }
  156. if (engine.getAlphaTesting()) {
  157. this._defines.ALPHATEST = true;
  158. }
  159. // Point size
  160. if (this.pointsCloud || scene.forcePointsCloud) {
  161. this._defines.POINTSIZE = true;
  162. }
  163. // Fog
  164. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled) {
  165. this._defines.FOG = true;
  166. }
  167. var lightIndex = 0;
  168. if (scene.lightsEnabled && !this.disableLighting) {
  169. for (var index = 0; index < scene.lights.length; index++) {
  170. var light = scene.lights[index];
  171. if (!light.isEnabled()) {
  172. continue;
  173. }
  174. // Excluded check
  175. if (light._excludedMeshesIds.length > 0) {
  176. for (var excludedIndex = 0; excludedIndex < light._excludedMeshesIds.length; excludedIndex++) {
  177. var excludedMesh = scene.getMeshByID(light._excludedMeshesIds[excludedIndex]);
  178. if (excludedMesh) {
  179. light.excludedMeshes.push(excludedMesh);
  180. }
  181. }
  182. light._excludedMeshesIds = [];
  183. }
  184. // Included check
  185. if (light._includedOnlyMeshesIds.length > 0) {
  186. for (var includedOnlyIndex = 0; includedOnlyIndex < light._includedOnlyMeshesIds.length; includedOnlyIndex++) {
  187. var includedOnlyMesh = scene.getMeshByID(light._includedOnlyMeshesIds[includedOnlyIndex]);
  188. if (includedOnlyMesh) {
  189. light.includedOnlyMeshes.push(includedOnlyMesh);
  190. }
  191. }
  192. light._includedOnlyMeshesIds = [];
  193. }
  194. if (!light.canAffectMesh(mesh)) {
  195. continue;
  196. }
  197. needNormals = true;
  198. this._defines["LIGHT" + lightIndex] = true;
  199. var type;
  200. if (light instanceof SpotLight) {
  201. type = "SPOTLIGHT" + lightIndex;
  202. } else if (light instanceof HemisphericLight) {
  203. type = "HEMILIGHT" + lightIndex;
  204. } else if (light instanceof PointLight) {
  205. type = "POINTLIGHT" + lightIndex;
  206. } else {
  207. type = "DIRLIGHT" + lightIndex;
  208. }
  209. this._defines[type] = true;
  210. // Specular
  211. if (!light.specular.equalsFloats(0, 0, 0)) {
  212. this._defines.SPECULARTERM = true;
  213. }
  214. // Shadows
  215. if (scene.shadowsEnabled) {
  216. var shadowGenerator = light.getShadowGenerator();
  217. if (mesh && mesh.receiveShadows && shadowGenerator) {
  218. this._defines["SHADOW" + lightIndex] = true;
  219. this._defines.SHADOWS = true;
  220. if (shadowGenerator.useVarianceShadowMap || shadowGenerator.useBlurVarianceShadowMap) {
  221. this._defines["SHADOWVSM" + lightIndex] = true;
  222. }
  223. if (shadowGenerator.usePoissonSampling) {
  224. this._defines["SHADOWPCF" + lightIndex] = true;
  225. }
  226. }
  227. }
  228. lightIndex++;
  229. if (lightIndex === maxSimultaneousLights)
  230. break;
  231. }
  232. }
  233. // Attribs
  234. if (mesh) {
  235. if (needNormals && mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  236. this._defines.NORMAL = true;
  237. }
  238. if (mesh.useVertexColors && mesh.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  239. this._defines.VERTEXCOLOR = true;
  240. if (mesh.hasVertexAlpha) {
  241. this._defines.VERTEXALPHA = true;
  242. }
  243. }
  244. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  245. this._defines.BONES = true;
  246. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  247. this._defines.BONES4 = true;
  248. }
  249. // Instances
  250. if (useInstances) {
  251. this._defines.INSTANCES = true;
  252. }
  253. }
  254. // Get correct effect
  255. if (!this._defines.isEqual(this._cachedDefines)) {
  256. this._defines.cloneTo(this._cachedDefines);
  257. scene.resetCachedMaterial();
  258. // Fallbacks
  259. var fallbacks = new EffectFallbacks();
  260. if (this._defines.FOG) {
  261. fallbacks.addFallback(1, "FOG");
  262. }
  263. for (lightIndex = 0; lightIndex < maxSimultaneousLights; lightIndex++) {
  264. if (!this._defines["LIGHT" + lightIndex]) {
  265. continue;
  266. }
  267. if (lightIndex > 0) {
  268. fallbacks.addFallback(lightIndex, "LIGHT" + lightIndex);
  269. }
  270. if (this._defines["SHADOW" + lightIndex]) {
  271. fallbacks.addFallback(0, "SHADOW" + lightIndex);
  272. }
  273. if (this._defines["SHADOWPCF" + lightIndex]) {
  274. fallbacks.addFallback(0, "SHADOWPCF" + lightIndex);
  275. }
  276. if (this._defines["SHADOWVSM" + lightIndex]) {
  277. fallbacks.addFallback(0, "SHADOWVSM" + lightIndex);
  278. }
  279. }
  280. if (this._defines.BONES4) {
  281. fallbacks.addFallback(0, "BONES4");
  282. }
  283. //Attributes
  284. var attribs = [VertexBuffer.PositionKind];
  285. if (this._defines.NORMAL) {
  286. attribs.push(VertexBuffer.NormalKind);
  287. }
  288. if (this._defines.VERTEXCOLOR) {
  289. attribs.push(VertexBuffer.ColorKind);
  290. }
  291. if (this._defines.BONES) {
  292. attribs.push(VertexBuffer.MatricesIndicesKind);
  293. attribs.push(VertexBuffer.MatricesWeightsKind);
  294. }
  295. if (this._defines.INSTANCES) {
  296. attribs.push("world0");
  297. attribs.push("world1");
  298. attribs.push("world2");
  299. attribs.push("world3");
  300. }
  301. // Legacy browser patch
  302. var shaderName = "triplanar";
  303. var join = this._defines.toString();
  304. this._effect = scene.getEngine().createEffect(shaderName,
  305. attribs,
  306. ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor", "vSpecularColor",
  307. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  308. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  309. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  310. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  311. "vFogInfos", "vFogColor", "pointSize",
  312. "mBones",
  313. "vClipPlane",
  314. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3",
  315. "tileSize"
  316. ],
  317. ["diffuseSamplerX", "diffuseSamplerY", "diffuseSamplerZ",
  318. "normalSamplerX", "normalSamplerY", "normalSamplerZ",
  319. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  320. ],
  321. join, fallbacks, this.onCompiled, this.onError);
  322. }
  323. if (!this._effect.isReady()) {
  324. return false;
  325. }
  326. this._renderId = scene.getRenderId();
  327. this._wasPreviouslyReady = true;
  328. if (mesh) {
  329. if (!mesh._materialDefines) {
  330. mesh._materialDefines = new TriPlanarMaterialDefines();
  331. }
  332. this._defines.cloneTo(mesh._materialDefines);
  333. }
  334. return true;
  335. }
  336. public bindOnlyWorldMatrix(world: Matrix): void {
  337. this._effect.setMatrix("world", world);
  338. }
  339. public bind(world: Matrix, mesh?: Mesh): void {
  340. var scene = this.getScene();
  341. // Matrices
  342. this.bindOnlyWorldMatrix(world);
  343. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  344. // Bones
  345. if (mesh && mesh.useBones && mesh.computeBonesUsingShaders) {
  346. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  347. }
  348. this._effect.setFloat("tileSize", this.tileSize);
  349. if (scene.getCachedMaterial() !== this) {
  350. // Textures
  351. if (this.diffuseTextureX) {
  352. this._effect.setTexture("diffuseSamplerX", this.diffuseTextureX);
  353. }
  354. if (this.diffuseTextureY) {
  355. this._effect.setTexture("diffuseSamplerY", this.diffuseTextureY);
  356. }
  357. if (this.diffuseTextureZ) {
  358. this._effect.setTexture("diffuseSamplerZ", this.diffuseTextureZ);
  359. }
  360. if (this.normalTextureX) {
  361. this._effect.setTexture("normalSamplerX", this.normalTextureX);
  362. }
  363. if (this.normalTextureY) {
  364. this._effect.setTexture("normalSamplerY", this.normalTextureY);
  365. }
  366. if (this.normalTextureZ) {
  367. this._effect.setTexture("normalSamplerZ", this.normalTextureZ);
  368. }
  369. // Clip plane
  370. if (scene.clipPlane) {
  371. var clipPlane = scene.clipPlane;
  372. this._effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  373. }
  374. // Point size
  375. if (this.pointsCloud) {
  376. this._effect.setFloat("pointSize", this.pointSize);
  377. }
  378. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  379. }
  380. this._effect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  381. if (this._defines.SPECULARTERM) {
  382. this._effect.setColor4("vSpecularColor", this.specularColor, this.specularPower);
  383. }
  384. if (scene.lightsEnabled && !this.disableLighting) {
  385. var lightIndex = 0;
  386. for (var index = 0; index < scene.lights.length; index++) {
  387. var light = scene.lights[index];
  388. if (!light.isEnabled()) {
  389. continue;
  390. }
  391. if (!light.canAffectMesh(mesh)) {
  392. continue;
  393. }
  394. if (light instanceof PointLight) {
  395. // Point Light
  396. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  397. } else if (light instanceof DirectionalLight) {
  398. // Directional Light
  399. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  400. } else if (light instanceof SpotLight) {
  401. // Spot Light
  402. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightDirection" + lightIndex);
  403. } else if (light instanceof HemisphericLight) {
  404. // Hemispheric Light
  405. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightGround" + lightIndex);
  406. }
  407. light.diffuse.scaleToRef(light.intensity, this._scaledDiffuse);
  408. this._effect.setColor4("vLightDiffuse" + lightIndex, this._scaledDiffuse, light.range);
  409. if (this._defines.SPECULARTERM) {
  410. light.specular.scaleToRef(light.intensity, this._scaledSpecular);
  411. this._effect.setColor3("vLightSpecular" + lightIndex, this._scaledSpecular);
  412. }
  413. // Shadows
  414. if (scene.shadowsEnabled) {
  415. var shadowGenerator = light.getShadowGenerator();
  416. if (mesh.receiveShadows && shadowGenerator) {
  417. this._effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
  418. this._effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
  419. this._effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
  420. }
  421. }
  422. lightIndex++;
  423. if (lightIndex === maxSimultaneousLights)
  424. break;
  425. }
  426. }
  427. // View
  428. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  429. this._effect.setMatrix("view", scene.getViewMatrix());
  430. }
  431. // Fog
  432. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  433. this._effect.setFloat4("vFogInfos", scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity);
  434. this._effect.setColor3("vFogColor", scene.fogColor);
  435. }
  436. super.bind(world, mesh);
  437. }
  438. public getAnimatables(): IAnimatable[] {
  439. var results = [];
  440. if (this.mixTexture && this.mixTexture.animations && this.mixTexture.animations.length > 0) {
  441. results.push(this.mixTexture);
  442. }
  443. return results;
  444. }
  445. public dispose(forceDisposeEffect?: boolean): void {
  446. if (this.mixTexture) {
  447. this.mixTexture.dispose();
  448. }
  449. super.dispose(forceDisposeEffect);
  450. }
  451. public clone(name: string): TriPlanarMaterial {
  452. var newMaterial = new TriPlanarMaterial(name, this.getScene());
  453. // Base material
  454. this.copyTo(newMaterial);
  455. // Simple material
  456. if (this.mixTexture && this.mixTexture.clone) {
  457. newMaterial.mixTexture = this.mixTexture.clone();
  458. }
  459. newMaterial.diffuseColor = this.diffuseColor.clone();
  460. return newMaterial;
  461. }
  462. public serialize(): any {
  463. var serializationObject = super.serialize();
  464. serializationObject.customType = "BABYLON.TerrainMaterial";
  465. serializationObject.diffuseColor = this.diffuseColor.asArray();
  466. serializationObject.specularColor = this.specularColor.asArray();
  467. serializationObject.specularPower = this.specularPower;
  468. serializationObject.disableLighting = this.disableLighting;
  469. if (this.diffuseTextureX) {
  470. serializationObject.diffuseTextureX = this.diffuseTextureX.serialize();
  471. }
  472. if (this.diffuseTextureY) {
  473. serializationObject.diffuseTextureY = this.diffuseTextureY.serialize();
  474. }
  475. if (this.diffuseTextureZ) {
  476. serializationObject.diffuseTextureZ = this.diffuseTextureZ.serialize();
  477. }
  478. return serializationObject;
  479. }
  480. public static Parse(source: any, scene: Scene, rootUrl: string): TriPlanarMaterial {
  481. var material = new TriPlanarMaterial(source.name, scene);
  482. material.diffuseColor = Color3.FromArray(source.diffuseColor);
  483. material.specularColor = Color3.FromArray(source.specularColor);
  484. material.specularPower = source.specularPower;
  485. material.disableLighting = source.disableLighting;
  486. material.alpha = source.alpha;
  487. material.id = source.id;
  488. Tags.AddTagsTo(material, source.tags);
  489. material.backFaceCulling = source.backFaceCulling;
  490. material.wireframe = source.wireframe;
  491. if (source.diffuseTextureX) {
  492. material.diffuseTextureX = <Texture>Texture.Parse(source.diffuseTextureX, scene, rootUrl);
  493. }
  494. if (source.diffuseTextureY) {
  495. material.diffuseTextureY = <Texture>Texture.Parse(source.diffuseTextureY, scene, rootUrl);
  496. }
  497. if (source.diffuseTextureZ) {
  498. material.diffuseTextureZ = <Texture>Texture.Parse(source.diffuseTextureZ, scene, rootUrl);
  499. }
  500. return material;
  501. }
  502. }
  503. }