@hology/core 0.0.59 → 0.0.61

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
- import{__decorate as t,__metadata as e}from"tslib";import*as i from"@dimforge/rapier3d-compat";import{QueryFilterFlags as s}from"@dimforge/rapier3d-compat";import{BehaviorSubject as o,distinctUntilChanged as n,filter as r,map as a,Subject as c,takeUntil as l}from"rxjs";import*as d from"three";import{ArrowHelper as h,BufferAttribute as u,BufferGeometry as y,Group as p,LineSegments as g,Matrix4 as m,MeshBasicMaterial as f,Object3D as w,Quaternion as v,Raycaster as b,Scene as x,Vector3 as B}from"three";import{Service as A}from"typedi";import{AssetMeshInstance as C}from"../../../scene/asset-resource-loader.js";import{BoxCollisionShape as D,CapsuleCollisionShape as z,ConeCollisionShape as M,ConvexPolyhedronCollisionShape as T,CylinderCollisionShape as R,PhysicalShapeMesh as S,PlaneCollisionShape as E,SphereCollisionShape as W,TrimeshCollisionShape as F}from"../../../index.js";import{LandscapeGroup as _}from"../../../scene/landscape/landscape.js";import{ViewController as V}from"../render.js";import{World as k}from"../world.js";import*as I from"three/examples/jsm/utils/BufferGeometryUtils.js";import{calculateEffectiveScale as P}from"../../../utils/three/traverse.js";export class RayTestResult{constructor(){this.hasHit=!1,this.hitPoint=new B,this.hitNormal=new B}}export var PhysicsBodyType;!function(t){t[t.dynamic=1]="dynamic",t[t.static=2]="static",t[t.kinematic=4]="kinematic",t[t.kinematicVelocityBased=8]="kinematicVelocityBased"}(PhysicsBodyType||(PhysicsBodyType={}));let L=class{set showDebug(t){this.shouldRenderDebug=t,this.debugMesh&&(this.debugMesh.visible=t)}get showDebug(){return this.shouldRenderDebug}constructor(t,e){this.viewController=t,this.gameWorld=e,this.staticMeshes=new Map,this.staticBodies=new Map,this.actorBodies=new Map,this.bodyActors=new Map,this.collisionEvents=new c,this.beforeStep=new c,this.afterStep=new c,this.shouldRenderDebug=!1,this._raycaster=new b,this._reusableResult=new RayTestResult,this._raytestDiff=new B,this._raytestDirection=new B,this.controlledActors=new Set,this.ready=this.setup()}createDebugMesh(){return new g(new y,new f({color:255}))}async start(){return await this.ready,this.handleTick(),this.ready}renderDebug(){null==this.debugMesh&&(this.debugMesh=this.createDebugMesh(),this.debugMesh.visible=this.shouldRenderDebug,this.debugMesh.raycast=function(){},this.gameWorld.scene.add(this.debugMesh));const t=this.world.debugRender();this.debugMesh.geometry.setAttribute("position",new u(t.vertices,3))}async setup(){if(null!=this.rapier)throw new Error("Rapier is already estup");this.rapier=await K(),this.eventQueue=new i.EventQueue(!0),this.setupWorld()}handleTick(){this.fixedupdateSub=this.viewController.onUpdate().subscribe((t=>{t=Math.min(.1,t),this.beforeStep.next(t),this.updatePhysics(t),this.afterStep.next(t),this.showDebug&&this.renderDebug(),this.world.bodies.forEach((t=>{if(t.isFixed())return;const e=this.staticMeshes.get(t)??this.bodyActors.get(t)?.object;var i,s;null!=e&&(e.parent instanceof x&&(J(e.position,t.translation()),(t.isDynamic()||t.isKinematic()&&!this.controlledActors.has(this.bodyActors.get(t)?.id))&&(i=e.quaternion,s=t.rotation(),i.x=s.x,i.y=s.y,i.z=s.z,i.w=s.w)))}))}))}_updateWorld(){this.world.timestep=0,this.world.step()}updatePhysics(t){this.world.timestep=t,this.world.step(this.eventQueue),this.eventQueue.drainCollisionEvents(((t,e,i)=>{this.collisionEvents.next({handle1:t,handle2:e,started:i}),this.collisionEvents.next({handle1:e,handle2:t,started:i})}))}rayTestFromCamera(t,e,i){this._raycaster.setFromCamera(U,this.viewController.getCamera());const s=this._raycaster.ray.origin,o=this._raycaster.ray.direction.multiplyScalar(t).add(s);return this.rayTest(s,o,e,i)}rayTest(t,e,s,o){null==s&&(s=this._reusableResult);const n=this._raytestDiff,r=this._raytestDirection;if(n.subVectors(e,t),r.copy(n).normalize(),0===r.length())return console.warn("Ray test called with to and from being equal"),s;const a=new i.Ray(t,r),c=n.length(),l=this.world.castRayAndGetNormal(a,c,!1,void 0,void 0,void 0,null!=o?.excludeActor?this.actorBodies.get(o.excludeActor.id):void 0,o?.excludeTriggers?t=>!t.isSensor():void 0);if(s.hasHit=null!=l,s.hasHit){const e=a.pointAt(l.timeOfImpact);s._internal=l,J(s.hitNormal,l.normal),J(s.hitPoint,e),s.distance=Z.subVectors(s.hitPoint,t).length();const i=this.world.bodies.getAll().find((t=>function(t,e){for(let i=0,s=t.numColliders();i<s;i++){const s=t.collider(i);if(e(s))return s}}(t,(t=>t===l.collider))));s.actor=null!=i?this.bodyActors.get(i):null}if(this.showDebug){const e=new h(r,t,c,o?.debugColor??255);this.gameWorld.scene.add(e),setTimeout((()=>this.gameWorld.scene.remove(e)),o?.debugLifetime??200)}return this._reusableResult}setGravity(t,e,i){this.world.gravity.x=t,this.world.gravity.y=e,this.world.gravity.z=i}getGravity(){return q.set(this.world.gravity.x,this.world.gravity.y,this.world.gravity.z)}addFromScene(t){this.addRecursively(t);for(const t of this.staticBodies.values())Y(t,(t=>t.setActiveEvents(i.ActiveEvents.COLLISION_EVENTS)))}addRecursively(t){if(this.removeSceneObject(t),!function(t){if(null!=t.userData?.src){const e=t.userData?.src;return"actor"===e.type}return!1}(t))if(t instanceof S&&null!=t.collisionShape){const e=this.createStaticBody(t,[t.collisionShape],t.physics);this.staticMeshes.set(e,t),this.staticBodies.set(t,e)}else if(t instanceof C&&!1!==t.userData?.src?.collisionDetection)if(t.children[0]&&t.children[0].instanceMatrix)this.createForInstancedMesh(t.children[0],t.collisionShapes);else{const e=this.createStaticBody(t,t.collisionShapes,t.physics);this.staticMeshes.set(e,t),this.staticBodies.set(t,e)}else t instanceof _?this.addLandscapeGroup(t):(t instanceof p||t instanceof x)&&t.children.forEach((t=>this.addRecursively(t)))}createForInstancedMesh(t,e){const i=new m;for(let s=0;s<t.count;s++){const o=new w;o.matrix.identity(),i.fromArray(t.instanceMatrix.array,16*s),o.applyMatrix4(i);this.createStaticBody(o,e)}}getCharacterController(t=.01){return this.world?.createCharacterController(t)}getActorComputedMovement(t,e,i){const o=this.actorBodies.get(t.id);this.controlledActors.add(t.id);const n=o.collider(0);e.computeColliderMovement(n,i,s.EXCLUDE_SENSORS,null,tt);const r=e.computedMovement();return J($,r),$}setNextKinematicTranslation(t,e){const i=this.actorBodies.get(t.id),s=i.translation();s.x+=e.x,s.y+=e.y,s.z+=e.z,i?.setNextKinematicTranslation(s)}setAngularVelocity(t,e){const i=this.actorBodies.get(t.id);G.x=e.x,G.y=e.y,G.z=e.z,i?.setAngvel(G,!0)}setLinearVelocity(t,e){const i=this.actorBodies.get(t.id);G.x=e.x,G.y=e.y,G.z=e.z,i?.setLinvel(G,!0)}getLinearVelocity(t,e=new B){const i=this.actorBodies.get(t.id).linvel();return e.x=i.x,e.y=i.y,e.z=i.z,e}getAngularVelocity(t,e=new B){const i=this.actorBodies.get(t.id).angvel();return e.x=i.x,e.y=i.y,e.z=i.z,e}setLinearDamping(t,e){const i=this.actorBodies.get(t.id);i?.setLinearDamping(e)}setAngularDamping(t,e){const i=this.actorBodies.get(t.id);i?.setAngularDamping(e)}createCharacterCollision(){return new i.CharacterCollision}addLandscapeGroup(t){const e=t.userData.src,s=e.landscape.heightMaps;for(const n of t.sections){this.staticBodies.has(n)&&this.world.removeRigidBody(this.staticBodies.get(n));const t=e.landscape.options.density+1,r=e.landscape.options.sectionSize,a=new Array(t);for(let e=0;e<t;e++)a[e]=new Array(t).fill(0);const c=s.find((t=>t.x===n.x&&t.y==n.y));if(null!=c)for(const e of c.points){if(null==a[e.i%t])continue;const i=t-1-Math.floor(e.i/t);i in a[e.i%t]?a[e.i%t][i]=e.y/r:console.warn("wrong index",{points:a,point:e,i:e.i%t,k:i,heightMap:c})}const l=e.landscape.options.density,d=a.flatMap((t=>t.reverse())),h=i.ColliderDesc.heightfield(l,l,new Float32Array(d),new i.Vector3(r,r,r));var o=n.getWorldPosition(new B);if(!1!==e.collisionDetection){const t=this.world.createRigidBody(i.RigidBodyDesc.fixed()),e=new i.Vector3(0,0,0);X(e,o),t.setTranslation(e,!1),this.world.createCollider(h,t),this.staticBodies.set(n,t)}}}addActor(t,e,s={}){if(0==e.length)return void console.error("No collision shapes were defined when adding actor to the physics system.");this.removeActor(t);const o=t.object;let n;switch(s.type??PhysicsBodyType.static){case PhysicsBodyType.dynamic:n=i.RigidBodyDesc.dynamic(),n.mass=s.mass??1;break;case PhysicsBodyType.kinematic:n=i.RigidBodyDesc.kinematicPositionBased();break;case PhysicsBodyType.kinematicVelocityBased:n=i.RigidBodyDesc.kinematicVelocityBased();break;default:n=s.isTrigger?i.RigidBodyDesc.kinematicVelocityBased():i.RigidBodyDesc.fixed()}const r=this.world.createRigidBody(n);r.enableCcd(1==s.continousCollisionDetection);for(const t of e)this.addShape(r,t,o);Y(r,(t=>{null!=s.isTrigger&&(t.setSensor(s.isTrigger),t.setActiveCollisionTypes(i.ActiveCollisionTypes.ALL),t.setActiveEvents(i.ActiveEvents.COLLISION_EVENTS)),null!=s.friction&&t.setFriction(s.friction),null!=s.density&&t.setDensity(s.density),null!=s.mass&&t.setMass(s.mass),null!=s.restitution&&t.setRestitution(s.restitution)})),N(r,o),!0===s.ignoreForNavMesh&&(r.userData={ignoreForNavMesh:!0}),this.actorBodies.set(t.id,r),this.bodyActors.set(r,t)}applyTorque(t,e){const i=this.actorBodies.get(t.id);G.x=e.x,G.y=e.y,G.z=e.z,i?.addTorque(G,!0)}applyTorqueImpulse(t,e){const i=this.actorBodies.get(t.id);G.x=e.x,G.y=e.y,G.z=e.z,i?.applyTorqueImpulse(G,!0)}resetForces(t){const e=this.actorBodies.get(t.id);e?.resetForces(!1)}resetTorques(t){const e=this.actorBodies.get(t.id);e?.resetTorques(!1)}applyForce(t,e){const i=this.actorBodies.get(t.id);G.x=e.x,G.y=e.y,G.z=e.z,i?.addForce(G,!0)}applyImpulse(t,e){const i=this.actorBodies.get(t.id);G.x=e.x,G.y=e.y,G.z=e.z,i?.applyImpulse(G,!0)}applyLocalForce(t,e,i){const s=this.actorBodies.get(t.id);X(G,e),null==i?s?.addForce(G,!0):(X(Q,i),s?.addForceAtPoint(G,Q,!0))}applyLocalImpulse(t,e,i){const s=this.actorBodies.get(t.id);X(G,e),null==i?s.applyImpulse(G,!0):(X(Q,i),s.applyImpulseAtPoint(G,Q,!0))}applyRadiusImpulse(t,e,s){this.world.bodies.forEach((o=>{if(o.collider(0)?.isSensor())return;if(o.bodyType()!==i.RigidBodyType.Dynamic)return;const n=H;J(n,o.translation());const r=n.clone().sub(t);if(r.length()>e)return;const a=r.clone().normalize().multiplyScalar(s);G.x=a.x,G.y=a.y,G.z=a.z,o.applyImpulse(G,!0)}))}removeActor(t){if(null==t)return;this.controlledActors.delete(t.id);const e=this.actorBodies.get(t.id);null!=e&&(this.bodyActors.delete(e),this.world.removeRigidBody(e)),this.actorBodies.delete(t.id)}removeSceneObject(t){let e=this.staticBodies.get(t);null!=e&&this.world.getRigidBody(e.handle)&&this.world.removeRigidBody(e),this.staticBodies.delete(t)}activateActorEvents(t){this.actorBodies.get(t.id)}_onCollisionWithActorEvent(t,e,i){return this.activateActorEvents(t),this.collisionEvents.pipe(l(t.disposed),r((({started:t})=>t===i)),a((({handle1:t,handle2:e,started:i})=>({a1:this.bodyActors.get(this.world.getCollider(t)?.parent()),a2:this.bodyActors.get(this.world.getCollider(e)?.parent()),started:i}))),r((({a1:i,a2:s})=>null!=i&&null!=s&&i.id===t.id&&e(i,s))),a((({a2:t})=>t)))}onBeginContact(t){return this.activateActorEvents(t),this.collisionEvents.pipe(l(t.disposed),r((t=>t.started)),r((({handle1:e})=>{const i=this.bodyActors.get(this.world.getCollider(e)?.parent());return null!=i&&i.id===t.id})),a((t=>t.handle2)))}onEndContact(t){return this.activateActorEvents(t),this.collisionEvents.pipe(l(t.disposed),r((t=>!t.started)),r((({handle1:e})=>{const i=this.bodyActors.get(this.world.getCollider(e)?.parent());return null!=i&&i.id===t.id})),a((t=>t.handle2)))}onHasContactChanged(t){const e=new Set,i=new o(!1);return this.onBeginContact(t).subscribe((t=>{e.add(t),i.next(e.size>0)})),this.onEndContact(t).subscribe((t=>{e.delete(t),i.next(e.size>0)})),i.pipe(n())}onBeginOverlapWithActorType(t,e){return this._onCollisionWithActorEvent(t,((t,i)=>i instanceof e),!0)}onEndOverlapWithActorType(t,e){return this._onCollisionWithActorEvent(t,((t,i)=>i instanceof e),!1)}onBeginOverlapWithActor(t,e){return this._onCollisionWithActorEvent(t,((t,i)=>e.id===i.id),!0)}onEndOverlapWithActor(t,e){return this._onCollisionWithActorEvent(t,((t,i)=>e.id===i.id),!1)}onCollisionWithActor(t,e){return this.onBeginOverlapWithActor(t,e)}onCollisionWithActorType(t,e){return this.onBeginOverlapWithActorType(t,e)}updateActorTransform(t){const e=this.actorBodies.get(t.id);null!=e?N(e,t.object):console.warn("Actor has not been added to physics world",t)}setupWorld(){const t=new i.World({x:0,y:-9.81,z:0});this.world=t}getActorContacts(t,e){const s=this.actorBodies.get(t.id);if(s&&s.numColliders()>0){const o=s.collider(0);let n=o.shape,r=o.translation(),a=o.rotation(),c=e,l=.3;const d=this.world.castShape(r,a,c,n,.1,l,!0,null,null,null,this.actorBodies.get(t.id),(t=>t.shape.type!=i.ShapeType.HeightField));if(null!=d){const t=new B,e=new B,i=new B;return J(t,d.witness2),J(e,d.witness1),J(i,d.normal1),i.negate(),[{ri:t,rj:e,ni:i}]}return[]}return console.warn("Actor is not added to the physics system"),[]}stop(){this.world?.bodies.forEach((t=>this.world.removeRigidBody(t))),this.world?.free(),this.fixedupdateSub?.unsubscribe()}createStaticBody(t,e,s){const o=s?.type===PhysicsBodyType.dynamic?i.RigidBodyDesc.dynamic():i.RigidBodyDesc.fixed(),n=this.world.createRigidBody(o);for(const i of e){if(null==i){console.warn("Collision shape is missing for object",t);continue}const o=this.addShape(n,i,t);null!=s?.friction&&o.setFriction(s.friction),null!=s?.density&&o.setDensity(s.density),null!=s?.mass&&o.setMass(s.mass/e.length),null!=s?.restitution&&o.setRestitution(s.restitution)}return N(n,t),n}addShape(t,e,i){const s=i.getWorldScale(et),o=this.createShape(e,s);o.friction=.1;const n=e.offset.clone().multiply(s);var r,a;return X(o.translation,n),r=o.rotation,a=(new v).setFromEuler(e.rotation),r.x=a.x,r.y=a.y,r.z=a.z,r.w=a.w,this.world.createCollider(o,t)}createShape(t,e){if(t instanceof D)return i.ColliderDesc.cuboid(t.dimensions.x*e.x/2,t.dimensions.y*e.y/2,t.dimensions.z*e.z/2);if(t instanceof z){return i.ColliderDesc.capsule(t.length/2*e.y,t.radius*Math.max(e.z,e.x))}if(t instanceof F){const s=null!=t.geometry.getIndex()?t.geometry:I.mergeVertices(t.geometry),o=new Float32Array(s.getAttribute("position").array);for(let t=0;t<o.length;t+=3)o[t]*=e.x,o[t+1]*=e.y,o[t+2]*=e.z;return i.ColliderDesc.trimesh(o,new Uint32Array(s.getIndex().array))}if(t instanceof T){let s;t.mesh instanceof d.Mesh?s=t.mesh.geometry:t.mesh instanceof d.BufferGeometry?s=t.mesh:console.log("Unknownd shape",{shapeInfo:t});const o=new Float32Array(s.getAttribute("position").array);if(t.mesh instanceof d.Mesh){const e=P(t.mesh);for(let t=0;t<o.length;t+=3)o[t]*=e.x,o[t+1]*=e.y,o[t+2]*=e.z}for(let t=0;t<o.length;t+=3)o[t]*=e.x,o[t+1]*=e.y,o[t+2]*=e.z;const n=o.length>1e3?function(t,e,i){const s=[],o={},n=i,r=i*i;for(let i=0;i<t.length;i+=3){const a=t[i],c=t[i+1],l=t[i+2],d=it(a,c,l,n);let h=!1;for(const[t,e,i]of ot){const s=it(a+t*n,c+e*n,l+i*n,n);if(o[s])for(let t=0;t<o[s].length;t+=3){if(st(a,c,l,o[s][t],o[s][t+1],o[s][t+2])<r){h=!0;break}}if(h)break}if(h||(s.push(a,c,l),o[d]||(o[d]=[]),o[d].push(a,c,l)),s.length>=3*e)break}return new Float32Array(s)}(o,1e3,.1):o;return i.ColliderDesc.convexHull(n)}return t instanceof W?i.ColliderDesc.ball(t.radius*Math.max(e.x,e.y,e.z)):t instanceof R?i.ColliderDesc.cylinder(t.height/2*e.y,t.radiusTop*Math.max(e.z,e.x)):t instanceof M?i.ColliderDesc.cone(t.height*e.y,t.radiusBottom/2*Math.max(e.z,e.x)):t instanceof E?i.ColliderDesc.cuboid(t.width/2*e.x,t.height/2*e.y,.01):(console.error("Unsupported shape",t),i.ColliderDesc.cuboid(1,1,1))}};L=t([A(),e("design:paramtypes",[V,k])],L);export{L as PhysicsSystem};const j=new B,O=new d.Quaternion;function N(t,e){const s=e.getWorldPosition(j),o=e.getWorldQuaternion(O);t.setTranslation(new i.Vector3(s.x,s.y,s.z),!1),t.setRotation(new i.Quaternion(o.x,o.y,o.z,o.w),!1)}const q=new B,G=new i.Vector3(0,0,0),Q=new i.Vector3(0,0,0),H=new B,U=new d.Vector2,K=async()=>{let t=await import("@dimforge/rapier3d-compat");return await t.init(),t};function X(t,e){t.x=e.x,t.y=e.y,t.z=e.z}function J(t,e){t.x=e.x,t.y=e.y,t.z=e.z}function Y(t,e){for(let i=0,s=t.numColliders();i<s;i++){e(t.collider(i))}}const Z=new B,$=new B,tt=t=>!t.isSensor(),et=new B;function it(t,e,i,s){return 73856093*Math.floor(t/s)^19349663*Math.floor(e/s)^83492791*Math.floor(i/s)}function st(t,e,i,s,o,n){const r=t-s,a=e-o,c=i-n;return r*r+a*a+c*c}const ot=[[0,0,0],[1,0,0],[0,1,0],[0,0,1],[1,1,0],[1,0,1],[0,1,1],[1,1,1]];
1
+ import{__decorate as e,__metadata as t}from"tslib";import*as i from"@dimforge/rapier3d-compat";import{QueryFilterFlags as s}from"@dimforge/rapier3d-compat";import{BehaviorSubject as o,distinctUntilChanged as n,filter as r,map as a,Subject as c,takeUntil as l}from"rxjs";import*as d from"three";import{ArrowHelper as h,BufferAttribute as u,BufferGeometry as y,Group as p,LineSegments as g,Matrix4 as m,MeshBasicMaterial as f,Object3D as w,Quaternion as v,Raycaster as x,Scene as b,Vector3 as B}from"three";import{Service as A}from"typedi";import{AssetMeshInstance as C}from"../../../scene/asset-resource-loader.js";import{BoxCollisionShape as D,CapsuleCollisionShape as z,ConeCollisionShape as T,ConvexPolyhedronCollisionShape as M,CylinderCollisionShape as R,PhysicalShapeMesh as S,PlaneCollisionShape as E,SphereCollisionShape as W,TrimeshCollisionShape as F}from"../../../index.js";import{LandscapeGroup as _}from"../../../scene/landscape/landscape.js";import{ViewController as V}from"../render.js";import{World as I}from"../world.js";import*as P from"three/examples/jsm/utils/BufferGeometryUtils.js";import{calculateEffectiveScale as L}from"../../../utils/three/traverse.js";export class RayTestResult{constructor(){this.hasHit=!1,this.hitPoint=new B,this.hitNormal=new B}}export var PhysicsBodyType;!function(e){e[e.dynamic=1]="dynamic",e[e.static=2]="static",e[e.kinematic=4]="kinematic",e[e.kinematicVelocityBased=8]="kinematicVelocityBased"}(PhysicsBodyType||(PhysicsBodyType={}));let j=class{set showDebug(e){this.shouldRenderDebug=e,this.debugMesh&&(this.debugMesh.visible=e)}get showDebug(){return this.shouldRenderDebug}constructor(e,t){this.viewController=e,this.gameWorld=t,this.staticMeshes=new Map,this.staticBodies=new Map,this.actorBodies=new Map,this.bodyActors=new Map,this.collisionEvents=new c,this.beforeStep=new c,this.afterStep=new c,this.shouldRenderDebug=!1,this._raycaster=new x,this._reusableResult=new RayTestResult,this._raytestDiff=new B,this._raytestDirection=new B,this.controlledActors=new Set,this.ready=this.setup()}createDebugMesh(){return new g(new y,new f({color:255}))}async start(){return await this.ready,this.handleTick(),this.ready}renderDebug(){null==this.debugMesh&&(this.debugMesh=this.createDebugMesh(),this.debugMesh.visible=this.shouldRenderDebug,this.debugMesh.raycast=function(){},this.gameWorld.scene.add(this.debugMesh));const e=this.world.debugRender();this.debugMesh.geometry.setAttribute("position",new u(e.vertices,3))}async setup(){if(null!=this.rapier)throw new Error("Rapier is already estup");this.rapier=await K(),this.eventQueue=new i.EventQueue(!0),this.setupWorld()}handleTick(){this.fixedupdateSub=this.viewController.onUpdate().subscribe((e=>{e=Math.min(.1,e),this.beforeStep.next(e),this.updatePhysics(e),this.afterStep.next(e),this.showDebug&&this.renderDebug(),this.world.bodies.forEach((e=>{if(e.isFixed())return;const t=this.staticMeshes.get(e)??this.bodyActors.get(e)?.object;var i,s;null!=t&&(t.parent instanceof b&&(J(t.position,e.translation()),(e.isDynamic()||e.isKinematic()&&!this.controlledActors.has(this.bodyActors.get(e)?.id))&&(i=t.quaternion,s=e.rotation(),i.x=s.x,i.y=s.y,i.z=s.z,i.w=s.w)))}))}))}_updateWorld(){this.world.timestep=0,this.world.step()}updatePhysics(e){this.world.timestep=e,this.world.step(this.eventQueue),this.eventQueue.drainCollisionEvents(((e,t,i)=>{this.collisionEvents.next({handle1:e,handle2:t,started:i}),this.collisionEvents.next({handle1:t,handle2:e,started:i})}))}rayTestFromCamera(e,t,i){this._raycaster.setFromCamera(U,this.viewController.getCamera());const s=this._raycaster.ray.origin,o=this._raycaster.ray.direction.multiplyScalar(e).add(s);return this.rayTest(s,o,t,i)}rayTest(e,t,s,o){null==s&&(s=this._reusableResult);const n=this._raytestDiff,r=this._raytestDirection;if(n.subVectors(t,e),r.copy(n).normalize(),0===r.length())return console.warn("Ray test called with to and from being equal"),s;const a=new i.Ray(e,r),c=n.length(),l=this.world.castRayAndGetNormal(a,c,!1,void 0,void 0,void 0,null!=o?.excludeActor?this.actorBodies.get(o.excludeActor.id):void 0,o?.excludeTriggers?e=>!e.isSensor():void 0);if(s.hasHit=null!=l,s.hasHit){const t=a.pointAt(l.timeOfImpact);s._internal=l,J(s.hitNormal,l.normal),J(s.hitPoint,t),s.distance=Z.subVectors(s.hitPoint,e).length();const i=this.world.bodies.getAll().find((e=>function(e,t){for(let i=0,s=e.numColliders();i<s;i++){const s=e.collider(i);if(t(s))return s}}(e,(e=>e===l.collider))));s.actor=null!=i?this.bodyActors.get(i):null}if(this.showDebug){const t=new h(r,e,c,o?.debugColor??255);this.gameWorld.scene.add(t),setTimeout((()=>this.gameWorld.scene.remove(t)),o?.debugLifetime??200)}return this._reusableResult}setGravity(e,t,i){this.world.gravity.x=e,this.world.gravity.y=t,this.world.gravity.z=i}getGravity(){return q.set(this.world.gravity.x,this.world.gravity.y,this.world.gravity.z)}addFromScene(e){this.addRecursively(e);for(const e of this.staticBodies.values())Y(e,(e=>e.setActiveEvents(i.ActiveEvents.COLLISION_EVENTS)))}addRecursively(e){if(this.removeSceneObject(e),!function(e){if(null!=e.userData?.src){const t=e.userData?.src;return"actor"===t.type}return!1}(e))if(e instanceof S&&null!=e.collisionShape){const t=this.createStaticBody(e,[e.collisionShape],e.physics);this.staticMeshes.set(t,e),this.staticBodies.set(e,t)}else if(e instanceof C&&!1!==e.userData?.src?.collisionDetection)if(e.children[0]&&e.children[0].instanceMatrix)this.createForInstancedMesh(e.children[0],e.collisionShapes);else{const t=this.createStaticBody(e,e.collisionShapes,e.physics);this.staticMeshes.set(t,e),this.staticBodies.set(e,t)}else e instanceof _?this.addLandscapeGroup(e):(e instanceof p||e instanceof b)&&e.children.forEach((e=>this.addRecursively(e)))}createForInstancedMesh(e,t){const i=new m;for(let s=0;s<e.count;s++){const o=new w;o.matrix.identity(),i.fromArray(e.instanceMatrix.array,16*s),o.applyMatrix4(i);this.createStaticBody(o,t)}}getCharacterController(e=.01){return this.world?.createCharacterController(e)}getActorComputedMovement(e,t,i){const o=this.actorBodies.get(e.id);this.controlledActors.add(e.id);const n=o.collider(0);t.computeColliderMovement(n,i,s.EXCLUDE_SENSORS,null,ee);const r=t.computedMovement();return J($,r),$}setNextKinematicTranslation(e,t){const i=this.actorBodies.get(e.id),s=i.translation();s.x+=t.x,s.y+=t.y,s.z+=t.z,i?.setNextKinematicTranslation(s)}setAngularVelocity(e,t){const i=this.actorBodies.get(e.id);G.x=t.x,G.y=t.y,G.z=t.z,i?.setAngvel(G,!0)}setLinearVelocity(e,t){const i=this.actorBodies.get(e.id);G.x=t.x,G.y=t.y,G.z=t.z,i?.setLinvel(G,!0)}getLinearVelocity(e,t=new B){const i=this.actorBodies.get(e.id).linvel();return t.x=i.x,t.y=i.y,t.z=i.z,t}getAngularVelocity(e,t=new B){const i=this.actorBodies.get(e.id).angvel();return t.x=i.x,t.y=i.y,t.z=i.z,t}setLinearDamping(e,t){const i=this.actorBodies.get(e.id);i?.setLinearDamping(t)}setAngularDamping(e,t){const i=this.actorBodies.get(e.id);i?.setAngularDamping(t)}createCharacterCollision(){return new i.CharacterCollision}addLandscapeGroup(e){const t=e.userData.src,s=t.landscape.heightMaps;for(const n of e.sections){this.staticBodies.has(n)&&this.world.removeRigidBody(this.staticBodies.get(n));const e=t.landscape.options.density+1,r=t.landscape.options.sectionSize,a=new Array(e);for(let t=0;t<e;t++)a[t]=new Array(e).fill(0);const c=s.find((e=>e.x===n.x&&e.y==n.y));if(null!=c)for(const t of c.points){if(null==a[t.i%e])continue;const i=e-1-Math.floor(t.i/e);i in a[t.i%e]?a[t.i%e][i]=t.y/r:console.warn("wrong index",{points:a,point:t,i:t.i%e,k:i,heightMap:c})}const l=t.landscape.options.density,d=a.flatMap((e=>e.reverse())),h=i.ColliderDesc.heightfield(l,l,new Float32Array(d),new i.Vector3(r,r,r));var o=n.getWorldPosition(new B);if(!1!==t.collisionDetection){const e=this.world.createRigidBody(i.RigidBodyDesc.fixed()),t=new i.Vector3(0,0,0);X(t,o),e.setTranslation(t,!1),this.world.createCollider(h,e),this.staticBodies.set(n,e)}}}addActor(e,t,s={}){if(0==t.length)return void console.error("No collision shapes were defined when adding actor to the physics system.");this.removeActor(e);const o=e.object;let n;switch(s.type??PhysicsBodyType.static){case PhysicsBodyType.dynamic:n=i.RigidBodyDesc.dynamic(),n.mass=s.mass??1;break;case PhysicsBodyType.kinematic:n=i.RigidBodyDesc.kinematicPositionBased();break;case PhysicsBodyType.kinematicVelocityBased:n=i.RigidBodyDesc.kinematicVelocityBased();break;default:n=s.isTrigger?i.RigidBodyDesc.kinematicVelocityBased():i.RigidBodyDesc.fixed()}const r=this.world.createRigidBody(n);r.enableCcd(1==s.continousCollisionDetection);for(const e of t)this.addShape(r,e,o);Y(r,(e=>{null!=s.isTrigger&&(e.setSensor(s.isTrigger),e.setActiveCollisionTypes(i.ActiveCollisionTypes.ALL),e.setActiveEvents(i.ActiveEvents.COLLISION_EVENTS)),null!=s.friction&&e.setFriction(s.friction),null!=s.density&&e.setDensity(s.density),null!=s.mass&&e.setMass(s.mass),null!=s.restitution&&e.setRestitution(s.restitution)})),N(r,o),!0===s.ignoreForNavMesh&&(r.userData={ignoreForNavMesh:!0}),this.actorBodies.set(e.id,r),this.bodyActors.set(r,e)}applyTorque(e,t){const i=this.actorBodies.get(e.id);G.x=t.x,G.y=t.y,G.z=t.z,i?.addTorque(G,!0)}applyTorqueImpulse(e,t){const i=this.actorBodies.get(e.id);G.x=t.x,G.y=t.y,G.z=t.z,i?.applyTorqueImpulse(G,!0)}resetForces(e){const t=this.actorBodies.get(e.id);t?.resetForces(!1)}resetTorques(e){const t=this.actorBodies.get(e.id);t?.resetTorques(!1)}applyForce(e,t){const i=this.actorBodies.get(e.id);G.x=t.x,G.y=t.y,G.z=t.z,i?.addForce(G,!0)}applyImpulse(e,t){const i=this.actorBodies.get(e.id);G.x=t.x,G.y=t.y,G.z=t.z,i?.applyImpulse(G,!0)}applyLocalForce(e,t,i){const s=this.actorBodies.get(e.id);X(G,t),null==i?s?.addForce(G,!0):(X(Q,i),s?.addForceAtPoint(G,Q,!0))}applyLocalImpulse(e,t,i){const s=this.actorBodies.get(e.id);X(G,t),null==i?s.applyImpulse(G,!0):(X(Q,i),s.applyImpulseAtPoint(G,Q,!0))}applyRadiusImpulse(e,t,s){this.world.bodies.forEach((o=>{if(o.collider(0)?.isSensor())return;if(o.bodyType()!==i.RigidBodyType.Dynamic)return;const n=H;J(n,o.translation());const r=n.clone().sub(e);if(r.length()>t)return;const a=r.clone().normalize().multiplyScalar(s);G.x=a.x,G.y=a.y,G.z=a.z,o.applyImpulse(G,!0)}))}removeActor(e){if(null==e)return;this.controlledActors.delete(e.id);const t=this.actorBodies.get(e.id);null!=t&&(this.bodyActors.delete(t),this.world.removeRigidBody(t)),this.actorBodies.delete(e.id)}removeSceneObject(e){let t=this.staticBodies.get(e);null!=t&&this.world.getRigidBody(t.handle)&&this.world.removeRigidBody(t),this.staticBodies.delete(e)}activateActorEvents(e){this.actorBodies.get(e.id)}_onCollisionWithActorEvent(e,t,i){return this.activateActorEvents(e),this.collisionEvents.pipe(l(e.disposed),r((({started:e})=>e===i)),a((({handle1:e,handle2:t,started:i})=>({a1:this.bodyActors.get(this.world.getCollider(e)?.parent()),a2:this.bodyActors.get(this.world.getCollider(t)?.parent()),started:i}))),r((({a1:i,a2:s})=>null!=i&&null!=s&&i.id===e.id&&t(i,s))),a((({a2:e})=>e)))}onBeginContact(e){return this.activateActorEvents(e),this.collisionEvents.pipe(l(e.disposed),r((e=>e.started)),r((({handle1:t})=>{const i=this.bodyActors.get(this.world.getCollider(t)?.parent());return null!=i&&i.id===e.id})),a((e=>e.handle2)))}onEndContact(e){return this.activateActorEvents(e),this.collisionEvents.pipe(l(e.disposed),r((e=>!e.started)),r((({handle1:t})=>{const i=this.bodyActors.get(this.world.getCollider(t)?.parent());return null!=i&&i.id===e.id})),a((e=>e.handle2)))}onHasContactChanged(e){const t=new Set,i=new o(!1);return this.onBeginContact(e).subscribe((e=>{t.add(e),i.next(t.size>0)})),this.onEndContact(e).subscribe((e=>{t.delete(e),i.next(t.size>0)})),i.pipe(n())}onBeginOverlapWithActorType(e,t){return this._onCollisionWithActorEvent(e,((e,i)=>i instanceof t),!0)}onEndOverlapWithActorType(e,t){return this._onCollisionWithActorEvent(e,((e,i)=>i instanceof t),!1)}onBeginOverlapWithActor(e,t){return this._onCollisionWithActorEvent(e,((e,i)=>t.id===i.id),!0)}onEndOverlapWithActor(e,t){return this._onCollisionWithActorEvent(e,((e,i)=>t.id===i.id),!1)}onCollisionWithActor(e,t){return this.onBeginOverlapWithActor(e,t)}onCollisionWithActorType(e,t){return this.onBeginOverlapWithActorType(e,t)}updateActorTransform(e){const t=this.actorBodies.get(e.id);null!=t?N(t,e.object):console.warn("Actor has not been added to physics world",e)}setupWorld(){const e=new i.World({x:0,y:-9.81,z:0});this.world=e}getActorContacts(e,t){const s=this.actorBodies.get(e.id);if(s&&s.numColliders()>0){const o=s.collider(0);let n=o.shape,r=o.translation(),a=o.rotation(),c=t,l=.3;const d=this.world.castShape(r,a,c,n,.1,l,!0,null,null,null,this.actorBodies.get(e.id),(e=>e.shape.type!=i.ShapeType.HeightField));if(null!=d){const e=new B,t=new B,i=new B;return J(e,d.witness2),J(t,d.witness1),J(i,d.normal1),i.negate(),[{ri:e,rj:t,ni:i}]}return[]}return console.warn("Actor is not added to the physics system"),[]}stop(){this.world?.bodies.forEach((e=>this.world.removeRigidBody(e))),this.world?.free(),this.fixedupdateSub?.unsubscribe()}createStaticBody(e,t,s){const o=s?.type===PhysicsBodyType.dynamic?i.RigidBodyDesc.dynamic():i.RigidBodyDesc.fixed(),n=this.world.createRigidBody(o);for(const i of t){if(null==i){console.warn("Collision shape is missing for object",e);continue}const o=this.addShape(n,i,e);null!=s?.friction&&o.setFriction(s.friction),null!=s?.density&&o.setDensity(s.density),null!=s?.mass&&o.setMass(s.mass/t.length),null!=s?.restitution&&o.setRestitution(s.restitution)}return N(n,e),n}addShape(e,t,i){const s=i.getWorldScale(te),o=this.createShape(t,s);o.friction=.1;const n=t.offset.clone().multiply(s);var r,a;return X(o.translation,n),r=o.rotation,a=(new v).setFromEuler(t.rotation),r.x=a.x,r.y=a.y,r.z=a.z,r.w=a.w,this.world.createCollider(o,e)}createShape(e,t){if(e instanceof D)return i.ColliderDesc.cuboid(e.dimensions.x*t.x/2,e.dimensions.y*t.y/2,e.dimensions.z*t.z/2);if(e instanceof z){return i.ColliderDesc.capsule(e.length/2*t.y,e.radius*Math.max(t.z,t.x))}if(e instanceof F){const s=null!=e.geometry.getIndex()?e.geometry:P.mergeVertices(e.geometry),o=new Float32Array(s.getAttribute("position").array);for(let e=0;e<o.length;e+=3)o[e]*=t.x,o[e+1]*=t.y,o[e+2]*=t.z;return i.ColliderDesc.trimesh(o,new Uint32Array(s.getIndex().array))}if(e instanceof M){let s;e.mesh instanceof d.Mesh?s=e.mesh.geometry:e.mesh instanceof d.BufferGeometry?s=e.mesh:console.log("Unknownd shape",{shapeInfo:e});const o=new Float32Array(s.getAttribute("position").array);if(e.mesh instanceof d.Mesh){const t=L(e.mesh);for(let e=0;e<o.length;e+=3)o[e]*=t.x,o[e+1]*=t.y,o[e+2]*=t.z}for(let e=0;e<o.length;e+=3)o[e]*=t.x,o[e+1]*=t.y,o[e+2]*=t.z;const n=o;return i.ColliderDesc.convexHull(n)}return e instanceof W?i.ColliderDesc.ball(e.radius*Math.max(t.x,t.y,t.z)):e instanceof R?i.ColliderDesc.cylinder(e.height/2*t.y,e.radiusTop*Math.max(t.z,t.x)):e instanceof T?i.ColliderDesc.cone(e.height*t.y,e.radiusBottom/2*Math.max(t.z,t.x)):e instanceof E?i.ColliderDesc.cuboid(e.width/2*t.x,e.height/2*t.y,.01):(console.error("Unsupported shape",e),i.ColliderDesc.cuboid(1,1,1))}};j=e([A(),t("design:paramtypes",[V,I])],j);export{j as PhysicsSystem};const k=new B,O=new d.Quaternion;function N(e,t){const s=t.getWorldPosition(k),o=t.getWorldQuaternion(O);e.setTranslation(new i.Vector3(s.x,s.y,s.z),!1),e.setRotation(new i.Quaternion(o.x,o.y,o.z,o.w),!1)}const q=new B,G=new i.Vector3(0,0,0),Q=new i.Vector3(0,0,0),H=new B,U=new d.Vector2,K=async()=>{let e=await import("@dimforge/rapier3d-compat");return await e.init(),e};function X(e,t){e.x=t.x,e.y=t.y,e.z=t.z}function J(e,t){e.x=t.x,e.y=t.y,e.z=t.z}function Y(e,t){for(let i=0,s=e.numColliders();i<s;i++){t(e.collider(i))}}const Z=new B,$=new B,ee=e=>!e.isSensor(),te=new B;
2
2
  /*
3
3
  * Copyright (©) 2023. All rights reserved.
4
4
  * See the LICENSE.md file for details.
package/dist/rendering.js CHANGED
@@ -1,4 +1,4 @@
1
- var e;import{__decorate as t,__metadata as i}from"tslib";import*as s from"three";import{Color as r,Material as n,Matrix4 as a,Mesh as o,PerspectiveCamera as h,ShaderChunk as l,ShaderMaterial as d}from"three";import{EffectComposer as c,FXAAShader as m,GammaCorrectionShader as u,RenderPass as p,ShaderPass as g,UnrealBloomPass as f,VRButton as v}from"three-stdlib";import{CSMShader as w,CSMUtil as x}from"./csm.js";import b from"three/examples/jsm/libs/stats.module.js";import{Reflector as C}from"three-stdlib";import{CSM as S}from"three/examples/jsm/csm/CSM.js";import{Service as y}from"typedi";import{depthUniformName as P,farUniformName as R,nearUniformName as M,resolutionUniformName as T,supportsDepthTextureExtension as W}from"./shader-nodes/depth.js";import{elapsedTimeUniformName as E}from"./shader-nodes/time.js";import{DepthPass as L}from"./utils/three/depth-pass.js";import{GPUStatsPanel as H}from"./utils/three/gpu-stats-panel.js";import{OutlinePass as B}from"./utils/three/outline-pass.js";import{findFirstVisibleObject as D}from"./utils/three/traverse.js";(new s.Layers).set(9);const F=new s.MeshBasicMaterial({color:"black"}),j=new s.MeshDepthMaterial;j.depthPacking=s.RGBADepthPacking,j.blending=s.NoBlending;const V=/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);let k=e=class{setPaused(e){this.paused=e}resizeRender(){this.previousClientWith===this.container.clientWidth&&this.previousClientHeight===this.container.clientHeight||(this.previousClientWith=this.container.clientWidth,this.previousClientHeight=this.container.clientHeight,this.camera instanceof h&&(this.camera.aspect=this.container.clientWidth/this.container.clientHeight,this.camera.updateProjectionMatrix()),this.renderer.setPixelRatio(Math.min(this.maxPixelRatio,window.devicePixelRatio)*this.resolutionScale),this.renderer.setSize(this.container.clientWidth,this.container.clientHeight),this.composer.setSize(this.container.clientWidth*this.resolutionScale,this.container.clientHeight*this.resolutionScale))}constructor(t,i={}){this.container=t,this.options=i,this.windowVisible=!0,this.running=!0,this.paused=!1,this.fpsCap=null,this.resolutionScale=V?.5:1,this.maxPixelRatio=V?1:window.devicePixelRatio,this.onResize=()=>{this.resizeRender(),this.paused||this.render()},this.onVisiblityChane=()=>{this.windowVisible=!document.hidden},this.isDepthTextureExtensionSupported=!1,this.onLoopCallbacks=[],this.stats=new b,this._showStats=!0,this.insetHeight=200,this.insetWidth=this.insetHeight*(16/9),this.insetOffsetY=250,this.insetMargin=10,this.maxInsetCameras=4,this.overlayCameras=new Set,this.prevClearColor=new r,this.hadBloom=!1,this.bloomStoredMaterials={},null!=i.maxPixelRatio&&(this.maxPixelRatio=i.maxPixelRatio),window.renderer=this.renderer=new s.WebGLRenderer({antialias:!0,powerPreference:"high-performance"}),this.scene=new s.Scene,this.renderer.setPixelRatio(Math.min(this.maxPixelRatio,window.devicePixelRatio)*this.resolutionScale),this.renderer.setSize(t.clientWidth,t.clientHeight),this.renderer.xr.enabled=this.options.enableXR??!1,!0===this.options.enableXR&&document.body.appendChild(v.createButton(this.renderer)),this.composer=new c(this.renderer);var n=t.clientWidth/t.clientHeight;const a=new s.PerspectiveCamera(45,n,.5,800);a.layers.enable(19),this.setCamera(a),this.renderer.shadowMap.enabled=!0,this.renderer.shadowMap.type=s.PCFSoftShadowMap,this.renderer.shadowMap.autoUpdate=i.shadows?.autoUpdate??!1,this.renderer.outputColorSpace=s.SRGBColorSpace,this.renderer.toneMapping=s.NoToneMapping,this.renderer.toneMappingExposure=1,this.renderer.gammaFactor=1.4,x.renderingView=this,x.patchThreeAdd(),this.isDepthTextureExtensionSupported=W(this.renderer),t.replaceChildren(this.renderer.domElement),this.setupEventListeners(),this.depthRenderTarget=e.createDepthRenderTarget(this.renderer,this.container);const o=new p(this.scene,this.camera);this.composer.addPass(o);const h=new f(new s.Vector2(t.clientWidth,t.clientHeight),1.5,.4,.85);h.threshold=0,h.strength=.9,h.radius=1,this.renderer.info.autoReset=!1;const l=new c(this.renderer);l.renderToScreen=!1,l.addPass(o),l.addPass(h),this.bloomComposer=l;const d=new g(new s.ShaderMaterial({uniforms:{baseTexture:{value:null},bloomTexture:{value:l.renderTarget2.texture}},vertexShader:"\n varying vec2 vUv;\n void main() {\n vUv = uv;\n gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n }",fragmentShader:"\n uniform sampler2D baseTexture;\n uniform sampler2D bloomTexture;\n varying vec2 vUv;\n void main() {\n gl_FragColor = ( texture2D( baseTexture, vUv ) + vec4( 1.0 ) * texture2D( bloomTexture, vUv ) );\n }",defines:{}}),"baseTexture");if(d.needsSwap=!0,this.composer.addPass(d),this.outlinePass=new B(new s.Vector2(t.clientWidth,t.clientHeight),this.scene,this.camera),!0===i.enableOutlines){this.outlinePass.edgeThickness=0,this.outlinePass.edgeGlow=0,this.outlinePass.edgeThickness=1.5,this.outlinePass.edgeStrength=5,this.outlinePass.clear=!1,this.composer.addPass(this.outlinePass);const e=new g(m);e.uniforms.resolution.value.set(1/t.clientWidth,1/t.clientHeight),this.composer.addPass(e)}var w=new g(u);w.clear=!1,this.composer.addPass(w),this.fixStatsStyle()}fixStatsStyle(){const e=this.stats.dom;e.style.position="absolute";const t=e.getElementsByTagName("canvas");for(let e=0;e<t.length;e++)t.item(e).style.display="inline-block"}setCamera(e){this.camera=e,this.composer.passes.forEach((t=>{t instanceof p?t.camera=e:t instanceof B?t.renderCamera=e:t instanceof L&&(t.camera=e)})),null==this.csm?(this.csm=new S({maxFar:250,lightFar:250,lightMargin:20,cascades:4,shadowMapSize:2048,lightDirection:new s.Vector3(.5,-1,-.6).normalize(),lightIntensity:.5*Math.PI,camera:this.camera,parent:this.scene,mode:"logarithmic"}),l.lights_fragment_begin=w.lights_fragment_begin):(this.csm.camera=this.camera,this.camera instanceof h&&(this.csm.maxFar=this.camera.far)),this.csm.updateFrustums()}setSelectedObjects(e){const t=new Map;for(const i of e)t.set(i.uuid,i);for(const i of e)i.traverse((e=>{e.uuid!==i.uuid&&t.has(e.uuid)&&t.delete(e.uuid)}));this.outlinePass.selectedObjects=Array.from(t.values())}static createDepthRenderTarget(e,t){var i=!!e.extensions.get("WEBGL_depth_texture");const r=new s.WebGLRenderTarget(t.clientWidth*e.getPixelRatio(),t.clientHeight*e.getPixelRatio());return r.texture.minFilter=s.NearestFilter,r.texture.magFilter=s.NearestFilter,r.texture.generateMipmaps=!1,r.stencilBuffer=!1,!0===i&&(r.depthTexture=new s.DepthTexture(128,128),r.depthTexture.type=s.UnsignedShortType,r.depthTexture.minFilter=s.NearestFilter,r.depthTexture.magFilter=s.NearestFilter),r}setupEventListeners(){window.addEventListener("resize",this.onResize),window.addEventListener("orientationchange",this.onResize),document.addEventListener("visibilitychange",this.onVisiblityChane)}stop(){this.running=!1,window.removeEventListener("resize",this.onResize),window.removeEventListener("orientationchange",this.onResize),document.removeEventListener("visibilitychange",this.onVisiblityChane),this.onLoopCallbacks=[],this.renderer.dispose(),this.depthRenderTarget.dispose(),this.csm.dispose(),this.container.replaceChildren()}onLoop(e){this.onLoopCallbacks.push(e)}removeOnLoop(e){const t=this.onLoopCallbacks.find(e);t>=0&&this.onLoopCallbacks.splice(t,1)}set showStats(e){this._showStats=e,this._showStats&&!this.container.contains(this.stats.dom)?this.container.appendChild(this.stats.dom):!this._showStats&&this.container.contains(this.stats.dom)&&this.container.removeChild(this.stats.dom)}get showStats(){return this._showStats}loop(e,t=!1){const i=this.stats,r=i.addPanel(new b.Panel("Calls","#83f","#002"));let h;navigator.userAgent.includes("Chrome")&&navigator.userAgent.includes("HologyEngine")&&(h=new H(this.renderer.getContext()),i.addPanel(h)),this.showStats=t;let l=10;const d=()=>{const e=this.renderer.info.render.calls;e>l&&(l=e,setTimeout((()=>l=10),5e3)),r.update(e,l)};performance.now();s.Ray.prototype.intersectTriangle;const c=[],m=[];let u=0;const p=new a,g=new a,f=t=>{const r=this.renderer.getContext();if(this.paused&&this.running&&r.drawingBufferHeight>1)return void setTimeout((()=>f(t)),500);this.renderer.autoClear=!1,this.renderer.clear(),this.renderer.setViewport(0,0,this.container.clientWidth,this.container.clientHeight),this.camera,i.begin();let a=(t*=.001)-u;if(u=t,p.copy(this.camera.matrixWorld),a>1){let t=a;for(;t>.05;)e(z),t-=z;e(t)}else e(a);if(this.onLoopCallbacks.forEach((e=>e(a))),this.camera?.updateMatrixWorld(),g.copy(this.camera.matrixWorld),g.equals(p)||(this.renderer.shadowMap.needsUpdate=!0),this.resizeRender(),c.length=0,m.length=0,this.scene.traverseVisible((e=>{(e instanceof o||e instanceof s.Sprite)&&e.visible&&(e.material?.userData?.water||e.material?.uniforms&&null!=e.material?.uniforms[P])?(e.visible=!1,c.push(e),this.initDepthUniform(e.material),e.renderOrder=100):(e instanceof C||(e instanceof o||e instanceof s.Sprite)&&function(e){if(e.material instanceof n)return e.material.transparent||e.material.alphaTest>0;if(Array.isArray(e.material))for(const t of e.material)if(t.transparent||t.alphaTest>0)return!0}(e))&&(e.visible=!1,m.push(e)),e instanceof o&&e.material?.uniforms&&null!=e.material?.uniforms[E]&&(e.material.uniforms[E].value=t)})),c.length>0){if(this.scene.overrideMaterial=j,this.renderer.setRenderTarget(this.depthRenderTarget),!this.paused&&null!=this.camera)try{this.renderer.clear(),this.renderer.render(this.scene,this.camera)}catch(e){console.warn(e)}this.renderer.setRenderTarget(null),this.scene.overrideMaterial=null}c.forEach((e=>e.visible=!0)),m.forEach((e=>e.visible=!0));try{!this.paused&&this.running&&(h?.startQuery(),this.render(a),h?.endQuery(),this.showStats&&d(),this.renderer.info.reset(),this.renderOverlay())}catch(e){console.warn(e)}i.end(),this.csm?.update(),this.running&&!0!==this.options.enableXR&&(this.fpsCap?setTimeout((()=>{requestAnimationFrame(f)}),1e3/this.fpsCap):requestAnimationFrame(f))};!0===this.options.enableXR?this.renderer.setAnimationLoop(f):requestAnimationFrame(f)}renderOverlay(){const e=Array.from(this.overlayCameras.values()).slice(0,this.maxInsetCameras),t=this.container.clientWidth/2,i=e.length*this.insetWidth+(e.length-1)*this.insetMargin;for(let s=0;s<e.length;s++)this.renderer.clearDepth(),this.renderer.setViewport(t-i/2+this.insetWidth*s+this.insetMargin*s,this.insetOffsetY,this.insetWidth,this.insetHeight),this.renderer.render(this.scene,e[s])}addOverlayCamera(e){this.overlayCameras.add(e)}clearOverlayCameras(){this.overlayCameras.clear()}removeOverlayCamera(e){this.overlayCameras.delete(e)}render(e){const t=this.hasBloom();if(t||this.hadBloom){const e=this.scene.fog;this.scene.fog=null;const t=this.renderer.getClearColor(this.prevClearColor);this.renderer.setClearColor(0),this.scene.traverseVisible((e=>this.darkenNonBloomed(e))),this.bloomComposer.render(),this.scene.traverse((e=>this.restoreMaterial(e))),this.renderer.setClearColor(t),this.scene.fog=e}this.hadBloom=t,this.composer.render(e)}hasBloom(){return null!=D(this.scene,(e=>e instanceof o&&!0===e.material?.userData?.hasBloom))}darkenNonBloomed(e){e.isMesh&&e.visible&&(null==e.material.userData||!0!==e.material.userData.hasBloom)&&(this.bloomStoredMaterials[e.uuid]=e.material,!0!==e.material.transparent?e.material=F:e.visible=!1)}restoreMaterial(e){this.bloomStoredMaterials[e.uuid]&&(e.material=this.bloomStoredMaterials[e.uuid],delete this.bloomStoredMaterials[e.uuid],e.visible=!0)}initDepthUniform(e){e instanceof d&&(e.uniforms[P].value=this.isDepthTextureExtensionSupported?this.depthRenderTarget.depthTexture:this.depthRenderTarget.texture,null!=e.uniforms[T]&&e.uniforms[T].value.set(this.container.clientWidth*this.renderer.getPixelRatio(),this.container.clientHeight*this.renderer.getPixelRatio()),this.camera instanceof h&&(e.uniforms[M].value=this.camera.near,e.uniforms[R].value=this.camera.far))}};k=e=t([y(),i("design:paramtypes",[HTMLElement,Object])],k);export{k as RenderingView};export function setRenderingPaused(e){null!=window.editor?.viewer?.renderingView&&(window.editor.viewer.renderingView.paused=e)}const z=.05;
1
+ var e;import{__decorate as t,__metadata as i}from"tslib";import*as s from"three";import{Color as r,Material as n,Matrix4 as a,Mesh as o,PerspectiveCamera as h,ShaderChunk as l,ShaderMaterial as d}from"three";import{EffectComposer as c,FXAAShader as m,GammaCorrectionShader as u,RenderPass as p,ShaderPass as g,UnrealBloomPass as f,VRButton as v}from"three-stdlib";import{CSMShader as w,CSMUtil as x}from"./csm.js";import b from"three/examples/jsm/libs/stats.module.js";import{Reflector as C}from"three-stdlib";import{CSM as S}from"three/examples/jsm/csm/CSM.js";import{Service as y}from"typedi";import{depthUniformName as P,farUniformName as R,nearUniformName as M,resolutionUniformName as T,supportsDepthTextureExtension as W}from"./shader-nodes/depth.js";import{elapsedTimeUniformName as E}from"./shader-nodes/time.js";import{DepthPass as L}from"./utils/three/depth-pass.js";import{GPUStatsPanel as H}from"./utils/three/gpu-stats-panel.js";import{OutlinePass as B}from"./utils/three/outline-pass.js";import{findFirstVisibleObject as D}from"./utils/three/traverse.js";(new s.Layers).set(9);const F=new s.MeshBasicMaterial({color:"black"}),j=new s.MeshDepthMaterial;j.depthPacking=s.RGBADepthPacking,j.blending=s.NoBlending;const V=/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);let k=e=class{setPaused(e){this.paused=e}resizeRender(){this.previousClientWith===this.container.clientWidth&&this.previousClientHeight===this.container.clientHeight||(this.previousClientWith=this.container.clientWidth,this.previousClientHeight=this.container.clientHeight,this.camera instanceof h&&(this.camera.aspect=this.container.clientWidth/this.container.clientHeight,this.camera.updateProjectionMatrix()),this.renderer.setPixelRatio(Math.min(this.maxPixelRatio,window.devicePixelRatio)*this.resolutionScale),this.renderer.setSize(this.container.clientWidth,this.container.clientHeight),this.composer.setSize(this.container.clientWidth*this.resolutionScale,this.container.clientHeight*this.resolutionScale))}constructor(t,i={}){this.container=t,this.options=i,this.windowVisible=!0,this.running=!0,this.paused=!1,this.fpsCap=null,this.resolutionScale=V?.5:1,this.maxPixelRatio=V?1:window.devicePixelRatio,this.onResize=()=>{this.resizeRender(),this.paused||this.render()},this.onVisiblityChane=()=>{this.windowVisible=!document.hidden},this.isDepthTextureExtensionSupported=!1,this.onLoopCallbacks=[],this.stats=new b,this._showStats=!0,this.insetHeight=200,this.insetWidth=this.insetHeight*(16/9),this.insetOffsetY=250,this.insetMargin=10,this.maxInsetCameras=4,this.overlayCameras=new Set,this.prevClearColor=new r,this.hadBloom=!1,this.bloomStoredMaterials={},null!=i.maxPixelRatio&&(this.maxPixelRatio=i.maxPixelRatio),window.renderer=this.renderer=new s.WebGLRenderer({antialias:!0,powerPreference:"high-performance"}),this.scene=new s.Scene,this.renderer.setPixelRatio(Math.min(this.maxPixelRatio,window.devicePixelRatio)*this.resolutionScale),this.renderer.setSize(t.clientWidth,t.clientHeight),this.renderer.xr.enabled=this.options.enableXR??!1,!0===this.options.enableXR&&document.body.appendChild(v.createButton(this.renderer)),this.composer=new c(this.renderer);var n=t.clientWidth/t.clientHeight;const a=new s.PerspectiveCamera(45,n,.5,800);a.layers.enable(19),this.setCamera(a),this.renderer.shadowMap.enabled=!0,this.renderer.shadowMap.type=s.PCFSoftShadowMap,this.renderer.shadowMap.autoUpdate=i.shadows?.autoUpdate??!1,this.renderer.outputColorSpace=s.SRGBColorSpace,this.renderer.toneMapping=s.NoToneMapping,this.renderer.toneMappingExposure=1,this.renderer.gammaFactor=1.4,x.renderingView=this,x.patchThreeAdd(),this.isDepthTextureExtensionSupported=W(this.renderer),t.replaceChildren(this.renderer.domElement),this.setupEventListeners(),this.depthRenderTarget=e.createDepthRenderTarget(this.renderer,this.container);const o=new p(this.scene,this.camera);this.composer.addPass(o);const h=new f(new s.Vector2(t.clientWidth,t.clientHeight),1.5,.4,.85);h.threshold=0,h.strength=.9,h.radius=1,this.renderer.info.autoReset=!1;const l=new c(this.renderer);l.renderToScreen=!1,l.addPass(o),l.addPass(h),this.bloomComposer=l;const d=new g(new s.ShaderMaterial({uniforms:{baseTexture:{value:null},bloomTexture:{value:l.renderTarget2.texture}},vertexShader:"\n varying vec2 vUv;\n void main() {\n vUv = uv;\n gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n }",fragmentShader:"\n uniform sampler2D baseTexture;\n uniform sampler2D bloomTexture;\n varying vec2 vUv;\n void main() {\n gl_FragColor = ( texture2D( baseTexture, vUv ) + vec4( 1.0 ) * texture2D( bloomTexture, vUv ) );\n }",defines:{}}),"baseTexture");if(d.needsSwap=!0,this.composer.addPass(d),this.outlinePass=new B(new s.Vector2(t.clientWidth,t.clientHeight),this.scene,this.camera),!0===i.enableOutlines){this.outlinePass.edgeThickness=0,this.outlinePass.edgeGlow=0,this.outlinePass.edgeThickness=1.5,this.outlinePass.edgeStrength=5,this.outlinePass.clear=!1,this.composer.addPass(this.outlinePass);const e=new g(m);e.uniforms.resolution.value.set(1/t.clientWidth,1/t.clientHeight),this.composer.addPass(e)}var w=new g(u);w.clear=!1,this.composer.addPass(w),this.fixStatsStyle()}fixStatsStyle(){const e=this.stats.dom;e.style.position="absolute";const t=e.getElementsByTagName("canvas");for(let e=0;e<t.length;e++)t.item(e).style.display="inline-block"}setCamera(e){this.camera=e,this.composer.passes.forEach((t=>{t instanceof p?t.camera=e:t instanceof B?t.renderCamera=e:t instanceof L&&(t.camera=e)})),null==this.csm?(this.csm=new S({maxFar:250,lightFar:250,lightMargin:20,cascades:4,shadowMapSize:2048,lightDirection:new s.Vector3(.5,-1,-.6).normalize(),lightIntensity:.5*Math.PI,camera:this.camera,parent:this.scene,mode:"logarithmic"}),l.lights_fragment_begin=w.lights_fragment_begin):(this.csm.camera=this.camera,this.camera instanceof h&&(this.csm.maxFar=this.camera.far)),this.csm.updateFrustums()}setSelectedObjects(e){const t=new Map;for(const i of e)t.set(i.uuid,i);for(const i of e)i.traverse((e=>{e.uuid!==i.uuid&&t.has(e.uuid)&&t.delete(e.uuid)}));this.outlinePass.selectedObjects=Array.from(t.values())}static createDepthRenderTarget(e,t){var i=!!e.extensions.get("WEBGL_depth_texture");const r=new s.WebGLRenderTarget(t.clientWidth*e.getPixelRatio(),t.clientHeight*e.getPixelRatio());return r.texture.minFilter=s.NearestFilter,r.texture.magFilter=s.NearestFilter,r.texture.generateMipmaps=!1,r.stencilBuffer=!1,!0===i&&(r.depthTexture=new s.DepthTexture(128,128),r.depthTexture.type=s.UnsignedShortType,r.depthTexture.minFilter=s.NearestFilter,r.depthTexture.magFilter=s.NearestFilter),r}setupEventListeners(){window.addEventListener("resize",this.onResize),window.addEventListener("orientationchange",this.onResize),document.addEventListener("visibilitychange",this.onVisiblityChane)}stop(){this.running=!1,window.removeEventListener("resize",this.onResize),window.removeEventListener("orientationchange",this.onResize),document.removeEventListener("visibilitychange",this.onVisiblityChane),this.onLoopCallbacks=[],this.renderer.dispose(),this.depthRenderTarget.dispose(),this.csm.dispose(),this.container.replaceChildren()}onLoop(e){this.onLoopCallbacks.push(e)}removeOnLoop(e){const t=this.onLoopCallbacks.find(e);t>=0&&this.onLoopCallbacks.splice(t,1)}set showStats(e){this._showStats=e,this._showStats&&!this.container.contains(this.stats.dom)?this.container.appendChild(this.stats.dom):!this._showStats&&this.container.contains(this.stats.dom)&&this.container.removeChild(this.stats.dom)}get showStats(){return this._showStats}loop(e,t=!1){const i=this.stats,r=i.addPanel(new b.Panel("Calls","#83f","#002"));let h;navigator.userAgent.includes("Chrome")&&navigator.userAgent.includes("HologyEngine")&&(h=new H(this.renderer.getContext()),i.addPanel(h)),this.showStats=t;let l=10;const d=()=>{const e=this.renderer.info.render.calls;e>l&&(l=e,setTimeout((()=>l=10),5e3)),r.update(e,l)};performance.now();s.Ray.prototype.intersectTriangle;const c=[],m=[];let u=0;const p=new a,g=new a,f=t=>{const r=this.renderer.getContext();if(this.paused&&this.running&&r.drawingBufferHeight>1)return void setTimeout((()=>f(t)),500);this.renderer.autoClear=!1,this.renderer.clear(),this.renderer.setViewport(0,0,this.container.clientWidth,this.container.clientHeight),this.camera,i.begin();let a=(t*=.001)-u;if(u=t,p.copy(this.camera.matrixWorld),a>1){let t=a;for(;t>.05;)e(z),t-=z;e(t)}else e(a);if(this.onLoopCallbacks.forEach((e=>e(a))),this.camera?.updateMatrixWorld(),g.copy(this.camera.matrixWorld),g.equals(p)||(this.renderer.shadowMap.needsUpdate=!0),this.resizeRender(),c.length=0,m.length=0,this.scene.traverseVisible((e=>{(e instanceof o||e instanceof s.Sprite)&&e.visible&&(e.material?.userData?.water||e.material?.uniforms&&null!=e.material?.uniforms[P])?(e.visible=!1,c.push(e),this.initDepthUniform(e.material),e.renderOrder=100):(e instanceof C||(e instanceof o||e instanceof s.Sprite)&&function(e){if(e.material instanceof n)return e.material.transparent||e.material.alphaTest>0;if(Array.isArray(e.material))for(const t of e.material)if(t.transparent||t.alphaTest>0)return!0}(e))&&(e.visible=!1,m.push(e)),e instanceof o&&e.material?.uniforms&&null!=e.material?.uniforms[E]&&(e.material.uniforms[E].value=t)})),c.length>0){if(this.scene.overrideMaterial=j,this.renderer.setRenderTarget(this.depthRenderTarget),!this.paused&&null!=this.camera)try{this.renderer.clear(),this.renderer.render(this.scene,this.camera)}catch(e){console.warn(e)}this.renderer.setRenderTarget(null),this.scene.overrideMaterial=null}c.forEach((e=>e.visible=!0)),m.forEach((e=>e.visible=!0));try{!this.paused&&this.running&&(this.showStats&&h?.startQuery(),this.render(a),this.showStats&&h?.endQuery(),this.showStats&&d(),this.renderer.info.reset(),this.renderOverlay())}catch(e){console.warn(e)}i.end(),this.csm?.update(),this.running&&!0!==this.options.enableXR&&(this.fpsCap?setTimeout((()=>{requestAnimationFrame(f)}),1e3/this.fpsCap):requestAnimationFrame(f))};!0===this.options.enableXR?this.renderer.setAnimationLoop(f):requestAnimationFrame(f)}renderOverlay(){const e=Array.from(this.overlayCameras.values()).slice(0,this.maxInsetCameras),t=this.container.clientWidth/2,i=e.length*this.insetWidth+(e.length-1)*this.insetMargin;for(let s=0;s<e.length;s++)this.renderer.clearDepth(),this.renderer.setViewport(t-i/2+this.insetWidth*s+this.insetMargin*s,this.insetOffsetY,this.insetWidth,this.insetHeight),this.renderer.render(this.scene,e[s])}addOverlayCamera(e){this.overlayCameras.add(e)}clearOverlayCameras(){this.overlayCameras.clear()}removeOverlayCamera(e){this.overlayCameras.delete(e)}render(e){const t=this.hasBloom();if(t||this.hadBloom){const e=this.scene.fog;this.scene.fog=null;const t=this.renderer.getClearColor(this.prevClearColor);this.renderer.setClearColor(0),this.scene.traverseVisible((e=>this.darkenNonBloomed(e))),this.bloomComposer.render(),this.scene.traverse((e=>this.restoreMaterial(e))),this.renderer.setClearColor(t),this.scene.fog=e}this.hadBloom=t,this.composer.render(e)}hasBloom(){return null!=D(this.scene,(e=>e instanceof o&&!0===e.material?.userData?.hasBloom))}darkenNonBloomed(e){e.isMesh&&e.visible&&(null==e.material.userData||!0!==e.material.userData.hasBloom)&&(this.bloomStoredMaterials[e.uuid]=e.material,!0!==e.material.transparent?e.material=F:e.visible=!1)}restoreMaterial(e){this.bloomStoredMaterials[e.uuid]&&(e.material=this.bloomStoredMaterials[e.uuid],delete this.bloomStoredMaterials[e.uuid],e.visible=!0)}initDepthUniform(e){e instanceof d&&(e.uniforms[P].value=this.isDepthTextureExtensionSupported?this.depthRenderTarget.depthTexture:this.depthRenderTarget.texture,null!=e.uniforms[T]&&e.uniforms[T].value.set(this.container.clientWidth*this.renderer.getPixelRatio(),this.container.clientHeight*this.renderer.getPixelRatio()),this.camera instanceof h&&(e.uniforms[M].value=this.camera.near,e.uniforms[R].value=this.camera.far))}};k=e=t([y(),i("design:paramtypes",[HTMLElement,Object])],k);export{k as RenderingView};export function setRenderingPaused(e){null!=window.editor?.viewer?.renderingView&&(window.editor.viewer.renderingView.paused=e)}const z=.05;
2
2
  /*
3
3
  * Copyright (©) 2023. All rights reserved.
4
4
  * See the LICENSE.md file for details.
@@ -29,6 +29,8 @@ export declare class AssetResourceLoader {
29
29
  private collisionShapeCache;
30
30
  private computeCollisionShapes;
31
31
  private loadMesh;
32
+ private optimizedMeshes;
33
+ private optimizeDrawGroups;
32
34
  private loadByAsset;
33
35
  }
34
36
  export interface LoadedMesh {
@@ -1,4 +1,4 @@
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 l}from"three-stdlib";import{cloneMesh as c}from"../utils/mesh.js";import{pathJoin as d}from"../utils/files.js";import{Subject as m,firstValueFrom as p}from"rxjs";import{importCollisionShapes as g}from"./collision/collision-shape-import.js";import*as u from"three";import{iterateMaterials as f}from"../utils/materials.js";import{KTX2Loader as w,TGALoader as y}from"three/examples/jsm/Addons.js";const L=["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 l(this.loadingManager),this.objLoader=new h(this.loadingManager),this.textureLoader=new r(this.loadingManager),this.tgaLoader=new y(this.loadingManager),this.ktx2Loader=new w(this.loadingManager),this._textureLoader=new u.ImageBitmapLoader(this.loadingManager),this.audioLoader=new e(this.loadingManager),this.makeReady=new m,this.ready=p(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.textureCache.has(e.id)||await this._getTextureLoader(e.fileKey).loadAsync(this.getUri(e.fileKey)).then((t=>(t.wrapS=b(e.texture?.wrapS),t.wrapT=b(e.texture?.wrapT),t.flipY=e.texture?.flipY??!0,this.textureCache.set(e.id,t),t))),this.textureCache.get(e.id))}_getTextureLoader(e){return e.toLowerCase().endsWith(".tga")?this.tgaLoader:e.toLowerCase().endsWith(".ktx2")?this.ktx2Loader:this.textureLoader}async getMesh(e){if(await this.ready,!L.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=c(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,g(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=>{M.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(M);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=>(x(e),e))).then((e=>({scene:e,animations:e.animations})))}}}function x(e){if(e instanceof s)for(const t of f(e.material))t instanceof u.MeshPhongMaterial&&!t.color.isLinear&&(t.color.isLinear=!0);e.children?.forEach(x)}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 b(e){switch(e){case"clamp":return u.ClampToEdgeWrapping;case"repeat":return u.RepeatWrapping;case"mirror":return u.MirroredRepeatWrapping}return u.RepeatWrapping}const M=/_LOD(\d+)$/;
1
+ import{AudioLoader as e,BufferGeometry as t,Group as a,LoadingManager as s,Mesh as i,Object3D as r,TextureLoader as o}from"three";import{GLTFLoader as n,MTLLoader as h,OBJLoader as c}from"three-stdlib";import{FBXLoader as l}from"three-stdlib";import{cloneMesh as d}from"../utils/mesh.js";import{pathJoin as m}from"../utils/files.js";import{Subject as p,firstValueFrom as u}from"rxjs";import{importCollisionShapes as f,isCollisionMesh as g}from"./collision/collision-shape-import.js";import*as w from"three";import{iterateMaterials as y}from"../utils/materials.js";import{BufferGeometryUtils as x,KTX2Loader as L,TGALoader as M}from"three/examples/jsm/Addons.js";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 s,this.glbLoader=new n(this.loadingManager),this.fbxLoader=new l(this.loadingManager),this.objLoader=new c(this.loadingManager),this.textureLoader=new o(this.loadingManager),this.tgaLoader=new M(this.loadingManager),this.ktx2Loader=new L(this.loadingManager),this._textureLoader=new w.ImageBitmapLoader(this.loadingManager),this.audioLoader=new e(this.loadingManager),this.makeReady=new p,this.ready=u(this.makeReady),this.collisionShapeCache=new Map,this.optimizedMeshes=new Set}setDataDir(e){this.basePath=m(e,"asset-resources"),this.makeReady.next(!0)}getUri(e){return m(this.basePath,e)+`?windowId=${getElectronArg("windowId")}`}async getTexture(e){return null==e?null:(await this.ready,this.textureCache.has(e.id)||await this._getTextureLoader(e.fileKey).loadAsync(this.getUri(e.fileKey)).then((t=>(t.wrapS=S(e.texture?.wrapS),t.wrapT=S(e.texture?.wrapT),t.flipY=e.texture?.flipY??!0,this.textureCache.set(e.id,t),t))),this.textureCache.get(e.id))}_getTextureLoader(e){return e.toLowerCase().endsWith(".tga")?this.tgaLoader:e.toLowerCase().endsWith(".ktx2")?this.ktx2Loader:this.textureLoader}async getMesh(e){if(await this.ready,!b.includes(e.fileFormat?.toLowerCase()))return console.error("Unsupported mesh file format "+e.fileFormat,e),{scene:new a,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 a,animations:[]}}const t=this.cache.get(e.fileKey).scene,s=this.computeCollisionShapes(e,t);this.optimizeDrawGroups(t);const r=d(t),o=this.cache.get(e.fileKey).animations;r.traverse((e=>{e instanceof i&&e.material instanceof Array&&(e.material=e.material.slice())}));const n=new AssetMeshInstance;n.add(r),n.collisionShapes=s,n.animations=o;const h=e.receiveShadow??!0,c=e.castShadow??!1;return r.traverse((e=>{e.castShadow=c,e.receiveShadow=h})),{scene:n,animations:o}}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=>{C.test(e.name)&&(a=!0)})),!a)return t;const s=new w.LOD,i=[t];for(;i.length>0;){const e=i.shift(),t=e.name.match(C);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)))}optimizeDrawGroups(e){if(this.optimizedMeshes.has(e.uuid))return;this.optimizedMeshes.add(e.uuid);let a=!0,s=0;if(e.traverse((e=>{e instanceof i&&e.geometry instanceof t&&!g(e)?s++:e instanceof w.SkinnedMesh&&(a=!1)})),s>1&&a){const a=[],s=[],r=[];e.updateWorldMatrix(!0,!0),e.traverse((e=>{e instanceof i&&e.geometry instanceof t&&!g(e)&&!Array.isArray(e.material)&&(e.updateWorldMatrix(!0,!0),e.geometry.applyMatrix4(e.matrixWorld),a.push(e.geometry),s.push(e.material),r.push(e))}));for(const e of r)e.removeFromParent();const o=x.mergeGeometries(a,!0),n=[];let h=0;e:for(const e of s){for(const t of n)if(t.m.id===e.id){t.indices.push(h),h++;continue e}n.push({m:e,indices:[h]}),h++}let c=0;for(const e of n){for(const t of e.indices)o.groups[t].materialIndex=c;c++}e.add(new i(o,n.map((e=>e.m))))}e.traverse((e=>{if(e instanceof i&&e.geometry instanceof t){const t=e.geometry;Array.isArray(e.material)&&t.groups.length>1&&t.groups.length>e.material.length&&x.mergeGroups(t)}}))}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 h;t.materialOptions={normalizeRGB:!1};const a=await t.loadAsync(this.getUri(e.materialLib));this.objLoader.setMaterials(a)}return this.objLoader.loadAsync(t).then((e=>(A(e),e))).then((e=>({scene:e,animations:e.animations})))}}}function A(e){if(e instanceof i)for(const t of y(e.material))t instanceof w.MeshPhongMaterial&&!t.color.isLinear&&(t.color.isLinear=!0);e.children?.forEach(A)}export class AssetMeshInstance extends r{}export function getElectronArg(e){const t=`--${e}=`,a=window.process?.argv.find((e=>e.startsWith(t)));return a?.substring(t.length)}function S(e){switch(e){case"clamp":return w.ClampToEdgeWrapping;case"repeat":return w.RepeatWrapping;case"mirror":return w.MirroredRepeatWrapping}return w.RepeatWrapping}const C=/_LOD(\d+)$/;
2
2
  /*
3
3
  * Copyright (©) 2023. All rights reserved.
4
4
  * See the LICENSE.md file for details.
@@ -1,4 +1,4 @@
1
- import{Subject as e}from"rxjs";import*as t from"three";import{BoxGeometry as a,Color as s,Euler as r,Fog as i,FogExp2 as n,Group as o,Material as c,Matrix4 as l,Mesh as h,MeshLambertMaterial as m,MeshPhongMaterial as p,MeshStandardMaterial as d,Object3D as u,PointLight as f,Quaternion as y,SphereGeometry as g,Texture as w,Vector2 as S,Vector3 as v,Vector4 as A}from"three";import b,{SpriteRenderer as M}from"three-nebula";import{bool as x,BooleanNode as j,float as I,FloatNode as P,NodeShaderMaterial as D,rgb as N,RgbNode as E,Texture2dLookupNode as V,textureSampler2d as C,vec2 as F,Vec2Node as k,vec3 as z,Vec3Node as O,vec4 as _}from"three-shader-graph";import{VfxActor as B}from"../effects/vfx/vfx-actor.js";import{VisualEffect as T}from"../effects/vfx/vfx-param.js";import{BaseActor as $}from"../gameplay/actors/actor.js";import L from"../gameplay/actors/builtin/index.js";import{PhysicsBodyType as U,withInjectionContext as R}from"../gameplay/index.js";import{Sampler2DNode as W}from"../shader-nodes/index.js";import{LambertShader as J}from"../shader/builtin/lambert-shader.js";import{StandardShader as H}from"../shader/builtin/standard-shader.js";import{extractShaderParameters as q}from"../shader/parameter.js";import{ArrayMap as X,groupBy as G}from"../utils/collections.js";import{iterateMaterials as Y}from"../utils/materials.js";import{filterChildrenShallow as Z,filterSceneShallow as Q,findFirstVisibleObject as K}from"../utils/three/traverse.js";import{AssetMeshInstance as ee}from"./asset-resource-loader.js";import{isCollisionMesh as te}from"./collision/collision-shape-import.js";import{BoxCollisionShape as ae,PhysicalShapeMesh as se}from"./collision/collision-shape.js";import{LandscapeManager as re}from"./landscape/landscape-manager.js";import{initLandscape as ie}from"./landscape/landscape.js";import{SectionGrid as ne,smoothNormalsCrossMeshes as oe}from"./landscape/utils.js";import{createGrassFoliageMaterial as ce}from"./materials/grass-foliage.js";import{createGrassMaterial as le}from"./materials/grass.js";import{getMaterialAttribute as he}from"./materials/utils/material-painting.js";import{createWaterMaterial as me}from"./materials/water.js";import{SerializedParamType as pe}from"./model.js";import{ShapeLibrary as de,ShapeLibraryKeys as ue}from"./objects/shapes.js";import{ambientLightName as fe,createSky as ye,defaultSkyMaterial as ge}from"./sky.js";const we={};export const shapeDefaultColor="#aaaaaa";export class SceneMaterializerLoader{constructor(e,t,a){this.dataProvider=e,this.assetsService=t,this.assetManagerService=a}get(e,t){return new SceneMaterializer(e,this.dataProvider,this.assetsService,this.assetManagerService,t,[],[],{create:()=>null,initActor:async()=>{}})}}export class SceneMaterializer{constructor(a,s,r,i,n,o,c,l){this.scene=a,this.dataProvider=s,this.assetsService=r,this.assetManagerService=i,this.renderingView=n,this.shaders=o,this.actorTypes=c,this.actorProvider=l,this.objectMap=new Map,this.sceneObjectMap=new Map,this.components=[],this.landscapeManagers=[],this.materializedActors=new Map,this.inEditor=!0,this.updated$=new e,this.removed$=new e,this.error$=new e,this.editorActorParamSnapshot=new Map,this.assets=new Map,this._canBeInstancedCache=new Map,this._originalMaterials=new Map,this.geometryCache=new Map,this.collisionShapeCache=new Map,this.originalFog=null,s.onUpdate((e=>this.update(e))),s.onRemove((e=>this.remove(e))),this.createAssetSubscription=r.onCreate.subscribe((e=>{this.assets.set(e.id,e)})),this.updateSubscription=r.onUpdate.subscribe((async e=>{this.assets.set(e.id,e),"material"==e.type?a.traverse((a=>{if(a instanceof t.Mesh)if(Array.isArray(a.material))for(let t=0;t<a.material.length;t++)this.refreshMaterial(a,a.material[t],e,t);else this.refreshMaterial(a,a.material,e)})):"mesh"==e.type?(this.findByAssetId(e.id).forEach((t=>{Ne(t.userData.src.materialAssignments,e.materialAssignments).forEach((e=>{this.applyMaterial(t,e)}))})),this.landscapeManagers.forEach((t=>{t.source.grass.layers.some((t=>t.meshes.some((t=>t.assetId===e.id))))&&t.queueRefreshScatter(this.renderingView?.camera.position??new v,!0)}))):"prefab"===e.type&&this.findByAssetId(e.id).forEach((e=>{const t=e.userData.src;this.remove(t),this.materializeAndInitActor(t)}))}))}async refreshMaterial(e,t,a,s){if(t?.userData?.assetId!==a.id)return;const r=await materialFromAsset(a,this.renderingView,this.assetsService,this.assetManagerService,this.shaders,!0);r.userData=t.userData,null!=s?Ve(e.material[s],r)||(e.material[s]=r):Ve(e.material,r)||(e.material=r)}get actorInstances(){return Array.from(this.materializedActors.values())}async prefetchAssets(){const e=Array.from(new Set(this.dataProvider.getObjects().filter((e=>null!=e.assetId&&"asset_mesh"==e.type)).filter((e=>e.assetId))));await Promise.all(e.map((e=>this.assetsService.getAsset(e.assetId).then((e=>{if(null!=e)return this.assetManagerService.getMesh(e)})))))}async init(){await this.preInit(),ve.clear(),await this.prefetchAssets(),await Promise.all(this.dataProvider.getObjects().map((e=>this.materialize(e)))),await this.initActorsPostInit()}initActorsPostInit(e=this.actorInstances){const t=e.map((async e=>{const t=e.object.userData.src;if("vfx"===t.type)return Promise.resolve();const a=await this.assetsService.getAsset(t.assetId),s={...a?.actor?.params??{},...t.actor?.params??{}};for(const a of t.actor.innerParams??[])await this.applyActorComponentParams(e,a.path.slice(),a.params);const r=await Me(s,e.constructor,this.assetsService,this.assetManagerService,this.materializedActors,this.renderingView,this.shaders,this.actorProvider);Object.assign(e,r);try{return await this.actorProvider.initActor(e)}catch(e){console.error(`Failed to initiate actor (name="${t.name}", id=${t.id})`,e)}}));return Promise.all(t)}addVfxChildActors(e,t=e){}async applyActorComponentParams(e,t,a){const s=t.length,r=t.shift();if(0==s){const t=await Me(a,null,this.assetsService,this.assetManagerService,this.materializedActors,this.renderingView,this.shaders);for(const[a,s]of Object.entries(t))null!=s&&(e[a]=s)}else null!=e[r]&&await this.applyActorComponentParams(e[r],t,a)}canObjectBeInstanced(e){return e.physics?.type!==U.dynamic&&"sky"!==e.type&&"global_fog"!==e.type}async canAssetBeInstanced(e){if(!this._canBeInstancedCache.has(e.assetId)){const t=await this.createFromAsset(e);if(null==t)return!1;const a=[];t.traverse((e=>{!te(e)&&e.isMesh&&a.push(e)}));const s=1==a.length&&0==a[0].children.length,r=a[0]instanceof h&&null!=a[0].geometry.morphAttributes&&Object.keys(a[0].geometry.morphAttributes).length>0,i=!0;this._canBeInstancedCache.set(e.assetId,s&&i&&!r)}return this._canBeInstancedCache.get(e.assetId)}async preInit(){this.renderingView?.onLoop((()=>{null!=this.sky&&this.renderingView.camera.getWorldPosition(this.sky.position)})),this.assetsService.getAssets().then((e=>{for(const t of e)this.assets.set(t.id,t)}))}async initWithInstancing(){await this.preInit(),await this.prefetchAssets();const e=[],a=new X,s=new X;for(const t of this.dataProvider.getObjects())await De(t,(async(t,r,i)=>{const n="asset_mesh"==t.type&&this.canObjectBeInstanced(t)&&await this.canAssetBeInstanced(t),o="shape_mesh"===t.type&&"landscape"!==t.shape&&t.physics?.type!==U.dynamic;if(n||o)if(r&&r.children?.length>0&&r.children.splice(r.children.findIndex((e=>e.id===t.id)),1),o){const e=t.shape+JSON.stringify(t.materialAssignments??[])+JSON.stringify(t.shapeParams??{})+t.castShadow+t.receiveShadow;s.push(e,{...t,parentTransform:i})}else{const e=t.assetId+JSON.stringify(t.materialAssignments??[]);a.push(e,{...t,parentTransform:i})}else null==r&&e.push({...t,parentTransform:i})}));for(const e of a.values()){if(0==e.length)continue;const t=await this.createFromAsset(e[0]);if(null==t)continue;const a=await this.createInstancedMesh(e,t),s=new ee;s.add(a),s.userData.src=e[0],t instanceof ee&&(s.collisionShapes=t.collisionShapes),s.castShadow=!1,s.receiveShadow=!1,this.scene.add(s)}for(const e of s.values()){if(0==e.length)continue;const a=await this.createFromShape(e[0]),s=K(a,(e=>!te(e)&&null!=e.geometry)),i=s.geometry,n=new t.InstancedMesh(i,s.material.clone(),e.length);for(let i=0;i<e.length;i++){const o=(new t.Matrix4).compose((new v).fromArray(e[i].position),(new y).setFromEuler((new r).fromArray(e[i].rotation)),(new v).fromArray(e[i].scale)),c=(new l).copy(e[i].parentTransform).multiply(o);n.setMatrixAt(i,c),n.castShadow=a.castShadow??!0,n.receiveShadow=s.receiveShadow??!0;const h=new ee;h.add(n),h.userData.src=e[0],a instanceof se&&(h.collisionShapes=[a.collisionShape]),h.castShadow=!1,h.receiveShadow=!1,this.scene.add(h)}}await Promise.all(e.map((e=>this.materialize(e)))),await this.initActorsPostInit()}async createInstancedMesh(e,a){const s=K(a,(e=>!te(e)&&null!=e.geometry)),i=await this.assetsService.getAsset(e[0].assetId);await this.applyMaterials(a,Ne(e[0].materialAssignments,i.materialAssignments)),s.updateMatrix();const n=s.geometry.clone().applyMatrix4(s.matrix),o=new t.InstancedMesh(n,s.material,e.length);s.material instanceof c&&(o.material.side=t.FrontSide);for(let a=0;a<e.length;a++){const s=(new t.Matrix4).compose((new v).fromArray(e[a].position),(new y).setFromEuler((new r).fromArray(e[a].rotation)),(new v).fromArray(e[a].scale)),i=(new l).copy(e[a].parentTransform).multiply(s);o.setMatrixAt(a,i)}return o.castShadow=e[0].castShadow??i.castShadow??!0,o.receiveShadow=e[0].receiveShadow??i.receiveShadow??!0,o}remove(e){if(console.log("Remove scene object",e),"global_fog"==e.type)return void(this.scene.fog=this.originalFog);if("actor"==e.type){const t=this.materializedActors.get(e.id);null!=t?(t.disposed.next(!0),t.onEndPlay()):console.warn("Failed to remove actor",e)}const t=this.sceneObjectMap.get(e.id);t?.parent.remove(t),this.sceneObjectMap.delete(e.id),this.components.filter((t=>t.object.userData.src?.id===e.id)).forEach((e=>this.components.splice(this.components.indexOf(e,1)))),this.landscapeManagers.filter((t=>t.source.id===e.id)).forEach((e=>{e.clear(),e.stop(),this.landscapeManagers.splice(this.landscapeManagers.indexOf(e,1))})),this.removed$.next({object:t,source:e})}deleteSceneObject(e){const t=this.sceneObjectMap.get(e.id);if(this.scene.remove(t),"landscape"==e.type){const t=this.landscapeManagers.findIndex((t=>t.source.id===e.id));if(t>-1){const e=this.landscapeManagers.splice(t,1)[0];e.clear(),e.stop()}}}findByAssetId(e){return Q(this.scene,(t=>t.userData.src?.assetId==e),(e=>null!=e.userData.src))}applyMaterials(e,t){return null==t?Promise.resolve([]):Promise.all(t.filter((e=>"null"!==e.materialId)).map((t=>this.applyMaterial(e,t))))}async applyMaterial(e,t){await applyMaterial(e,t,(e=>{const t=this.assets.get(e);if(null!=t)return materialFromAsset(t,this.renderingView,this.assetsService,this.assetManagerService,this.shaders)}),this._originalMaterials)}unapplyMaterials(e){e.traverse((async e=>{if(e instanceof h)if(e.material instanceof Array)for(let t=0;t<e.material.length;t++)e.material[t]=this._originalMaterials.get(e.id+"#"+t)??e.material[t];else e.material=this._originalMaterials.get(e.id)??e.material}))}updateActors(e){console.log("update actors"),this.actorTypes=e;const t=new Set(Object.values(L));Q(this.scene,(e=>e.userData.src?.id&&"actor"===e.userData.src.type&&this.materializedActors.has(e.userData.src?.id)&&!t.has(e.userData.src.actor.type))).forEach((async e=>{this.remove(e.userData.src),await this.materializeAndInitActor(e.userData.src)}))}updateShaders(e){this.shaders=e;for(const[e,t]of ve.entries())t.userData.customShaderName&&ve.delete(e);this.landscapeManagers.forEach((t=>t.updateShaders(e))),Q(this.scene,(e=>!0)).forEach((e=>{e.traverse((async e=>{if(e instanceof h)if(Array.isArray(e.material))for(let t=0;t<e.material.length;t++){const a=e.material[t].userData?.customShaderName;if(null!=a){const a=this.assets.get(e.material[t].userData.assetId);this.refreshMaterial(e,e.material[t],a,t)}}else{const t=e.material.userData?.customShaderName;if(null!=t){const t=this.assets.get(e.material.userData.assetId);this.refreshMaterial(e,e.material,t)}}}))}))}async update(e){if("sky"===e.type&&null!=this.sky&&null!=this.sky.parent)return void this.updateSky(e);const t=this.sceneObjectMap.get(e.id);if(t){let r=!1;if(t.traverseAncestors((e=>{"_hology_transform_group"===e.name&&(r=!0)})),!r){const a=this.findParent(e);null!=a&&a.uuid!=t.uuid?a.attach(t):console.error("Parent is wrong")}if("prefab"!==e.type&&"group"!==e.type){this.unapplyMaterials(t);this.inEditor&&e.hidden&&!1?t.traverse((e=>{e instanceof h&&(e.material.wireframe=!0)})):t.traverse((e=>{e instanceof h&&(e.material.wireframe=!1)}))}if("asset_mesh"===e.type){const a=this.assets.get(e.assetId);Ne(e.materialAssignments,a.materialAssignments).forEach((e=>this.applyMaterial(t,e)))}else"shape_mesh"===e.type&&this.applyMaterials(t,e.materialAssignments);if(r||(null!=e.position&&t.position.fromArray(e.position),null!=e.scale&&t.scale.fromArray(e.scale),null!=e.rotation&&t.rotation.fromArray(e.rotation)),this.applyVertexMaterials(e,t),"light"==e.type)if("point"==e.light.type){const a=t;a.color=new s(e.light.point.color),a.intensity=e.light.point.intensity,a.decay=e.light.point.decay,a.castShadow=e.light.point.castShadow,a.distance=Math.max(e.light.point.distance,0)}else"directional"===e.light.type?this.applyDirectionalLight(e.light.directional):"ambient"===e.light.type&&this.applyDirectionalAmbientLight(t,e.light.ambient);else if("landscape"===e.shape)this.applyHeightMaps(t,e.landscape.heightMaps),this.inEditor&&this.landscapeManagers.filter((t=>t.source.id===e.id)).forEach((e=>e.queueRefreshScatter(this.renderingView.camera.position,!0,(e=>!0))));else if("global_fog"===e.type){const t=(this.scene.fog instanceof n?"density":"linear")!==e.fog.type;this.scene.fog=Ie(e.fog),t&&(a=this.scene).traverse((e=>{if(e instanceof h){const t=e.material;t instanceof D&&(a.fog instanceof i?(t.uniforms.fogFar.value=a.fog.far,t.uniforms.fogNear.value=a.fog.near):a.fog instanceof n&&(t.uniforms.density={value:a.fog.density}),t.needsUpdate=!0,t.uniformsNeedUpdate=!0)}})),this.fixFogColor()}else if("actor"===e.type){if(this.materializedActors.has(e.id)){const t=this.editorActorParamSnapshot.get(e.id);null!=t&&t===JSON.stringify(e.actor)||(console.log("Rematerializing actor because parameters changed"),r||(this.remove(e),await this.materializeAndInitActor(e)))}}else if("shape_mesh"===e.type){const a=await this.createMeshByShape(e.shape,t.material,e.shapeParams);t instanceof se&&(t.geometry=a.geometry,t.collisionShape=a.collisionShape)}("asset_mesh"===e.type||"shape_mesh"===e.type&&"landscape"!==e.shape)&&Se(t,e.castShadow,e.receiveShadow),e.name&&e.name.length>0&&(t.name=e.name),this.updated$.next({object:t,source:e})}else{const t=await this.materializeAndInitActor(e);this.updated$.next({object:t,source:e})}var a}async materializeAndInitActor(e,t=this.findParent(e)){console.log("materialize actor and init");const a=await this.materialize(e,t);return De(e,(async e=>{if("actor"===e.type){const t=this.materializedActors.get(e.id);null!=t?await this.initActorsPostInit([t]):console.error(`Something went wrong when creating actor ${e.id}`)}})),a}findParent(e){const t=this.dataProvider.getObjects().flatMap((t=>t.id===e.id?null:Z(t,(t=>t.children?.some((t=>t.id===e.id))),(()=>!0))))[0];return null==t?this.scene:null!=t?Q(this.scene,(e=>e.userData?.src?.id===t.id),(e=>null!=e.userData?.src))[0]:void 0}fixFogColor(){!0===this.renderingView.options.enableOutlines&&(this.scene.fog.color=new s(this.scene.fog.color))}findMeshWithGeometry(e){let t;return e.traverse((e=>{e instanceof h&&e.geometry&&(t=e)})),t}applyVertexMaterials(e,t){if(null==e.vertexMaterials||0===e.vertexMaterials.length)return;const a=G(e.vertexMaterials,(e=>e.m));t.traverse((e=>{if(e instanceof h){const t=he(e,!1);if(null!=t){for(let e=0;e<t.array.length;e++)t.setX(e,0);t.needsUpdate=!0}}}));const s=new Set;for(const[e,r]of a.entries()){const a=null!=e?t.getObjectByName(e):this.findMeshWithGeometry(t);let i=!1;if(null==a)return void console.warn(`Failed to apply vertex materials on mesh with name "${e}"`);const n=he(a,!0);for(let e=0;e<n.array.length;e++)n.setX(e,0);for(const e of r)n.setX(e.i,e.w[0]),n.setY(e.i,e.w[1]),n.setZ(e.i,e.w[2]),i=!0;i&&s.add(e)}this.inEditor&&this.landscapeManagers.filter((t=>t.source.id===e.id)).forEach((e=>e.queueRefreshScatter(this.renderingView.camera.position,!0,(e=>s.has(e.name)))))}async materialize(e,t,s=!1){let r;switch(e.type){case"asset_mesh":r=await this.createFromAsset(e);break;case"shape_mesh":r=await this.createFromShape(e);break;case"light":r=await this.createLight(e);break;case"particles":r=await this.createParticleSystem(e),e.collisionDetection=!1;break;case"global_fog":this.scene.fog=Ie(e.fog),this.fixFogColor(),r=new o;break;case"sky":this.sky=ye(),this.updateSky(e),r=this.sky;break;case"actor":r=await this.createFromActor(e);break;case"group":r=new o;break;case"prefab":r=await this.createFromPrefabAsset(e);break;case"vfx":r=await this.createFromVfx(e);break;default:if(this.inEditor)throw new Error("unknown type "+e.type);console.warn(`Failed to materialize object. Unknown type '${e.type}'. This might be because the hology/core library is not compatible with the editor version.`)}if(null!=r){if(e.name&&e.name.length>0&&(r.name=e.name),null!=e.position&&r.position.fromArray(e.position),null!=e.scale&&r.scale.fromArray(e.scale),null!=e.rotation&&r.rotation.fromArray(e.rotation),s||(r.userData.src=e),this.inEditor,this.inEditor){let e=null;r instanceof se&&(e=function(e){if(e instanceof ae)return new h(new a(...e.offset.toArray()),Pe);return null}(r.collisionShape)),null!=e&&(e.layers.disable(0),e.layers.enable(18),e.scale.multiplyScalar(1.1),r.add(e))}return this.objectMap.set(r.uuid,e),this.sceneObjectMap.set(e.id,r),e.physics?.type!==U.dynamic||null==t||this.inEditor?null==t?this.scene.add(r):t?.add(r):(t.add(r),r.getWorldPosition(r.position),r.getWorldQuaternion(r.quaternion),r.getWorldScale(r.scale),this.scene?.attach(r)),null!=e.children&&await Promise.all(e.children?.map((e=>this.materialize(e,r,s)))),this.inEditor||"asset_mesh"!=e.type&&"shape_mesh"!==e.type||null!=e.physics?.type&&e.physics.type==U.dynamic||je(r),r}}async updateSky(e){if(null==e?.sky?.materialId)return void(this.sky.material=ge);const a=await this.assetsService.getAsset(e.sky.materialId),s=await materialFromAsset(a,this.renderingView,this.assetsService,this.assetManagerService,this.shaders,!1);s.side=t.BackSide,null!=this.sky?this.sky.material=s:console.warn("No sky has been created")}async createComponent(e,t,a,s){const r=new we[a.path+"/"+a.className],i=t.id+s;r.id=i,r.object=e;for(const e of a.params)null!=e.value&&(r[e.name]=e.value);return this.components.push(r),i}async createFromActor(e){const t=this.actorTypes.find((t=>t.name===e.actor?.type))?.type??L[e.actor?.type];if(null==t)return null;this.inEditor&&this.editorActorParamSnapshot.set(e.id,JSON.stringify(e.actor));const a=await this.actorProvider.create(t,(new v).fromArray(e.position),(new r).fromArray(e.rotation),!0);return this.materializedActors.set(e.id,a),a?.object}async createFromVfx(e){const t=await this.assetsService.getAsset(e.assetId);null==t&&console.error("Could not find asset",e);const a=await this.actorProvider.create(B,(new v).fromArray(e.position),(new r).fromArray(e.rotation),!1);return await a.fromAsset(t),a.play(),this.materializedActors.set(e.id,a),a?.object}cleanup(){this.materializedActors.clear()}async createFromShape(e){const t=this.inEditor&&e.hidden;let a;if("landscape"==e.shape)a=this.createLandscape(e),a.traverse((e=>{e instanceof h&&this._originalMaterials.set(e.id,e.material)}));else{let r=new d({name:"Default",color:new s("#aaaaaa"),visible:this.inEditor||!e.hidden,wireframe:!!t});const i=await this.createMeshByShape(e.shape,r,e.shapeParams);i.castShadow=e.castShadow??!0,i.receiveShadow=e.castShadow??!1,e.collisionDetection||(i.collisionShape=null),i.physics=e.physics,a=i,this._originalMaterials.set(a.id,i.material),a.traverse((e=>{}))}return t||(await Promise.all((e.materialAssignments??[]).filter((e=>null!=e.materialId)).map((e=>this.applyMaterial(a,e)))),this.applyVertexMaterials(e,a)),a}createLandscape(e){const t=e.landscape?.options;if(null==t)return console.error(`No landscape options exist on scene object ${e.id} ${e.name}`),new o;const a=ie(e.landscape.options);this.applyHeightMaps(a,e.landscape.heightMaps,!0);const s=new re(e,this.renderingView,a,this.assetManagerService,this.assetsService,this.shaders,(t=>{(e.materialAssignments??[]).filter((e=>null!=e.materialId)).forEach((e=>this.applyMaterial(t,e)))}));return this.landscapeManagers.push(s),s.refreshGeometry(),a}applyHeightMaps(e,t,a=!1){const s=new ne(e.sections);for(const e of t??[]){const t=s.find(e.x,e.y);if(!t)return;const a=t.geometry.getAttribute("position");for(const t of e.points)a.setY(t.i,t.y);a.needsUpdate=!0}const r=e.sections;r.forEach((e=>{e.geometry.computeBoundsTree(),e.geometry.computeVertexNormals()})),this.inEditor&&!a||setTimeout((()=>oe(r)),50)}async createMeshByShape(e,t,a={}){if("landscape"!==e&&ue.includes(e)){const s=await prepareShapeParameters(a??{}),r=e+JSON.stringify(a);return this.geometryCache.has(r)||this.geometryCache.set(r,de[e].geometry(s)),this.collisionShapeCache.has(r)||this.collisionShapeCache.set(r,de[e].collision(s)),new se(this.geometryCache.get(r),t,this.collisionShapeCache.get(r))}if(this.inEditor)throw new Error(`Unsupported shape '${e}'`);console.warn(`Failed to create shape. Unsupported shape '${e}'. This might be because the hology/core library is not compatible with the editor version.`)}async createFromAsset(e){const t=await this.assetsService.getAsset(e.assetId);if(null==t)return void console.warn(`Can not find asset with id ${e.assetId} and name ${e.name}`);let{scene:a}=await this.assetManagerService.getMesh(t);Ne(e.materialAssignments,t.materialAssignments).forEach((e=>this.applyMaterial(a,e)));const s=e.receiveShadow??!!t.receiveShadow??!0,r=e.castShadow??!!t.castShadow??!1;return a.receiveShadow=s,Se(a,r,s),e.collisionDetection||(a.collisionShapes=[]),null!=e.physics&&!0!==this.inEditor&&(a.physics=e.physics),this.applyVertexMaterials(e,a),a.traverse((e=>{e instanceof h&&"computeBoundsTree"in e.geometry&&null==e.geometry.boundsTree&&e.geometry.computeBoundsTree()})),a}async createFromPrefabAsset(e){const t=await this.assetsService.getAsset(e.assetId);if(null==t)return void console.warn(`Can not find asset with id ${e.assetId} and name ${e.name}`);const a=new o;return t.prefab.objects.filter((e=>"global_fog"!==e.type)).forEach((e=>this.materialize(e,a,!0))),a}async createParticleSystem(e){const a=await this.assetsService.getAsset(e.assetId),s=new u;return await b.fromJSONAsync(a.particleSystem,t).then((e=>{const a=new M(s,t);e.addRenderer(a),this.renderingView.onLoop((t=>e.update()))})),s}async createLight(e){if("point"===e.light.type){const t=new f(e.light.point.color,e.light.point.intensity,e.light.point.distance,e.light.point.decay);if(t.castShadow=e.light.point.castShadow??!0,this.inEditor){const e=new g(.3,10,10),a=new d({color:new s(16771709)}),r=new h(e,a);t.add(r)}return t}return"directional"===e.light.type?(this.applyDirectionalLight(e.light.directional),new o):"ambient"===e.light.type?(this.applyDirectionalAmbientLight(null,e.light.ambient),new o):void 0}applyDirectionalAmbientLight(e,t){const a=this.scene.children.find((e=>e.name===fe));null!=a?(a.intensity=t.intensity,a.color.set(t.color),a.groundColor.set(t.color)):console.warn("Couldn't find ambient light")}applyDirectionalLight(e){for(const t of this.renderingView.csm.lights)t.intensity=e.intensity,t.color.set(e.color),t.castShadow=e.castShadow;this.renderingView.csm.lightDirection.fromArray(e.direction).normalize()}dispose(){this.updateSubscription.unsubscribe(),this.createAssetSubscription.unsubscribe(),this.materializedActors.forEach((e=>e.disposed.next(!0)))}}function Se(e,t,a){e.castShadow=t,e.receiveShadow=a,e.traverse((e=>{e.castShadow=t,e.receiveShadow=a}))}const ve=new Map,Ae=new m({color:16711935}),be=new Map;export async function materialFromAsset(e,a,r,i,n,o=!0){const c=JSON.stringify(e.material);if(o&&ve.has(c))return ve.get(c);const l={opacity:e.material.params.opacity,map:null,emissive:e.material.params.emissive??null,metalness:e.material.params.metalness??0,flatShading:e.material.params.flatShading??!1,color:new s(e.material.params.color),transparent:null!=e.material.params.opacity&&e.material.params.opacity<1},h={};if(null!=e.material.params.map){const t=e.material.params.map,a=await r.getAsset(t);null!=a&&(l.map=await i.getTexture(a))}let m;switch(e.material.type){case"phong":m=new p({...l,...h});break;case"water":m=me(l,a);break;case"grassFoliage":m=ce({color:l.color,map:l.map},a);break;case"grass":m=le({...l,colorTwo:new s(e.material.params.colorTwo),colorThree:new s(e.material.params.colorThree)},a);break;case"standard":case"lambert":case"shader":const t={standard:H,lambert:J}[e.material.type]??n.find((t=>t.name==e.material.shader))?.type;if(t){const s=new t,o=await Me(e.material?.shaderParams??{},t,r,i,null,a,n);Object.assign(s,o);try{m=s.build()}catch(t){console.log("Shader runtime error: "+t),be.has(e.material.shader)||be.set(e.material.shader,Ae.clone()),m=be.get(e.material.shader)}m.userData.customShaderName=e.material.shader}else console.warn("Missing shader implementation with name "+e.material.shader),m=Ae;break;default:throw new Error("Unsupported material type"+e.material.type)}return a?.csm.setupMaterial(m),o&&ve.set(c,m),m.side=e.material.side??m.side??t.FrontSide,m.transparent=(e.material.transparent??l.transparent??!1)||m.transparent,e.material.bloom&&(m.userData.hasBloom=!0),m.userData.assetId=e.id,m}async function Me(e,t,a,s,r,i,n,o){const c={};for(const[t,l]of Object.entries(e)){const e=await xe(l,a,s,r,i,n,o);null!=e&&(c[t]=e)}return c}export async function prepareShapeParameters(e){const t={};for(const[a,s]of Object.entries(e)){const e=await xe(s,null,null,null);null!=e&&(t[a]=e)}return t}async function xe(e,t,a,i,n,o,c){if(pe.String,null==e||null==e.value||""==e.value)return null;const l=e.value;switch(e.type){case pe.Number:case pe.FloatNode:let h="string"==typeof l?parseFloat(l):l;return e.type===pe.FloatNode?I(h):h;case pe.Texture:return await a.getTexture(await t.getAsset(l));case pe.Sampler2DNode:return C(await a.getTexture(await t.getAsset(l)));case pe.Boolean:return l;case pe.BooleanNode:return x(l);case pe.Vector2:case pe.Vec2Node:if("object"==typeof l){const t=l instanceof Array?(new S).fromArray(l):new S(l.x,l.y);return e.type===pe.Vec2Node?F(t):t}return null;case pe.Vector3:case pe.Vec3Node:if("object"==typeof l){const t=l instanceof Array?(new v).fromArray(l):new v(l.x,l.y,l.z);return e.type===pe.Vec3Node?z(t):t}return null;case pe.Color:case pe.RgbNode:const m=new s(l);return e.type===pe.RgbNode?N(m):m;case pe.String:return l;case pe.BaseActor:const p=l;return null==i&&console.warn("Class parameters can not be prepared as actors are not passed in"),i?.get(p);case pe.Euler:const d=l;return(new r).fromArray(d);case pe.Object3D:return(await a.getMesh(await t.getAsset(l))).scene;case pe.Material:return await materialFromAsset(await t.getAsset(l),n,t,a,o);case pe.AudioBuffer:return await a.getAudio(await t.getAsset(l));case pe.VisualEffect:const u=await t.getAsset(l);if(null==c){console.error("Can not create instance of visual effect because missing actor provider");break}if("vfx"in u)return new T(c,u);console.error("Using a non-vfx asset for visual effect parameter")}return null}function je(e){e.updateWorldMatrix(!1,!0),e.traverse((e=>{e.matrixAutoUpdate=!1,e.matrixWorldNeedsUpdate=!1}));const t=e.updateMatrixWorld;e.updateMatrixWorld=function(){t.apply(e),e.updateMatrixWorld=function(){}}}function Ie(e){return"linear"===e.type?new i(new s(e.color),e.near??100,e.far??1e3):"density"===e.type?new n(e.color,e.density):void console.warn("Invalid fog type",e)}const Pe=new d({color:4229780});async function De(e,a,s,i){null==i&&(i=(new l).identity()),await a(e,s,i);const n=i.clone().multiply(function(e,t){if(null==e.position||null==e.rotation||null==e.scale)return t.identity();return t.compose((new v).fromArray(e.position),(new y).setFromEuler((new r).fromArray(e.rotation)),(new v).fromArray(e.scale))}(e,new t.Matrix4));return Promise.all((e.children??[]).map((t=>De(t,a,e,n))))}export function toSerializedParamType(e){const t=e.constructor.prototype;return t instanceof Number||e===Number?pe.Number:t instanceof P||"function"==typeof e.prototype.isFloat?pe.FloatNode:t instanceof w||e===w||e.isTexture?pe.Texture:t instanceof W||e===V?pe.Sampler2DNode:t instanceof Boolean||e===Boolean?pe.Boolean:t instanceof j?pe.BooleanNode:t instanceof s||e==s?pe.Color:t instanceof E||"function"==typeof e.prototype.isRgb?pe.RgbNode:t instanceof S||e==S?pe.Vector2:t instanceof k||"function"==typeof e.prototype.isVec2?pe.Vec2Node:t instanceof v||e==v?pe.Vector3:t instanceof O||"function"==typeof e.prototype.isVec3?pe.Vec3Node:t instanceof String||e===String?pe.String:t instanceof $||e==$||e.prototype instanceof $||e.prototype==$?pe.BaseActor:t instanceof r||e==r?pe.Euler:t instanceof u||e==u?pe.Object3D:t instanceof c||e==c?pe.Material:t instanceof AudioBuffer||e==AudioBuffer?pe.AudioBuffer:t instanceof T||e==T?pe.VisualEffect:void console.warn("Failed to map parameter type to serialized version",{type:e})}export function prepareCustomParams(e,t,a={}){return Object.fromEntries(e.map((e=>[e.name,{type:toSerializedParamType(e.type),value:t[e.name]?.value??a[e.name]??customParameterDefaultValueByType.get(toSerializedParamType(e.type))}])))}export function prepareCustomParamsFromType(e,t,a=null){const s=q(e);if(0===s.length)return{};let r;null!=a?R(a,(()=>{r=a.get(e)})):r=new e;const i={};for(const e of s){const t=r[e.name];if(null!=t){const a=serializeCustomParameter(e.type,t);null!=a&&(i[e.name]=a)}}return prepareCustomParams(s,t,i)}export function serializeCustomParameter(e,t){function a(){console.error("Failed to serialize value",{type:e,value:t})}switch(e){case Number:case Boolean:return t;case S:return t instanceof S?t.toArray():void a();case v:return t instanceof v?t.toArray():void a();case A:return t instanceof A?t.toArray():void a();case s:return t instanceof s?"#"+t.getHexString():"string"==typeof t?t:"number"==typeof t?"#"+new s(t).getHexString():void a();case String:return t;case r:return t instanceof r?t.toArray():void a()}}function Ne(e,t){return function(e,t,a){const s=[],r=new Set;for(const i of[...e??[],...t??[]]){const e=a(i);r.has(e)||(r.add(e),s.push(i))}return s}((e??[]).filter((e=>Ee(e.materialId))),(t??[]).filter((e=>Ee(e.materialId))),(e=>e.color+e.name))}function Ee(e){return"null"!=e&&null!=e}export const customParameterDefaultValueByType=new Map([[pe.RgbNode,"#000000"],[pe.Color,"#000000"],[pe.Vector4,[0,0,0,0]],[pe.Vec4Node,[0,0,0,0]],[pe.Vector3,[0,0,0]],[pe.Vec3Node,[0,0,0]],[pe.Vector2,[0,0]],[pe.Vec2Node,[0,0]],[pe.Euler,[0,0,0,"XYZ"]]]);export function applyMaterial(e,a,r,i){const n=[];return e.traverse((async e=>{if(e instanceof h||e.isMesh||e instanceof t.SkinnedMesh||e.isSkinnedMesh)for(const t of Y(e.material))t.hasOwnProperty("color")&&n.push(e)})),Promise.all(n.map((async e=>{if(e.material instanceof Array)for(let t=0;t<e.material.length;t++){const n=e.material[t];if(null==n.color||!(n.color instanceof s))continue;const o="#"+n.color.getHexString(),c=n.name;if(o===a.color&&(n.name===a.name||null==a.name)||e.userData["originalColor_"+t]===a.color&&e.userData["originalMaterialName_"+t]===a.name){const s=await r(a.materialId),n=e.material[t];null!=s&&(e.material[t]=s,e.userData["originalColor_"+t]=e.userData["originalColor_"+t]??o,e.userData["originalMaterialName_"+t]=e.userData["originalMaterialName_"+t]??c,null!=i&&i.set(e.id+"#"+t,n))}}else if("color"in e.material){const t="#"+e.material.color.getHexString(),s=e.material.name;if(t===a.color&&(e.material.name===a.name||null==a.name)||e.userData.originalColor===a.color&&e.userData.originalName===a.name){const n=await r(a.materialId),o=e.material;null!=n&&(e.material=n,e.userData.originalColor=e.userData.originalColor??t,e.userData.originalMaterialName=e.userData.originalMaterialName??s,null!=i&&(i.has(e.id)||i.set(e.id,o)))}}})))}function Ve(e,a){if(e instanceof t.ShaderMaterial&&a instanceof t.ShaderMaterial){return e.fragmentShader+e.vertexShader==a.fragmentShader+a.vertexShader&&function(e,a){if(e instanceof t.ShaderMaterial&&a instanceof t.ShaderMaterial){for(const t in e.uniforms){if(null==a.uniforms[t])return!1;if(a.uniforms[t].value!==e.uniforms[t].value)return console.log("Different values",a.uniforms[t].value,e.uniforms[t].value),!1}return!0}return!1}(e,a)}return!1}
1
+ import{Subject as e}from"rxjs";import*as t from"three";import{BoxGeometry as a,Color as s,Euler as r,Fog as i,FogExp2 as n,Group as o,Material as l,Matrix4 as c,Mesh as h,MeshLambertMaterial as m,MeshPhongMaterial as p,MeshStandardMaterial as d,Object3D as u,PointLight as f,Quaternion as y,SphereGeometry as g,Texture as w,Vector2 as S,Vector3 as v,Vector4 as A}from"three";import b,{SpriteRenderer as M}from"three-nebula";import{bool as x,BooleanNode as j,float as P,FloatNode as I,NodeShaderMaterial as D,rgb as N,RgbNode as E,Texture2dLookupNode as V,textureSampler2d as C,vec2 as F,Vec2Node as k,vec3 as z,Vec3Node as O,vec4 as _}from"three-shader-graph";import{VfxActor as B}from"../effects/vfx/vfx-actor.js";import{VisualEffect as T}from"../effects/vfx/vfx-param.js";import{BaseActor as $}from"../gameplay/actors/actor.js";import L from"../gameplay/actors/builtin/index.js";import{PhysicsBodyType as U,withInjectionContext as R}from"../gameplay/index.js";import{Sampler2DNode as W}from"../shader-nodes/index.js";import{LambertShader as J}from"../shader/builtin/lambert-shader.js";import{StandardShader as H}from"../shader/builtin/standard-shader.js";import{UnlitShader as q}from"../shader/builtin/unlit-shader.js";import{extractShaderParameters as X}from"../shader/parameter.js";import{ArrayMap as G,groupBy as Y}from"../utils/collections.js";import{iterateMaterials as Z}from"../utils/materials.js";import{filterChildrenShallow as Q,filterSceneShallow as K,findFirstVisibleObject as ee}from"../utils/three/traverse.js";import{AssetMeshInstance as te}from"./asset-resource-loader.js";import{isCollisionMesh as ae}from"./collision/collision-shape-import.js";import{BoxCollisionShape as se,PhysicalShapeMesh as re}from"./collision/collision-shape.js";import{LandscapeManager as ie}from"./landscape/landscape-manager.js";import{initLandscape as ne}from"./landscape/landscape.js";import{SectionGrid as oe,smoothNormalsCrossMeshes as le}from"./landscape/utils.js";import{createGrassFoliageMaterial as ce}from"./materials/grass-foliage.js";import{createGrassMaterial as he}from"./materials/grass.js";import{getMaterialAttribute as me}from"./materials/utils/material-painting.js";import{createWaterMaterial as pe}from"./materials/water.js";import{SerializedParamType as de}from"./model.js";import{ShapeLibrary as ue,ShapeLibraryKeys as fe}from"./objects/shapes.js";import{ambientLightName as ye,createSky as ge,defaultSkyMaterial as we}from"./sky.js";const Se={};export const shapeDefaultColor="#aaaaaa";export class SceneMaterializerLoader{constructor(e,t,a){this.dataProvider=e,this.assetsService=t,this.assetManagerService=a}get(e,t){return new SceneMaterializer(e,this.dataProvider,this.assetsService,this.assetManagerService,t,[],[],{create:()=>null,initActor:async()=>{}})}}export class SceneMaterializer{constructor(a,s,r,i,n,o,l,c){this.scene=a,this.dataProvider=s,this.assetsService=r,this.assetManagerService=i,this.renderingView=n,this.shaders=o,this.actorTypes=l,this.actorProvider=c,this.objectMap=new Map,this.sceneObjectMap=new Map,this.components=[],this.landscapeManagers=[],this.materializedActors=new Map,this.inEditor=!0,this.updated$=new e,this.removed$=new e,this.error$=new e,this.editorActorParamSnapshot=new Map,this.assets=new Map,this._canBeInstancedCache=new Map,this._originalMaterials=new Map,this.geometryCache=new Map,this.collisionShapeCache=new Map,this.originalFog=null,s.onUpdate((e=>this.update(e))),s.onRemove((e=>this.remove(e))),this.createAssetSubscription=r.onCreate.subscribe((e=>{this.assets.set(e.id,e)})),this.updateSubscription=r.onUpdate.subscribe((async e=>{this.assets.set(e.id,e),"material"==e.type?a.traverse((a=>{if(a instanceof t.Mesh)if(Array.isArray(a.material))for(let t=0;t<a.material.length;t++)this.refreshMaterial(a,a.material[t],e,t);else this.refreshMaterial(a,a.material,e)})):"mesh"==e.type?(this.findByAssetId(e.id).forEach((t=>{Ee(t.userData.src.materialAssignments,e.materialAssignments).forEach((e=>{this.applyMaterial(t,e)}))})),this.landscapeManagers.forEach((t=>{t.source.grass.layers.some((t=>t.meshes.some((t=>t.assetId===e.id))))&&t.queueRefreshScatter(this.renderingView?.camera.position??new v,!0)}))):"prefab"===e.type&&this.findByAssetId(e.id).forEach((e=>{const t=e.userData.src;this.remove(t),this.materializeAndInitActor(t)}))}))}async refreshMaterial(e,t,a,s){if(t?.userData?.assetId!==a.id)return;const r=await materialFromAsset(a,this.renderingView,this.assetsService,this.assetManagerService,this.shaders,!0);r.userData=t.userData,null!=s?Ce(e.material[s],r)||(e.material[s]=r):Ce(e.material,r)||(e.material=r)}get actorInstances(){return Array.from(this.materializedActors.values())}async prefetchAssets(){const e=Array.from(new Set(this.dataProvider.getObjects().filter((e=>null!=e.assetId&&"asset_mesh"==e.type)).filter((e=>e.assetId))));await Promise.all(e.map((e=>this.assetsService.getAsset(e.assetId).then((e=>{if(null!=e)return this.assetManagerService.getMesh(e)})))))}async init(){await this.preInit(),Ae.clear(),await this.prefetchAssets(),await Promise.all(this.dataProvider.getObjects().map((e=>this.materialize(e)))),await this.initActorsPostInit()}initActorsPostInit(e=this.actorInstances){const t=e.map((async e=>{const t=e.object.userData.src;if("vfx"===t.type)return Promise.resolve();const a=await this.assetsService.getAsset(t.assetId),s={...a?.actor?.params??{},...t.actor?.params??{}};for(const a of t.actor.innerParams??[])await this.applyActorComponentParams(e,a.path.slice(),a.params);const r=await xe(s,e.constructor,this.assetsService,this.assetManagerService,this.materializedActors,this.renderingView,this.shaders,this.actorProvider);Object.assign(e,r);try{return await this.actorProvider.initActor(e)}catch(e){console.error(`Failed to initiate actor (name="${t.name}", id=${t.id})`,e)}}));return Promise.all(t)}addVfxChildActors(e,t=e){}async applyActorComponentParams(e,t,a){const s=t.length,r=t.shift();if(0==s){const t=await xe(a,null,this.assetsService,this.assetManagerService,this.materializedActors,this.renderingView,this.shaders);for(const[a,s]of Object.entries(t))null!=s&&(e[a]=s)}else null!=e[r]&&await this.applyActorComponentParams(e[r],t,a)}canObjectBeInstanced(e){return e.physics?.type!==U.dynamic&&"sky"!==e.type&&"global_fog"!==e.type}async canAssetBeInstanced(e){if(!this._canBeInstancedCache.has(e.assetId)){const t=await this.createFromAsset(e);if(null==t)return!1;const a=[];t.traverse((e=>{!ae(e)&&e.isMesh&&a.push(e)}));const s=1==a.length&&0==a[0].children.length,r=a[0]instanceof h&&null!=a[0].geometry.morphAttributes&&Object.keys(a[0].geometry.morphAttributes).length>0,i=!0;this._canBeInstancedCache.set(e.assetId,s&&i&&!r)}return this._canBeInstancedCache.get(e.assetId)}async preInit(){this.renderingView?.onLoop((()=>{null!=this.sky&&this.renderingView.camera.getWorldPosition(this.sky.position)})),this.assetsService.getAssets().then((e=>{for(const t of e)this.assets.set(t.id,t)}))}async initWithInstancing(){await this.preInit(),await this.prefetchAssets();const e=[],a=new G,i=new G;for(const t of this.dataProvider.getObjects())await Ne(t,(async(t,r,n)=>{const o="asset_mesh"==t.type&&this.canObjectBeInstanced(t)&&await this.canAssetBeInstanced(t),l="shape_mesh"===t.type&&"landscape"!==t.shape&&t.physics?.type!==U.dynamic;if(o||l)if(r&&r.children?.length>0&&r.children.splice(r.children.findIndex((e=>e.id===t.id)),1),l){let e=t.shape+JSON.stringify(t.shapeParams??{})+t.castShadow+t.receiveShadow;const a=t.materialAssignments?.at(0)?.materialId,r=null!=a?this.assets.get(a):null;let o=null;if(null!=r&&"shader"!==r.material.type){if(e+=r.material.type+r.material.shader,null!=r.material.shaderParams){if(e+=Object.entries(r.material.shaderParams).filter((([e,t])=>"color"!=e)).map((e=>JSON.stringify(e))).join(),null!=r.material.shaderParams.color){const e=r.material.shaderParams.color;e.type===de.Color&&null!=e.value&&(o=new s(e.value))}}}else e+=a;i.push(e,{object:{...t,parentTransform:n},color:o})}else{const e=t.assetId+JSON.stringify(t.materialAssignments??[]);a.push(e,{...t,parentTransform:n})}else null==r&&e.push({...t,parentTransform:n})}));for(const e of a.values()){if(0==e.length)continue;const t=await this.createFromAsset(e[0]);if(null==t)continue;const a=await this.createInstancedMesh(e,t),s=new te;s.add(a),s.userData.src=e[0],t instanceof te&&(s.collisionShapes=t.collisionShapes),s.castShadow=!1,s.receiveShadow=!1,this.scene.add(s)}for(const e of i.values()){if(0==e.length)continue;const a=e[0].object,i=await this.createFromShape(a),n=ee(i,(e=>!ae(e)&&null!=e.geometry)),o=n.material.clone();null!=e[0].color&&null!=o.color&&(o.color=new s(16777215));const l=n.geometry,h=new t.InstancedMesh(l,o,e.length);for(let a=0;a<e.length;a++){const s=e[a],o=(new t.Matrix4).compose((new v).fromArray(s.object.position),(new y).setFromEuler((new r).fromArray(s.object.rotation)),(new v).fromArray(s.object.scale)),l=(new c).copy(s.object.parentTransform).multiply(o);h.setMatrixAt(a,l),null!=s.color&&h.setColorAt(a,s.color),h.castShadow=i.castShadow??!0,h.receiveShadow=n.receiveShadow??!0;const m=new te;m.add(h),m.userData.src=e[0],i instanceof re&&(m.collisionShapes=[i.collisionShape]),m.castShadow=!1,m.receiveShadow=!1,this.scene.add(m)}}await Promise.all(e.map((e=>this.materialize(e)))),await this.initActorsPostInit()}async createInstancedMesh(e,a){const s=ee(a,(e=>!ae(e)&&null!=e.geometry)),i=await this.assetsService.getAsset(e[0].assetId);await this.applyMaterials(a,Ee(e[0].materialAssignments,i.materialAssignments)),s.updateMatrix();const n=s.geometry.clone().applyMatrix4(s.matrix),o=new t.InstancedMesh(n,s.material,e.length);s.material instanceof l&&(o.material.side=t.FrontSide);for(let a=0;a<e.length;a++){const s=(new t.Matrix4).compose((new v).fromArray(e[a].position),(new y).setFromEuler((new r).fromArray(e[a].rotation)),(new v).fromArray(e[a].scale)),i=(new c).copy(e[a].parentTransform).multiply(s);o.setMatrixAt(a,i)}return o.castShadow=e[0].castShadow??i.castShadow??!0,o.receiveShadow=e[0].receiveShadow??i.receiveShadow??!0,o}remove(e){if(console.log("Remove scene object",e),"global_fog"==e.type)return void(this.scene.fog=this.originalFog);if("actor"==e.type){const t=this.materializedActors.get(e.id);null!=t?(t.disposed.next(!0),t.onEndPlay()):console.warn("Failed to remove actor",e)}const t=this.sceneObjectMap.get(e.id);t?.parent.remove(t),this.sceneObjectMap.delete(e.id),this.components.filter((t=>t.object.userData.src?.id===e.id)).forEach((e=>this.components.splice(this.components.indexOf(e,1)))),this.landscapeManagers.filter((t=>t.source.id===e.id)).forEach((e=>{e.clear(),e.stop(),this.landscapeManagers.splice(this.landscapeManagers.indexOf(e,1))})),this.removed$.next({object:t,source:e})}deleteSceneObject(e){const t=this.sceneObjectMap.get(e.id);if(this.scene.remove(t),"landscape"==e.type){const t=this.landscapeManagers.findIndex((t=>t.source.id===e.id));if(t>-1){const e=this.landscapeManagers.splice(t,1)[0];e.clear(),e.stop()}}}findByAssetId(e){return K(this.scene,(t=>t.userData.src?.assetId==e),(e=>null!=e.userData.src))}applyMaterials(e,t){return null==t?Promise.resolve([]):Promise.all(t.filter((e=>"null"!==e.materialId)).map((t=>this.applyMaterial(e,t))))}async applyMaterial(e,t){await applyMaterial(e,t,(e=>{const t=this.assets.get(e);if(null!=t)return materialFromAsset(t,this.renderingView,this.assetsService,this.assetManagerService,this.shaders)}),this._originalMaterials)}unapplyMaterials(e){e.traverse((async e=>{if(e instanceof h)if(e.material instanceof Array)for(let t=0;t<e.material.length;t++)e.material[t]=this._originalMaterials.get(e.id+"#"+t)??e.material[t];else e.material=this._originalMaterials.get(e.id)??e.material}))}updateActors(e){console.log("update actors"),this.actorTypes=e;const t=new Set(Object.values(L));K(this.scene,(e=>e.userData.src?.id&&"actor"===e.userData.src.type&&this.materializedActors.has(e.userData.src?.id)&&!t.has(e.userData.src.actor.type))).forEach((async e=>{this.remove(e.userData.src),await this.materializeAndInitActor(e.userData.src)}))}updateShaders(e){this.shaders=e;for(const[e,t]of Ae.entries())t.userData.customShaderName&&Ae.delete(e);this.landscapeManagers.forEach((t=>t.updateShaders(e))),K(this.scene,(e=>!0)).forEach((e=>{e.traverse((async e=>{if(e instanceof h)if(Array.isArray(e.material))for(let t=0;t<e.material.length;t++){const a=e.material[t].userData?.customShaderName;if(null!=a){const a=this.assets.get(e.material[t].userData.assetId);this.refreshMaterial(e,e.material[t],a,t)}}else{const t=e.material.userData?.customShaderName;if(null!=t){const t=this.assets.get(e.material.userData.assetId);this.refreshMaterial(e,e.material,t)}}}))}))}async update(e){if("sky"===e.type&&null!=this.sky&&null!=this.sky.parent)return void this.updateSky(e);const t=this.sceneObjectMap.get(e.id);if(t){let r=!1;if(t.traverseAncestors((e=>{"_hology_transform_group"===e.name&&(r=!0)})),!r){const a=this.findParent(e);null!=a&&a.uuid!=t.uuid?a.attach(t):console.error("Parent is wrong")}if("prefab"!==e.type&&"group"!==e.type){this.unapplyMaterials(t);this.inEditor&&e.hidden&&!1?t.traverse((e=>{e instanceof h&&(e.material.wireframe=!0)})):t.traverse((e=>{e instanceof h&&(e.material.wireframe=!1)}))}if("asset_mesh"===e.type){const a=this.assets.get(e.assetId);Ee(e.materialAssignments,a.materialAssignments).forEach((e=>this.applyMaterial(t,e)))}else"shape_mesh"===e.type&&this.applyMaterials(t,e.materialAssignments);if(r||(null!=e.position&&t.position.fromArray(e.position),null!=e.scale&&t.scale.fromArray(e.scale),null!=e.rotation&&t.rotation.fromArray(e.rotation)),this.applyVertexMaterials(e,t),"light"==e.type)if("point"==e.light.type){const a=t;a.color=new s(e.light.point.color),a.intensity=e.light.point.intensity,a.decay=e.light.point.decay,a.castShadow=e.light.point.castShadow,a.distance=Math.max(e.light.point.distance,0)}else"directional"===e.light.type?this.applyDirectionalLight(e.light.directional):"ambient"===e.light.type&&this.applyDirectionalAmbientLight(t,e.light.ambient);else if("landscape"===e.shape)this.applyHeightMaps(t,e.landscape.heightMaps),this.inEditor&&this.landscapeManagers.filter((t=>t.source.id===e.id)).forEach((e=>e.queueRefreshScatter(this.renderingView.camera.position,!0,(e=>!0))));else if("global_fog"===e.type){const t=(this.scene.fog instanceof n?"density":"linear")!==e.fog.type;this.scene.fog=Ie(e.fog),t&&(a=this.scene).traverse((e=>{if(e instanceof h){const t=e.material;t instanceof D&&(a.fog instanceof i?(t.uniforms.fogFar.value=a.fog.far,t.uniforms.fogNear.value=a.fog.near):a.fog instanceof n&&(t.uniforms.density={value:a.fog.density}),t.needsUpdate=!0,t.uniformsNeedUpdate=!0)}})),this.fixFogColor()}else if("actor"===e.type){if(this.materializedActors.has(e.id)){const t=this.editorActorParamSnapshot.get(e.id);null!=t&&t===JSON.stringify(e.actor)||(console.log("Rematerializing actor because parameters changed"),r||(this.remove(e),await this.materializeAndInitActor(e)))}}else if("shape_mesh"===e.type){const a=await this.createMeshByShape(e.shape,t.material,e.shapeParams);t instanceof re&&(t.geometry=a.geometry,t.collisionShape=a.collisionShape)}("asset_mesh"===e.type||"shape_mesh"===e.type&&"landscape"!==e.shape)&&ve(t,e.castShadow,e.receiveShadow),e.name&&e.name.length>0&&(t.name=e.name),this.updated$.next({object:t,source:e})}else{const t=await this.materializeAndInitActor(e);this.updated$.next({object:t,source:e})}var a}async materializeAndInitActor(e,t=this.findParent(e)){console.log("materialize actor and init");const a=await this.materialize(e,t);return Ne(e,(async e=>{if("actor"===e.type){const t=this.materializedActors.get(e.id);null!=t?await this.initActorsPostInit([t]):console.error(`Something went wrong when creating actor ${e.id}`)}})),a}findParent(e){const t=this.dataProvider.getObjects().flatMap((t=>t.id===e.id?null:Q(t,(t=>t.children?.some((t=>t.id===e.id))),(()=>!0))))[0];return null==t?this.scene:null!=t?K(this.scene,(e=>e.userData?.src?.id===t.id),(e=>null!=e.userData?.src))[0]:void 0}fixFogColor(){!0===this.renderingView.options.enableOutlines&&(this.scene.fog.color=new s(this.scene.fog.color))}findMeshWithGeometry(e){let t;return e.traverse((e=>{e instanceof h&&e.geometry&&(t=e)})),t}applyVertexMaterials(e,t){if(null==e.vertexMaterials||0===e.vertexMaterials.length)return;const a=Y(e.vertexMaterials,(e=>e.m));t.traverse((e=>{if(e instanceof h){const t=me(e,!1);if(null!=t){for(let e=0;e<t.array.length;e++)t.setX(e,0);t.needsUpdate=!0}}}));const s=new Set;for(const[e,r]of a.entries()){const a=null!=e?t.getObjectByName(e):this.findMeshWithGeometry(t);let i=!1;if(null==a)return void console.warn(`Failed to apply vertex materials on mesh with name "${e}"`);const n=me(a,!0);for(let e=0;e<n.array.length;e++)n.setX(e,0);for(const e of r)n.setX(e.i,e.w[0]),n.setY(e.i,e.w[1]),n.setZ(e.i,e.w[2]),i=!0;i&&s.add(e)}this.inEditor&&this.landscapeManagers.filter((t=>t.source.id===e.id)).forEach((e=>e.queueRefreshScatter(this.renderingView.camera.position,!0,(e=>s.has(e.name)))))}async materialize(e,t,s=!1){let r;switch(e.type){case"asset_mesh":r=await this.createFromAsset(e);break;case"shape_mesh":r=await this.createFromShape(e);break;case"light":r=await this.createLight(e);break;case"particles":r=await this.createParticleSystem(e),e.collisionDetection=!1;break;case"global_fog":this.scene.fog=Ie(e.fog),this.fixFogColor(),r=new o;break;case"sky":this.sky=ge(),this.updateSky(e),r=this.sky;break;case"actor":r=await this.createFromActor(e);break;case"group":r=new o;break;case"prefab":r=await this.createFromPrefabAsset(e);break;case"vfx":r=await this.createFromVfx(e);break;default:if(this.inEditor)throw new Error("unknown type "+e.type);console.warn(`Failed to materialize object. Unknown type '${e.type}'. This might be because the hology/core library is not compatible with the editor version.`)}if(null!=r){if(e.name&&e.name.length>0&&(r.name=e.name),null!=e.position&&r.position.fromArray(e.position),null!=e.scale&&r.scale.fromArray(e.scale),null!=e.rotation&&r.rotation.fromArray(e.rotation),s||(r.userData.src=e),this.inEditor,this.inEditor){let e=null;r instanceof re&&(e=function(e){if(e instanceof se)return new h(new a(...e.offset.toArray()),De);return null}(r.collisionShape)),null!=e&&(e.layers.disable(0),e.layers.enable(18),e.scale.multiplyScalar(1.1),r.add(e))}return this.objectMap.set(r.uuid,e),this.sceneObjectMap.set(e.id,r),e.physics?.type!==U.dynamic||null==t||this.inEditor?null==t?this.scene.add(r):t?.add(r):(t.add(r),r.getWorldPosition(r.position),r.getWorldQuaternion(r.quaternion),r.getWorldScale(r.scale),this.scene?.attach(r)),null!=e.children&&await Promise.all(e.children?.map((e=>this.materialize(e,r,s)))),this.inEditor||"asset_mesh"!=e.type&&"shape_mesh"!==e.type||null!=e.physics?.type&&e.physics.type==U.dynamic||Pe(r),r}}async updateSky(e){if(null==e?.sky?.materialId)return void(this.sky.material=we);const a=await this.assetsService.getAsset(e.sky.materialId),s=await materialFromAsset(a,this.renderingView,this.assetsService,this.assetManagerService,this.shaders,!1);s.side=t.BackSide,null!=this.sky?this.sky.material=s:console.warn("No sky has been created")}async createComponent(e,t,a,s){const r=new Se[a.path+"/"+a.className],i=t.id+s;r.id=i,r.object=e;for(const e of a.params)null!=e.value&&(r[e.name]=e.value);return this.components.push(r),i}async createFromActor(e){const t=this.actorTypes.find((t=>t.name===e.actor?.type))?.type??L[e.actor?.type];if(null==t)return null;this.inEditor&&this.editorActorParamSnapshot.set(e.id,JSON.stringify(e.actor));const a=await this.actorProvider.create(t,(new v).fromArray(e.position),(new r).fromArray(e.rotation),!0);return this.materializedActors.set(e.id,a),a?.object}async createFromVfx(e){const t=await this.assetsService.getAsset(e.assetId);null==t&&console.error("Could not find asset",e);const a=await this.actorProvider.create(B,(new v).fromArray(e.position),(new r).fromArray(e.rotation),!1);return await a.fromAsset(t),a.play(),this.materializedActors.set(e.id,a),a?.object}cleanup(){this.materializedActors.clear()}async createFromShape(e){const t=this.inEditor&&e.hidden;let a;if("landscape"==e.shape)a=this.createLandscape(e),a.traverse((e=>{e instanceof h&&this._originalMaterials.set(e.id,e.material)}));else{let r=new d({name:"Default",color:new s("#aaaaaa"),visible:this.inEditor||!e.hidden,wireframe:!!t});const i=await this.createMeshByShape(e.shape,r,e.shapeParams);i.castShadow=e.castShadow??!0,i.receiveShadow=e.castShadow??!1,e.collisionDetection||(i.collisionShape=null),i.physics=e.physics,a=i,this._originalMaterials.set(a.id,i.material),a.traverse((e=>{}))}return t||(await Promise.all((e.materialAssignments??[]).filter((e=>null!=e.materialId)).map((e=>this.applyMaterial(a,e)))),this.applyVertexMaterials(e,a)),a}createLandscape(e){const t=e.landscape?.options;if(null==t)return console.error(`No landscape options exist on scene object ${e.id} ${e.name}`),new o;const a=ne(e.landscape.options);this.applyHeightMaps(a,e.landscape.heightMaps,!0);const s=new ie(e,this.renderingView,a,this.assetManagerService,this.assetsService,this.shaders,(t=>{(e.materialAssignments??[]).filter((e=>null!=e.materialId)).forEach((e=>this.applyMaterial(t,e)))}));return this.landscapeManagers.push(s),s.refreshGeometry(),a}applyHeightMaps(e,t,a=!1){const s=new oe(e.sections);for(const e of t??[]){const t=s.find(e.x,e.y);if(!t)return;const a=t.geometry.getAttribute("position");for(const t of e.points)a.setY(t.i,t.y);a.needsUpdate=!0}const r=e.sections;r.forEach((e=>{e.geometry.computeBoundsTree(),e.geometry.computeVertexNormals()})),this.inEditor&&!a||setTimeout((()=>le(r)),50)}async createMeshByShape(e,t,a={}){if("landscape"!==e&&fe.includes(e)){const s=await prepareShapeParameters(a??{}),r=e+JSON.stringify(a);return this.geometryCache.has(r)||this.geometryCache.set(r,ue[e].geometry(s)),this.collisionShapeCache.has(r)||this.collisionShapeCache.set(r,ue[e].collision(s)),new re(this.geometryCache.get(r),t,this.collisionShapeCache.get(r))}if(this.inEditor)throw new Error(`Unsupported shape '${e}'`);console.warn(`Failed to create shape. Unsupported shape '${e}'. This might be because the hology/core library is not compatible with the editor version.`)}async createFromAsset(e){const t=await this.assetsService.getAsset(e.assetId);if(null==t)return void console.warn(`Can not find asset with id ${e.assetId} and name ${e.name}`);let{scene:a}=await this.assetManagerService.getMesh(t);Ee(e.materialAssignments,t.materialAssignments).forEach((e=>this.applyMaterial(a,e)));const s=e.receiveShadow??!!t.receiveShadow??!0,r=e.castShadow??!!t.castShadow??!1;return a.receiveShadow=s,ve(a,r,s),e.collisionDetection||(a.collisionShapes=[]),null!=e.physics&&!0!==this.inEditor&&(a.physics=e.physics),this.applyVertexMaterials(e,a),a.traverse((e=>{e instanceof h&&"computeBoundsTree"in e.geometry&&null==e.geometry.boundsTree&&e.geometry.computeBoundsTree()})),a}async createFromPrefabAsset(e){const t=await this.assetsService.getAsset(e.assetId);if(null==t)return void console.warn(`Can not find asset with id ${e.assetId} and name ${e.name}`);const a=new o;return t.prefab.objects.filter((e=>"global_fog"!==e.type)).forEach((e=>this.materialize(e,a,!0))),a}async createParticleSystem(e){const a=await this.assetsService.getAsset(e.assetId),s=new u;return await b.fromJSONAsync(a.particleSystem,t).then((e=>{const a=new M(s,t);e.addRenderer(a),this.renderingView.onLoop((t=>e.update()))})),s}async createLight(e){if("point"===e.light.type){const t=new f(e.light.point.color,e.light.point.intensity,e.light.point.distance,e.light.point.decay);if(t.castShadow=e.light.point.castShadow??!0,this.inEditor){const e=new g(.3,10,10),a=new d({color:new s(16771709)}),r=new h(e,a);t.add(r)}return t}return"directional"===e.light.type?(this.applyDirectionalLight(e.light.directional),new o):"ambient"===e.light.type?(this.applyDirectionalAmbientLight(null,e.light.ambient),new o):void 0}applyDirectionalAmbientLight(e,t){const a=this.scene.children.find((e=>e.name===ye));null!=a?(a.intensity=t.intensity,a.color.set(t.color),a.groundColor.set(t.color)):console.warn("Couldn't find ambient light")}applyDirectionalLight(e){for(const t of this.renderingView.csm.lights)t.intensity=e.intensity,t.color.set(e.color),t.castShadow=e.castShadow;this.renderingView.csm.lightDirection.fromArray(e.direction).normalize()}dispose(){this.updateSubscription.unsubscribe(),this.createAssetSubscription.unsubscribe(),this.materializedActors.forEach((e=>e.disposed.next(!0)))}}function ve(e,t,a){e.castShadow=t,e.receiveShadow=a,e.traverse((e=>{e.castShadow=t,e.receiveShadow=a}))}const Ae=new Map,be=new m({color:16711935}),Me=new Map;export async function materialFromAsset(e,a,r,i,n,o=!0){const l=JSON.stringify(e.material);if(o&&Ae.has(l))return Ae.get(l);const c={opacity:e.material.params.opacity,map:null,emissive:e.material.params.emissive??null,metalness:e.material.params.metalness??0,flatShading:e.material.params.flatShading??!1,color:new s(e.material.params.color),transparent:null!=e.material.params.opacity&&e.material.params.opacity<1},h={};if(null!=e.material.params.map){const t=e.material.params.map,a=await r.getAsset(t);null!=a&&(c.map=await i.getTexture(a))}let m;switch(e.material.type){case"phong":m=new p({...c,...h});break;case"water":m=pe(c,a);break;case"grassFoliage":m=ce({color:c.color,map:c.map},a);break;case"grass":m=he({...c,colorTwo:new s(e.material.params.colorTwo),colorThree:new s(e.material.params.colorThree)},a);break;case"standard":case"unlit":case"lambert":case"shader":const t={standard:H,lambert:J,unlit:q}[e.material.type]??n.find((t=>t.name==e.material.shader))?.type;if(t){const s=new t,o=await xe(e.material?.shaderParams??{},t,r,i,null,a,n);Object.assign(s,o);try{m=s.build()}catch(t){console.log("Shader runtime error: "+t),Me.has(e.material.shader)||Me.set(e.material.shader,be.clone()),m=Me.get(e.material.shader)}m.userData.customShaderName=e.material.shader}else console.warn("Missing shader implementation with name "+e.material.shader),m=be;break;default:throw new Error("Unsupported material type"+e.material.type)}return a?.csm.setupMaterial(m),o&&Ae.set(l,m),m.side=e.material.side??m.side??t.FrontSide,m.transparent=(e.material.transparent??c.transparent??!1)||m.transparent,e.material.bloom&&(m.userData.hasBloom=!0),m.userData.assetId=e.id,m}async function xe(e,t,a,s,r,i,n,o){const l={};for(const[t,c]of Object.entries(e)){const e=await je(c,a,s,r,i,n,o);null!=e&&(l[t]=e)}return l}export async function prepareShapeParameters(e){const t={};for(const[a,s]of Object.entries(e)){const e=await je(s,null,null,null);null!=e&&(t[a]=e)}return t}async function je(e,t,a,i,n,o,l){if(de.String,null==e||null==e.value||""==e.value)return null;const c=e.value;switch(e.type){case de.Number:case de.FloatNode:let h="string"==typeof c?parseFloat(c):c;return e.type===de.FloatNode?P(h):h;case de.Texture:return await a.getTexture(await t.getAsset(c));case de.Sampler2DNode:return C(await a.getTexture(await t.getAsset(c)));case de.Boolean:return c;case de.BooleanNode:return x(c);case de.Vector2:case de.Vec2Node:if("object"==typeof c){const t=c instanceof Array?(new S).fromArray(c):new S(c.x,c.y);return e.type===de.Vec2Node?F(t):t}return null;case de.Vector3:case de.Vec3Node:if("object"==typeof c){const t=c instanceof Array?(new v).fromArray(c):new v(c.x,c.y,c.z);return e.type===de.Vec3Node?z(t):t}return null;case de.Color:case de.RgbNode:const m=new s(c);return e.type===de.RgbNode?N(m):m;case de.String:return c;case de.BaseActor:const p=c;return null==i&&console.warn("Class parameters can not be prepared as actors are not passed in"),i?.get(p);case de.Euler:const d=c;return(new r).fromArray(d);case de.Object3D:return(await a.getMesh(await t.getAsset(c))).scene;case de.Material:return await materialFromAsset(await t.getAsset(c),n,t,a,o);case de.AudioBuffer:return await a.getAudio(await t.getAsset(c));case de.VisualEffect:const u=await t.getAsset(c);if(null==l){console.error("Can not create instance of visual effect because missing actor provider");break}if("vfx"in u)return new T(l,u);console.error("Using a non-vfx asset for visual effect parameter")}return null}function Pe(e){e.updateWorldMatrix(!1,!0),e.traverse((e=>{e.matrixAutoUpdate=!1,e.matrixWorldNeedsUpdate=!1}));const t=e.updateMatrixWorld;e.updateMatrixWorld=function(){t.apply(e),e.updateMatrixWorld=function(){}}}function Ie(e){return"linear"===e.type?new i(new s(e.color),e.near??100,e.far??1e3):"density"===e.type?new n(e.color,e.density):void console.warn("Invalid fog type",e)}const De=new d({color:4229780});async function Ne(e,a,s,i){null==i&&(i=(new c).identity()),await a(e,s,i);const n=i.clone().multiply(function(e,t){if(null==e.position||null==e.rotation||null==e.scale)return t.identity();return t.compose((new v).fromArray(e.position),(new y).setFromEuler((new r).fromArray(e.rotation)),(new v).fromArray(e.scale))}(e,new t.Matrix4));return Promise.all((e.children??[]).map((t=>Ne(t,a,e,n))))}export function toSerializedParamType(e){const t=e.constructor.prototype;return t instanceof Number||e===Number?de.Number:t instanceof I||"function"==typeof e.prototype.isFloat?de.FloatNode:t instanceof w||e===w||e.isTexture?de.Texture:t instanceof W||e===V?de.Sampler2DNode:t instanceof Boolean||e===Boolean?de.Boolean:t instanceof j?de.BooleanNode:t instanceof s||e==s?de.Color:t instanceof E||"function"==typeof e.prototype.isRgb?de.RgbNode:t instanceof S||e==S?de.Vector2:t instanceof k||"function"==typeof e.prototype.isVec2?de.Vec2Node:t instanceof v||e==v?de.Vector3:t instanceof O||"function"==typeof e.prototype.isVec3?de.Vec3Node:t instanceof String||e===String?de.String:t instanceof $||e==$||e.prototype instanceof $||e.prototype==$?de.BaseActor:t instanceof r||e==r?de.Euler:t instanceof u||e==u?de.Object3D:t instanceof l||e==l?de.Material:t instanceof AudioBuffer||e==AudioBuffer?de.AudioBuffer:t instanceof T||e==T?de.VisualEffect:void console.warn("Failed to map parameter type to serialized version",{type:e})}export function prepareCustomParams(e,t,a={}){return Object.fromEntries(e.map((e=>[e.name,{type:toSerializedParamType(e.type),value:t[e.name]?.value??a[e.name]??customParameterDefaultValueByType.get(toSerializedParamType(e.type))}])))}export function prepareCustomParamsFromType(e,t,a=null){const s=X(e);if(0===s.length)return{};let r;null!=a?R(a,(()=>{r=a.get(e)})):r=new e;const i={};for(const e of s){const t=r[e.name];if(null!=t){const a=serializeCustomParameter(e.type,t);null!=a&&(i[e.name]=a)}}return prepareCustomParams(s,t,i)}export function serializeCustomParameter(e,t){function a(){console.error("Failed to serialize value",{type:e,value:t})}switch(e){case Number:case Boolean:return t;case S:return t instanceof S?t.toArray():void a();case v:return t instanceof v?t.toArray():void a();case A:return t instanceof A?t.toArray():void a();case s:return t instanceof s?"#"+t.getHexString():"string"==typeof t?t:"number"==typeof t?"#"+new s(t).getHexString():void a();case String:return t;case r:return t instanceof r?t.toArray():void a()}}function Ee(e,t){return function(e,t,a){const s=[],r=new Set;for(const i of[...e??[],...t??[]]){const e=a(i);r.has(e)||(r.add(e),s.push(i))}return s}((e??[]).filter((e=>Ve(e.materialId))),(t??[]).filter((e=>Ve(e.materialId))),(e=>e.color+e.name))}function Ve(e){return"null"!=e&&null!=e}export const customParameterDefaultValueByType=new Map([[de.RgbNode,"#000000"],[de.Color,"#000000"],[de.Vector4,[0,0,0,0]],[de.Vec4Node,[0,0,0,0]],[de.Vector3,[0,0,0]],[de.Vec3Node,[0,0,0]],[de.Vector2,[0,0]],[de.Vec2Node,[0,0]],[de.Euler,[0,0,0,"XYZ"]]]);export function applyMaterial(e,a,r,i){const n=[];return e.traverse((async e=>{if(e instanceof h||e.isMesh||e instanceof t.SkinnedMesh||e.isSkinnedMesh)for(const t of Z(e.material))t.hasOwnProperty("color")&&n.push(e)})),Promise.all(n.map((async e=>{if(e.material instanceof Array)for(let t=0;t<e.material.length;t++){const n=e.material[t];if(null==n.color||!(n.color instanceof s))continue;const o="#"+n.color.getHexString(),l=n.name;if(o===a.color&&(n.name===a.name||null==a.name)||e.userData["originalColor_"+t]===a.color&&e.userData["originalMaterialName_"+t]===a.name){const s=await r(a.materialId),n=e.material[t];null!=s&&(e.material[t]=s,e.userData["originalColor_"+t]=e.userData["originalColor_"+t]??o,e.userData["originalMaterialName_"+t]=e.userData["originalMaterialName_"+t]??l,null!=i&&i.set(e.id+"#"+t,n))}}else if("color"in e.material){const t="#"+e.material.color.getHexString(),s=e.material.name;if(t===a.color&&(e.material.name===a.name||null==a.name)||e.userData.originalColor===a.color&&e.userData.originalName===a.name){const n=await r(a.materialId),o=e.material;null!=n&&(e.material=n,e.userData.originalColor=e.userData.originalColor??t,e.userData.originalMaterialName=e.userData.originalMaterialName??s,null!=i&&(i.has(e.id)||i.set(e.id,o)))}}})))}function Ce(e,a){if(e instanceof t.ShaderMaterial&&a instanceof t.ShaderMaterial){return e.fragmentShader+e.vertexShader==a.fragmentShader+a.vertexShader&&function(e,a){if(e instanceof t.ShaderMaterial&&a instanceof t.ShaderMaterial){for(const t in e.uniforms){if(null==a.uniforms[t])return!1;if(a.uniforms[t].value!==e.uniforms[t].value)return console.log("Different values",a.uniforms[t].value,e.uniforms[t].value),!1}return!0}return!1}(e,a)}return!1}
2
2
  /*
3
3
  * Copyright (©) 2023. All rights reserved.
4
4
  * See the LICENSE.md file for details.
@@ -85,7 +85,7 @@ export interface Asset {
85
85
  export type VfxAsset = Asset & {
86
86
  vfx: VfxAssetData;
87
87
  };
88
- export type MaterialType = 'standard' | 'phong' | 'lambert' | 'shader' | 'water' | 'grass' | 'grassFoliage';
88
+ export type MaterialType = 'standard' | 'phong' | 'lambert' | 'shader' | 'water' | 'grass' | 'grassFoliage' | 'unlit';
89
89
  export type MaterialParameters = {
90
90
  opacity: number;
91
91
  transparent: boolean;
@@ -1,2 +1,3 @@
1
1
  export * from './standard-shader.js';
2
2
  export * from './lambert-shader.js';
3
+ export * from './unlit-shader.js';
@@ -1,4 +1,4 @@
1
- export*from"./standard-shader.js";export*from"./lambert-shader.js";
1
+ export*from"./standard-shader.js";export*from"./lambert-shader.js";export*from"./unlit-shader.js";
2
2
  /*
3
3
  * Copyright (©) 2023. All rights reserved.
4
4
  * See the LICENSE.md file for details.
@@ -2,6 +2,7 @@ import { Color, Material, Texture } from 'three';
2
2
  import { Shader } from '../shader.js';
3
3
  export declare class LambertShader extends Shader {
4
4
  color: Color;
5
+ opacity: number;
5
6
  emissive?: Color;
6
7
  emissiveIntensity?: number;
7
8
  emissiveMap?: Texture;
@@ -1,4 +1,4 @@
1
- import{__decorate as e,__metadata as t}from"tslib";import{Color as i,Texture as r,MeshLambertMaterial as a}from"three";import{Shader as p}from"../shader.js";import{Parameter as o}from"../parameter.js";import{removeObjectUndefined as s}from"../../utils/collections.js";export class LambertShader extends p{constructor(){super(...arguments),this.color=new i("#FFFFFF")}build(){return new a(s({color:this.color,emissive:this.emissive,emissiveIntensity:this.emissiveIntensity,emissiveMap:this.emissiveMap,map:this.map,lightMap:this.lightMap,lightMapIntensity:this.lightMapIntensity,aoMap:this.aoMap,aoMapIntensity:this.aoMapIntensity,specularMap:this.specularMap,alphaMap:this.alphaMap,envMap:this.envMap,reflectivity:this.reflectivity,refractionRatio:this.refractionRatio}))}}e([o(),t("design:type",i)],LambertShader.prototype,"color",void 0),e([o(),t("design:type",i)],LambertShader.prototype,"emissive",void 0),e([o({range:[0,1]}),t("design:type",Number)],LambertShader.prototype,"emissiveIntensity",void 0),e([o(),t("design:type",r)],LambertShader.prototype,"emissiveMap",void 0),e([o(),t("design:type",r)],LambertShader.prototype,"map",void 0),e([o(),t("design:type",r)],LambertShader.prototype,"lightMap",void 0),e([o(),t("design:type",Number)],LambertShader.prototype,"lightMapIntensity",void 0),e([o(),t("design:type",r)],LambertShader.prototype,"aoMap",void 0),e([o(),t("design:type",Number)],LambertShader.prototype,"aoMapIntensity",void 0),e([o(),t("design:type",r)],LambertShader.prototype,"specularMap",void 0),e([o(),t("design:type",r)],LambertShader.prototype,"alphaMap",void 0),e([o(),t("design:type",r)],LambertShader.prototype,"envMap",void 0),e([o({range:[0,1]}),t("design:type",Number)],LambertShader.prototype,"reflectivity",void 0),e([o({range:[0,1]}),t("design:type",Number)],LambertShader.prototype,"refractionRatio",void 0);
1
+ import{__decorate as e,__metadata as t}from"tslib";import{Color as i,Texture as r,MeshLambertMaterial as a}from"three";import{Shader as p}from"../shader.js";import{Parameter as o}from"../parameter.js";import{removeObjectUndefined as s}from"../../utils/collections.js";export class LambertShader extends p{constructor(){super(...arguments),this.color=new i("#FFFFFF"),this.opacity=1}build(){return new a(s({color:this.color,opacity:this.opacity,emissive:this.emissive,emissiveIntensity:this.emissiveIntensity,emissiveMap:this.emissiveMap,map:this.map,lightMap:this.lightMap,lightMapIntensity:this.lightMapIntensity,aoMap:this.aoMap,aoMapIntensity:this.aoMapIntensity,specularMap:this.specularMap,alphaMap:this.alphaMap,envMap:this.envMap,reflectivity:this.reflectivity,refractionRatio:this.refractionRatio}))}}e([o(),t("design:type",i)],LambertShader.prototype,"color",void 0),e([o({range:[0,1]}),t("design:type",Number)],LambertShader.prototype,"opacity",void 0),e([o(),t("design:type",i)],LambertShader.prototype,"emissive",void 0),e([o({range:[0,1]}),t("design:type",Number)],LambertShader.prototype,"emissiveIntensity",void 0),e([o(),t("design:type",r)],LambertShader.prototype,"emissiveMap",void 0),e([o(),t("design:type",r)],LambertShader.prototype,"map",void 0),e([o(),t("design:type",r)],LambertShader.prototype,"lightMap",void 0),e([o(),t("design:type",Number)],LambertShader.prototype,"lightMapIntensity",void 0),e([o(),t("design:type",r)],LambertShader.prototype,"aoMap",void 0),e([o(),t("design:type",Number)],LambertShader.prototype,"aoMapIntensity",void 0),e([o(),t("design:type",r)],LambertShader.prototype,"specularMap",void 0),e([o(),t("design:type",r)],LambertShader.prototype,"alphaMap",void 0),e([o(),t("design:type",r)],LambertShader.prototype,"envMap",void 0),e([o({range:[0,1]}),t("design:type",Number)],LambertShader.prototype,"reflectivity",void 0),e([o({range:[0,1]}),t("design:type",Number)],LambertShader.prototype,"refractionRatio",void 0);
2
2
  /*
3
3
  * Copyright (©) 2023. All rights reserved.
4
4
  * See the LICENSE.md file for details.
@@ -2,6 +2,7 @@ import { Color, Material, Texture, Vector2 } from 'three';
2
2
  import { Shader } from '../shader.js';
3
3
  export declare class StandardShader extends Shader {
4
4
  color: Color;
5
+ opacity: number;
5
6
  roughness?: number;
6
7
  metalness?: number;
7
8
  map?: Texture;
@@ -1,4 +1,4 @@
1
- import{__decorate as e,__metadata as t}from"tslib";import{Color as a,MeshStandardMaterial as p,Texture as i,Vector2 as d}from"three";import{Shader as r}from"../shader.js";import{Parameter as s}from"../parameter.js";import{removeObjectUndefined as n}from"../../utils/collections.js";export class StandardShader extends r{constructor(){super(...arguments),this.color=new a("#FFFFFF")}build(){return new p(n({color:this.color,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,bumpMap:this.bumpMap,bumpScale:this.bumpScale,normalMap:this.normalMap,normalScale:this.normalScale,displacementMap:this.displacementMap,displacementScale:this.displacementScale,displacementBias:this.displacementBias,roughnessMap:this.roughnessMap,metalnessMap:this.metalnessMap,alphaMap:this.alphaMap,envMap:this.envMap,envMapIntensity:this.envMapIntensity,refractionRatio:this.refractionRatio}))}}e([s(),t("design:type",a)],StandardShader.prototype,"color",void 0),e([s({range:[0,1]}),t("design:type",Number)],StandardShader.prototype,"roughness",void 0),e([s({range:[0,1]}),t("design:type",Number)],StandardShader.prototype,"metalness",void 0),e([s(),t("design:type",i)],StandardShader.prototype,"map",void 0),e([s(),t("design:type",i)],StandardShader.prototype,"lightMap",void 0),e([s(),t("design:type",Number)],StandardShader.prototype,"lightMapIntensity",void 0),e([s(),t("design:type",i)],StandardShader.prototype,"aoMap",void 0),e([s(),t("design:type",Number)],StandardShader.prototype,"aoMapIntensity",void 0),e([s(),t("design:type",a)],StandardShader.prototype,"emissive",void 0),e([s({range:[0,1]}),t("design:type",Number)],StandardShader.prototype,"emissiveIntensity",void 0),e([s(),t("design:type",i)],StandardShader.prototype,"emissiveMap",void 0),e([s(),t("design:type",i)],StandardShader.prototype,"bumpMap",void 0),e([s(),t("design:type",Number)],StandardShader.prototype,"bumpScale",void 0),e([s(),t("design:type",i)],StandardShader.prototype,"normalMap",void 0),e([s(),t("design:type",d)],StandardShader.prototype,"normalScale",void 0),e([s(),t("design:type",i)],StandardShader.prototype,"displacementMap",void 0),e([s(),t("design:type",Number)],StandardShader.prototype,"displacementScale",void 0),e([s(),t("design:type",Number)],StandardShader.prototype,"displacementBias",void 0),e([s(),t("design:type",i)],StandardShader.prototype,"roughnessMap",void 0),e([s(),t("design:type",i)],StandardShader.prototype,"metalnessMap",void 0),e([s(),t("design:type",i)],StandardShader.prototype,"alphaMap",void 0),e([s(),t("design:type",i)],StandardShader.prototype,"envMap",void 0),e([s({range:[0,1]}),t("design:type",Number)],StandardShader.prototype,"envMapIntensity",void 0),e([s({range:[0,1]}),t("design:type",Number)],StandardShader.prototype,"refractionRatio",void 0);
1
+ import{__decorate as e,__metadata as t}from"tslib";import{Color as a,MeshStandardMaterial as p,Texture as i,Vector2 as d}from"three";import{Shader as r}from"../shader.js";import{Parameter as s}from"../parameter.js";import{removeObjectUndefined as o}from"../../utils/collections.js";export class StandardShader extends r{constructor(){super(...arguments),this.color=new a("#FFFFFF"),this.opacity=1}build(){return new p(o({color:this.color,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,bumpMap:this.bumpMap,bumpScale:this.bumpScale,normalMap:this.normalMap,normalScale:this.normalScale,displacementMap:this.displacementMap,displacementScale:this.displacementScale,displacementBias:this.displacementBias,roughnessMap:this.roughnessMap,metalnessMap:this.metalnessMap,alphaMap:this.alphaMap,envMap:this.envMap,envMapIntensity:this.envMapIntensity,refractionRatio:this.refractionRatio}))}}e([s(),t("design:type",a)],StandardShader.prototype,"color",void 0),e([s({range:[0,1]}),t("design:type",Number)],StandardShader.prototype,"opacity",void 0),e([s({range:[0,1]}),t("design:type",Number)],StandardShader.prototype,"roughness",void 0),e([s({range:[0,1]}),t("design:type",Number)],StandardShader.prototype,"metalness",void 0),e([s(),t("design:type",i)],StandardShader.prototype,"map",void 0),e([s(),t("design:type",i)],StandardShader.prototype,"lightMap",void 0),e([s(),t("design:type",Number)],StandardShader.prototype,"lightMapIntensity",void 0),e([s(),t("design:type",i)],StandardShader.prototype,"aoMap",void 0),e([s(),t("design:type",Number)],StandardShader.prototype,"aoMapIntensity",void 0),e([s(),t("design:type",a)],StandardShader.prototype,"emissive",void 0),e([s({range:[0,1]}),t("design:type",Number)],StandardShader.prototype,"emissiveIntensity",void 0),e([s(),t("design:type",i)],StandardShader.prototype,"emissiveMap",void 0),e([s(),t("design:type",i)],StandardShader.prototype,"bumpMap",void 0),e([s(),t("design:type",Number)],StandardShader.prototype,"bumpScale",void 0),e([s(),t("design:type",i)],StandardShader.prototype,"normalMap",void 0),e([s(),t("design:type",d)],StandardShader.prototype,"normalScale",void 0),e([s(),t("design:type",i)],StandardShader.prototype,"displacementMap",void 0),e([s(),t("design:type",Number)],StandardShader.prototype,"displacementScale",void 0),e([s(),t("design:type",Number)],StandardShader.prototype,"displacementBias",void 0),e([s(),t("design:type",i)],StandardShader.prototype,"roughnessMap",void 0),e([s(),t("design:type",i)],StandardShader.prototype,"metalnessMap",void 0),e([s(),t("design:type",i)],StandardShader.prototype,"alphaMap",void 0),e([s(),t("design:type",i)],StandardShader.prototype,"envMap",void 0),e([s({range:[0,1]}),t("design:type",Number)],StandardShader.prototype,"envMapIntensity",void 0),e([s({range:[0,1]}),t("design:type",Number)],StandardShader.prototype,"refractionRatio",void 0);
2
2
  /*
3
3
  * Copyright (©) 2023. All rights reserved.
4
4
  * See the LICENSE.md file for details.
@@ -0,0 +1,18 @@
1
+ import { Color, Material, Texture } from 'three';
2
+ import { Shader } from '../shader.js';
3
+ export declare class UnlitShader extends Shader {
4
+ color: Color;
5
+ opacity: number;
6
+ map?: Texture;
7
+ lightMap?: Texture;
8
+ lightMapIntensity?: number;
9
+ aoMap?: Texture;
10
+ aoMapIntensity?: number;
11
+ specularMap?: Texture;
12
+ alphaMap?: Texture;
13
+ envMap?: Texture;
14
+ combine?: number;
15
+ reflectivity?: number;
16
+ refractionRatio?: number;
17
+ build(): Material;
18
+ }
@@ -0,0 +1,5 @@
1
+ import{__decorate as t,__metadata as e}from"tslib";import{AddOperation as i,MultiplyOperation as o,MixOperation as p,Color as a,MeshBasicMaterial as r,Texture as n}from"three";import{Shader as s}from"../shader.js";import{Parameter as d}from"../parameter.js";import{removeObjectUndefined as l}from"../../utils/collections.js";export class UnlitShader extends s{constructor(){super(...arguments),this.color=new a("#FFFFFF"),this.opacity=1}build(){return new r(l({color:this.color,opacity:this.opacity,map:this.map,lightMap:this.lightMap,lightMapIntensity:this.lightMapIntensity,aoMap:this.aoMap,aoMapIntensity:this.aoMapIntensity,specularMap:this.specularMap,alphaMap:this.alphaMap,envMap:this.envMap,combine:this.combine,reflectivity:this.reflectivity,refractionRatio:this.refractionRatio}))}}t([d(),e("design:type",a)],UnlitShader.prototype,"color",void 0),t([d({range:[0,1]}),e("design:type",Number)],UnlitShader.prototype,"opacity",void 0),t([d(),e("design:type",n)],UnlitShader.prototype,"map",void 0),t([d(),e("design:type",n)],UnlitShader.prototype,"lightMap",void 0),t([d(),e("design:type",Number)],UnlitShader.prototype,"lightMapIntensity",void 0),t([d(),e("design:type",n)],UnlitShader.prototype,"aoMap",void 0),t([d(),e("design:type",Number)],UnlitShader.prototype,"aoMapIntensity",void 0),t([d(),e("design:type",n)],UnlitShader.prototype,"specularMap",void 0),t([d(),e("design:type",n)],UnlitShader.prototype,"alphaMap",void 0),t([d(),e("design:type",n)],UnlitShader.prototype,"envMap",void 0),t([d({options:[{name:"Mix",value:p},{name:"Multiply",value:o},{name:"Add",value:i}]}),e("design:type",Number)],UnlitShader.prototype,"combine",void 0),t([d({range:[0,1]}),e("design:type",Number)],UnlitShader.prototype,"reflectivity",void 0),t([d({range:[0,1]}),e("design:type",Number)],UnlitShader.prototype,"refractionRatio",void 0);
2
+ /*
3
+ * Copyright (©) 2023. All rights reserved.
4
+ * See the LICENSE.md file for details.
5
+ */
@@ -0,0 +1,5 @@
1
+ import { FloatNode, Vec2Node } from "three-shader-graph";
2
+ /**
3
+ * Create a distortion such that the center is has a smaller is expanded
4
+ */
5
+ export declare function bulge(uv: Vec2Node, center: Vec2Node, power?: number | FloatNode): Vec2Node;
@@ -0,0 +1,5 @@
1
+ import{length as r,pow as t}from"three-shader-graph";export function bulge(e,a,l=1){let o=e;o=e.subtract(a);const u=r(o),c=1===l?u:t(u,l);return o=o.multiplyScalar(c),o=e.add(a),o}
2
+ /*
3
+ * Copyright (©) 2023. All rights reserved.
4
+ * See the LICENSE.md file for details.
5
+ */
@@ -1,4 +1,4 @@
1
- import{Vector2 as e}from"three";import{dot as t,FloatExpressionNode as o,FloatNode as r,texture2d as n,uniformVec2 as s,Vec2ExpressionNode as a,Vec4Node as c,uniformSampler2d as p,uniformFloat as i}from"three-shader-graph";export function supportsDepthTextureExtension(e){return!!e.extensions.get("WEBGL_depth_texture")}export const depthUniformName="hology_depth_map";export const resolutionUniformName="hology_resolution";export const nearUniformName="hology_camera_near";export const farUniformName="hology_camera_far";export const depthSampler=p(depthUniformName);const h=i(nearUniformName,.5),m=i(farUniformName,500);class l extends r{compile(e){const t=e.variable();var o=e.get(h),r=e.get(m);return{chunk:`\n float depth_${t} = 2.0 * ${o} * ${r} / (${r} + ${o} - (2.0 * ${e.get(this.depth)} - 1.0) * (${r} - ${o}));\n `,out:`depth_${t}`}}constructor(e){super(),this.depth=e}}function u(e){return new l(e)}const _=new class extends c{constructor(){super(...arguments),this.k="31u50"}compile(e){return{pars:`\n const float UnpackDownscale_${this.k} = 255. / 256.; // 0..1 -> fraction (excluding 1)\n const vec3 PackFactors_${this.k} = vec3( 256. * 256. * 256., 256. * 256., 256. );\n const vec4 UnpackFactors_${this.k} = UnpackDownscale_${this.k} / vec4( PackFactors_${this.k}, 1. );\n `,out:`UnpackFactors_${this.k}`}}};const d=new o("gl_FragCoord.z");export const fragmentLinearEyeDepth=u(d);const f=s("hology_resolution",new e(250,1e3)),g=new a("gl_FragCoord.xy").divide(f);export const linearEyeDepth=u((x=g,$=n(depthSampler,x),t($,_)));var x,$;
1
+ import{Vector2 as e}from"three";import{dot as t,FloatExpressionNode as o,FloatNode as r,texture2d as n,uniformVec2 as a,Vec2ExpressionNode as s,Vec4Node as c,uniformSampler2d as p,uniformFloat as i}from"three-shader-graph";import*as h from"three";export function supportsDepthTextureExtension(e){return!!e.extensions.get("WEBGL_depth_texture")}export const depthUniformName="hology_depth_map";export const resolutionUniformName="hology_resolution";export const nearUniformName="hology_camera_near";export const farUniformName="hology_camera_far";export const depthSampler=p(depthUniformName,new h.DepthTexture(1,1));const m=i(nearUniformName,.5),l=i(farUniformName,500);class u extends r{compile(e){const t=e.variable();var o=e.get(m),r=e.get(l);return{chunk:`\n float depth_${t} = 2.0 * ${o} * ${r} / (${r} + ${o} - (2.0 * ${e.get(this.depth)} - 1.0) * (${r} - ${o}));\n `,out:`depth_${t}`}}constructor(e){super(),this.depth=e}}function _(e){return new u(e)}const f=new class extends c{constructor(){super(...arguments),this.k="31u50"}compile(e){return{pars:`\n const float UnpackDownscale_${this.k} = 255. / 256.; // 0..1 -> fraction (excluding 1)\n const vec4 PackFactors_${this.k} = vec4( 1.0, 256.0, 256.0 * 256.0, 256.0 * 256.0 * 256.0 );\n const vec4 UnpackFactors_${this.k} = vec4( UnpackDownscale_${this.k} / PackFactors_${this.k}.rgb, 1.0 / PackFactors_${this.k}.a );\n `,out:`UnpackFactors_${this.k}`}}};const d=new o("gl_FragCoord.z");export const fragmentLinearEyeDepth=_(d);const g=a("hology_resolution",new e(250,1e3)),x=new s("gl_FragCoord.xy").divide(g);export const linearEyeDepth=_((k=x,$=n(depthSampler,k),t($,f)));var k,$;
2
2
  /*
3
3
  * Copyright (©) 2023. All rights reserved.
4
4
  * See the LICENSE.md file for details.
@@ -8,3 +8,4 @@ export * from './shapes.js';
8
8
  export * from './voronoi.js';
9
9
  export * from './effects.js';
10
10
  export * from './math.js';
11
+ export * from './bulge.js';
@@ -1,4 +1,4 @@
1
- export*from"three-shader-graph";export{linearEyeDepth,fragmentLinearEyeDepth,depthSampler}from"./depth.js";export{timeUniforms}from"./time.js";export{particleUniforms}from"./particle.js";export*from"./layers.js";export*from"./landscape.js";export*from"./shapes.js";export*from"./voronoi.js";export*from"./effects.js";export*from"./math.js";
1
+ export*from"three-shader-graph";export{linearEyeDepth,fragmentLinearEyeDepth,depthSampler}from"./depth.js";export{timeUniforms}from"./time.js";export{particleUniforms}from"./particle.js";export*from"./layers.js";export*from"./landscape.js";export*from"./shapes.js";export*from"./voronoi.js";export*from"./effects.js";export*from"./math.js";export*from"./bulge.js";
2
2
  /*
3
3
  * Copyright (©) 2023. All rights reserved.
4
4
  * See the LICENSE.md file for details.
@@ -1,4 +1,4 @@
1
- import e from"three/examples/jsm/libs/stats.module.js";class t extends e.Panel{constructor(e,t="GPU MS"){super(t,"#f90","#210");let i=!0,n=e.getExtension("EXT_disjoint_timer_query_webgl2");null===n&&(i=!1,n=e.getExtension("EXT_disjoint_timer_query"),null===n&&console.warn("GPUStatsPanel: disjoint_time_query extension not available.")),this.context=e,this.extension=n,this.maxTime=30,this.activeQueries=0,this.startQuery=function(){const e=this.context,t=this.extension;if(null===t)return;let n;i?(n=e.createQuery(),e.beginQuery(t.TIME_ELAPSED_EXT,n)):(n=t.createQueryEXT(),t.beginQueryEXT(t.TIME_ELAPSED_EXT,n)),this.activeQueries++;const s=()=>{let r,E,a;i?(r=e.getQueryParameter(n,e.QUERY_RESULT_AVAILABLE),E=e.getParameter(t.GPU_DISJOINT_EXT),a=e.getQueryParameter(n,e.QUERY_RESULT)):(r=t.getQueryObjectEXT(n,t.QUERY_RESULT_AVAILABLE_EXT),E=e.getParameter(t.GPU_DISJOINT_EXT),a=t.getQueryObjectEXT(n,t.QUERY_RESULT_EXT));const u=1e-6*a;r?(E||this.update(u,this.maxTime),this.activeQueries--):requestAnimationFrame(s)};requestAnimationFrame(s)},this.endQuery=function(){const e=this.extension,t=this.context;null!==e&&(i?t.endQuery(e.TIME_ELAPSED_EXT):e.endQueryEXT(e.TIME_ELAPSED_EXT))}}}export{t as GPUStatsPanel};
1
+ import e from"three/examples/jsm/libs/stats.module.js";class t extends e.Panel{constructor(e,t="GPU MS"){super(t,"#f90","#210");let i=!0,s=e.getExtension("EXT_disjoint_timer_query_webgl2");null===s&&(i=!1,s=e.getExtension("EXT_disjoint_timer_query"),null===s&&console.warn("GPUStatsPanel: disjoint_time_query extension not available.")),this.context=e,this.extension=s,this.maxTime=30,this.activeQueries=0,this.startQuery=function(){if(this.activeQueries>2)return;const e=this.context,t=this.extension;if(null===t)return;let s;i?(s=e.createQuery(),e.beginQuery(t.TIME_ELAPSED_EXT,s)):(s=t.createQueryEXT(),t.beginQueryEXT(t.TIME_ELAPSED_EXT,s)),this.activeQueries++;const n=()=>{let r,E,u;i?(r=e.getQueryParameter(s,e.QUERY_RESULT_AVAILABLE),E=e.getParameter(t.GPU_DISJOINT_EXT),u=e.getQueryParameter(s,e.QUERY_RESULT)):(r=t.getQueryObjectEXT(s,t.QUERY_RESULT_AVAILABLE_EXT),E=e.getParameter(t.GPU_DISJOINT_EXT),u=t.getQueryObjectEXT(s,t.QUERY_RESULT_EXT));const a=1e-6*u;r?(E||this.update(a,this.maxTime),this.activeQueries--):requestAnimationFrame(n)};n()},this.endQuery=function(){const e=this.extension,t=this.context;null!==e&&(i?t.endQuery(e.TIME_ELAPSED_EXT):e.endQueryEXT(e.TIME_ELAPSED_EXT))}}}export{t as GPUStatsPanel};
2
2
  /*
3
3
  * Copyright (©) 2023. All rights reserved.
4
4
  * See the LICENSE.md file for details.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hology/core",
3
- "version": "0.0.59",
3
+ "version": "0.0.61",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -82,6 +82,9 @@
82
82
  },
83
83
  "author": "",
84
84
  "license": "SEE LICENSE IN LICENSE.md",
85
+ "peerDependencies": {
86
+ "three": ">=0.169.0"
87
+ },
85
88
  "dependencies": {
86
89
  "@babel/runtime": "^7.24.8",
87
90
  "@dimforge/rapier3d-compat": "^0.13.1",
@@ -89,11 +92,10 @@
89
92
  "@types/three-nebula": "^10.0.3",
90
93
  "recast-navigation": "^0.30.0",
91
94
  "rxjs": "7.8.1",
92
- "three": "^0.169.0",
93
95
  "three-csm": "^4.2.1",
94
96
  "three-mesh-bvh": "^0.7.5",
95
97
  "three-nebula": "^10.0.3",
96
- "three-shader-graph": "0.2.1",
98
+ "three-shader-graph": "0.2.6",
97
99
  "three-stdlib": "2.34.0",
98
100
  "ts-key-enum": "^2.0.12",
99
101
  "typedi": "^0.10.0"