@hology/core 0.0.49 → 0.0.51
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/gameplay/initiate.js +1 -1
- package/dist/scene/asset-resource-loader.js +1 -1
- package/dist/scene/collision/collision-shape-import.d.ts +2 -1
- package/dist/scene/collision/collision-shape-import.js +1 -1
- package/dist/scene/model.d.ts +4 -0
- package/dist/scene/storage/storage.d.ts +6 -4
- package/dist/scene/storage/storage.js +1 -1
- package/package.json +1 -1
- package/tsconfig.tsbuildinfo +1 -1
@@ -1,4 +1,4 @@
|
|
1
|
-
import e from"typedi";import{loadScene as t}from"../scene/bootstrap.js";import{ActorFactory as n}from"./actors/factory.js";import{World as s}from"./services/world.js";import{ViewController as o}from"./services/render.js";import{RenderingView as r}from"../rendering.js";import{PhysicsSystem as i}from"./services/physics/physics-system.js";import{MeshComponent as a}from"./actors/builtin/components/mesh-component.js";import{activeContainerInstance as c}from"./actors/internal/container-map.js";import{InputService as m}from"./input/index.js";import{RuntimeAssetsService as l}from"../scene/runtime-asset-service.js";import{AssetResourceLoader as d}from"../scene/asset-resource-loader.js";import{AssetLoader as p}from"./services/asset-loader.js";import{polyfillClient as h}from"./polyfill.js";import{Subject as u}from"rxjs";import{PointerEvents as f}from"./services/pointer-events.js";import{RuntimeBundledBackendService as g}from"../scene/runtime-bundled-backend-service.js";export function initiateGame(m,u){if(h(),0!=u.element.childNodes.length)return console.error("Can not initialize the game with a non-empty html element"),null;e.has(o);const w=e.of("default"),j=new HologyRuntime(w),v=new n(w,{inEditor:!1});var S;e.set(n,v),S=u.element,Object.assign(S.style,{position:"absolute",top:"0",left:"0",width:"100%",height:"100%"});const y=new r(u.element,{enableXR:!0===u.xr?.enabled,maxPixelRatio:u.rendering?.maxPixelRatio});y.renderer.shadowMap.autoUpdate=!0,null!=u?.rendering?.resolutionScale&&(y.resolutionScale=u.rendering.resolutionScale),e.set(r,y);const I=new o(y);e.set(o,I);const x=e.get(s);e.set(s,x);const b=new g,D=new l(b),R=new d;R.setDataDir(u.dataDir);const G=Object.entries(u.shaders).map((([e,t])=>({name:e,type:t}))),H=new p(R,D,G);return e.set(p,H),(async()=>{
|
1
|
+
import e from"typedi";import{loadScene as t}from"../scene/bootstrap.js";import{ActorFactory as n}from"./actors/factory.js";import{World as s}from"./services/world.js";import{ViewController as o}from"./services/render.js";import{RenderingView as r}from"../rendering.js";import{PhysicsSystem as i}from"./services/physics/physics-system.js";import{MeshComponent as a}from"./actors/builtin/components/mesh-component.js";import{activeContainerInstance as c}from"./actors/internal/container-map.js";import{InputService as m}from"./input/index.js";import{RuntimeAssetsService as l}from"../scene/runtime-asset-service.js";import{AssetResourceLoader as d}from"../scene/asset-resource-loader.js";import{AssetLoader as p}from"./services/asset-loader.js";import{polyfillClient as h}from"./polyfill.js";import{Subject as u}from"rxjs";import{PointerEvents as f}from"./services/pointer-events.js";import{RuntimeBundledBackendService as g}from"../scene/runtime-bundled-backend-service.js";export function initiateGame(m,u){if(h(),0!=u.element.childNodes.length)return console.error("Can not initialize the game with a non-empty html element"),null;e.has(o);const w=e.of("default"),j=new HologyRuntime(w),v=new n(w,{inEditor:!1});var S;e.set(n,v),S=u.element,Object.assign(S.style,{position:"absolute",top:"0",left:"0",width:"100%",height:"100%"});const y=new r(u.element,{enableXR:!0===u.xr?.enabled,maxPixelRatio:u.rendering?.maxPixelRatio});y.renderer.shadowMap.autoUpdate=!0,null!=u?.rendering?.resolutionScale&&(y.resolutionScale=u.rendering.resolutionScale),e.set(r,y);const I=new o(y);e.set(o,I);const x=e.get(s);e.set(s,x);const b=new g,D=new l(b),R=new d;R.setDataDir(u.dataDir);const G=Object.entries(u.shaders).map((([e,t])=>({name:e,type:t}))),H=new p(R,D,G);return e.set(p,H),(async()=>{const n=e.get(i);if(await n.start(),j.isShutdown)return;if(await b.preloadData(),j.isShutdown)return;const{scene:s,actors:o}=await t(y,u.sceneName,u.dataDir,u.shaders,u.actors,v,b,D,R);if(x.scene=s,j.isShutdown)return void y.stop();e.import([a]);for(const e of o)x.addActor(e);n.addFromScene(s),y.loop((e=>{})),j.status=5,c.value=w,w.remove(m),w.set({id:m,type:m});const r=w.get(m);c.value=null,j.gameInstance=r,w.get(f).start(),r instanceof GameInstance&&r.onStart(),j._resolver(!0)})(),j}export class GameInstance{onStart(){}onShutdown(){}}export function createHologyScene(){}export class HologyRuntime{constructor(e){this.containerInstance=e,this.status=0,this.isShutdown=!1,this.shutdownStarted=new u,this.ready=new Promise((e=>{this._resolver=e}))}getWorld(){return this.containerInstance.get(s)}getService(e){return this.containerInstance.get(e)}shutdown(){this.isShutdown=!0,this.shutdownStarted.next(),this.gameInstance instanceof GameInstance&&this.gameInstance.onShutdown(),this.containerInstance.get(m).stop();const e=this.containerInstance.get(r);e?.stop();for(const e of this.getWorld().actors)this.getWorld().removeActor(e);this.containerInstance.get(i).stop(),this.containerInstance.get(f).stop(),this.containerInstance.reset()}}
|
2
2
|
/*
|
3
3
|
* Copyright (©) 2023. All rights reserved.
|
4
4
|
* See the LICENSE.md file for details.
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import{AudioLoader as e,Group as t,LoadingManager as a,Mesh as
|
1
|
+
import{AudioLoader as e,Group as t,LoadingManager as a,Mesh as s,Object3D as i,TextureLoader as r}from"three";import{GLTFLoader as n,MTLLoader as o,OBJLoader as h}from"three-stdlib";import{FBXLoader as c}from"three-stdlib";import{cloneMesh as l}from"../utils/mesh.js";import{pathJoin as d}from"../utils/files.js";import{Subject as p,firstValueFrom as m}from"rxjs";import{importCollisionShapes as f}from"./collision/collision-shape-import.js";import*as u from"three";import{iterateMaterials as g}from"../utils/materials.js";const w=["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 n(this.loadingManager),this.fbxLoader=new c(this.loadingManager),this.objLoader=new h(this.loadingManager),this.textureLoader=new r(this.loadingManager),this.audioLoader=new e(this.loadingManager),this.makeReady=new p,this.ready=m(this.makeReady),this.collisionShapeCache=new Map}setDataDir(e){this.basePath=d(e,"asset-resources"),this.makeReady.next(!0)}getUri(e){return d(this.basePath,e)+`?windowId=${getElectronArg("windowId")}`}async getTexture(e){return null==e?null:(await this.ready,this.cache.has(e.id)||await this.textureLoader.loadAsync(this.getUri(e.fileKey)).then((t=>(t.wrapS=L(e.texture?.wrapS),t.wrapT=L(e.texture?.wrapT),t.flipY=e.texture?.flipY??!0,this.textureCache.set(e.id,t),t))),this.textureCache.get(e.id))}async getMesh(e){if(await this.ready,!w.includes(e.fileFormat?.toLowerCase()))return console.error("Unsupported mesh file format "+e.fileFormat,e),{scene:new t,animations:[]};if(!this.cache.has(e.fileKey))try{this.cache.set(e.fileKey,await this.loadMesh(e))}catch(e){return this.onError(e),{scene:new t,animations:[]}}const a=l(this.cache.get(e.fileKey).scene),i=this.cache.get(e.fileKey).animations;a.traverse((e=>{e instanceof s&&e.material instanceof Array&&(e.material=e.material.slice())}));const r=this.computeCollisionShapes(e,a),n=new AssetMeshInstance;n.add(a),n.collisionShapes=r,n.animations=i;const o=!!e.receiveShadow??!0,h=!!e.castShadow??!1;return a.traverse((e=>{e.castShadow=h,e.receiveShadow=o})),{scene:n,animations:i}}async getAudio(e){return await this.ready,this.audioLoader.loadAsync(this.getUri(e.fileKey))}computeCollisionShapes(e,t){const a=e.id+e.mesh?.collisions?.shapeType;return this.collisionShapeCache.has(a)||this.collisionShapeCache.set(a,f(t,e)),this.collisionShapeCache.get(a)}async loadMesh(e){return await this.ready,await this.loadByAsset(e).then((e=>(e.scene=function(e,t){let a=!1;if(t.traverseVisible((e=>{b.test(e.name)&&(a=!0)})),!a)return t;const s=new u.LOD,i=[t];for(;i.length>0;){const e=i.shift(),t=e.name.match(b);if(null!=t){const a=parseInt(t[1]);0===a?s.addLevel(e,0):console.warn(`Skipping LOD level ${a} for now as LOD is not fully supported`)}else i.push(...e.children)}return s}(0,e.scene),e)))}async loadByAsset(e){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 o;t.materialOptions={normalizeRGB:!1};const a=await t.loadAsync(this.getUri(e.materialLib));this.objLoader.setMaterials(a)}return this.objLoader.loadAsync(t).then((e=>(y(e),e))).then((e=>({scene:e,animations:e.animations})))}}}function y(e){if(e instanceof s)for(const t of g(e.material))t instanceof u.MeshPhongMaterial&&!t.color.isLinear&&(t.color.isLinear=!0);e.children?.forEach(y)}export class AssetMeshInstance extends i{}export function getElectronArg(e){const t=`--${e}=`,a=window.process?.argv.find((e=>e.startsWith(t)));return a?.substring(t.length)}function L(e){switch(e){case"clamp":return u.ClampToEdgeWrapping;case"repeat":return u.RepeatWrapping;case"mirror":return u.MirroredRepeatWrapping}return u.RepeatWrapping}const b=/_LOD(\d+)$/;
|
2
2
|
/*
|
3
3
|
* Copyright (©) 2023. All rights reserved.
|
4
4
|
* See the LICENSE.md file for details.
|
@@ -1,4 +1,5 @@
|
|
1
1
|
import { Object3D } from "three";
|
2
2
|
import { CollisionShape } from './collision-shape.js';
|
3
|
-
|
3
|
+
import { Asset } from "../../scene/model.js";
|
4
|
+
export declare function importCollisionShapes(group: Object3D, asset: Asset): CollisionShape[];
|
4
5
|
export declare function isCollisionMesh(object: Object3D): boolean;
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import{Box3 as e,Euler as t,Mesh as n,Quaternion as
|
1
|
+
import{Box3 as e,Euler as t,Mesh as n,Quaternion as s,Vector3 as i}from"three";import{BoxCollisionShape as o,ConvexPolyhedronCollisionShape as r,SphereCollisionShape as a,TrimeshCollisionShape as m}from"./collision-shape.js";const h=new s;export function importCollisionShapes(s,c){let u=c.mesh?.collisions?.shapeType;if(null==u){let e=!1;s.traverse((t=>{(t instanceof n||t.isMesh)&&isCollisionMesh(t)&&(e=!0)})),u=e?"imported":"convex"}const p=[];return s.traverse((s=>{if(s instanceof n||s.isMesh){let n;"imported"===u?n=function(t){if(t.name.startsWith(l.convex))return new r(t);if(t.name.startsWith(l.box)){const n=t.clone();n.quaternion.set(0,0,0,1),n.updateMatrixWorld();const s=(new e).setFromObject(n);return isFinite(s.min.lengthSq())?new o(new i(s.max.x-s.min.x,s.max.y-s.min.y,s.max.z-s.min.z)):null}if(t.name.startsWith(l.sphere)){t.geometry.computeBoundingSphere();const e=t.geometry.boundingSphere.radius*Math.max(t.scale.x,t.scale.y,t.scale.z);return new a(e)}if(t.name.startsWith(l.trimesh))return new m(t.geometry)}(s):"convex"===u?n=new r(s):"mesh"===u&&(n=new m(s.geometry)),null!=n&&(n.offset=s.getWorldPosition(new i),n.rotation=(new t).setFromQuaternion(s.getWorldQuaternion(h)),p.push(n),isCollisionMesh(s)&&(s.visible=!1))}})),p}export function isCollisionMesh(e){return e.name.startsWith(l.convex)||e.name.startsWith(l.box)||e.name.startsWith(l.sphere)||e.name.startsWith(l.trimesh)}var l;!function(e){e.convex="UCX_",e.box="UBX_",e.sphere="USP_",e.trimesh="UTM_"}(l||(l={}));new e;
|
2
2
|
/*
|
3
3
|
* Copyright (©) 2023. All rights reserved.
|
4
4
|
* See the LICENSE.md file for details.
|
package/dist/scene/model.d.ts
CHANGED
@@ -35,6 +35,7 @@ export type CustomParamValue = {
|
|
35
35
|
value: unknown;
|
36
36
|
};
|
37
37
|
export type WrapMode = 'clamp' | 'repeat' | 'mirror';
|
38
|
+
export type CollisionShapeType = 'convex' | 'mesh' | 'imported';
|
38
39
|
export type TextureSettings = {
|
39
40
|
flipY?: boolean;
|
40
41
|
wrapS?: WrapMode;
|
@@ -65,6 +66,9 @@ export interface Asset {
|
|
65
66
|
};
|
66
67
|
mesh?: {
|
67
68
|
rescale?: number;
|
69
|
+
collisions?: {
|
70
|
+
shapeType?: CollisionShapeType;
|
71
|
+
};
|
68
72
|
};
|
69
73
|
prefab?: {
|
70
74
|
objects: SceneObject[];
|
@@ -21,21 +21,23 @@ export declare class ObjectStorage<T extends Entity> {
|
|
21
21
|
constructor(name: string, filePathFn?: (o: ObjectIndexEntry) => string);
|
22
22
|
setBasePath(path: string): void;
|
23
23
|
private determineIfMetaFileShouldBeCreated;
|
24
|
+
watch(): void;
|
24
25
|
getAll(): Promise<T[]>;
|
25
26
|
get(id: string): Promise<T>;
|
26
27
|
save(obj: T): Promise<T>;
|
27
28
|
rename(obj: T, name: string): Promise<T>;
|
28
29
|
delete(obj: T): Promise<void>;
|
29
30
|
create(obj: Partial<T>): Promise<T>;
|
31
|
+
private serialize;
|
30
32
|
private updateIndex;
|
31
33
|
private get indexFilePath();
|
32
34
|
private loadIndex;
|
33
35
|
ensureResourceDir(): Promise<void>;
|
34
|
-
saveFile(asset: T, file: File): Promise<
|
35
|
-
saveExtraFile(sourcePath: string, relativePath: string): Promise<
|
36
|
+
saveFile(asset: T, file: File): Promise<void>;
|
37
|
+
saveExtraFile(sourcePath: string, relativePath: string): Promise<void>;
|
36
38
|
getAssetPath(asset: T): any;
|
37
|
-
replaceFile(asset: T, replacementFile:
|
38
|
-
deleteFile(fileKey: string): Promise<
|
39
|
+
replaceFile(asset: T, replacementFile: string): Promise<void>;
|
40
|
+
deleteFile(fileKey: string): Promise<void>;
|
39
41
|
private privateObjectPath;
|
40
42
|
}
|
41
43
|
export declare function tokenizeName(name: string): string;
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import{randomUUID as t}from"../../utils/uuid.js";import{pathJoin as e}from"../../utils/files.js";import{firstValueFrom as i,map as a,
|
1
|
+
import{randomUUID as t}from"../../utils/uuid.js";import{pathJoin as e}from"../../utils/files.js";import{firstValueFrom as i,map as a,Observable as s,Subject as n,tap as r}from"rxjs";import{sleepDelay as h}from"../../utils/async.js";const o={},c={},l={};null==o.read&&window.require&&(Object.assign(o,window.require("fs")),Object.assign(c,o.promises),Object.assign(l,window.require("path")));const d=null!=o.existsSync;function u(){if(d){const t="--path=",e=window.process.argv.find((e=>e.startsWith(t)));return e?e.substring(t.length):""}return""}const p=/^[A-Z]:/;function w(...t){return 0===t.length?"":p.test(t[0])?l.join(...t):e(...t)}export class ObjectStorage{constructor(t,e){this.name=t,this.filePathFn=e,this.basePathUpdates=new n,this.basePath=this.basePathUpdates.pipe(a((t=>w(u(),t))),r((t=>{this.path=w(t,this.name),f(this.path),this.determineIfMetaFileShouldBeCreated()}))),this.loaded=i(this.basePath),this.shouldCreateIndex=!0}setBasePath(t){this.basePathUpdates.next(t)}async determineIfMetaFileShouldBeCreated(){try{(await c.readFile(w(u(),"vite.config.ts"))).toString().includes("hologyBuild")&&(this.shouldCreateIndex=!1)}catch(t){console.warn("Failed to read vite config to determine if meta files should be created")}}watch(){y(),function(t){return new s((e=>{const i=new AbortController,{signal:a}=i,s=c.watch(t,{recursive:!0,signal:a});return(async()=>{for await(const t of s)console.log(t),e.next(t)})(),()=>{i.abort()}}))}(this.path).pipe(a((t=>t.eventType)))}async getAll(){if(d){await this.loaded,await f(this.path);const t=(await c.readdir(this.path,{recursive:!0,withFileTypes:!0})).filter((t=>t.isFile()&&t.name.endsWith(".json")&&!/^[\._]/.test(t.name)));return await Promise.all(t.map((t=>c.readFile(w(this.path,t.path,t.name)).then((e=>({...JSON.parse(e.toString()),path:t}))))))}const t=await this.loadIndex();return Promise.all(Object.keys(t).map((t=>this.get(t))))}async get(t){const e=await this.loadIndex(),i=e[t]??Object.values(e).find((e=>e.name===t));if(null==i)return;const a=this.privateObjectPath(i);if(!d)return(await fetch(a)).json();return await m(a)?JSON.parse((await c.readFile(a)).toString()):null}async save(t){return y(),await this.loaded,await c.writeFile(this.privateObjectPath(t),this.serialize(t)),await this.updateIndex(),t}async rename(t,e){const i={...t,name:e},a=this.privateObjectPath(t),s=this.privateObjectPath(i);try{await c.rename(a,s)}catch(t){console.error(t),console.warn("Rename failed, retrying",{currentPath:a,newPath:s}),await h(400),await c.rename(a,s)}return await this.save(i),await this.updateIndex(),i}async delete(t){await c.unlink(this.privateObjectPath(t)),this.updateIndex()}async create(e){return y(),await this.loaded,e.id=t(),await c.writeFile(this.privateObjectPath(e),this.serialize(e)),await this.updateIndex(),e}serialize(t){const e={...t};return delete e.path,JSON.stringify(e,null,2)}async updateIndex(){y();const t=await this.getAll(),e={};for(const i of t)e[i.id]={id:i.id,name:i.name??i.id,path:i.path};if(this.cachedIndex=e,d){if(!this.shouldCreateIndex)return;await c.writeFile(this.indexFilePath,JSON.stringify(e,null,2))}}get indexFilePath(){return w(this.path,"_meta.json")}async loadIndex(){return null==this.cachedIndex&&(d?await this.updateIndex():this.cachedIndex=await(await fetch(this.indexFilePath)).json()),this.cachedIndex}async ensureResourceDir(){await f(w(this.path+"-resources"))}async saveFile(t,e){return y(),await f(w(this.path+"-resources")),c.copyFile(e.path,w(this.path+"-resources",t.fileKey))}async saveExtraFile(t,e){return y(),await f(w(this.path+"-resources")),c.copyFile(t,w(this.path+"-resources",e))}getAssetPath(t){return window&&"function"==typeof window.require?window.require("path").join(this.path+"-resources",t.fileKey):w(this.path+"-resources",t.fileKey)}async replaceFile(t,e){if(await m(e))return c.copyFile(e,w(this.path+"-resources",t.fileKey));console.error("Failed to replace file using path "+e)}async deleteFile(t){if(null==t)return;y();const e=w(this.path+"-resources",t);return await m(e)?c.unlink(e):void 0}privateObjectPath(t){return this.filePathFn?w(this.path,this.filePathFn(t)):w(this.path,tokenizeName(t.name??t.id)+".json")}}export function tokenizeName(t){return t.trim().replace(/\s/g,"_").replace(/[^a-z0-9_\-\.]/gi,"")}async function f(t){d&&(await m(t)||await c.mkdir(t,{recursive:!0}))}function m(t){return!!d&&new Promise((function(e,i){o.exists(t,(function(t){e(t)}))}))}function y(){if(!d)throw new Error("Must have direct access to filesystem")}
|
2
2
|
/*
|
3
3
|
* Copyright (©) 2023. All rights reserved.
|
4
4
|
* See the LICENSE.md file for details.
|