babylon.triPlanarMaterial.ts 22 KB

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