@hology/core 0.0.192 → 0.0.193
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{AudioLoader as e,BufferGeometry as t,Euler as s,Group as r,LoadingManager as a,Mesh as i,Object3D as n,Texture as o,TextureLoader as l,Vector3 as c}from"three";import{GLTFLoader as h,MTLLoader as u,OBJLoader as d,DRACOLoader as m,LUTCubeLoader as f}from"three-stdlib";import{FBXLoader as p}from"three-stdlib";import{cloneMesh as g}from"../utils/mesh.js";import{pathJoin as y}from"../utils/files.js";import{Subject as w,firstValueFrom as x}from"rxjs";import{BoxCollisionShape as M,CapsuleCollisionShape as A,CollisionShapeSource as b,CylinderCollisionShape as L,SphereCollisionShape as v}from"./collision/collision-shape.js";import{importCollisionShapes as S,isCollisionMesh as _}from"./collision/collision-shape-import.js";import*as C from"three";import{iterateMaterials as T}from"../utils/materials.js";import{applyMaterial as R}from"./materializer.js";import{BufferGeometryUtils as P,EXRLoader as j,KTX2Loader as k,TGALoader as K,UltraHDRLoader as D}from"three/examples/jsm/Addons.js";import{disposeScene as I}from"../utils/three/cleanup.js";const B=new m;B.setDecoderConfig({type:"js"}),B.setDecoderPath("/assets/draco/");const G=["glb","gltf","fbx","obj"];export class AssetResourceLoader{onError(e){console.error(e)}constructor(){this.cache=new Map,this.textureCache=new Map,this.loadingManager=new a,this.glbLoader=new h(this.loadingManager).setDRACOLoader(B),this.fbxLoader=new p(this.loadingManager),this.objLoader=new d(this.loadingManager),this.textureLoader=new l(this.loadingManager),this.tgaLoader=new K(this.loadingManager),this.exrLoader=new j(this.loadingManager),this.cubeLoader=new f(this.loadingManager),this.hdrLoader=new D(this.loadingManager),this.ktx2Loader=new k(this.loadingManager),this._textureLoader=new C.ImageBitmapLoader(this.loadingManager),this.audioLoader=new e(this.loadingManager),this.initialisedKtx2=!1,this._retrievedMeshes=[],this._retrievedTextures=[],this.makeReady=new w,this.ready=x(this.makeReady),this._texturePromises=new Map,this._arrayTexturesByFileKey=new Map,this._animationPromises=new Map,this._animationCache=new Map,this.asyncMeshResults=new Map,this.asyncAudioResults=new Map,this.collisionShapeCache=new Map,this.optimizedMeshes=new Set}initKtx2(e){this.initialisedKtx2=!0;null!=getElectronArg("windowId")||import.meta.url.includes(".vite")||import.meta.url.includes("hology/packages/core")?this.ktx2Loader.setTranscoderPath("/assets/basis/"):this.ktx2Loader.setTranscoderPath(new URL("../assets/basis/",import.meta.url).href),this.ktx2Loader.detectSupport(e)}setDataDir(e){this.basePath=y(e,"asset-resources"),this.makeReady.next(!0)}getUri(e){const t=function(e){return z.get(e)}(e),s=getElectronArg("windowId");return null!=s||import.meta.url.includes(".vite")||import.meta.url.includes("hology/packages/core")?y(this.basePath,e)+(null!=s?`?windowId=${s}&v=${t}`:""):new URL(y("..",this.basePath,e),import.meta.url).href}async getTexture(e){if(!e||!e.fileKey)return null;if(this._texturePromises.has(e.id))return this._texturePromises.get(e.id);const t=this._getTexture(e).finally(()=>{this._texturePromises.delete(e.id)});return this._texturePromises.set(e.id,t),t}async _getTexture(e){if(null==e||null==e.fileKey)return null;if(await this.ready,this.textureCache.has(e.id)){const t=this.textureCache.get(e.id);null!=e.texture&&(t.flipY!==e.texture?.flipY?(t.needsUpdate=!0,t.flipY=e.texture?.flipY??!0):t.wrapS!==W(e.texture?.wrapS)?(t.needsUpdate=!0,t.wrapS=W(e.texture?.wrapS)):t.wrapT!==W(e.texture?.wrapT)&&(t.wrapT=W(e.texture?.wrapT),t.needsUpdate=!0))}else try{await this._getTextureLoader(e.fileKey).loadAsync(this.getUri(e.fileKey)).then(t=>(t.texture3D?t=t.texture3D:(t.wrapS=W(e.texture?.wrapS),t.wrapT=W(e.texture?.wrapT),t.flipY=e.texture?.flipY??!0,t.colorSpace="linear"===e.texture?.colorSpace||"exr"===e.fileFormat?C.LinearSRGBColorSpace:C.SRGBColorSpace),this.textureCache.set(e.id,t),this._retrievedTextures.push(t),t))}catch(t){return console.warn("Failed to load texture "+e.name,t),null}const t=this.textureCache.get(e.id);return null!=t&&(t.userData.assetId=e.id),t}async getTextureArray(e){const t=e.texture?.textureArrayFileKey,s=e.texture?.textureArrayLayer;if(!t||null==s)return{texture:null,layerIndex:null};if(!this._arrayTexturesByFileKey.has(t)){const e=this._getTextureLoader(t);let s=await e.loadAsync(this.getUri(t));s instanceof o||(s=s.texture3D),this._arrayTexturesByFileKey.set(t,s),this._retrievedTextures.push(s)}return{texture:this._arrayTexturesByFileKey.get(t),layerIndex:s}}_getTextureLoader(e){return e?.toLowerCase().endsWith(".tga")?this.tgaLoader:e?.toLowerCase().endsWith(".ktx2")?this.ktx2Loader:e?.toLowerCase().endsWith(".exr")?this.exrLoader:e?.toLowerCase().endsWith(".cube")?this.cubeLoader:this.textureLoader}clearCache(e){this.clearCacheById(e.id)}clearCacheById(e){const t=Array.from(this.cache.values()).find(t=>t.scene.userData.assetId===e);var s;t&&(Array.from(this.cache.keys()).forEach(e=>{this.cache.get(e)===t&&this.cache.delete(e)}),s=e,z.set(s,z.get(s)??1)),this.textureCache.delete(e),Array.from(this.collisionShapeCache.keys()).forEach(t=>{t.startsWith(e+"|")&&this.collisionShapeCache.delete(t)})}async getMesh(e,t){if(await this.ready,null==e)return console.error("No asset was provided"),{scene:new r,animations:[]};if(!G.includes(e.fileFormat?.toLowerCase()))return console.error("Unsupported mesh file format "+e.fileFormat,e),{scene:new r,animations:[]};const s=e.fileKey+(!0===t?.mergeGeomtries?"1":"0");if(!this.cache.has(s))try{this.asyncMeshResults.has(s)||this.asyncMeshResults.set(s,this.loadMesh(e).finally(()=>this.asyncMeshResults.delete(s))),this.cache.set(s,await this.asyncMeshResults.get(s))}catch(e){return this.onError(e),{scene:new r,animations:[]}}const a=this.cache.get(s).scene,n=this.computeCollisionShapes(e,a);!0===t?.mergeGeomtries&&this.optimizeDrawGroups(a);const o=g(a),l=this.cache.get(s).animations;o.traverse(e=>{e instanceof i&&e.material instanceof Array&&(e.material=e.material.slice())});const c=new AssetMeshInstance;c.add(o),c.collisionShapes=n,c.animations=l;const h=e.receiveShadow??!0,u=e.castShadow??!1;if(o.traverse(e=>{e.castShadow=u,e.receiveShadow=h}),!0===t?.rescale&&"number"==typeof e.mesh?.rescale&&c.scale.setScalar(e.mesh?.rescale),!0===t?.applyMaterials){const s=t?.materialResolver??this.materialProvider;if(null!=s)for(const t of e.materialAssignments??[])await R(c,t,s);else console.warn(`applyMaterials was set to true for ${e.name} but no material resolver was provided.`)}return{scene:c,animations:l}}async getAudio(e){await this.ready;const t=e.fileKey;if(this.asyncAudioResults.has(t))return await this.asyncAudioResults.get(t);const s=this.audioLoader.loadAsync(this.getUri(e.fileKey)).finally(()=>{setTimeout(()=>{this.asyncAudioResults.delete(t)},1e3)});return this.asyncAudioResults.set(t,s),s}async getAnimationClip(e){if(null==e||null==e.fileKey||null==!e.anim?.clip)return console.warn("Asset or animation clip name is not defined",e),null;const t=e.anim.clip,s=e.fileKey+"|"+t;if(this._animationCache.has(s))return this._animationCache.get(s);if(this._animationPromises.has(s))return this._animationPromises.get(s);const r=(async()=>{try{const r=await this.getMesh(e),a=r.animations.find(e=>e.name===t);return null!=a&&this._animationCache.set(s,a),a}catch(e){return this.onError(e),null}finally{setTimeout(()=>this._animationPromises.delete(s),1e3)}})();return this._animationPromises.set(s,r),r}computeCollisionShapes(e,t){if(!0!==e.collisionDetection)return[];const r=e.id+"|"+(e.mesh?.collisions?.shapeType??"")+"|"+JSON.stringify(e.mesh?.colliders??[]);if(!this.collisionShapeCache.has(r)){const a=e.mesh?.colliders??[],i=a.length>0?a.map(e=>{let t;switch(e.type){case"box":t=new M((new c).fromArray(e.size));break;case"sphere":t=new v(e.radius);break;case"capsule":t=new A(e.length,e.radius);break;case"cylinder":t=new L(e.radius,e.radius,e.height,16,new s)}return t.source=b.custom,t.offset.fromArray(e.position??[0,0,0]),t.rotation.fromArray(e.rotation??[0,0,0,"XYZ"]),t}):S(t,e);this.collisionShapeCache.set(r,i)}return this.collisionShapeCache.get(r)}async loadMesh(e){return await this.ready,await this.loadByAsset(e).then(e=>(e.scene.traverse(e=>{e instanceof i&&e.material,_(e)?e.visible=!1:e instanceof i&&e.geometry instanceof t&&(e.geometry.hasAttribute("normal")||e.geometry.computeVertexNormals())}),this._retrievedMeshes.push(e.scene),e.scene=function(e,t){let s=!1;if(t.traverseVisible(e=>{F.test(e.name)&&(s=!0)}),!s)return t;const r=new C.LOD,a=[t];for(;a.length>0;){const e=a.shift(),t=e.name.match(F);if(null!=t){const s=parseInt(t[1]);0===s?r.addLevel(e,0):console.warn(`Skipping LOD level ${s} for now as LOD is not fully supported`)}else a.push(...e.children)}return r}(0,e.scene),function(e){const t=E;E.length=0,e.traverse(e=>{(e instanceof C.Camera||e instanceof C.Light)&&t.push(e)}),t.forEach(e=>e.removeFromParent())}(e.scene),e))}optimizeDrawGroups(e){if(this.optimizedMeshes.has(e.uuid))return;this.optimizedMeshes.add(e.uuid),e.updateWorldMatrix(!0,!0);const s=[];e.traverse(e=>{e instanceof i&&!(e instanceof C.SkinnedMesh)&&e.geometry instanceof t&&Array.isArray(e.material)&&!_(e)&&s.push({mesh:e,geometry:e.geometry,materialArray:e.material,parent:e.parent})});for(const{mesh:t,geometry:r,materialArray:a,parent:n}of s){t.removeFromParent();for(let s=0;s<a.length;s++){const o=a[s],l=r.groups.filter(e=>e.materialIndex===s);if(0===l.length)continue;const c=this.extractGeometryGroups(r,l);c.groups.length>1&&P.mergeGroups(c);const h=new i(c,o);h.copy(t,!1),h.geometry=c,h.material=o,n?n.add(h):e.add(h)}r.dispose()}let r=!0,a=0,n=0;if(e.traverse(e=>{if(e instanceof i&&e.geometry instanceof t&&!_(e)){a++;const t=Object.keys(e.geometry.attributes).length;t!==n&&0!==n&&(r=!1),n=t}else(e instanceof C.SkinnedMesh||e instanceof C.Bone)&&(r=!1)}),a>1&&r){const s=new Map;e.traverse(r=>{if(r instanceof i&&r.geometry instanceof t&&!_(r)){if(Array.isArray(r.material))return;r.updateWorldMatrix(!0,!0);const t=r.material?.uuid||"default";s.has(t)||s.set(t,{material:r.material,geometries:[],objects:[]});const a=s.get(t),i=e.matrixWorld.clone().invert().multiply(r.matrixWorld);a.geometries.push(r.geometry.clone().applyMatrix4(i)),a.objects.push(r)}});for(const e of s.values())for(const t of e.objects)t.removeFromParent();for(const t of s.values()){let s;s=1===t.geometries.length?t.geometries[0]:P.mergeGeometries(t.geometries,!0),s.groups.length>1&&P.mergeGroups(s),e.add(new i(s,t.material))}}e.traverse(e=>{if(e instanceof i&&e.geometry instanceof t&&!_(e)){const t=e.geometry;!Array.isArray(e.material)&&t.groups.length>1&&P.mergeGroups(t)}})}extractGeometryGroups(e,s){const r=e.index;if(!r){const r=new t;let a=0;for(const e of s)a+=e.count;if(0===a)return r;for(const t in e.attributes){const i=e.attributes[t],n=i.itemSize,o=i.array,l=new(0,o.constructor)(a*n);let c=0;for(const e of s){const t=e.start*n,s=e.count*n;for(let e=0;e<s;e++)l[c+e]=o[t+e];c+=s}const h=new C.BufferAttribute(l,n,i.normalized);r.setAttribute(t,h)}return r.groups=[{start:0,count:a,materialIndex:0}],r}const a=[];for(const e of s)for(let t=0;t<e.count;t++){const s=r.getX(e.start+t);a.push(s)}if(0===a.length)return new t;const i=new Map,n=[];let o=0;for(const e of a)i.has(e)||(i.set(e,o),o++),n.push(i.get(e));if(0===n.length||0===i.size)return new t;const l=new t;for(const t in e.attributes){const s=e.attributes[t],r=s.itemSize,a=s.array,n=new(0,a.constructor)(i.size*r);for(const[e,t]of i)for(let s=0;s<r;s++)n[t*r+s]=a[e*r+s];const o=new C.BufferAttribute(n,r,s.normalized);l.setAttribute(t,o)}const c=new((n.length>0?Math.max(...n):0)>65535?Uint32Array:Uint16Array)(n.length);for(let e=0;e<n.length;e++)c[e]=n[e];return l.setIndex(new C.BufferAttribute(c,1)),l.groups=[{start:0,count:n.length,materialIndex:0}],l}async loadByAsset(e){this.fbxLoader;const t=this.getUri(e.fileKey);switch(e.fileFormat){case"glb":case"gltf":return this.glbLoader.loadAsync(t).then(e=>({scene:e.scene,animations:e.animations}));case"fbx":return this.fbxLoader.loadAsync(t).then(e=>({scene:e,animations:e.animations}));case"obj":if(null!=e.materialLib){const t=new u;t.materialOptions={normalizeRGB:!1};const s=await t.loadAsync(this.getUri(e.materialLib));this.objLoader.setMaterials(s)}return this.objLoader.loadAsync(t).then(e=>(U(e),e)).then(e=>({scene:e,animations:e.animations}))}}disposeAll(){this.cache.clear(),this._retrievedMeshes.forEach(e=>I(e)),this._retrievedTextures.forEach(e=>e.dispose()),this._retrievedMeshes.length=0,this._retrievedTextures.length=0}}const z=new Map;function U(e){if(e instanceof i)for(const t of T(e.material))t instanceof C.MeshPhongMaterial&&!t.color.isLinear&&(t.color.isLinear=!0);e.children?.forEach(U)}export class AssetMeshInstance extends n{}export function getElectronArg(e){const t=`--${e}=`,s=window.process?.argv.find(e=>e.startsWith(t));return s?.substring(t.length)}function W(e){switch(e){case"clamp":return C.ClampToEdgeWrapping;case"repeat":return C.RepeatWrapping;case"mirror":return C.MirroredRepeatWrapping}return C.RepeatWrapping}new C.Matrix4;const E=[];const F=/_LOD(\d+)$/;/*
|
|
1
|
+
import{AudioLoader as e,BufferGeometry as t,Euler as s,Group as r,LoadingManager as a,Mesh as i,Object3D as n,Texture as o,TextureLoader as l,Vector3 as c}from"three";import{GLTFLoader as h,MTLLoader as u,OBJLoader as d,DRACOLoader as m,LUTCubeLoader as f}from"three-stdlib";import{FBXLoader as p}from"three-stdlib";import{cloneMesh as g}from"../utils/mesh.js";import{pathJoin as y}from"../utils/files.js";import{Subject as w,firstValueFrom as x}from"rxjs";import{BoxCollisionShape as M,CapsuleCollisionShape as A,CollisionShapeSource as b,CylinderCollisionShape as L,SphereCollisionShape as v}from"./collision/collision-shape.js";import{importCollisionShapes as _,isCollisionMesh as C}from"./collision/collision-shape-import.js";import*as S from"three";import{iterateMaterials as T}from"../utils/materials.js";import{applyMaterial as R}from"./materializer.js";import{BufferGeometryUtils as P,EXRLoader as j,KTX2Loader as k,TGALoader as K,UltraHDRLoader as D}from"three/examples/jsm/Addons.js";import{disposeScene as I}from"../utils/three/cleanup.js";const z=new m;z.setDecoderConfig({type:"js"}),z.setDecoderPath("/assets/draco/");const B=["glb","gltf","fbx","obj"];export class AssetResourceLoader{onError(e){console.error(e)}constructor(){this.cache=new Map,this.textureCache=new Map,this.loadingManager=new a,this.glbLoader=new h(this.loadingManager).setDRACOLoader(z),this.fbxLoader=new p(this.loadingManager),this.objLoader=new d(this.loadingManager),this.textureLoader=new l(this.loadingManager),this.tgaLoader=new K(this.loadingManager),this.exrLoader=new j(this.loadingManager),this.cubeLoader=new f(this.loadingManager),this.hdrLoader=new D(this.loadingManager),this.ktx2Loader=new k(this.loadingManager),this._textureLoader=new S.ImageBitmapLoader(this.loadingManager),this.audioLoader=new e(this.loadingManager),this.initialisedKtx2=!1,this._retrievedMeshes=[],this._retrievedTextures=[],this.makeReady=new w,this.ready=x(this.makeReady),this._texturePromises=new Map,this._arrayTexturesByFileKey=new Map,this._animationPromises=new Map,this._animationCache=new Map,this.asyncMeshResults=new Map,this.asyncAudioResults=new Map,this.collisionShapeCache=new Map,this.optimizedMeshes=new Set}initKtx2(e){this.initialisedKtx2=!0;null!=getElectronArg("windowId")||import.meta.url.includes(".vite")||import.meta.url.includes("hology/packages/core")?this.ktx2Loader.setTranscoderPath("/assets/basis/"):this.ktx2Loader.setTranscoderPath(new URL("../assets/basis/",import.meta.url).href),this.ktx2Loader.detectSupport(e)}setDataDir(e){this.basePath=y(e,"asset-resources"),this.makeReady.next(!0)}getUri(e){const t=function(e){return U.get(e)}(e),s=getElectronArg("windowId");return null!=s||import.meta.url.includes(".vite")||import.meta.url.includes("hology/packages/core")?y(this.basePath,e)+(null!=s?`?windowId=${s}&v=${t}`:""):new URL(y("..",this.basePath,e),import.meta.url).href}async getTexture(e){if(!e||!e.fileKey)return null;if(this._texturePromises.has(e.id))return this._texturePromises.get(e.id);const t=this._getTexture(e).finally(()=>{this._texturePromises.delete(e.id)});return this._texturePromises.set(e.id,t),t}async _getTexture(e){if(null==e||null==e.fileKey)return null;if(await this.ready,this.textureCache.has(e.id)){const t=this.textureCache.get(e.id);null!=e.texture&&(t.flipY!==e.texture?.flipY?(t.needsUpdate=!0,t.flipY=e.texture?.flipY??!0):t.wrapS!==E(e.texture?.wrapS)?(t.needsUpdate=!0,t.wrapS=E(e.texture?.wrapS)):t.wrapT!==E(e.texture?.wrapT)&&(t.wrapT=E(e.texture?.wrapT),t.needsUpdate=!0))}else try{await this._getTextureLoader(e.fileKey).loadAsync(this.getUri(e.fileKey)).then(t=>(t.texture3D?t=t.texture3D:(t.wrapS=E(e.texture?.wrapS),t.wrapT=E(e.texture?.wrapT),t.flipY=e.texture?.flipY??!0),this.textureCache.set(e.id,t),this._retrievedTextures.push(t),t))}catch(t){return console.warn("Failed to load texture "+e.name,t),null}const t=this.textureCache.get(e.id);return null!=t&&(t.userData.assetId=e.id),t}async getTextureArray(e){const t=e.texture?.textureArrayFileKey,s=e.texture?.textureArrayLayer;if(!t||null==s)return{texture:null,layerIndex:null};if(!this._arrayTexturesByFileKey.has(t)){const e=this._getTextureLoader(t);let s=await e.loadAsync(this.getUri(t));s instanceof o||(s=s.texture3D),this._arrayTexturesByFileKey.set(t,s),this._retrievedTextures.push(s)}return{texture:this._arrayTexturesByFileKey.get(t),layerIndex:s}}_getTextureLoader(e){return e?.toLowerCase().endsWith(".tga")?this.tgaLoader:e?.toLowerCase().endsWith(".ktx2")?this.ktx2Loader:e?.toLowerCase().endsWith(".exr")?this.exrLoader:e?.toLowerCase().endsWith(".cube")?this.cubeLoader:this.textureLoader}clearCache(e){this.clearCacheById(e.id)}clearCacheById(e){const t=Array.from(this.cache.values()).find(t=>t.scene.userData.assetId===e);var s;t&&(Array.from(this.cache.keys()).forEach(e=>{this.cache.get(e)===t&&this.cache.delete(e)}),s=e,U.set(s,U.get(s)??1)),this.textureCache.delete(e),Array.from(this.collisionShapeCache.keys()).forEach(t=>{t.startsWith(e+"|")&&this.collisionShapeCache.delete(t)})}async getMesh(e,t){if(await this.ready,null==e)return console.error("No asset was provided"),{scene:new r,animations:[]};if(!B.includes(e.fileFormat?.toLowerCase()))return console.error("Unsupported mesh file format "+e.fileFormat,e),{scene:new r,animations:[]};const s=e.fileKey+(!0===t?.mergeGeomtries?"1":"0");if(!this.cache.has(s))try{this.asyncMeshResults.has(s)||this.asyncMeshResults.set(s,this.loadMesh(e).finally(()=>this.asyncMeshResults.delete(s))),this.cache.set(s,await this.asyncMeshResults.get(s))}catch(e){return this.onError(e),{scene:new r,animations:[]}}const a=this.cache.get(s).scene,n=this.computeCollisionShapes(e,a);!0===t?.mergeGeomtries&&this.optimizeDrawGroups(a);const o=g(a),l=this.cache.get(s).animations;o.traverse(e=>{e instanceof i&&e.material instanceof Array&&(e.material=e.material.slice())});const c=new AssetMeshInstance;c.add(o),c.collisionShapes=n,c.animations=l;const h=e.receiveShadow??!0,u=e.castShadow??!1;if(o.traverse(e=>{e.castShadow=u,e.receiveShadow=h}),!0===t?.rescale&&"number"==typeof e.mesh?.rescale&&c.scale.setScalar(e.mesh?.rescale),!0===t?.applyMaterials){const s=t?.materialResolver??this.materialProvider;if(null!=s)for(const t of e.materialAssignments??[])await R(c,t,s);else console.warn(`applyMaterials was set to true for ${e.name} but no material resolver was provided.`)}return{scene:c,animations:l}}async getAudio(e){await this.ready;const t=e.fileKey;if(this.asyncAudioResults.has(t))return await this.asyncAudioResults.get(t);const s=this.audioLoader.loadAsync(this.getUri(e.fileKey)).finally(()=>{setTimeout(()=>{this.asyncAudioResults.delete(t)},1e3)});return this.asyncAudioResults.set(t,s),s}async getAnimationClip(e){if(null==e||null==e.fileKey||null==!e.anim?.clip)return console.warn("Asset or animation clip name is not defined",e),null;const t=e.anim.clip,s=e.fileKey+"|"+t;if(this._animationCache.has(s))return this._animationCache.get(s);if(this._animationPromises.has(s))return this._animationPromises.get(s);const r=(async()=>{try{const r=await this.getMesh(e),a=r.animations.find(e=>e.name===t);return null!=a&&this._animationCache.set(s,a),a}catch(e){return this.onError(e),null}finally{setTimeout(()=>this._animationPromises.delete(s),1e3)}})();return this._animationPromises.set(s,r),r}computeCollisionShapes(e,t){if(!0!==e.collisionDetection)return[];const r=e.id+"|"+(e.mesh?.collisions?.shapeType??"")+"|"+JSON.stringify(e.mesh?.colliders??[]);if(!this.collisionShapeCache.has(r)){const a=e.mesh?.colliders??[],i=a.length>0?a.map(e=>{let t;switch(e.type){case"box":t=new M((new c).fromArray(e.size));break;case"sphere":t=new v(e.radius);break;case"capsule":t=new A(e.length,e.radius);break;case"cylinder":t=new L(e.radius,e.radius,e.height,16,new s)}return t.source=b.custom,t.offset.fromArray(e.position??[0,0,0]),t.rotation.fromArray(e.rotation??[0,0,0,"XYZ"]),t}):_(t,e);this.collisionShapeCache.set(r,i)}return this.collisionShapeCache.get(r)}async loadMesh(e){return await this.ready,await this.loadByAsset(e).then(e=>(e.scene.traverse(e=>{e instanceof i&&e.material,C(e)?e.visible=!1:e instanceof i&&e.geometry instanceof t&&(e.geometry.hasAttribute("normal")||e.geometry.computeVertexNormals())}),this._retrievedMeshes.push(e.scene),e.scene=function(e,t){let s=!1;if(t.traverseVisible(e=>{F.test(e.name)&&(s=!0)}),!s)return t;const r=new S.LOD,a=[t];for(;a.length>0;){const e=a.shift(),t=e.name.match(F);if(null!=t){const s=parseInt(t[1]);0===s?r.addLevel(e,0):console.warn(`Skipping LOD level ${s} for now as LOD is not fully supported`)}else a.push(...e.children)}return r}(0,e.scene),function(e){const t=G;G.length=0,e.traverse(e=>{(e instanceof S.Camera||e instanceof S.Light)&&t.push(e)}),t.forEach(e=>e.removeFromParent())}(e.scene),e))}optimizeDrawGroups(e){if(this.optimizedMeshes.has(e.uuid))return;this.optimizedMeshes.add(e.uuid),e.updateWorldMatrix(!0,!0);const s=[];e.traverse(e=>{e instanceof i&&!(e instanceof S.SkinnedMesh)&&e.geometry instanceof t&&Array.isArray(e.material)&&!C(e)&&s.push({mesh:e,geometry:e.geometry,materialArray:e.material,parent:e.parent})});for(const{mesh:t,geometry:r,materialArray:a,parent:n}of s){t.removeFromParent();for(let s=0;s<a.length;s++){const o=a[s],l=r.groups.filter(e=>e.materialIndex===s);if(0===l.length)continue;const c=this.extractGeometryGroups(r,l);c.groups.length>1&&P.mergeGroups(c);const h=new i(c,o);h.copy(t,!1),h.geometry=c,h.material=o,n?n.add(h):e.add(h)}r.dispose()}let r=!0,a=0,n=0;if(e.traverse(e=>{if(e instanceof i&&e.geometry instanceof t&&!C(e)){a++;const t=Object.keys(e.geometry.attributes).length;t!==n&&0!==n&&(r=!1),n=t}else(e instanceof S.SkinnedMesh||e instanceof S.Bone)&&(r=!1)}),a>1&&r){const s=new Map;e.traverse(r=>{if(r instanceof i&&r.geometry instanceof t&&!C(r)){if(Array.isArray(r.material))return;r.updateWorldMatrix(!0,!0);const t=r.material?.uuid||"default";s.has(t)||s.set(t,{material:r.material,geometries:[],objects:[]});const a=s.get(t),i=e.matrixWorld.clone().invert().multiply(r.matrixWorld);a.geometries.push(r.geometry.clone().applyMatrix4(i)),a.objects.push(r)}});for(const e of s.values())for(const t of e.objects)t.removeFromParent();for(const t of s.values()){let s;s=1===t.geometries.length?t.geometries[0]:P.mergeGeometries(t.geometries,!0),s.groups.length>1&&P.mergeGroups(s),e.add(new i(s,t.material))}}e.traverse(e=>{if(e instanceof i&&e.geometry instanceof t&&!C(e)){const t=e.geometry;!Array.isArray(e.material)&&t.groups.length>1&&P.mergeGroups(t)}})}extractGeometryGroups(e,s){const r=e.index;if(!r){const r=new t;let a=0;for(const e of s)a+=e.count;if(0===a)return r;for(const t in e.attributes){const i=e.attributes[t],n=i.itemSize,o=i.array,l=new(0,o.constructor)(a*n);let c=0;for(const e of s){const t=e.start*n,s=e.count*n;for(let e=0;e<s;e++)l[c+e]=o[t+e];c+=s}const h=new S.BufferAttribute(l,n,i.normalized);r.setAttribute(t,h)}return r.groups=[{start:0,count:a,materialIndex:0}],r}const a=[];for(const e of s)for(let t=0;t<e.count;t++){const s=r.getX(e.start+t);a.push(s)}if(0===a.length)return new t;const i=new Map,n=[];let o=0;for(const e of a)i.has(e)||(i.set(e,o),o++),n.push(i.get(e));if(0===n.length||0===i.size)return new t;const l=new t;for(const t in e.attributes){const s=e.attributes[t],r=s.itemSize,a=s.array,n=new(0,a.constructor)(i.size*r);for(const[e,t]of i)for(let s=0;s<r;s++)n[t*r+s]=a[e*r+s];const o=new S.BufferAttribute(n,r,s.normalized);l.setAttribute(t,o)}const c=new((n.length>0?Math.max(...n):0)>65535?Uint32Array:Uint16Array)(n.length);for(let e=0;e<n.length;e++)c[e]=n[e];return l.setIndex(new S.BufferAttribute(c,1)),l.groups=[{start:0,count:n.length,materialIndex:0}],l}async loadByAsset(e){this.fbxLoader;const t=this.getUri(e.fileKey);switch(e.fileFormat){case"glb":case"gltf":return this.glbLoader.loadAsync(t).then(e=>({scene:e.scene,animations:e.animations}));case"fbx":return this.fbxLoader.loadAsync(t).then(e=>({scene:e,animations:e.animations}));case"obj":if(null!=e.materialLib){const t=new u;t.materialOptions={normalizeRGB:!1};const s=await t.loadAsync(this.getUri(e.materialLib));this.objLoader.setMaterials(s)}return this.objLoader.loadAsync(t).then(e=>(W(e),e)).then(e=>({scene:e,animations:e.animations}))}}disposeAll(){this.cache.clear(),this._retrievedMeshes.forEach(e=>I(e)),this._retrievedTextures.forEach(e=>e.dispose()),this._retrievedMeshes.length=0,this._retrievedTextures.length=0}}const U=new Map;function W(e){if(e instanceof i)for(const t of T(e.material))t instanceof S.MeshPhongMaterial&&!t.color.isLinear&&(t.color.isLinear=!0);e.children?.forEach(W)}export class AssetMeshInstance extends n{}export function getElectronArg(e){const t=`--${e}=`,s=window.process?.argv.find(e=>e.startsWith(t));return s?.substring(t.length)}function E(e){switch(e){case"clamp":return S.ClampToEdgeWrapping;case"repeat":return S.RepeatWrapping;case"mirror":return S.MirroredRepeatWrapping}return S.RepeatWrapping}new S.Matrix4;const G=[];const F=/_LOD(\d+)$/;/*
|
|
2
2
|
* Copyright (©) 2026 Hology Interactive AB. All rights reserved.
|
|
3
3
|
* See the LICENSE.md file for details.
|
|
4
4
|
*/
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{__decorate as e,__metadata as a}from"tslib";import{Color as t,MeshStandardMaterial as n,Texture as r,Vector2 as i}from"three";import{Shader as o}from"../shader.js";import{Parameter as s}from"../parameter.js";import{removeObjectUndefined as p}from"../../utils/collections.js";import*as d from"three";const l=new r;export class StandardShader extends o{constructor(){super(...arguments),this.color=new t("#FFFFFF"),this.opacity=1,this.roughness=1,this.metalness=0,this.normalScale=new i(1,1),this.sheen=0,this.sheenColor=new t("#FFFFFF"),this.sheenRoughness=.5,this.anisotropy=0,this.heightMap=l,this.vertexColor=!1}build(){const e=this.sheen>0,a=this.anisotropy>0,t=a||e,r=(null!=this.map||null!=this.normalMap)&&null!=this.heightMap&&this.heightMap!=l&&0!==this.heightScale,i=r?ParallaxStandardMaterial:t?d.MeshPhysicalMaterial:n;null!=this.map&&(this.map.colorSpace=d.SRGBColorSpace),null!=this.normalMap&&(this.normalMap.colorSpace=d.LinearSRGBColorSpace),null!=this.metalnessMap&&(this.metalnessMap.colorSpace=d.LinearSRGBColorSpace),null!=this.roughnessMap&&(this.roughnessMap.colorSpace=d.LinearSRGBColorSpace),null!=this.aoMap&&(this.aoMap.colorSpace=d.LinearSRGBColorSpace),null!=this.heightMap&&(this.heightMap.colorSpace=d.NoColorSpace),null!=this.emissiveMap&&(this.emissiveMap.colorSpace=d.SRGBColorSpace),null!=this.alphaMap&&(this.alphaMap.colorSpace=d.LinearSRGBColorSpace),null!=this.lightMap&&(this.lightMap.colorSpace=d.LinearSRGBColorSpace);return new i(p({color:this.color,vertexColors:this.vertexColor,opacity:this.opacity,roughness:this.roughness,metalness:this.metalness,map:this.map,lightMap:this.lightMap,lightMapIntensity:this.lightMapIntensity,aoMap:this.aoMap,aoMapIntensity:this.aoMapIntensity,emissive:this.emissive,emissiveIntensity:this.emissiveIntensity,emissiveMap:this.emissiveMap,normalMap:this.normalMap,normalScale:this.normalScale,sheen:e?this.sheen:void 0,sheenColor:e?this.sheenColor:void 0,sheenColorMap:e?this.sheenColorMap:void 0,sheenRoughness:e?this.sheenRoughness:void 0,sheenRoughnessMap:e?this.sheenRoughnessMap:void 0,anisotropy:a?this.anisotropy:void 0,anisotropyMap:a?this.anisotropyMap:void 0,anisotropyRotation:a?this.anisotropyRotation:void 0,roughnessMap:this.roughnessMap,metalnessMap:this.metalnessMap,alphaMap:this.alphaMap,envMap:this.envMap,envMapIntensity:this.envMapIntensity,...r?{heightMap:this.heightMap,heightScale:this.heightScale}:{}}))}}e([s(),a("design:type",t)],StandardShader.prototype,"color",void 0),e([s({label:"Color Map"}),a("design:type",r)],StandardShader.prototype,"map",void 0),e([s({range:[0,1]}),a("design:type",Number)],StandardShader.prototype,"opacity",void 0),e([s(),a("design:type",r)],StandardShader.prototype,"alphaMap",void 0),e([s({range:[0,1]}),a("design:type",Number)],StandardShader.prototype,"roughness",void 0),e([s(),a("design:type",r)],StandardShader.prototype,"roughnessMap",void 0),e([s({range:[0,1]}),a("design:type",Number)],StandardShader.prototype,"metalness",void 0),e([s(),a("design:type",r)],StandardShader.prototype,"metalnessMap",void 0),e([s(),a("design:type",r)],StandardShader.prototype,"lightMap",void 0),e([s(),a("design:type",Number)],StandardShader.prototype,"lightMapIntensity",void 0),e([s(),a("design:type",r)],StandardShader.prototype,"aoMap",void 0),e([s(),a("design:type",Number)],StandardShader.prototype,"aoMapIntensity",void 0),e([s(),a("design:type",t)],StandardShader.prototype,"emissive",void 0),e([s({range:[0,10]}),a("design:type",Number)],StandardShader.prototype,"emissiveIntensity",void 0),e([s(),a("design:type",r)],StandardShader.prototype,"emissiveMap",void 0),e([s(),a("design:type",r)],StandardShader.prototype,"normalMap",void 0),e([s(),a("design:type",i)],StandardShader.prototype,"normalScale",void 0),e([s({range:[0,1]}),a("design:type",Number)],StandardShader.prototype,"sheen",void 0),e([s(),a("design:type",t)],StandardShader.prototype,"sheenColor",void 0),e([s(),a("design:type",r)],StandardShader.prototype,"sheenColorMap",void 0),e([s({range:[0,1]}),a("design:type",Number)],StandardShader.prototype,"sheenRoughness",void 0),e([s(),a("design:type",r)],StandardShader.prototype,"sheenRoughnessMap",void 0),e([s(),a("design:type",Number)],StandardShader.prototype,"anisotropyRotation",void 0),e([s(),a("design:type",r)],StandardShader.prototype,"anisotropyMap",void 0),e([s({range:[0,1]}),a("design:type",Number)],StandardShader.prototype,"anisotropy",void 0),e([s(),a("design:type",r)],StandardShader.prototype,"envMap",void 0),e([s({range:[0,1]}),a("design:type",Number)],StandardShader.prototype,"envMapIntensity",void 0),e([s(),a("design:type",r)],StandardShader.prototype,"heightMap",void 0),e([s(),a("design:type",Number)],StandardShader.prototype,"heightScale",void 0),e([s(),a("design:type",Boolean)],StandardShader.prototype,"vertexColor",void 0);export var ParallaxType;!function(e){e[e.none=0]="none",e[e.offset=1]="offset",e[e.pom=2]="pom"}(ParallaxType||(ParallaxType={}));export class ParallaxStandardMaterial extends n{constructor(e={}){super(e),e.heightMap&&(this.heightMap=e.heightMap),this.heightScale=e.heightScale??.05,this.parallaxType=e.parallaxType??ParallaxType.pom,this.minLayers=e.minLayers??10,this.maxLayers=e.maxLayers??32}onBeforeCompile(e){e.uniforms.heightMap={value:this.heightMap},e.uniforms.heightScale={value:this.heightScale},e.uniforms.minLayers={value:this.minLayers},e.uniforms.maxLayers={value:this.maxLayers},e.uniforms.parallaxType={value:this.parallaxType},e.vertexTangents=!0,e.vertexShader=e.vertexShader.replace("#include <uv_pars_vertex>","\n #include <uv_pars_vertex>\n "),e.vertexShader=e.vertexShader.replace("#include <begin_vertex>","\n #include <begin_vertex>\n "),e.fragmentShader=e.fragmentShader.replace("#include <uv_pars_fragment>","\n #include <uv_pars_fragment>\n uniform sampler2D heightMap;\n uniform float heightScale;\n uniform int minLayers;\n uniform int maxLayers;\n uniform int parallaxType;\n\n vec2 parallaxOffset(vec2 uv, vec3 viewDir, vec3 normal) {\n // Compute TBN in fragment shader using screen-space derivatives\n vec3 dp1 = dFdx(-vViewPosition);\n vec3 dp2 = dFdy(-vViewPosition);\n vec2 duv1 = dFdx(uv);\n vec2 duv2 = dFdy(uv);\n \n vec3 dp2perp = cross(dp2, normal);\n vec3 dp1perp = cross(normal, dp1);\n vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;\n vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;\n \n float invmax = inversesqrt(max(dot(T,T), dot(B,B)));\n mat3 tbn = mat3(T * invmax, B * invmax, normal);\n vec3 viewDirTS = -viewDir * tbn;\n\n float h = texture(heightMap, uv).r;\n return uv - (viewDirTS.xy / viewDirTS.z) * (h * heightScale);\n }\n\n vec2 parallaxOcclusion2(vec2 uv, vec3 viewDir, vec3 normal) {\n // Compute TBN in fragment shader using screen-space derivatives\n vec3 dp1 = dFdx(-vViewPosition);\n vec3 dp2 = dFdy(-vViewPosition);\n vec2 duv1 = dFdx(uv);\n vec2 duv2 = dFdy(uv);\n \n vec3 dp2perp = cross(dp2, normal);\n vec3 dp1perp = cross(normal, dp1);\n vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;\n vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;\n \n float invmax = inversesqrt(max(dot(T,T), dot(B,B)));\n mat3 tbn = mat3(T * invmax, B * invmax, normal);\n vec3 vv = -viewDir * tbn;\n\n float parallaxLimit = -length(vv.xy) / vv.z;\n parallaxLimit *= heightScale;\n\n vec2 vOffsetDir = normalize(vv.xy);\n vec2 vMaxOffset = vOffsetDir * parallaxLimit;\n\n float factor = pow(1.0 - abs(vv.z), 2.0);\n float nNumSamples = mix(float(minLayers), float(maxLayers), factor);\n float fStepSize = 1.0 / nNumSamples;\n\n float fCurrRayHeight = 1.0;\n vec2 vCurrOffset = vec2(0.0);\n vec2 vLastOffset = vec2(0.0);\n float fLastSampledHeight = 1.0;\n float fCurrSampledHeight = 1.0;\n\n for (int nCurrSample = 0; nCurrSample < 128; nCurrSample++) {\n if (float(nCurrSample) > nNumSamples) break;\n\n fCurrSampledHeight = texture2D(heightMap, uv + vCurrOffset).r;\n if (fCurrSampledHeight > fCurrRayHeight) {\n float delta1 = fCurrSampledHeight - fCurrRayHeight;\n float delta2 = (fCurrRayHeight + fStepSize) - fLastSampledHeight;\n float ratio = delta1 / (delta1 + delta2);\n vCurrOffset = ratio * vLastOffset + (1.0 - ratio) * vCurrOffset;\n break;\n } else {\n fCurrRayHeight -= fStepSize;\n vLastOffset = vCurrOffset;\n vCurrOffset += fStepSize * vMaxOffset;\n fLastSampledHeight = fCurrSampledHeight;\n }\n }\n\n return uv + vCurrOffset;\n }\n "),e.fragmentShader=e.fragmentShader.replace("#include <map_fragment>","\n vec3 pomViewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );\n vec2 uvPOM = vMapUv ;\n if (parallaxType == 1) {\n uvPOM = parallaxOffset(vMapUv, pomViewDir, vNormal);\n } else if (parallaxType == 2) {\n uvPOM = parallaxOcclusion2(vMapUv, pomViewDir, vNormal);\n }\n\n vec4 texelColor = texture2D(map, uvPOM);\n diffuseColor *= texelColor;\n "),e.fragmentShader=e.fragmentShader.replace("#include <normal_fragment_maps>","\n #ifdef USE_NORMALMAP\n // RE-COMPUTE TBN for normal mapping consistency\n vec3 dp1 = dFdx(-vViewPosition);\n vec3 dp2 = dFdy(-vViewPosition);\n vec2 duv1 = dFdx(uvPOM);\n vec2 duv2 = dFdy(uvPOM);\n vec3 dp2perp = cross(dp2, vNormal);\n vec3 dp1perp = cross(vNormal, dp1);\n vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;\n vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;\n float invmax = inversesqrt(max(dot(T,T), dot(B,B)));\n mat3 localTBN = mat3(T * invmax, B * invmax, vNormal);\n\n vec3 mapN = texture2D(normalMap, uvPOM).xyz * 2.0 - 1.0;\n mapN.xy *= normalScale;\n normal = normalize( localTBN * mapN );\n #endif\n "),e.fragmentShader=e.fragmentShader.replace("#include <roughnessmap_fragment>","\n float roughnessFactor = roughness;\n #ifdef USE_ROUGHNESSMAP\n vec4 texelRoughness = texture2D( roughnessMap, uvPOM );\n // reads channel G, compatible with a combined OcclusionRoughnessMetallic (RGB) texture\n roughnessFactor *= texelRoughness.g;\n #endif\n "),e.fragmentShader=e.fragmentShader.replace("#include <metalnesssmap_fragment>","\n float metalnessFactor = metalness;\n #ifdef USE_METALNESSMAP\n vec4 texelMetalness = texture2D( metalnessMap, uvPOM );\n // reads channel B, compatible with a combined OcclusionRoughnessMetallic (RGB) texture\n metalnessFactor *= texelMetalness.b;\n #endif\n "),e.fragmentShader=e.fragmentShader.replace("#include <aomap_fragment>","\n #ifdef USE_AOMAP\n\n // reads channel R, compatible with a combined OcclusionRoughnessMetallic (RGB) texture\n float ambientOcclusion = ( texture2D( aoMap, uvPOM ).r - 1.0 ) * aoMapIntensity + 1.0;\n\n reflectedLight.indirectDiffuse *= ambientOcclusion;\n\n #if defined( USE_CLEARCOAT ) \n clearcoatSpecularIndirect *= ambientOcclusion;\n #endif\n\n #if defined( USE_SHEEN ) \n sheenSpecularIndirect *= ambientOcclusion;\n #endif\n\n #if defined( USE_ENVMAP ) && defined( STANDARD )\n\n float dotNV = saturate( dot( geometryNormal, geometryViewDir ) );\n\n reflectedLight.indirectSpecular *= computeSpecularOcclusion( dotNV, ambientOcclusion, material.roughness );\n\n #endif\n\n #endif\n "),this.userData.shader=e}clone(){const e=super.clone();return e.heightMap=this.heightMap,e.heightScale=this.heightScale,e.parallaxType=this.parallaxType,e.minLayers=this.minLayers,e.maxLayers=this.maxLayers,e}}/*
|
|
1
|
+
import{__decorate as e,__metadata as a}from"tslib";import{Color as t,MeshStandardMaterial as n,Texture as r,Vector2 as i}from"three";import{Shader as o}from"../shader.js";import{Parameter as s}from"../parameter.js";import{removeObjectUndefined as p}from"../../utils/collections.js";import*as d from"three";const l=new r;export class StandardShader extends o{constructor(){super(...arguments),this.color=new t("#FFFFFF"),this.opacity=1,this.roughness=1,this.metalness=0,this.normalScale=new i(1,1),this.sheen=0,this.sheenColor=new t("#FFFFFF"),this.sheenRoughness=.5,this.anisotropy=0,this.heightMap=l,this.vertexColor=!1}build(){const e=this.sheen>0,a=this.anisotropy>0,t=a||e,r=(null!=this.map||null!=this.normalMap)&&null!=this.heightMap&&this.heightMap!=l&&0!==this.heightScale,i=r?ParallaxStandardMaterial:t?d.MeshPhysicalMaterial:n;this.map,null!=this.normalMap&&(this.normalMap.colorSpace=d.LinearSRGBColorSpace),null!=this.metalnessMap&&(this.metalnessMap.colorSpace=d.LinearSRGBColorSpace),null!=this.roughnessMap&&(this.roughnessMap.colorSpace=d.LinearSRGBColorSpace),null!=this.aoMap&&(this.aoMap.colorSpace=d.LinearSRGBColorSpace),null!=this.heightMap&&(this.heightMap.colorSpace=d.NoColorSpace),this.emissiveMap,null!=this.alphaMap&&(this.alphaMap.colorSpace=d.LinearSRGBColorSpace),null!=this.lightMap&&(this.lightMap.colorSpace=d.LinearSRGBColorSpace);return new i(p({color:this.color,vertexColors:this.vertexColor,opacity:this.opacity,roughness:this.roughness,metalness:this.metalness,map:this.map,lightMap:this.lightMap,lightMapIntensity:this.lightMapIntensity,aoMap:this.aoMap,aoMapIntensity:this.aoMapIntensity,emissive:this.emissive,emissiveIntensity:this.emissiveIntensity,emissiveMap:this.emissiveMap,normalMap:this.normalMap,normalScale:this.normalScale,sheen:e?this.sheen:void 0,sheenColor:e?this.sheenColor:void 0,sheenColorMap:e?this.sheenColorMap:void 0,sheenRoughness:e?this.sheenRoughness:void 0,sheenRoughnessMap:e?this.sheenRoughnessMap:void 0,anisotropy:a?this.anisotropy:void 0,anisotropyMap:a?this.anisotropyMap:void 0,anisotropyRotation:a?this.anisotropyRotation:void 0,roughnessMap:this.roughnessMap,metalnessMap:this.metalnessMap,alphaMap:this.alphaMap,envMap:this.envMap,envMapIntensity:this.envMapIntensity,...r?{heightMap:this.heightMap,heightScale:this.heightScale}:{}}))}}e([s(),a("design:type",t)],StandardShader.prototype,"color",void 0),e([s({label:"Color Map"}),a("design:type",r)],StandardShader.prototype,"map",void 0),e([s({range:[0,1]}),a("design:type",Number)],StandardShader.prototype,"opacity",void 0),e([s(),a("design:type",r)],StandardShader.prototype,"alphaMap",void 0),e([s({range:[0,1]}),a("design:type",Number)],StandardShader.prototype,"roughness",void 0),e([s(),a("design:type",r)],StandardShader.prototype,"roughnessMap",void 0),e([s({range:[0,1]}),a("design:type",Number)],StandardShader.prototype,"metalness",void 0),e([s(),a("design:type",r)],StandardShader.prototype,"metalnessMap",void 0),e([s(),a("design:type",r)],StandardShader.prototype,"lightMap",void 0),e([s(),a("design:type",Number)],StandardShader.prototype,"lightMapIntensity",void 0),e([s(),a("design:type",r)],StandardShader.prototype,"aoMap",void 0),e([s(),a("design:type",Number)],StandardShader.prototype,"aoMapIntensity",void 0),e([s(),a("design:type",t)],StandardShader.prototype,"emissive",void 0),e([s({range:[0,10]}),a("design:type",Number)],StandardShader.prototype,"emissiveIntensity",void 0),e([s(),a("design:type",r)],StandardShader.prototype,"emissiveMap",void 0),e([s(),a("design:type",r)],StandardShader.prototype,"normalMap",void 0),e([s(),a("design:type",i)],StandardShader.prototype,"normalScale",void 0),e([s({range:[0,1]}),a("design:type",Number)],StandardShader.prototype,"sheen",void 0),e([s(),a("design:type",t)],StandardShader.prototype,"sheenColor",void 0),e([s(),a("design:type",r)],StandardShader.prototype,"sheenColorMap",void 0),e([s({range:[0,1]}),a("design:type",Number)],StandardShader.prototype,"sheenRoughness",void 0),e([s(),a("design:type",r)],StandardShader.prototype,"sheenRoughnessMap",void 0),e([s(),a("design:type",Number)],StandardShader.prototype,"anisotropyRotation",void 0),e([s(),a("design:type",r)],StandardShader.prototype,"anisotropyMap",void 0),e([s({range:[0,1]}),a("design:type",Number)],StandardShader.prototype,"anisotropy",void 0),e([s(),a("design:type",r)],StandardShader.prototype,"envMap",void 0),e([s({range:[0,1]}),a("design:type",Number)],StandardShader.prototype,"envMapIntensity",void 0),e([s(),a("design:type",r)],StandardShader.prototype,"heightMap",void 0),e([s(),a("design:type",Number)],StandardShader.prototype,"heightScale",void 0),e([s(),a("design:type",Boolean)],StandardShader.prototype,"vertexColor",void 0);export var ParallaxType;!function(e){e[e.none=0]="none",e[e.offset=1]="offset",e[e.pom=2]="pom"}(ParallaxType||(ParallaxType={}));export class ParallaxStandardMaterial extends n{constructor(e={}){super(e),e.heightMap&&(this.heightMap=e.heightMap),this.heightScale=e.heightScale??.05,this.parallaxType=e.parallaxType??ParallaxType.pom,this.minLayers=e.minLayers??10,this.maxLayers=e.maxLayers??32}onBeforeCompile(e){e.uniforms.heightMap={value:this.heightMap},e.uniforms.heightScale={value:this.heightScale},e.uniforms.minLayers={value:this.minLayers},e.uniforms.maxLayers={value:this.maxLayers},e.uniforms.parallaxType={value:this.parallaxType},e.vertexTangents=!0,e.vertexShader=e.vertexShader.replace("#include <uv_pars_vertex>","\n #include <uv_pars_vertex>\n "),e.vertexShader=e.vertexShader.replace("#include <begin_vertex>","\n #include <begin_vertex>\n "),e.fragmentShader=e.fragmentShader.replace("#include <uv_pars_fragment>","\n #include <uv_pars_fragment>\n uniform sampler2D heightMap;\n uniform float heightScale;\n uniform int minLayers;\n uniform int maxLayers;\n uniform int parallaxType;\n\n vec2 parallaxOffset(vec2 uv, vec3 viewDir, vec3 normal) {\n // Compute TBN in fragment shader using screen-space derivatives\n vec3 dp1 = dFdx(-vViewPosition);\n vec3 dp2 = dFdy(-vViewPosition);\n vec2 duv1 = dFdx(uv);\n vec2 duv2 = dFdy(uv);\n \n vec3 dp2perp = cross(dp2, normal);\n vec3 dp1perp = cross(normal, dp1);\n vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;\n vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;\n \n float invmax = inversesqrt(max(dot(T,T), dot(B,B)));\n mat3 tbn = mat3(T * invmax, B * invmax, normal);\n vec3 viewDirTS = -viewDir * tbn;\n\n float h = texture(heightMap, uv).r;\n return uv - (viewDirTS.xy / viewDirTS.z) * (h * heightScale);\n }\n\n vec2 parallaxOcclusion2(vec2 uv, vec3 viewDir, vec3 normal) {\n // Compute TBN in fragment shader using screen-space derivatives\n vec3 dp1 = dFdx(-vViewPosition);\n vec3 dp2 = dFdy(-vViewPosition);\n vec2 duv1 = dFdx(uv);\n vec2 duv2 = dFdy(uv);\n \n vec3 dp2perp = cross(dp2, normal);\n vec3 dp1perp = cross(normal, dp1);\n vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;\n vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;\n \n float invmax = inversesqrt(max(dot(T,T), dot(B,B)));\n mat3 tbn = mat3(T * invmax, B * invmax, normal);\n vec3 vv = -viewDir * tbn;\n\n float parallaxLimit = -length(vv.xy) / vv.z;\n parallaxLimit *= heightScale;\n\n vec2 vOffsetDir = normalize(vv.xy);\n vec2 vMaxOffset = vOffsetDir * parallaxLimit;\n\n float factor = pow(1.0 - abs(vv.z), 2.0);\n float nNumSamples = mix(float(minLayers), float(maxLayers), factor);\n float fStepSize = 1.0 / nNumSamples;\n\n float fCurrRayHeight = 1.0;\n vec2 vCurrOffset = vec2(0.0);\n vec2 vLastOffset = vec2(0.0);\n float fLastSampledHeight = 1.0;\n float fCurrSampledHeight = 1.0;\n\n for (int nCurrSample = 0; nCurrSample < 128; nCurrSample++) {\n if (float(nCurrSample) > nNumSamples) break;\n\n fCurrSampledHeight = texture2D(heightMap, uv + vCurrOffset).r;\n if (fCurrSampledHeight > fCurrRayHeight) {\n float delta1 = fCurrSampledHeight - fCurrRayHeight;\n float delta2 = (fCurrRayHeight + fStepSize) - fLastSampledHeight;\n float ratio = delta1 / (delta1 + delta2);\n vCurrOffset = ratio * vLastOffset + (1.0 - ratio) * vCurrOffset;\n break;\n } else {\n fCurrRayHeight -= fStepSize;\n vLastOffset = vCurrOffset;\n vCurrOffset += fStepSize * vMaxOffset;\n fLastSampledHeight = fCurrSampledHeight;\n }\n }\n\n return uv + vCurrOffset;\n }\n "),e.fragmentShader=e.fragmentShader.replace("#include <map_fragment>","\n vec3 pomViewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );\n vec2 uvPOM = vMapUv ;\n if (parallaxType == 1) {\n uvPOM = parallaxOffset(vMapUv, pomViewDir, vNormal);\n } else if (parallaxType == 2) {\n uvPOM = parallaxOcclusion2(vMapUv, pomViewDir, vNormal);\n }\n\n vec4 texelColor = texture2D(map, uvPOM);\n diffuseColor *= texelColor;\n "),e.fragmentShader=e.fragmentShader.replace("#include <normal_fragment_maps>","\n #ifdef USE_NORMALMAP\n // RE-COMPUTE TBN for normal mapping consistency\n vec3 dp1 = dFdx(-vViewPosition);\n vec3 dp2 = dFdy(-vViewPosition);\n vec2 duv1 = dFdx(uvPOM);\n vec2 duv2 = dFdy(uvPOM);\n vec3 dp2perp = cross(dp2, vNormal);\n vec3 dp1perp = cross(vNormal, dp1);\n vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;\n vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;\n float invmax = inversesqrt(max(dot(T,T), dot(B,B)));\n mat3 localTBN = mat3(T * invmax, B * invmax, vNormal);\n\n vec3 mapN = texture2D(normalMap, uvPOM).xyz * 2.0 - 1.0;\n mapN.xy *= normalScale;\n normal = normalize( localTBN * mapN );\n #endif\n "),e.fragmentShader=e.fragmentShader.replace("#include <roughnessmap_fragment>","\n float roughnessFactor = roughness;\n #ifdef USE_ROUGHNESSMAP\n vec4 texelRoughness = texture2D( roughnessMap, uvPOM );\n // reads channel G, compatible with a combined OcclusionRoughnessMetallic (RGB) texture\n roughnessFactor *= texelRoughness.g;\n #endif\n "),e.fragmentShader=e.fragmentShader.replace("#include <metalnesssmap_fragment>","\n float metalnessFactor = metalness;\n #ifdef USE_METALNESSMAP\n vec4 texelMetalness = texture2D( metalnessMap, uvPOM );\n // reads channel B, compatible with a combined OcclusionRoughnessMetallic (RGB) texture\n metalnessFactor *= texelMetalness.b;\n #endif\n "),e.fragmentShader=e.fragmentShader.replace("#include <aomap_fragment>","\n #ifdef USE_AOMAP\n\n // reads channel R, compatible with a combined OcclusionRoughnessMetallic (RGB) texture\n float ambientOcclusion = ( texture2D( aoMap, uvPOM ).r - 1.0 ) * aoMapIntensity + 1.0;\n\n reflectedLight.indirectDiffuse *= ambientOcclusion;\n\n #if defined( USE_CLEARCOAT ) \n clearcoatSpecularIndirect *= ambientOcclusion;\n #endif\n\n #if defined( USE_SHEEN ) \n sheenSpecularIndirect *= ambientOcclusion;\n #endif\n\n #if defined( USE_ENVMAP ) && defined( STANDARD )\n\n float dotNV = saturate( dot( geometryNormal, geometryViewDir ) );\n\n reflectedLight.indirectSpecular *= computeSpecularOcclusion( dotNV, ambientOcclusion, material.roughness );\n\n #endif\n\n #endif\n "),this.userData.shader=e}clone(){const e=super.clone();return e.heightMap=this.heightMap,e.heightScale=this.heightScale,e.parallaxType=this.parallaxType,e.minLayers=this.minLayers,e.maxLayers=this.maxLayers,e}}/*
|
|
2
2
|
* Copyright (©) 2026 Hology Interactive AB. All rights reserved.
|
|
3
3
|
* See the LICENSE.md file for details.
|
|
4
4
|
*/
|