@hology/core 0.0.25 → 0.0.27
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/gameplay/actors/builtin/components/character/character-movement.js +1 -1
- package/dist/gameplay/actors/camera/third-party-camera-component.d.ts +1 -0
- package/dist/gameplay/actors/camera/third-party-camera-component.js +1 -1
- package/dist/scene/asset-resource-loader.js +1 -1
- package/dist/scene/landscape/landscape-manager.js +1 -1
- package/dist/scene/landscape/landscape.js +1 -1
- package/dist/scene/materializer.js +1 -1
- package/dist/scene/model.d.ts +3 -0
- package/dist/scene/objects/shapes.d.ts +27 -1
- package/dist/scene/objects/shapes.js +1 -1
- package/dist/scene/objects/stairs-geometry.d.ts +3 -0
- package/dist/scene/objects/stairs-geometry.js +5 -0
- package/package.json +1 -1
- package/tsconfig.tsbuildinfo +1 -1
@@ -1,4 +1,4 @@
|
|
1
|
-
import{__decorate as t,__metadata as i}from"tslib";import{ActionInput as e,AxisInput as s,RotationInput as o}from"../../../../../gameplay/input";import{PhysicsSystem as n,RayTestResult as a}from"../../../../../gameplay/services/physics/physics-system";import{MathUtils as l,Vector3 as r}from"three";import{ActorComponent as c,Component as h}from"../../../component";import{CharacterMovementMode as p}from"./modes";import{Quaternion as m,Vec3 as y}from"cannon-es";import{CapsuleCollisionShape as d}from"../../../../../scene/collision/collision-shape";import{lerp as u}from"three/src/math/MathUtils";import{takeUntil as g}from"rxjs";import{PhysicsBodyType as S}from"../../../../services/physics/physics-system";const f=new r,v=new r,w=1/30;let x=class extends c{get autoStepMinWidth(){return this.cc.autostepMinWidth()}set autoStepMinWidth(t){this.cc.enableAutostep(this.cc.autostepMaxHeight(),t,this.cc.autostepIncludesDynamicBodies())}get autoStepDynamicObjects(){return this.cc.autostepIncludesDynamicBodies()}set autoStepDynamicObjects(t){this.cc.enableAutostep(this.cc.autostepMaxHeight(),this.cc.autostepMinWidth(),t)}get autoStepMaxHeight(){return this.cc.autostepMaxHeight()}set autoStepMaxHeight(t){this.cc.enableAutostep(t,this.cc.autostepMinWidth(),this.cc.autostepIncludesDynamicBodies())}get snapToGround(){return this.cc.snapToGroundDistance()}set snapToGround(t){this.cc.enableSnapToGround(t)}constructor(t){super(),this.physicsSystem=t,this.directionInput=new s,this.jumpInput=new e,this.sprintInput=new e,this.rotationInput=new o,this.horizontalSpeed=0,this.maxSpeed=8,this.maxSpeedBackwards=8,this.maxSpeedSprint=12,this.jumpVelocity=7,this.fallingMovementControl=.5,this.fallingReorientation=!1,this.gravityOverride=null,this.colliderHeight=2,this.colliderRadius=.5,this.jumpInAir=!1,this.mass=50,this.allowSliding=!0,this.minSlopeSlideAngle=l.degToRad(70),this.maxSlopeClimbAngle=l.degToRad(70),this.applyImpulsesToDynamicBodies=!0,this.velocity=new r,this.mode=p.walking,this.isSprinting=!1,this.pressedJump=!1,this.rayTestResult=new a;const i=this.cc=this.physicsSystem.getCharacterController(.1);i.enableSnapToGround(.1),i.enableAutostep(0,.1,!1)}onInit(){const t=this.cc;t.setApplyImpulsesToDynamicBodies(this.applyImpulsesToDynamicBodies),t.setMinSlopeSlideAngle(this.minSlopeSlideAngle),t.setMaxSlopeClimbAngle(this.maxSlopeClimbAngle),t.setCharacterMass(this.mass),t.setSlideEnabled(this.allowSliding),this.physicsSystem.addActor(this.actor,[this.createCollisionShape()],{mass:0,type:S.kinematic,continousCollisionDetection:!0,friction:1}),this.rotationInput.rotation.copy(this.actor.rotation);let i=this.rotationInput.rotation.y;const e=new r,s=new r,o=new r,n=new r,a=new r,l=new r;let c=0,h=null;this.physicsSystem.beforeStep.pipe(g(this.disposed)).subscribe((m=>{m>w&&(m=w),this.pressedJump=this.jumpInput.activated,this.isSprinting=this.sprintInput.activated;let y=i-this.rotationInput.rotation.y;i=this.rotationInput.rotation.y,o.copy(this.actor.position),n.set(-this.directionInput.vector.x,0,this.directionInput.vector.y).normalize();const d=n.z<0?this.maxSpeedBackwards:this.isSprinting?this.maxSpeedSprint:this.maxSpeed;l.copy(n).applyAxisAngle(I,this.rotationInput.rotation.y),this.mode===p.walking&&(0!==l.length()?(c=Math.min(d,c),c=u(c,d,4*m)):c=0,a.copy(l).multiplyScalar(c),this.pressedJump&&(this.mode=p.falling,this.velocity.copy(a),this.velocity.y=this.jumpVelocity),a.y=m*this.getEffectiveGravity()),this.mode===p.falling&&(this.pressedJump&&this.jumpInAir&&(this.mode=p.falling,this.velocity.copy(a),this.velocity.y=this.jumpVelocity),this.velocity.y+=m*this.getEffectiveGravity(),a.copy(this.velocity),a.add(l.clone().multiplyScalar(d*this.fallingMovementControl*m)),this.fallingReorientation&&a.applyAxisAngle(new r(0,1,0),-y)),s.copy(a).normalize(),e.copy(a),this.actor.rotation.y-=y,a.length(),f.copy(a).multiplyScalar(m),v.copy(this.physicsSystem.getActorComputedMovement(this.actor,t,f)),this.physicsSystem.setNextKinematicTranslation(this.actor,v);let g=function(t){if(t.numComputedCollisions()>0){const i=t.computedCollision(0);A.x=i.normal2.x,A.y=i.normal2.y,A.z=i.normal2.z;const e=A.angleTo(I);A.x=i.normal1.x,A.y=i.normal1.y,A.z=i.normal1.z;const s=A.angleTo(I);return!(e<100)&&s>t.minSlopeSlideAngle()}return!1}(t);!t.computedGrounded()||g?this.mode!==p.falling&&(null==h?h=performance.now():performance.now()-h>100&&(this.mode=p.falling,this.velocity.copy(e))):this.velocity.y<=0&&(this.mode,p.falling,this.mode=p.walking,this.velocity.y=0,h=null),this.mode,p.walking,this.horizontalSpeed=c}))}getWallDirection(t,i){const e=t.clone().negate().cross(I);return e.dot(i)<0?e.negate():e}moveTo(t){this.actor.position.copy(t),this.physicsSystem.updateActorTransform(this.actor)}getEffectiveGravity(){var t;return null!==(t=this.gravityOverride)&&void 0!==t?t:this.physicsSystem.getGravity().y}get isGrounded(){return this.rayTestResult.hasHit}createCollisionShape(){const t=new d(this.colliderHeight,this.colliderRadius);return t.offset.y=this.colliderRadius+this.colliderHeight/2,t}step(t){}performMovement(t){}};x=t([h({inEditor:!1}),i("design:paramtypes",[n])],x);export{x as CharacterMovementComponent};const I=new r(0,1,0),A=new r;
|
1
|
+
import{__decorate as t,__metadata as i}from"tslib";import{ActionInput as e,AxisInput as s,RotationInput as o}from"../../../../../gameplay/input";import{PhysicsSystem as n,RayTestResult as a}from"../../../../../gameplay/services/physics/physics-system";import{MathUtils as l,Vector3 as r}from"three";import{ActorComponent as c,Component as h}from"../../../component";import{CharacterMovementMode as p}from"./modes";import{Quaternion as m,Vec3 as y}from"cannon-es";import{CapsuleCollisionShape as d}from"../../../../../scene/collision/collision-shape";import{lerp as u}from"three/src/math/MathUtils";import{takeUntil as g}from"rxjs";import{PhysicsBodyType as S}from"../../../../services/physics/physics-system";const f=new r,v=new r,w=1/30;let x=class extends c{get autoStepMinWidth(){return this.cc.autostepMinWidth()}set autoStepMinWidth(t){this.cc.enableAutostep(this.cc.autostepMaxHeight(),t,this.cc.autostepIncludesDynamicBodies())}get autoStepDynamicObjects(){return this.cc.autostepIncludesDynamicBodies()}set autoStepDynamicObjects(t){this.cc.enableAutostep(this.cc.autostepMaxHeight(),this.cc.autostepMinWidth(),t)}get autoStepMaxHeight(){return this.cc.autostepMaxHeight()}set autoStepMaxHeight(t){this.cc.enableAutostep(t,this.cc.autostepMinWidth(),this.cc.autostepIncludesDynamicBodies())}get snapToGround(){return this.cc.snapToGroundDistance()}set snapToGround(t){this.cc.enableSnapToGround(t)}constructor(t){super(),this.physicsSystem=t,this.directionInput=new s,this.jumpInput=new e,this.sprintInput=new e,this.rotationInput=new o,this.horizontalSpeed=0,this.maxSpeed=8,this.maxSpeedBackwards=8,this.maxSpeedSprint=12,this.jumpVelocity=7,this.fallingMovementControl=.5,this.fallingReorientation=!1,this.gravityOverride=null,this.colliderHeight=2,this.colliderRadius=.5,this.jumpInAir=!1,this.mass=50,this.allowSliding=!0,this.minSlopeSlideAngle=l.degToRad(70),this.maxSlopeClimbAngle=l.degToRad(70),this.applyImpulsesToDynamicBodies=!0,this.velocity=new r,this.mode=p.walking,this.isSprinting=!1,this.pressedJump=!1,this.rayTestResult=new a;const i=this.cc=this.physicsSystem.getCharacterController(.1);i.enableSnapToGround(.1),i.enableAutostep(0,.1,!1)}onInit(){const t=this.cc;t.setApplyImpulsesToDynamicBodies(this.applyImpulsesToDynamicBodies),t.setMinSlopeSlideAngle(this.minSlopeSlideAngle),t.setMaxSlopeClimbAngle(this.maxSlopeClimbAngle),t.setCharacterMass(this.mass),t.setSlideEnabled(this.allowSliding),this.physicsSystem.addActor(this.actor,[this.createCollisionShape()],{mass:0,type:S.kinematic,continousCollisionDetection:!0,friction:1}),this.rotationInput.rotation.copy(this.actor.rotation);let i=this.rotationInput.rotation.y;const e=new r,s=new r,o=new r,n=new r,a=new r,l=new r;let c=0,h=null;this.physicsSystem.beforeStep.pipe(g(this.disposed)).subscribe((m=>{m>w&&(m=w),this.pressedJump=this.jumpInput.activated,this.isSprinting=this.sprintInput.activated&&this.directionInput.vertical>=0;let y=i-this.rotationInput.rotation.y;i=this.rotationInput.rotation.y,o.copy(this.actor.position),n.set(-this.directionInput.vector.x,0,this.directionInput.vector.y).normalize();const d=n.z<0?this.maxSpeedBackwards:this.isSprinting?this.maxSpeedSprint:this.maxSpeed;l.copy(n).applyAxisAngle(I,this.rotationInput.rotation.y),this.mode===p.walking&&(0!==l.length()?(c=Math.min(d,c),c=u(c,d,4*m)):c=0,a.copy(l).multiplyScalar(c),this.pressedJump&&(this.mode=p.falling,this.velocity.copy(a),this.velocity.y=this.jumpVelocity),a.y=m*this.getEffectiveGravity()),this.mode===p.falling&&(this.pressedJump&&this.jumpInAir&&(this.mode=p.falling,this.velocity.copy(a),this.velocity.y=this.jumpVelocity),this.velocity.y+=m*this.getEffectiveGravity(),a.copy(this.velocity),a.add(l.clone().multiplyScalar(d*this.fallingMovementControl*m)),this.fallingReorientation&&a.applyAxisAngle(new r(0,1,0),-y)),s.copy(a).normalize(),e.copy(a),this.actor.rotation.y-=y,a.length(),f.copy(a).multiplyScalar(m),v.copy(this.physicsSystem.getActorComputedMovement(this.actor,t,f)),this.physicsSystem.setNextKinematicTranslation(this.actor,v);let g=function(t){if(t.numComputedCollisions()>0){const i=t.computedCollision(0);A.x=i.normal2.x,A.y=i.normal2.y,A.z=i.normal2.z;const e=A.angleTo(I);A.x=i.normal1.x,A.y=i.normal1.y,A.z=i.normal1.z;const s=A.angleTo(I);return!(e<100)&&s>t.minSlopeSlideAngle()}return!1}(t);!t.computedGrounded()||g?this.mode!==p.falling&&(null==h?h=performance.now():performance.now()-h>100&&(this.mode=p.falling,this.velocity.copy(e))):this.velocity.y<=0&&(this.mode,p.falling,this.mode=p.walking,this.velocity.y=0,h=null),this.mode,p.walking,this.horizontalSpeed=c}))}getWallDirection(t,i){const e=t.clone().negate().cross(I);return e.dot(i)<0?e.negate():e}moveTo(t){this.actor.position.copy(t),this.physicsSystem.updateActorTransform(this.actor)}getEffectiveGravity(){var t;return null!==(t=this.gravityOverride)&&void 0!==t?t:this.physicsSystem.getGravity().y}get isGrounded(){return this.rayTestResult.hasHit}createCollisionShape(){const t=new d(this.colliderHeight,this.colliderRadius);return t.offset.y=this.colliderRadius+this.colliderHeight/2,t}step(t){}performMovement(t){}};x=t([h({inEditor:!1}),i("design:paramtypes",[n])],x);export{x as CharacterMovementComponent};const I=new r(0,1,0),A=new r;
|
2
2
|
/*
|
3
3
|
* Copyright (©) 2023. All rights reserved.
|
4
4
|
* See the LICENSE.md file for details.
|
@@ -28,6 +28,7 @@ export declare class ThirdPartyCameraComponent extends ActorComponent {
|
|
28
28
|
constructor(viewController: ViewController, physicsSystem: PhysicsSystem);
|
29
29
|
isMouseLocked: boolean;
|
30
30
|
onInit(): Promise<void>;
|
31
|
+
onLateUpdate(deltaTime: number): void;
|
31
32
|
private get element();
|
32
33
|
private canvas;
|
33
34
|
hideCursor(): void;
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import{__awaiter as t,__decorate as
|
1
|
+
import{__awaiter as t,__decorate as e,__metadata as s}from"tslib";import{ActorComponent as i,Component as o,Attach as n}from"../component";import{CameraComponent as h}from"./camera-component";import{Vector3 as a}from"three";import{ViewController as r}from"../../services/render";import{DecimalInput as c,RestrictedRotationInput as m}from"../../input";import{PhysicsSystem as d,RayTestResult as l}from"../../services/physics/physics-system";import{clamp as u,lerp as p}from"three/src/math/MathUtils";let f=class extends i{constructor(t,e){super(),this.viewController=t,this.physicsSystem=e,this.distance=9,this.minDistance=1.5,this.maxDistance=this.distance,this.height=3,this.offsetX=-1,this.offsetZ=1.5,this.autoActivate=!0,this.bounceBackSpeed=5,this.collisionCheckRadius=.5,this.restrictedDistance=Math.max(this.distance,this.maxDistance),this.rotationInput=new m(-Math.PI/4,Math.PI/2-.7),this.zoomInput=new c(1,0,1),this.offset=new a,this.lookAtOffset=new a(this.offsetX,0,this.offsetZ),this.isMouseLocked=!1,this.canvas=null,this.pointerLockInactivatedAt=null,this.onMouseDown=t=>{this.isMouseLocked||this.hideCursor()},this.onKeyDown=t=>{"Escape"===t.key&&this.showCursor()},this.onPointerLockChange=()=>{null!=document.pointerLockElement||null!=document.mozPointerLockElement||this.showCursor()}}onInit(){return t(this,void 0,void 0,(function*(){this.restrictedDistance=Math.max(this.distance,this.maxDistance),this.lookAtOffset.set(this.offsetX,0,this.offsetZ),this.autoActivate&&this.viewController.setCamera(this.camera.instance);const t=this.element;t.addEventListener("mousedown",this.onMouseDown),t.addEventListener("keydown",this.onKeyDown),document.addEventListener("pointerlockchange",this.onPointerLockChange,!1),this.disposed.subscribe((()=>{t.removeEventListener("mousedown",this.onMouseDown),t.removeEventListener("keydown",this.onKeyDown),document.removeEventListener("pointerlockchange",this.onPointerLockChange,!1)}))}))}onLateUpdate(t){this.setFromRotation(t)}get element(){return this.viewController.htmlElement}hideCursor(){null!=this.pointerLockInactivatedAt&&performance.now()-this.pointerLockInactivatedAt<1600||(this.element.style.cursor="none",null==this.canvas&&(this.canvas=this.element.getElementsByTagName("canvas")[0]),this.canvas&&(this.canvas.requestPointerLock(),this.isMouseLocked=!0))}showCursor(){this.pointerLockInactivatedAt=performance.now(),this.element.style.cursor="default",window.document.exitPointerLock(),this.isMouseLocked=!1}setFromRotation(t){this.checkForCollision(t);const e=u(Math.min(this.restrictedDistance,this.distance),Math.min(this.minDistance,this.restrictedDistance),Math.max(this.distance*this.zoomInput.value,this.minDistance)),s=Math.cos(this.rotationInput.rotation.x)*e;this.offset.x=Math.sin(this.rotationInput.rotation.y)*s,this.offset.y=Math.sin(this.rotationInput.rotation.x)*e+2,this.offset.z=Math.cos(this.rotationInput.rotation.y)*-s,this.offset.add(this.lookAtOffset),this.updateCameraPosition()}checkForCollision(t){const e=this.getLookAtPosition(),s=v;let i=!1,o=this.distance;const n=new l,h=this.camera.instance.getWorldPosition(y);for(let t=-1;t<=1;t++){const a=w.subVectors(h,e);s.copy(h).add(a.multiplyScalar(1.2)),s.x+=t*this.collisionCheckRadius,this.physicsSystem.rayTest(e,s,n,{debugLifetime:0,excludeActor:this.actor}),n.hasHit&&n.distance<this.distance&&(o=Math.min(n.distance,o),i||(i=n.hasHit))}i||(this.restrictedDistance=p(this.restrictedDistance,this.distance,u(this.bounceBackSpeed*t,0,1)))}getLookAtPosition(){const t=k;return t.set(0,0,0),t.y=this.height,t.add(this.lookAtOffset),t.applyMatrix4(this.actor.object.matrixWorld),t}updateCameraPosition(){this.camera.instance.position.copy(this.offset);const t=this.getLookAtPosition();this.camera.instance.lookAt(t)}};e([n(),s("design:type",h)],f.prototype,"camera",void 0),f=e([o(),s("design:paramtypes",[r,d])],f);export{f as ThirdPartyCameraComponent};const k=new a,v=new a,w=(new a,new a),y=new a;
|
2
2
|
/*
|
3
3
|
* Copyright (©) 2023. All rights reserved.
|
4
4
|
* See the LICENSE.md file for details.
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import{__awaiter as e}from"tslib";import{AudioLoader as t,Group as i,LoadingManager as
|
1
|
+
import{__awaiter as e}from"tslib";import{AudioLoader as t,Group as i,LoadingManager as r,Mesh as s,Object3D as o,TextureLoader as n}from"three";import{GLTFLoader as a,MTLLoader as l,OBJLoader as h}from"three-stdlib";import{FBXLoader as c}from"three-stdlib";import{cloneMesh as d}from"../utils/mesh";import{pathJoin as u}from"../utils/files";import{Subject as p,firstValueFrom as f}from"rxjs";import{importCollisionShapes as m}from"./collision/collision-shape-import";import*as g from"three";import{iterateMaterials as y}from"../utils/materials";const v=["glb","gltf","fbx","obj"];export class AssetResourceLoader{onError(e){console.error(e)}constructor(){this.cache=new Map,this.textureCache=new Map,this.loadingManager=new r,this.glbLoader=new a(this.loadingManager),this.fbxLoader=new c(this.loadingManager),this.objLoader=new h(this.loadingManager),this.textureLoader=new n(this.loadingManager),this.audioLoader=new t(this.loadingManager),this.makeReady=new p,this.ready=f(this.makeReady),this.collisionShapeCache=new Map}setDataDir(e){this.basePath=u(e,"asset-resources"),this.makeReady.next(!0)}getUri(e){return u(this.basePath,e)+`?windowId=${getElectronArg("windowId")}`}getTexture(t){return e(this,void 0,void 0,(function*(){return null==t?null:(yield this.ready,this.cache.has(t.id)||(yield this.textureLoader.loadAsync(this.getUri(t.fileKey)).then((e=>{var i,r,s,o;return e.wrapS=L(null===(i=t.texture)||void 0===i?void 0:i.wrapS),e.wrapT=L(null===(r=t.texture)||void 0===r?void 0:r.wrapT),e.flipY=null===(o=null===(s=t.texture)||void 0===s?void 0:s.flipY)||void 0===o||o,this.textureCache.set(t.id,e),e}))),this.textureCache.get(t.id))}))}getMesh(t){var r;return e(this,void 0,void 0,(function*(){if(yield this.ready,!v.includes(null===(r=t.fileFormat)||void 0===r?void 0:r.toLowerCase()))return console.error("Unsupported mesh file format "+t.fileFormat,t),{scene:new i};if(!this.cache.has(t.fileKey))try{this.cache.set(t.fileKey,yield this.loadMesh(t))}catch(e){return this.onError(e),{scene:new i}}const e=d(this.cache.get(t.fileKey).scene);e.traverse((e=>{e instanceof s&&e.material instanceof Array&&(e.material=e.material.slice())}));const o=this.computeCollisionShapes(t,e),n=(new AssetMeshInstance).copy(e);return n.collisionShapes=o,{scene:n}}))}getAudio(t){return e(this,void 0,void 0,(function*(){return yield this.ready,this.audioLoader.loadAsync(this.getUri(t.fileKey))}))}computeCollisionShapes(e,t){return this.collisionShapeCache.get(e.id)||this.collisionShapeCache.set(e.id,m(t)),this.collisionShapeCache.get(e.id)}loadMesh(t){return e(this,void 0,void 0,(function*(){return yield this.ready,yield this.loadByAsset(t).then((e=>(e.scene.animations=[],e.scene=function(e,t){let i=!1;if(t.traverseVisible((e=>{b.test(e.name)&&(i=!0)})),!i)return t;const r=new g.LOD,s=[t];for(;s.length>0;){const e=s.shift(),t=e.name.match(b);if(null!=t){const i=parseInt(t[1]);0===i?r.addLevel(e,0):console.warn(`Skipping LOD level ${i} for now as LOD is not fully supported`)}else s.push(...e.children)}return r}(0,e.scene),e)))}))}loadByAsset(t){return e(this,void 0,void 0,(function*(){const e=this.getUri(t.fileKey);switch(t.fileFormat){case"glb":case"gltf":return this.glbLoader.loadAsync(e).then((e=>({scene:e.scene})));case"fbx":return this.fbxLoader.loadAsync(e).then((e=>({scene:e})));case"obj":if(null!=t.materialLib){const e=new l;e.materialOptions={normalizeRGB:!1};const i=yield e.loadAsync(this.getUri(t.materialLib));this.objLoader.setMaterials(i)}return this.objLoader.loadAsync(e).then((e=>(w(e),e))).then((e=>({scene:e})))}}))}}function w(e){var t;if(e instanceof s)for(const t of y(e.material))t instanceof g.MeshPhongMaterial&&!t.color.isLinear&&(t.color.isLinear=!0,t.color.convertSRGBToLinear());null===(t=e.children)||void 0===t||t.forEach(w)}export class AssetMeshInstance extends o{}export function getElectronArg(e){var t;const i=`--${e}=`,r=null===(t=window.process)||void 0===t?void 0:t.argv.find((e=>e.startsWith(i)));return null==r?void 0:r.substring(i.length)}function L(e){switch(e){case"clamp":return g.ClampToEdgeWrapping;case"repeat":return g.RepeatWrapping;case"mirror":return g.MirroredRepeatWrapping}return g.RepeatWrapping}const b=/_LOD(\d+)$/;
|
2
2
|
/*
|
3
3
|
* Copyright (©) 2023. All rights reserved.
|
4
4
|
* See the LICENSE.md file for details.
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import{__awaiter as e}from"tslib";import{materialFromAsset as t}from"../../scene/materializer";import{Mesh as s,PlaneGeometry as o,Vector3 as n,InstancedMesh as i,Matrix4 as a,MeshLambertMaterial as r,Color as c,Vector2 as l,Triangle as h,Box3 as u,MathUtils as d,MeshStandardMaterial as f,ShaderMaterial as m,DoubleSide as p,PerspectiveCamera as w}from"three";import{indexBy as y}from"../../utils/collections";import{smoothNormalsCrossMeshes as v}from"./utils";import*as M from"three";import{defaultLandscapeMaterial as g,LandscapeMesh as x}from"./landscape";import{meanVectors3withWeight as b,clamp as S}from"../../utils/math";import{whenIdle as A}from"../../utils/async";import{Subject as z,debounceTime as P}from"rxjs";new n,new n;const L=new n,B=new n,G=new n;export class LandscapeManager{constructor(e,t,s,o,i,r,c){this.source=e,this.view=t,this.landscape=s,this.assetManagerService=o,this.assetService=i,this.shaders=r,this.applyMaterial=c,this.scatterMeshes=new Map,this.loadedScatterSquares=new Set,this.refreshRequests=new z,this.defaultLandscapeMaterial=g.clone(),this.onLoopHandler=()=>this.update(),this.sectionCache=new Map,this._matrix=new a,this.scatterGeometryCache=new Map,this._lastUpdatePosition=new n,this._cameraPosition=new n,this.view.onLoop(this.onLoopHandler),this.defaultLandscapeMaterial.color=g.color,this.refreshRequests.pipe(P(500)).subscribe((e=>this.refreshScatter(e.origin,e.force,e.predicate)))}updateShaders(e){this.shaders=e}loadGrass(){return e(this,void 0,void 0,(function*(){const e=yield this.assetService.getAsset("6ij937n72g");yield this.assetManagerService.getMesh(e);this.grassGeometry=new o(2,2,3,3);const t=this.grassGeometry.getAttribute("normal");for(let e=0;e<t.count;e++)t.setXYZ(e,0,1,0);t.needsUpdate=!0,this.grassMaterial=new f({color:3765785})}))}refreshGeometry(){const e=this.source.landscape.options,t=(new n,new n);this.view.camera.getWorldPosition(t);const s=[];G.fromArray(this.source.position);const o=this.view.camera instanceof w?Math.min(this.view.camera.far,1e3):1e3,i=1.1*o,a=e.sections.y*e.sectionSize/-2,r=e.sections.x*e.sectionSize/-2;for(let n=0;n<e.sections.x;n++)if(L.x=r+n*e.sectionSize,!(Math.abs(t.x-L.x)>i))for(let c=0;c<e.sections.y;c++){L.z=a+c*e.sectionSize,B.copy(G).add(L);const l=B.distanceTo(t),h=`${n},${c}`,u=this.landscape.sections.find((e=>e.x===n&&e.y===c));if(l<=o){if(null==u){this.sectionCache.has(h)||this.sectionCache.set(h,this.createLandscapeMesh(this.source,e,r,a,n,c));const t=this.sectionCache.get(h);this.applyMaterial(t),this.landscape.add(t),s.push(t)}}else l>i&&this.landscape.remove(u)}v(s)}applyHeightMap(e,t,s,o=1){var n,i,a,r;const c=Math.pow(s+1,2),l=e.getAttribute("position"),h=y(null!==(n=t.points)&&void 0!==n?n:[],(e=>e.i));if(1===o)for(const e of t.points)l.setY(e.i,e.y);else for(let e=0;e<l.count;e++){const t=$(e,l.count,c);let s=0;s=t%1==0?null!==(a=null===(i=h.get(t))||void 0===i?void 0:i.y)&&void 0!==a?a:0:Math.floor(null===(r=h.get(t))||void 0===r?void 0:r.y),l.setY(e,s)}l.needsUpdate=!0,e.computeVertexNormals()}deleteOldScatterMeshes(){var e,t;const s=new Set;for(const[o,n]of null!==(t=null===(e=this.source.grass)||void 0===e?void 0:e.layers.entries())&&void 0!==t?t:[])for(const[e,t]of n.meshes.entries()){const t=`${o}-${e}`;s.add(t)}for(const e of this.scatterMeshes.keys())if(!s.has(e)){this.scatterMeshes.get(e).forEach((e=>{var t;null===(t=e.parent)||void 0===t||t.remove(e),e.dispose()})),this.scatterMeshes.delete(e)}}queueRefreshScatter(e,t=!1,s=(()=>!0)){this.refreshRequests.next({origin:e,force:t,predicate:s})}refreshScatter(a,l=!1,u=(()=>!0)){var f,w,v,g;return e(this,void 0,void 0,(function*(){l&&this.scatterGeometryCache.clear(),this.deleteOldScatterMeshes();for(const[x,z]of null!==(w=null===(f=this.source.grass)||void 0===f?void 0:f.layers.entries())&&void 0!==w?w:[])for(const[f,w]of z.meshes.entries()){const z=`${x}-${f}`;this.scatterMeshes.has(z)||this.scatterMeshes.set(z,new Map);const P=this.scatterMeshes.get(z),L=yield this.assetService.getAsset(w.assetId),B=yield this.assetManagerService.getMesh(L),G=[];if(B.scene.traverse((e=>{e instanceof s&&G.push(e)})),1!==G.length){console.log(B),console.warn("Dynamic grass only works for meshes with a single geometry.");continue}if(!(G[0]instanceof s)){console.warn("Only meshes can be used for dynamic grass. Found:",B.scene);continue}const $=G[0];let _=$.geometry;this.scatterGeometryCache.has($.geometry.uuid)?_=this.scatterGeometryCache.get($.geometry.uuid):(_=$.geometry.clone(),!0===w.normalsUp&&E(_),this.scatterGeometryCache.set($.geometry.uuid,_),null==_.userData.updatedMatrix&&(B.scene.updateMatrixWorld(),_.applyMatrix4($.matrixWorld),_.userData.updatedMatrix=!0));const U=_.getIndex().count/3;if(U>400){console.warn(`The triangle count of ${L.name} is too big ${U}. Keep it below 400`);continue}const D=null!=L.materialAssignments&&L.materialAssignments.length>0?L.materialAssignments[0].materialId:null,I=null!=D&&"null"!==D?yield t(yield this.assetService.getAsset(D),null,this.assetService,this.assetManagerService,this.shaders,!1):null;let O=null!=I?I:$.material;O instanceof m&&(O.side=p);const Y=d.degToRad(null!==(v=w.maxSlope)&&void 0!==v?v:90),X=Math.cos(Y),F=this.landscape.sections,N=F.filter(k(a,w.viewDistance)).filter((e=>!P.has(e.uuid)||l)).filter((e=>u(e)));F.filter(C(a,2*w.viewDistance)).forEach((e=>{var t;const s=P.get(e.uuid);null===(t=null==s?void 0:s.parent)||void 0===t||t.remove(s),P.delete(e.uuid)}));performance.now();const Z=this.source.landscape.options,K=Z.sectionSize,V=null!==(g=w.density)&&void 0!==g?g:1,J=null!=V?V:1,Q=Z.density,ee=K/Q,te=J,se=ee/Math.sqrt(te),oe=Math.pow(Q,2),ne=se/ee,ie=Math.floor(oe*te),ae=[0,0,0];for(const t of N)yield A((()=>e(this,void 0,void 0,(function*(){var e,s,a,u,d,f,m;t.updateWorldMatrix(!0,!1);const p=this._matrix,v=new n,g=t.geometry.getAttribute("position"),A=t.geometry.getAttribute("normal"),z=(null!==(e=this.source.vertexMaterials)&&void 0!==e?e:[]).filter((e=>e.m===t.name)),L=y(z,(e=>e.i));let B=P.get(t.uuid);if(null==B||B.count==ie&&!l||(null===(s=B.parent)||void 0===s||s.remove(B),P.delete(t.uuid),B=null),null==B){const e=new o(.15,.75).getAttribute("normal");for(let t=0;t<e.count;t++);new r({color:new c(3765785),side:M.DoubleSide});B=new i(_,O,ie),B.raycast=()=>{},B.receiveShadow=!0}const G=new h(new n,new n,new n);let[$,C,k,U]=[new n,new n,new n,new n],[D,I,Y]=[[],[],[]],[E,F,N]=[new n,new n,new n,new n];const Z=new n,K=new n,V=new n,J=new n,ee=new h(new n,new n,new n),se=new h(new n,new n,new n),re=new h(new n,new n,new n),ce=new h(new n,new n,new n);let le=0;e:for(let e=0;e<oe;e++){const s=Math.floor(e/Q);$.fromBufferAttribute(g,e+s),J.copy($).applyMatrix4(t.matrixWorld),ee.a.copy($),ee.b.fromBufferAttribute(g,e+1+s),ee.c.fromBufferAttribute(g,e+Q+1+s),se.a.copy(ee.b),se.b.copy(ee.c),se.c.fromBufferAttribute(g,e+Q+2+s),re.a.fromBufferAttribute(A,e+s),re.b.fromBufferAttribute(A,e+1+s),re.c.fromBufferAttribute(A,e+Q+1+s),ce.a.copy(re.b),ce.b.copy(re.c),ce.c.fromBufferAttribute(A,e+Q+2+s);const o=[];o[0]=null===(a=L.get(e+s))||void 0===a?void 0:a.w,o[1]=null===(u=L.get(e+1+s))||void 0===u?void 0:u.w,o[2]=null===(d=L.get(e+Q+1+s))||void 0===d?void 0:d.w,o[3]=null===(f=L.get(e+Q+2+s))||void 0===f?void 0:f.w;let n=0;for(let e=0;e<=1+ne;e+=ne)for(let s=0;s<=1+ne;s+=ne){if(le>ie)break e;if(n++,n>te)continue e;1-e>s?(C=ee.a,k=ee.b,U=ee.c,E=re.a,F=re.b,N=re.c,D=o[0],I=o[1],Y=o[2]):(C=se.a,k=se.b,U=se.c,E=ce.a,F=ce.b,N=ce.c,D=o[1],I=o[2],Y=o[3]),G.a.copy(C),G.b.copy(k),G.c.copy(U),q(G),Z.set($.x,0,$.z),Z.x=T(G.a.x,G.b.x),Z.z=T(G.a.z,G.c.z),G.getBarycoord(Z,v).toArray(ae);if(R([D,I,Y],ae,.2)!==x-1)continue;const i=[C,k,U];b(i,ae,K),b([E,F,N],ae,V);const a=i.map((e=>e.y)),r=Math.max(...a),c=Math.min(...a);if(K.y=S(K.y,c,r),null!=w.maxSlope&&w.maxSlope<90&&V.y<X)continue;const l=K.applyMatrix4(t.matrixWorld);l.y+=T(w.offsetMin,w.offsetMax);const h=T(w.scaleMin,w.scaleMax);p.makeScale(1,1,1),p.setPosition(l),p.scale(W(h)),w.alignToNormal&&H(p,l,B.matrixWorld,V),!1!==w.randomRotation&&j(p),B.setMatrixAt(le,p),le++}}B.instanceMatrix.count=le,p.makeScale(0,0,0);for(let e=le;e<ie;e++)B.setMatrixAt(e,p);B.instanceMatrix.needsUpdate=!0,P.has(t.uuid)||null===(m=this.landscape)||void 0===m||m.attach(B),P.set(t.uuid,B)}))));performance.now()}}))}stop(){this.view.removeOnLoop(this.onLoopHandler)}update(){this.view.camera&&(this.view.camera.getWorldPosition(this._cameraPosition),this._cameraPosition.distanceTo(this._lastUpdatePosition)>10&&(this._lastUpdatePosition.copy(this._cameraPosition),this.refreshGeometry(),this.refreshScatter(this._cameraPosition)))}clear(){this.scatterMeshes.forEach((e=>e.forEach((e=>{var t;return null===(t=e.parent)||void 0===t?void 0:t.remove(e)}))))}createLandscapeMesh(e,t,s,n,i,a){const r=new o(t.sectionSize,t.sectionSize,t.density,t.density);r.rotateX(Math.PI/-2);const c=this.defaultLandscapeMaterial,l=new x(r,c);l.position.x=s+i*t.sectionSize,l.position.z=n+a*t.sectionSize,l.receiveShadow=!0,l.castShadow=!1,l.userData.landscape={x:i,y:a},l.x=i,l.y=a,l.name=`${i},${a}`;const h=e.landscape.heightMaps.find((e=>e.x===i&&e.y===a));return null!=h&&this.applyHeightMap(r,h,t.density,1),r.computeBoundsTree(),l}}function $(e,t,s){const o=Math.sqrt(t),n=Math.floor(e/o)/(o-1),i=e%o/(o-1),a=Math.sqrt(s);return(s-1)*n-(a-1)*n+(a-1)*i}new Map,new l(0,0),new l(1,0),new l(0,1),new l(1,0),new l(0,1),new l(1,1),new n;const _=new u;function C(e,t){return function(s){return _.setFromObject(s).distanceToPoint(e)>t}}function k(e,t){return function(s){return _.setFromObject(s).distanceToPoint(e)<t}}function q(e){e.a.y=0,e.b.y=0,e.c.y=0}function R(e,t,s=.5){let o=-1,n=0;for(let i=0;i<e.length;i++)if(null!=e[i])for(let a=0;a<e[i].length;a++){const r=e[i][a]*t[i];r>s&&r>n&&(n=r,o=a)}return o}function T(e,t){let s=t-e,o=Math.random();return o*=s,o+=e,o}const U=new n;function W(e){return U.set(e,e,e)}new n;const D=new n,I=new n(0,1,0),O=(new a).makeRotationX(Math.PI/-2);function H(e,t,s,o){e.lookAt(D,o,I).multiply(O)}const Y=new a;function j(e){e.multiply(Y.makeRotationY(Math.random()*Math.PI/2))}function E(e){const t=e.getAttribute("normal");for(let e=0;e<t.count;e++)t.setXYZ(e,0,1,0);t.needsUpdate=!0}
|
1
|
+
import{__awaiter as e}from"tslib";import{materialFromAsset as t}from"../../scene/materializer";import{Mesh as s,PlaneGeometry as o,Vector3 as n,InstancedMesh as i,Matrix4 as a,MeshLambertMaterial as r,Color as c,Vector2 as l,Triangle as h,Box3 as u,MathUtils as d,MeshStandardMaterial as f,ShaderMaterial as m,DoubleSide as p,PerspectiveCamera as w}from"three";import{indexBy as y}from"../../utils/collections";import{smoothNormalsCrossMeshes as v}from"./utils";import*as M from"three";import{defaultLandscapeMaterial as g,LandscapeMesh as x}from"./landscape";import{meanVectors3withWeight as b,clamp as S}from"../../utils/math";import{whenIdle as A}from"../../utils/async";import{Subject as z,debounceTime as P}from"rxjs";new n,new n;const L=new n,B=new n,G=new n;export class LandscapeManager{constructor(e,t,s,o,i,r,c){this.source=e,this.view=t,this.landscape=s,this.assetManagerService=o,this.assetService=i,this.shaders=r,this.applyMaterial=c,this.scatterMeshes=new Map,this.loadedScatterSquares=new Set,this.refreshRequests=new z,this.defaultLandscapeMaterial=g.clone(),this.onLoopHandler=()=>this.update(),this.sectionCache=new Map,this._matrix=new a,this.scatterGeometryCache=new Map,this._lastUpdatePosition=new n,this._cameraPosition=new n,this.view.onLoop(this.onLoopHandler),this.defaultLandscapeMaterial.name=g.name,this.defaultLandscapeMaterial.color=g.color,this.refreshRequests.pipe(P(500)).subscribe((e=>this.refreshScatter(e.origin,e.force,e.predicate)))}updateShaders(e){this.shaders=e}loadGrass(){return e(this,void 0,void 0,(function*(){const e=yield this.assetService.getAsset("6ij937n72g");yield this.assetManagerService.getMesh(e);this.grassGeometry=new o(2,2,3,3);const t=this.grassGeometry.getAttribute("normal");for(let e=0;e<t.count;e++)t.setXYZ(e,0,1,0);t.needsUpdate=!0,this.grassMaterial=new f({color:3765785})}))}refreshGeometry(){const e=this.source.landscape.options,t=(new n,new n);this.view.camera.getWorldPosition(t);const s=[];G.fromArray(this.source.position);const o=this.view.camera instanceof w?Math.min(this.view.camera.far,1e3):1e3,i=1.1*o,a=e.sections.y*e.sectionSize/-2,r=e.sections.x*e.sectionSize/-2;for(let n=0;n<e.sections.x;n++)if(L.x=r+n*e.sectionSize,!(Math.abs(t.x-L.x)>i))for(let c=0;c<e.sections.y;c++){L.z=a+c*e.sectionSize,B.copy(G).add(L);const l=B.distanceTo(t),h=`${n},${c}`,u=this.landscape.sections.find((e=>e.x===n&&e.y===c));if(l<=o){if(null==u){this.sectionCache.has(h)||this.sectionCache.set(h,this.createLandscapeMesh(this.source,e,r,a,n,c));const t=this.sectionCache.get(h);this.applyMaterial(t),this.landscape.add(t),s.push(t)}}else l>i&&this.landscape.remove(u)}v(s)}applyHeightMap(e,t,s,o=1){var n,i,a,r;const c=Math.pow(s+1,2),l=e.getAttribute("position"),h=y(null!==(n=t.points)&&void 0!==n?n:[],(e=>e.i));if(1===o)for(const e of t.points)l.setY(e.i,e.y);else for(let e=0;e<l.count;e++){const t=$(e,l.count,c);let s=0;s=t%1==0?null!==(a=null===(i=h.get(t))||void 0===i?void 0:i.y)&&void 0!==a?a:0:Math.floor(null===(r=h.get(t))||void 0===r?void 0:r.y),l.setY(e,s)}l.needsUpdate=!0,e.computeVertexNormals()}deleteOldScatterMeshes(){var e,t;const s=new Set;for(const[o,n]of null!==(t=null===(e=this.source.grass)||void 0===e?void 0:e.layers.entries())&&void 0!==t?t:[])for(const[e,t]of n.meshes.entries()){const t=`${o}-${e}`;s.add(t)}for(const e of this.scatterMeshes.keys())if(!s.has(e)){this.scatterMeshes.get(e).forEach((e=>{var t;null===(t=e.parent)||void 0===t||t.remove(e),e.dispose()})),this.scatterMeshes.delete(e)}}queueRefreshScatter(e,t=!1,s=(()=>!0)){this.refreshRequests.next({origin:e,force:t,predicate:s})}refreshScatter(a,l=!1,u=(()=>!0)){var f,w,v,g;return e(this,void 0,void 0,(function*(){l&&this.scatterGeometryCache.clear(),this.deleteOldScatterMeshes();for(const[x,z]of null!==(w=null===(f=this.source.grass)||void 0===f?void 0:f.layers.entries())&&void 0!==w?w:[])for(const[f,w]of z.meshes.entries()){const z=`${x}-${f}`;this.scatterMeshes.has(z)||this.scatterMeshes.set(z,new Map);const P=this.scatterMeshes.get(z),L=yield this.assetService.getAsset(w.assetId),B=yield this.assetManagerService.getMesh(L),G=[];if(B.scene.traverse((e=>{e instanceof s&&G.push(e)})),1!==G.length){console.log(B),console.warn("Dynamic grass only works for meshes with a single geometry.");continue}if(!(G[0]instanceof s)){console.warn("Only meshes can be used for dynamic grass. Found:",B.scene);continue}const $=G[0];let _=$.geometry;this.scatterGeometryCache.has($.geometry.uuid)?_=this.scatterGeometryCache.get($.geometry.uuid):(_=$.geometry.clone(),!0===w.normalsUp&&E(_),this.scatterGeometryCache.set($.geometry.uuid,_),null==_.userData.updatedMatrix&&(B.scene.updateMatrixWorld(),_.applyMatrix4($.matrixWorld),_.userData.updatedMatrix=!0));const U=_.getIndex().count/3;if(U>400){console.warn(`The triangle count of ${L.name} is too big ${U}. Keep it below 400`);continue}const D=null!=L.materialAssignments&&L.materialAssignments.length>0?L.materialAssignments[0].materialId:null,I=null!=D&&"null"!==D?yield t(yield this.assetService.getAsset(D),null,this.assetService,this.assetManagerService,this.shaders,!1):null;let O=null!=I?I:$.material;O instanceof m&&(O.side=p);const Y=d.degToRad(null!==(v=w.maxSlope)&&void 0!==v?v:90),X=Math.cos(Y),F=this.landscape.sections,N=F.filter(k(a,w.viewDistance)).filter((e=>!P.has(e.uuid)||l)).filter((e=>u(e)));F.filter(C(a,2*w.viewDistance)).forEach((e=>{var t;const s=P.get(e.uuid);null===(t=null==s?void 0:s.parent)||void 0===t||t.remove(s),P.delete(e.uuid)}));performance.now();const Z=this.source.landscape.options,K=Z.sectionSize,V=null!==(g=w.density)&&void 0!==g?g:1,J=null!=V?V:1,Q=Z.density,ee=K/Q,te=J,se=ee/Math.sqrt(te),oe=Math.pow(Q,2),ne=se/ee,ie=Math.floor(oe*te),ae=[0,0,0];for(const t of N)yield A((()=>e(this,void 0,void 0,(function*(){var e,s,a,u,d,f,m;t.updateWorldMatrix(!0,!1);const p=this._matrix,v=new n,g=t.geometry.getAttribute("position"),A=t.geometry.getAttribute("normal"),z=(null!==(e=this.source.vertexMaterials)&&void 0!==e?e:[]).filter((e=>e.m===t.name)),L=y(z,(e=>e.i));let B=P.get(t.uuid);if(null==B||B.count==ie&&!l||(null===(s=B.parent)||void 0===s||s.remove(B),P.delete(t.uuid),B=null),null==B){const e=new o(.15,.75).getAttribute("normal");for(let t=0;t<e.count;t++);new r({color:new c(3765785),side:M.DoubleSide});B=new i(_,O,ie),B.raycast=()=>{},B.receiveShadow=!0}const G=new h(new n,new n,new n);let[$,C,k,U]=[new n,new n,new n,new n],[D,I,Y]=[[],[],[]],[E,F,N]=[new n,new n,new n,new n];const Z=new n,K=new n,V=new n,J=new n,ee=new h(new n,new n,new n),se=new h(new n,new n,new n),re=new h(new n,new n,new n),ce=new h(new n,new n,new n);let le=0;e:for(let e=0;e<oe;e++){const s=Math.floor(e/Q);$.fromBufferAttribute(g,e+s),J.copy($).applyMatrix4(t.matrixWorld),ee.a.copy($),ee.b.fromBufferAttribute(g,e+1+s),ee.c.fromBufferAttribute(g,e+Q+1+s),se.a.copy(ee.b),se.b.copy(ee.c),se.c.fromBufferAttribute(g,e+Q+2+s),re.a.fromBufferAttribute(A,e+s),re.b.fromBufferAttribute(A,e+1+s),re.c.fromBufferAttribute(A,e+Q+1+s),ce.a.copy(re.b),ce.b.copy(re.c),ce.c.fromBufferAttribute(A,e+Q+2+s);const o=[];o[0]=null===(a=L.get(e+s))||void 0===a?void 0:a.w,o[1]=null===(u=L.get(e+1+s))||void 0===u?void 0:u.w,o[2]=null===(d=L.get(e+Q+1+s))||void 0===d?void 0:d.w,o[3]=null===(f=L.get(e+Q+2+s))||void 0===f?void 0:f.w;let n=0;for(let e=0;e<=1+ne;e+=ne)for(let s=0;s<=1+ne;s+=ne){if(le>ie)break e;if(n++,n>te)continue e;1-e>s?(C=ee.a,k=ee.b,U=ee.c,E=re.a,F=re.b,N=re.c,D=o[0],I=o[1],Y=o[2]):(C=se.a,k=se.b,U=se.c,E=ce.a,F=ce.b,N=ce.c,D=o[1],I=o[2],Y=o[3]),G.a.copy(C),G.b.copy(k),G.c.copy(U),q(G),Z.set($.x,0,$.z),Z.x=T(G.a.x,G.b.x),Z.z=T(G.a.z,G.c.z),G.getBarycoord(Z,v).toArray(ae);if(R([D,I,Y],ae,.2)!==x-1)continue;const i=[C,k,U];b(i,ae,K),b([E,F,N],ae,V);const a=i.map((e=>e.y)),r=Math.max(...a),c=Math.min(...a);if(K.y=S(K.y,c,r),null!=w.maxSlope&&w.maxSlope<90&&V.y<X)continue;const l=K.applyMatrix4(t.matrixWorld);l.y+=T(w.offsetMin,w.offsetMax);const h=T(w.scaleMin,w.scaleMax);p.makeScale(1,1,1),p.setPosition(l),p.scale(W(h)),w.alignToNormal&&H(p,l,B.matrixWorld,V),!1!==w.randomRotation&&j(p),B.setMatrixAt(le,p),le++}}B.instanceMatrix.count=le,p.makeScale(0,0,0);for(let e=le;e<ie;e++)B.setMatrixAt(e,p);B.instanceMatrix.needsUpdate=!0,P.has(t.uuid)||null===(m=this.landscape)||void 0===m||m.attach(B),P.set(t.uuid,B)}))));performance.now()}}))}stop(){this.view.removeOnLoop(this.onLoopHandler)}update(){this.view.camera&&(this.view.camera.getWorldPosition(this._cameraPosition),this._cameraPosition.distanceTo(this._lastUpdatePosition)>10&&(this._lastUpdatePosition.copy(this._cameraPosition),this.refreshGeometry(),this.refreshScatter(this._cameraPosition)))}clear(){this.scatterMeshes.forEach((e=>e.forEach((e=>{var t;return null===(t=e.parent)||void 0===t?void 0:t.remove(e)}))))}createLandscapeMesh(e,t,s,n,i,a){const r=new o(t.sectionSize,t.sectionSize,t.density,t.density);r.rotateX(Math.PI/-2);const c=this.defaultLandscapeMaterial,l=new x(r,c);l.position.x=s+i*t.sectionSize,l.position.z=n+a*t.sectionSize,l.receiveShadow=!0,l.castShadow=!1,l.userData.landscape={x:i,y:a},l.x=i,l.y=a,l.name=`${i},${a}`;const h=e.landscape.heightMaps.find((e=>e.x===i&&e.y===a));return null!=h&&this.applyHeightMap(r,h,t.density,1),r.computeBoundsTree(),l}}function $(e,t,s){const o=Math.sqrt(t),n=Math.floor(e/o)/(o-1),i=e%o/(o-1),a=Math.sqrt(s);return(s-1)*n-(a-1)*n+(a-1)*i}new Map,new l(0,0),new l(1,0),new l(0,1),new l(1,0),new l(0,1),new l(1,1),new n;const _=new u;function C(e,t){return function(s){return _.setFromObject(s).distanceToPoint(e)>t}}function k(e,t){return function(s){return _.setFromObject(s).distanceToPoint(e)<t}}function q(e){e.a.y=0,e.b.y=0,e.c.y=0}function R(e,t,s=.5){let o=-1,n=0;for(let i=0;i<e.length;i++)if(null!=e[i])for(let a=0;a<e[i].length;a++){const r=e[i][a]*t[i];r>s&&r>n&&(n=r,o=a)}return o}function T(e,t){let s=t-e,o=Math.random();return o*=s,o+=e,o}const U=new n;function W(e){return U.set(e,e,e)}new n;const D=new n,I=new n(0,1,0),O=(new a).makeRotationX(Math.PI/-2);function H(e,t,s,o){e.lookAt(D,o,I).multiply(O)}const Y=new a;function j(e){e.multiply(Y.makeRotationY(Math.random()*Math.PI/2))}function E(e){const t=e.getAttribute("normal");for(let e=0;e<t.count;e++)t.setXYZ(e,0,1,0);t.needsUpdate=!0}
|
2
2
|
/*
|
3
3
|
* Copyright (©) 2023. All rights reserved.
|
4
4
|
* See the LICENSE.md file for details.
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import{Group as e,Mesh as a}from"three";import{Color as r}from"three";import{varyingAttributes as t,float as o,mod as
|
1
|
+
import{Group as e,Mesh as a}from"three";import{Color as r}from"three";import{varyingAttributes as t,float as o,mod as n,dot as s,vec2 as c,step as i,fract as l,mix as p,rgb as d,NodeShaderMaterial as u,rgba as m,standardMaterial as f}from"three-shader-graph";import{mixColorsByLayer as h,select as x}from"../../shader-nodes";export function initLandscape(e){e.sections.y,e.sectionSize,e.sections.x,e.sectionSize;return new LandscapeGroup}export function createLandscapeDefaultMaterial(){const e=t.uv,a=o(10),x=o(7),L=n(s(c(1,1),i(c(.5,.5),l(e.multiplyScalar(a)))),o(2)),y=n(s(c(1,1),i(c(.5,.5),l(e.multiplyScalar(x.multiply(a))))),o(2)),D=p(d(4473924),d(5592405),L),M=p(D,D.addScalar(o(.1)),y),S=h({layerColors:[m(M).rgb(),...["#55DDE0","#33658A","#2F4858"].map((e=>d(e))).reverse()],enableNoise:!1}),g=new u({color:f({color:S})});return g.color=new r("#aaaaaa"),g.name="Default",g}export const defaultLandscapeMaterial=createLandscapeDefaultMaterial();export class LandscapeMesh extends a{}export class LandscapeGroup extends e{get sections(){return this.children.filter((e=>e instanceof LandscapeMesh))}}
|
2
2
|
/*
|
3
3
|
* Copyright (©) 2023. All rights reserved.
|
4
4
|
* See the LICENSE.md file for details.
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import{__awaiter as e}from"tslib";import{Subject as t}from"rxjs";import*as i from"three";import{BoxGeometry as a,Color as r,Euler as s,Fog as n,FogExp2 as o,Group as l,Material as c,Mesh as d,MeshLambertMaterial as h,MeshPhongMaterial as u,MeshStandardMaterial as m,Object3D as p,PointLight as f,Quaternion as v,SphereGeometry as g,Texture as y,Vector2 as w,Vector3 as A,Vector4 as S}from"three";import b,{SpriteRenderer as M}from"three-nebula";import{bool as D,BooleanNode as x,float as I,FloatNode as N,NodeShaderMaterial as P,rgb as j,RgbNode as E,Texture2dLookupNode as O,textureSampler2d as F,vec2 as V,Vec2Node as B,vec3 as z,Vec3Node as C,vec4 as _}from"three-shader-graph";import T from"../gameplay/actors/builtin";import{extractShaderParameters as $}from"../shader/parameter";import{groupBy as k,ArrayMap as L}from"../utils/collections";import{filterChildrenShallow as R,filterSceneShallow as U}from"../utils/three/traverse";import{AssetMeshInstance as H}from"./asset-resource-loader";import{BoxCollisionShape as W,PhysicalShapeMesh as G}from"./collision/collision-shape";import{isCollisionMesh as J}from"./collision/collision-shape-import";import{initLandscape as X}from"./landscape/landscape";import{LandscapeManager as Y}from"./landscape/landscape-manager";import{SectionGrid as q,smoothNormalsCrossMeshes as Z}from"./landscape/utils";import{createGrassMaterial as K}from"./materials/grass";import{createGrassFoliageMaterial as Q}from"./materials/grass-foliage";import{getMaterialAttribute as ee}from"./materials/utils/material-painting";import{createWaterMaterial as te}from"./materials/water";import{SerializedParamType as ie}from"./model";import{Matrix4 as ae}from"three";import{BaseActor as re}from"../gameplay/actors/actor";import{Sampler2DNode as se}from"../shader-nodes";import{StandardShader as ne}from"../shader/builtin/standard-shader";import{LambertShader as oe}from"../shader/builtin/lambert-shader";import{ShapeLibrary as le,ShapeLibraryKeys as ce}from"./objects/shapes";import{ambientLightName as de}from"./sky";import{withInjectionContext as he}from"../gameplay";import{iterateMaterials as ue}from"../utils/materials";const me={};export const shapeDefaultColor="#aaaaaa";export class SceneMaterializerLoader{constructor(e,t,i){this.dataProvider=e,this.assetsService=t,this.assetManagerService=i}get(t,i){return new SceneMaterializer(t,this.dataProvider,this.assetsService,this.assetManagerService,i,[],[],{create:()=>null,initActor:()=>e(this,void 0,void 0,(function*(){}))})}}export class SceneMaterializer{constructor(i,a,r,s,n,o,l,c){this.scene=i,this.dataProvider=a,this.assetsService=r,this.assetManagerService=s,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 t,this.removed$=new t,this.error$=new t,this.editorActorParamSnapshot=new Map,this._canBeInstancedCache=new Map,this._originalMaterials=new Map,this.originalFog=null,a.onUpdate((e=>this.update(e))),a.onRemove((e=>this.remove(e))),this.updateSubscription=r.onUpdate.subscribe((t=>e(this,void 0,void 0,(function*(){"material"==t.type?((yield this.assetsService.getAssets()).filter((e=>{var i;return(null!==(i=e.materialAssignments)&&void 0!==i?i:[]).some((e=>e.materialId===t.id))})).forEach((e=>{const t=this.findByAssetId(e.id);e.materialAssignments.forEach((e=>{t.forEach((t=>{const i=t.userData.src.materialAssignments;null!=i&&i.some((t=>t.color===e.color))||this.applyMaterial(t,e)}))}))})),U(this.scene,(e=>{var i;return null!=e.userData.src&&(null!==(i=e.userData.src.materialAssignments)&&void 0!==i?i:[]).some((e=>e.materialId===t.id))}),(e=>null!=e.userData.src)).forEach((t=>e(this,void 0,void 0,(function*(){this.deleteSceneObject(t.userData.src),this.materialize(t.userData.src)}))))):"mesh"==t.type?this.findByAssetId(t.id).forEach((e=>{De(e.userData.src.materialAssignments,t.materialAssignments).forEach((t=>{this.applyMaterial(e,t)}))})):"prefab"===t.type&&this.findByAssetId(t.id).forEach((e=>{const t=e.userData.src;this.remove(t),this.materializeAndInitActor(t)}))}))))}get actorInstances(){return Array.from(this.materializedActors.values())}prefetchAssets(){return e(this,void 0,void 0,(function*(){const e=Array.from(new Set(this.dataProvider.getObjects().filter((e=>null!=e.assetId&&"asset_mesh"==e.type)).filter((e=>e.assetId))));yield Promise.all(e.map((e=>this.assetsService.getAsset(e.assetId).then((e=>e&&this.assetManagerService.getMesh(e))))))}))}init(){return e(this,void 0,void 0,(function*(){fe.clear(),yield this.prefetchAssets(),yield Promise.all(this.dataProvider.getObjects().map((e=>this.materialize(e)))),yield this.initActorsPostInit()}))}initActorsPostInit(t=this.actorInstances){const i=t.map((t=>e(this,void 0,void 0,(function*(){var e,i,a,r;const s=t.object.userData.src,n=yield this.assetsService.getAsset(s.assetId),o=Object.assign(Object.assign({},null!==(i=null===(e=null==n?void 0:n.actor)||void 0===e?void 0:e.params)&&void 0!==i?i:{}),null!==(a=s.actor.params)&&void 0!==a?a:{});for(const e of null!==(r=s.actor.innerParams)&&void 0!==r?r:[])yield this.applyActorComponentParams(t,e.path.slice(),e.params);const l=yield ye(o,t.constructor,this.assetsService,this.assetManagerService,this.materializedActors,this.renderingView,this.shaders);Object.assign(t,l);try{return yield this.actorProvider.initActor(t)}catch(e){console.error(`Failed to initiate actor (name="${s.name}", id=${s.id})`,e)}}))));return Promise.all(i)}applyActorComponentParams(t,i,a){return e(this,void 0,void 0,(function*(){const e=i.length,r=i.shift();if(0==e){const e=yield ye(a,null,this.assetsService,this.assetManagerService,this.materializedActors,this.renderingView,this.shaders);for(const[i,a]of Object.entries(e))null!=a&&(t[i]=a)}else null!=t[r]&&(yield this.applyActorComponentParams(t[r],i,a))}))}canAssetBeInstanced(t){return e(this,void 0,void 0,(function*(){if(!this._canBeInstancedCache.has(t.assetId)){const e=yield this.createFromAsset(t);if(null==e)return!1;const i=[];e.traverse((e=>{!J(e)&&e.isMesh&&i.push(e)}));const a=1==i.length&&0==i[0].children.length,r=!0;this._canBeInstancedCache.set(t.assetId,a&&r)}return this._canBeInstancedCache.get(t.assetId)}))}initWithInstancing(){return e(this,void 0,void 0,(function*(){const t=new L;for(const i of this.dataProvider.getObjects())yield be(i,((i,a,r)=>e(this,void 0,void 0,(function*(){var e,s;const n="asset_mesh"==i.type&&(yield this.canAssetBeInstanced(i))?i.assetId+JSON.stringify(null!==(e=i.materialAssignments)&&void 0!==e?e:[]):"other";"other"!==n&&a&&(null===(s=a.children)||void 0===s?void 0:s.length)>0&&a.children.splice(a.children.findIndex((e=>e.id===i.id)),1),"other"===n&&a||t.push(n,Object.assign(Object.assign({},i),{parentTransform:r}))}))));for(const[e,i]of t.entries())if("other"!==e&&i.length>0){const e=yield this.createFromAsset(i[0]);if(null==e)continue;const t=yield this.createInstancedMesh(i,e),a=new H;a.add(t),a.userData.src=i[0],e instanceof H&&(a.collisionShapes=e.collisionShapes),a.castShadow=!1,a.receiveShadow=!1,this.scene.add(a)}else yield Promise.all(i.map((e=>this.materialize(e))));yield this.initActorsPostInit()}))}createInstancedMesh(t,a){var r,n;return e(this,void 0,void 0,(function*(){const e=a.children.filter((e=>!J(e)))[0],o=yield this.assetsService.getAsset(t[0].assetId);yield this.applyMaterials(a,De(t[0].materialAssignments,o.materialAssignments)),e.updateMatrix();const l=e.geometry.clone().applyMatrix4(e.matrix),c=new i.InstancedMesh(l,e.material.clone(),t.length);c.material.side=i.FrontSide;for(let e=0;e<t.length;e++){const a=(new i.Matrix4).compose((new A).fromArray(t[e].position),(new v).setFromEuler((new s).fromArray(t[e].rotation)),(new A).fromArray(t[e].scale)),r=(new ae).copy(t[e].parentTransform).multiply(a);c.setMatrixAt(e,r)}return c.castShadow=null===(r=o.castShadow)||void 0===r||r,c.receiveShadow=null===(n=o.receiveShadow)||void 0===n||n,c}))}remove(e){if("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)}const t=this.sceneObjectMap.get(e.id);null==t||t.parent.remove(t),this.sceneObjectMap.delete(e.id),this.components.filter((t=>{var i;return(null===(i=t.object.userData.src)||void 0===i?void 0:i.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 U(this.scene,(t=>{var i;return(null===(i=t.userData.src)||void 0===i?void 0:i.assetId)==e}),(e=>null!=e.userData.src))}applyMaterials(e,t){return Promise.all(t.filter((e=>"null"!==e.materialId)).map((t=>this.applyMaterial(e,t))))}applyMaterial(t,i){const a=[];return t.traverse((t=>e(this,void 0,void 0,(function*(){if(t instanceof d||t.isMesh)for(const e of ue(t.material))e.hasOwnProperty("color")&&a.push(t)})))),Promise.all(a.map((t=>e(this,void 0,void 0,(function*(){var e,a,r,s;if(t.material instanceof Array)for(let r=0;r<t.material.length;r++){const s=t.material[r];if(null==s.color)continue;const n="#"+s.color.getHexString(),o=s.name;if(n===i.color&&(s.name===i.name||null==i.name)||t.userData["originalColor_"+r]===i.color&&t.userData["originalMaterialName_"+r]===i.name){const s=yield this.assetsService.getAsset(i.materialId),l=t.material[r];s&&(t.material[r]=yield materialFromAsset(s,this.renderingView,this.assetsService,this.assetManagerService,this.shaders),t.userData["originalColor_"+r]=null!==(e=t.userData["originalColor_"+r])&&void 0!==e?e:n,t.userData["originalMaterialName_"+r]=null!==(a=t.userData["originalMaterialName_"+r])&&void 0!==a?a:o,this.inEditor&&this._originalMaterials.set(t.id+"#"+r,l))}}else{const e="#"+t.material.color.getHexString(),a=t.material.name;if(e===i.color&&(t.material.name===i.name||null==i.name)||t.userData.originalColor===i.color&&t.userData.originalName===i.name){const n=yield this.assetsService.getAsset(i.materialId),o=t.material;n&&(t.material=yield materialFromAsset(n,this.renderingView,this.assetsService,this.assetManagerService,this.shaders),t.userData.originalColor=null!==(r=t.userData.originalColor)&&void 0!==r?r:e,t.userData.originalMaterialName=null!==(s=t.userData.originalMaterialName)&&void 0!==s?s:a,this.inEditor&&this._originalMaterials.set(t.id,o))}}})))))}unapplyMaterials(t){t.traverse((t=>e(this,void 0,void 0,(function*(){var e,i;if(t instanceof d)if(t.material instanceof Array)for(let i=0;i<t.material.length;i++)t.material[i]=null!==(e=this._originalMaterials.get(t.id+"#"+i))&&void 0!==e?e:t.material[i];else t.material=null!==(i=this._originalMaterials.get(t.id))&&void 0!==i?i:t.material}))))}updateActors(t){this.actorTypes=t;const i=new Set(Object.values(T));U(this.scene,(e=>{var t,a;return(null===(t=e.userData.src)||void 0===t?void 0:t.id)&&"actor"===e.userData.src.type&&this.materializedActors.has(null===(a=e.userData.src)||void 0===a?void 0:a.id)&&!i.has(e.userData.src.actor.type)})).forEach((t=>e(this,void 0,void 0,(function*(){this.remove(t.userData.src),yield this.materializeAndInitActor(t.userData.src)}))))}updateShaders(e){this.shaders=e;for(const[e,t]of fe.entries())t.userData.customShaderName&&fe.delete(e);this.landscapeManagers.forEach((t=>t.updateShaders(e))),U(this.scene,(e=>!0)).forEach((e=>{e.traverse((e=>{var t,i;if(e instanceof d){if(null!=(null===(t=e.material.userData)||void 0===t?void 0:t.customShaderName)){const t=null===(i=function(e,t){if(t(e))return e;let i;return e.traverseAncestors((e=>{null==i&&t(e)&&(i=e)})),i}(e,(e=>{var t;return null!=(null===(t=e.userData.src)||void 0===t?void 0:t.id)})))||void 0===i?void 0:i.userData.src;null!=t&&this.update(t)}}}))}))}update(t){var i;return e(this,void 0,void 0,(function*(){const e=this.sceneObjectMap.get(t.id);if(e){const s=this.findParent(t);if(null!=s&&s.uuid!=e.uuid?s.attach(e):console.error("Parent is wrong"),"prefab"!==t.type){this.unapplyMaterials(e);this.inEditor&&t.hidden&&!1?e.traverse((e=>{e instanceof d&&(e.material.wireframe=!0)})):(e.traverse((e=>{e instanceof d&&(e.material.wireframe=!1)})),(null!==(i=t.materialAssignments)&&void 0!==i?i:[]).forEach((t=>this.applyMaterial(e,t))))}if(e.position.fromArray(t.position),e.scale.fromArray(t.scale),e.rotation.fromArray(t.rotation),this.applyVertexMaterials(t,e),"light"==t.type)if("point"==t.light.type){const i=e;i.color=new r(t.light.point.color),i.intensity=t.light.point.intensity,i.decay=t.light.point.decay,i.castShadow=t.light.point.castShadow,i.distance=Math.max(t.light.point.distance,0)}else"directional"===t.light.type?this.applyDirectionalLight(t.light.directional):"ambient"===t.light.type&&this.applyDirectionalAmbientLight(e,t.light.ambient);else if("landscape"===t.shape)this.applyHeightMaps(e,t.landscape.heightMaps),this.inEditor&&this.landscapeManagers.filter((e=>e.source.id===t.id)).forEach((e=>e.queueRefreshScatter(this.renderingView.camera.position,!0,(e=>!0))));else if("global_fog"===t.type){const e=(this.scene.fog instanceof o?"density":"linear")!==t.fog.type;this.scene.fog=Ae(t.fog),e&&(a=this.scene).traverse((e=>{if(e instanceof d){const t=e.material;t instanceof P&&(a.fog instanceof n?(t.uniforms.fogFar.value=a.fog.far,t.uniforms.fogNear.value=a.fog.near):a.fog instanceof o&&(t.uniforms.density={value:a.fog.density}),t.needsUpdate=!0,t.uniformsNeedUpdate=!0)}})),this.fixFogColor()}else if("actor"===t.type){if(this.materializedActors.has(t.id)){const e=this.editorActorParamSnapshot.get(t.id);null!=e&&e===JSON.stringify(t.actor)||(console.log("Rematerializing actor because parameters changed"),this.remove(t),yield this.materializeAndInitActor(t))}}else if("shape_mesh"===t.type){const i=yield this.createMeshByShape(t.shape,e.material,t.shapeParams);e instanceof G&&(e.geometry=i.geometry,e.collisionShape=i.collisionShape)}("asset_mesh"===t.type||"shape_mesh"===t.type&&"landscape"!==t.shape)&&pe(e,t.castShadow,t.receiveShadow),t.name&&t.name.length>0&&(e.name=t.name),this.updated$.next({object:e,source:t})}else{const e=yield this.materializeAndInitActor(t);this.updated$.next({object:e,source:t})}var a}))}materializeAndInitActor(t,i=this.findParent(t)){return e(this,void 0,void 0,(function*(){const e=yield this.materialize(t,i);if("actor"===t.type){const e=this.materializedActors.get(t.id);null!=e?yield this.initActorsPostInit([e]):console.error(`Something went wrong when creating actor ${t.id}`)}return e}))}findParent(e){const t=this.dataProvider.getObjects().flatMap((t=>t.id===e.id?null:R(t,(t=>{var i;return null===(i=t.children)||void 0===i?void 0:i.some((t=>t.id===e.id))}),(()=>!0))))[0];return null==t?this.scene:null!=t?U(this.scene,(e=>{var i,a;return(null===(a=null===(i=e.userData)||void 0===i?void 0:i.src)||void 0===a?void 0:a.id)===t.id}),(e=>{var t;return null!=(null===(t=e.userData)||void 0===t?void 0:t.src)}))[0]:void 0}fixFogColor(){!0===this.renderingView.options.enableOutlines&&(this.scene.fog.color=new r(this.scene.fog.color).convertSRGBToLinear())}findMeshWithGeometry(e){let t;return e.traverse((e=>{e instanceof d&&e.geometry&&(t=e)})),t}applyVertexMaterials(e,t){if(null==e.vertexMaterials||0===e.vertexMaterials.length)return;const i=k(e.vertexMaterials,(e=>e.m));t.traverse((e=>{if(e instanceof d){const t=ee(e,!1);if(null!=t){for(let e=0;e<t.array.length;e++)t.setX(e,0);t.needsUpdate=!0}}}));const a=new Set;for(const[e,r]of i.entries()){const i=null!=e?t.getObjectByName(e):this.findMeshWithGeometry(t);let s=!1;if(null==i)return void console.warn(`Failed to apply vertex materials on mesh with name "${e}"`);const n=ee(i,!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]),s=!0;s&&a.add(e)}this.inEditor&&this.landscapeManagers.filter((t=>t.source.id===e.id)).forEach((e=>e.queueRefreshScatter(this.renderingView.camera.position,!0,(e=>a.has(e.name)))))}materialize(t,i,r=!1){var s,n,o,c;return e(this,void 0,void 0,(function*(){let e;switch(t.type){case"asset_mesh":e=yield this.createFromAsset(t);break;case"shape_mesh":e=yield this.createFromShape(t);break;case"light":e=yield this.createLight(t);break;case"particles":e=yield this.createParticleSystem(t),t.collisionDetection=!1;break;case"global_fog":this.scene.fog=Ae(t.fog),this.fixFogColor(),e=new l;break;case"actor":e=yield this.createFromActor(t);break;case"group":e=new l;break;case"prefab":e=yield this.createFromPrefabAsset(t);break;default:throw console.log(t),new Error("unknown type "+t.type)}if(null!=e){if(t.name&&t.name.length>0&&(e.name=t.name),e.position.fromArray(t.position),e.scale.fromArray(t.scale),e.rotation.fromArray(t.rotation),r||(e.userData.src=t),!this.inEditor){const i=null!==(s=t.components)&&void 0!==s?s:[],a=null!=t.assetId&&null!==(o=null===(n=yield this.assetsService.getAsset(t.assetId))||void 0===n?void 0:n.components)&&void 0!==o?o:[];i.push(...a),i.length>0?e.userData.componentRefs=yield Promise.all(i.map(((i,a)=>this.createComponent(e,t,i,a)))):"asset_mesh"==t.type&&function(e){e.traverse((e=>{e.matrixAutoUpdate=!1,e.matrixWorldNeedsUpdate=!1}));const t=e.updateMatrixWorld;e.updateMatrixWorld=function(){t.apply(e),e.updateMatrixWorld=function(){}}}(e)}if(this.inEditor){let t=null;e instanceof G&&(t=function(e){if(e instanceof W)return new d(new a(...e.offset.toArray()),Se);return null}(e.collisionShape)),null!=t&&(t.layers.disable(0),t.layers.enable(18),t.scale.multiplyScalar(1.1),e.add(t))}return this.objectMap.set(e.uuid,t),this.sceneObjectMap.set(t.id,e),null==i?this.scene.add(e):null==i||i.add(e),null===(c=t.children)||void 0===c||c.forEach((t=>{this.materialize(t,e,r)})),e}}))}createComponent(t,i,a,r){return e(this,void 0,void 0,(function*(){const e=new me[a.path+"/"+a.className],s=i.id+r;e.id=s,e.object=t;for(const t of a.params)null!=t.value&&(e[t.name]=t.value);return this.components.push(e),s}))}createFromActor(t){var i,a,r;return e(this,void 0,void 0,(function*(){const e=null!==(a=null===(i=this.actorTypes.find((e=>{var i;return e.name===(null===(i=t.actor)||void 0===i?void 0:i.type)})))||void 0===i?void 0:i.type)&&void 0!==a?a:T[null===(r=t.actor)||void 0===r?void 0:r.type];if(null==e)return null;this.inEditor&&this.editorActorParamSnapshot.set(t.id,JSON.stringify(t.actor));const n=yield this.actorProvider.create(e,(new A).fromArray(t.position),(new s).fromArray(t.rotation),!0);return this.materializedActors.set(t.id,n),null==n?void 0:n.object}))}cleanup(){this.materializedActors.clear()}createFromShape(t){var i,a,s;return e(this,void 0,void 0,(function*(){const e=this.inEditor&&t.hidden;let n;if("landscape"==t.shape)n=this.createLandscape(t);else{const s=new m({color:new r("#aaaaaa"),visible:this.inEditor||!t.hidden,wireframe:e}),o=yield this.createMeshByShape(t.shape,s,t.shapeParams);o.castShadow=null===(i=t.castShadow)||void 0===i||i,o.receiveShadow=null!==(a=t.castShadow)&&void 0!==a&&a,t.collisionDetection||(o.collisionShape=null),n=o,this._originalMaterials.set(n.id,o.material),n.traverse((e=>{}))}return e||((null!==(s=t.materialAssignments)&&void 0!==s?s:[]).filter((e=>null!=e.materialId)).forEach((e=>this.applyMaterial(n,e))),this.applyVertexMaterials(t,n)),n}))}createLandscape(e){var t;if(null==(null===(t=e.landscape)||void 0===t?void 0:t.options))return console.error(`No landscape options exist on scene object ${e.id} ${e.name}`),new l;const i=X(e.landscape.options);this.applyHeightMaps(i,e.landscape.heightMaps,!0);const a=new Y(e,this.renderingView,i,this.assetManagerService,this.assetsService,this.shaders,(t=>{var i;(null!==(i=e.materialAssignments)&&void 0!==i?i:[]).filter((e=>null!=e.materialId)).forEach((e=>this.applyMaterial(t,e)))}));return this.landscapeManagers.push(a),a.refreshGeometry(),i}applyHeightMaps(e,t,i=!1){const a=new q(e.sections);for(const e of null!=t?t:[]){const t=a.find(e.x,e.y);if(!t)return;const i=t.geometry.getAttribute("position");for(const t of e.points)i.setY(t.i,t.y);i.needsUpdate=!0}const r=e.sections;r.forEach((e=>{e.geometry.computeBoundsTree(),e.geometry.computeVertexNormals()})),this.inEditor&&!i||setTimeout((()=>Z(r)),50)}createMeshByShape(t,i,a={}){return e(this,void 0,void 0,(function*(){if("landscape"!==t&&ce.includes(t)){const r=yield function(t){return e(this,void 0,void 0,(function*(){const e={};for(const[i,a]of Object.entries(t)){const t=yield we(a,null,null,null);null!=t&&(e[i]=t)}return e}))}(null!=a?a:{});return"cylinder"==t&&r.openEnded,new G(le[t].geometry(r),i,le[t].collision(r))}throw new Error(`Unsupported shape '${t}'`)}))}createFromAsset(t){var i,a,r,s;return e(this,void 0,void 0,(function*(){const e=yield this.assetsService.getAsset(t.assetId);if(null==e)return void console.warn(`Can not find asset with id ${t.assetId} and name ${t.name}`);let{scene:n}=yield this.assetManagerService.getMesh(e);De(t.materialAssignments,e.materialAssignments).forEach((e=>this.applyMaterial(n,e)));const o=null===(a=null!==(i=t.receiveShadow)&&void 0!==i?i:!!e.receiveShadow)||void 0===a||a,l=null!==(s=null!==(r=t.castShadow)&&void 0!==r?r:!!e.castShadow)&&void 0!==s&&s;return n.receiveShadow=o,pe(n,l,o),t.collisionDetection||(n.collisionShapes=[]),this.applyVertexMaterials(t,n),n.traverse((e=>{e instanceof d&&"computeBoundsTree"in e.geometry&&e.geometry.computeBoundsTree()})),n}))}createFromPrefabAsset(t){return e(this,void 0,void 0,(function*(){const e=yield this.assetsService.getAsset(t.assetId);if(null==e)return void console.warn(`Can not find asset with id ${t.assetId} and name ${t.name}`);const i=new l;return e.prefab.objects.filter((e=>"global_fog"!==e.type)).forEach((e=>this.materialize(e,i,!0))),i}))}createParticleSystem(t){return e(this,void 0,void 0,(function*(){const e=yield this.assetsService.getAsset(t.assetId),a=new p;return yield b.fromJSONAsync(e.particleSystem,i).then((e=>{const t=new M(a,i);e.addRenderer(t),this.renderingView.onLoop((t=>e.update()))})),a}))}createLight(t){var i;return e(this,void 0,void 0,(function*(){if("point"===t.light.type){const e=new f(t.light.point.color,t.light.point.intensity,t.light.point.distance,t.light.point.decay);if(e.castShadow=null===(i=t.light.point.castShadow)||void 0===i||i,this.inEditor){const t=new g(.3,10,10),i=new m({color:new r(16771709)}),a=new d(t,i);e.add(a)}return e}return"directional"===t.light.type?(this.applyDirectionalLight(t.light.directional),new l):"ambient"===t.light.type?(this.applyDirectionalAmbientLight(null,t.light.ambient),new l):void 0}))}applyDirectionalAmbientLight(e,t){const i=this.scene.children.find((e=>e.name===de));null!=i?(i.intensity=t.intensity,i.color.set(t.color),i.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()}}function pe(e,t,i){e.castShadow=t,e.receiveShadow=i,e.traverse((e=>{e.castShadow=t,e.receiveShadow=i}))}const fe=new Map,ve=new h({color:16711935}),ge=new Map;export function materialFromAsset(t,a,s,n,o,l=!0){var c,d,h,m,p,f,v,g,y,w,A;return e(this,void 0,void 0,(function*(){const e=JSON.stringify(t.material);if(l&&fe.has(e))return fe.get(e);const S={opacity:t.material.params.opacity,map:null,emissive:null!==(c=t.material.params.emissive)&&void 0!==c?c:null,metalness:null!==(d=t.material.params.metalness)&&void 0!==d?d:0,flatShading:null!==(h=t.material.params.flatShading)&&void 0!==h&&h,color:new r(t.material.params.color).convertSRGBToLinear(),transparent:null!=t.material.params.opacity&&t.material.params.opacity<1},b={};if(null!=t.material.params.map){const e=t.material.params.map,i=yield s.getAsset(e);null!=i&&(S.map=yield n.getTexture(i))}let M;switch(t.material.type){case"phong":M=new u(Object.assign(Object.assign({},S),b));break;case"water":M=te(S,a);break;case"grassFoliage":M=Q({color:S.color,map:S.map},a);break;case"grass":M=K(Object.assign(Object.assign({},S),{colorTwo:new r(t.material.params.colorTwo),colorThree:new r(t.material.params.colorThree)}),a);break;case"standard":case"lambert":case"shader":const e=null!==(m={standard:ne,lambert:oe}[t.material.type])&&void 0!==m?m:null===(p=o.find((e=>e.name==t.material.shader)))||void 0===p?void 0:p.type;if(e){const i=new e,r=yield ye(null!==(v=null===(f=t.material)||void 0===f?void 0:f.shaderParams)&&void 0!==v?v:{},e,s,n,null,a,o);Object.assign(i,r);try{M=i.build()}catch(e){console.log("Shader runtime error: "+e),ge.has(t.material.shader)||ge.set(t.material.shader,ve.clone()),M=ge.get(t.material.shader)}M.userData.customShaderName=t.material.shader}else console.warn("Missing shader implementation with name "+t.material.shader),M=ve;break;default:throw new Error("Unsupported material type"+t.material.type)}return null==a||a.csm.setupMaterial(M),l&&fe.set(e,M),M.side=null!==(y=null!==(g=t.material.side)&&void 0!==g?g:M.side)&&void 0!==y?y:i.FrontSide,M.transparent=null!==(A=null!==(w=t.material.transparent)&&void 0!==w?w:S.transparent)&&void 0!==A&&A||M.transparent,M}))}function ye(t,i,a,r,s,n,o){return e(this,void 0,void 0,(function*(){const e={};for(const[i,l]of Object.entries(t)){const t=yield we(l,a,r,s,n,o);null!=t&&(e[i]=t)}return e}))}function we(t,i,a,n,o,l){return e(this,void 0,void 0,(function*(){if(null==t||null==t.value||""==t.value)return null;const e=t.value;switch(t.type){case ie.Number:case ie.FloatNode:let c="string"==typeof e?parseFloat(e):e;return t.type===ie.FloatNode?I(c):c;case ie.Texture:return yield a.getTexture(yield i.getAsset(e));case ie.Sampler2DNode:return F(yield a.getTexture(yield i.getAsset(e)));case ie.Boolean:return e;case ie.BooleanNode:return D(e);case ie.Vector2:case ie.Vec2Node:if("object"==typeof e){const i=e instanceof Array?(new w).fromArray(e):new w(i.x,i.y);return t.type===ie.Vec2Node?V(i):i}return null;case ie.Vector3:case ie.Vec3Node:if("object"==typeof e){const i=e instanceof Array?(new A).fromArray(e):new A(i.x,i.y,i.z);return t.type===ie.Vec3Node?z(i):i}return null;case ie.Color:case ie.RgbNode:const d=new r(e).convertSRGBToLinear();return t.type===ie.RgbNode?j(d):d;case ie.String:return e;case ie.BaseActor:const h=e;return null==n&&console.warn("Class parameters can not be prepared as actors are not passed in"),null==n?void 0:n.get(h);case ie.Euler:const u=e;return(new s).fromArray(u);case ie.Object3D:return(yield a.getMesh(yield i.getAsset(e))).scene;case ie.Material:return yield materialFromAsset(yield i.getAsset(e),o,i,a,l);case ie.AudioBuffer:return yield a.getAudio(yield i.getAsset(e))}return null}))}function Ae(e){var t,i;return"linear"===e.type?new n(new r(e.color),null!==(t=e.near)&&void 0!==t?t:100,null!==(i=e.far)&&void 0!==i?i:1e3):"density"===e.type?new o(e.color,e.density):void console.warn("Invalid fog type",e)}const Se=new m({color:4229780});function be(t,a,r,n){var o;return e(this,void 0,void 0,(function*(){null==n&&(n=(new ae).identity()),yield a(t,r,n);const e=n.clone().multiply(function(e,t){return t.compose((new A).fromArray(e.position),(new v).setFromEuler((new s).fromArray(e.rotation)),(new A).fromArray(e.scale))}(t,new i.Matrix4));return Promise.all((null!==(o=t.children)&&void 0!==o?o:[]).map((i=>be(i,a,t,e))))}))}function Me(e){const t=e.constructor.prototype;return t instanceof Number||e===Number?ie.Number:t instanceof N||"function"==typeof e.prototype.isFloat?ie.FloatNode:t instanceof y||e===y||e.isTexture?ie.Texture:t instanceof se||e===O?ie.Sampler2DNode:t instanceof Boolean||e===Boolean?ie.Boolean:t instanceof x?ie.BooleanNode:t instanceof r||e==r?ie.Color:t instanceof E||"function"==typeof e.prototype.isRgb?ie.RgbNode:t instanceof w||e==w?ie.Vector2:t instanceof B||"function"==typeof e.prototype.isVec2?ie.Vec2Node:t instanceof A||e==A?ie.Vector3:t instanceof C||"function"==typeof e.prototype.isVec3?ie.Vec3Node:t instanceof String||e===String?ie.String:t instanceof re||e==re||e.prototype instanceof re||e.prototype==re?ie.BaseActor:t instanceof s||e==s?ie.Euler:t instanceof p||e==p?ie.Object3D:t instanceof c||e==c?ie.Material:t instanceof AudioBuffer||e==AudioBuffer?ie.AudioBuffer:void console.warn("Failed to map parameter type to serialized version",{type:e})}export function prepareCustomParams(e,t,i={}){return Object.fromEntries(e.map((e=>{var a,r,s;return[e.name,{type:Me(e.type),value:null!==(s=null!==(r=null===(a=t[e.name])||void 0===a?void 0:a.value)&&void 0!==r?r:i[e.name])&&void 0!==s?s:Ie.get(Me(e.type))}]})))}export function prepareCustomParamsFromType(e,t,i=null){const a=$(e);if(0===a.length)return{};let r;null!=i?he(i,(()=>{r=i.get(e)})):r=new e;const s={};for(const e of a){const t=r[e.name];if(null!=t){const i=serializeCustomParameter(e.type,t);null!=i&&(s[e.name]=i)}}return prepareCustomParams(a,t,s)}export function serializeCustomParameter(e,t){function i(){console.error("Failed to serialize value",{type:e,value:t})}switch(e){case Number:case Boolean:return t;case w:return t instanceof w?t.toArray():void i();case A:return t instanceof A?t.toArray():void i();case S:return t instanceof S?t.toArray():void i();case r:return t instanceof r?"#"+t.getHexString():"string"==typeof t?t:"number"==typeof t?"#"+new r(t).getHexString():void i();case String:return t;case s:return t instanceof s?t.toArray():void i()}}function De(e,t){return function(e,t,i){const a=[],r=new Set;for(const s of[...null!=e?e:[],...null!=t?t:[]]){const e=i(s);r.has(e)||(r.add(e),a.push(s))}return a}((null!=e?e:[]).filter((e=>xe(e.materialId))),(null!=t?t:[]).filter((e=>xe(e.materialId))),(e=>e.color+e.name))}function xe(e){return"null"!=e&&null!=e}const Ie=new Map([[ie.RgbNode,"#000000"],[ie.Color,"#000000"],[ie.Vector4,[0,0,0,0]],[ie.Vec4Node,[0,0,0,0]],[ie.Vector3,[0,0,0]],[ie.Vec3Node,[0,0,0]],[ie.Vector2,[0,0]],[ie.Vec2Node,[0,0]],[ie.Euler,[0,0,0,"XYZ"]]]);
|
1
|
+
import{__awaiter as e}from"tslib";import{Subject as t}from"rxjs";import*as i from"three";import{BoxGeometry as a,Color as r,Euler as s,Fog as n,FogExp2 as o,Group as l,Material as c,Mesh as d,MeshLambertMaterial as h,MeshPhongMaterial as u,MeshStandardMaterial as m,Object3D as p,PointLight as f,Quaternion as v,SphereGeometry as g,Texture as y,Vector2 as w,Vector3 as A,Vector4 as S}from"three";import b,{SpriteRenderer as M}from"three-nebula";import{bool as D,BooleanNode as I,float as x,FloatNode as P,NodeShaderMaterial as N,rgb as j,RgbNode as E,Texture2dLookupNode as O,textureSampler2d as F,vec2 as V,Vec2Node as B,vec3 as z,Vec3Node as C,vec4 as _}from"three-shader-graph";import T from"../gameplay/actors/builtin";import{extractShaderParameters as $}from"../shader/parameter";import{groupBy as k,ArrayMap as L}from"../utils/collections";import{filterChildrenShallow as R,filterSceneShallow as U}from"../utils/three/traverse";import{AssetMeshInstance as H}from"./asset-resource-loader";import{BoxCollisionShape as W,PhysicalShapeMesh as G}from"./collision/collision-shape";import{isCollisionMesh as J}from"./collision/collision-shape-import";import{initLandscape as X}from"./landscape/landscape";import{LandscapeManager as Y}from"./landscape/landscape-manager";import{SectionGrid as q,smoothNormalsCrossMeshes as Z}from"./landscape/utils";import{createGrassMaterial as K}from"./materials/grass";import{createGrassFoliageMaterial as Q}from"./materials/grass-foliage";import{getMaterialAttribute as ee}from"./materials/utils/material-painting";import{createWaterMaterial as te}from"./materials/water";import{SerializedParamType as ie}from"./model";import{Matrix4 as ae}from"three";import{BaseActor as re}from"../gameplay/actors/actor";import{Sampler2DNode as se}from"../shader-nodes";import{StandardShader as ne}from"../shader/builtin/standard-shader";import{LambertShader as oe}from"../shader/builtin/lambert-shader";import{ShapeLibrary as le,ShapeLibraryKeys as ce}from"./objects/shapes";import{ambientLightName as de}from"./sky";import{withInjectionContext as he}from"../gameplay";import{iterateMaterials as ue}from"../utils/materials";const me={};export const shapeDefaultColor="#aaaaaa";export class SceneMaterializerLoader{constructor(e,t,i){this.dataProvider=e,this.assetsService=t,this.assetManagerService=i}get(t,i){return new SceneMaterializer(t,this.dataProvider,this.assetsService,this.assetManagerService,i,[],[],{create:()=>null,initActor:()=>e(this,void 0,void 0,(function*(){}))})}}export class SceneMaterializer{constructor(i,a,r,s,n,o,l,c){this.scene=i,this.dataProvider=a,this.assetsService=r,this.assetManagerService=s,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 t,this.removed$=new t,this.error$=new t,this.editorActorParamSnapshot=new Map,this._canBeInstancedCache=new Map,this._originalMaterials=new Map,this.originalFog=null,a.onUpdate((e=>this.update(e))),a.onRemove((e=>this.remove(e))),this.updateSubscription=r.onUpdate.subscribe((t=>e(this,void 0,void 0,(function*(){"material"==t.type?((yield this.assetsService.getAssets()).filter((e=>{var i;return(null!==(i=e.materialAssignments)&&void 0!==i?i:[]).some((e=>e.materialId===t.id))})).forEach((e=>{const t=this.findByAssetId(e.id);e.materialAssignments.forEach((e=>{t.forEach((t=>{const i=t.userData.src.materialAssignments;null!=i&&i.some((t=>t.color===e.color))||this.applyMaterial(t,e)}))}))})),U(this.scene,(e=>{var i;return null!=e.userData.src&&(null!==(i=e.userData.src.materialAssignments)&&void 0!==i?i:[]).some((e=>e.materialId===t.id))}),(e=>null!=e.userData.src)).forEach((t=>e(this,void 0,void 0,(function*(){this.deleteSceneObject(t.userData.src),this.materialize(t.userData.src)}))))):"mesh"==t.type?this.findByAssetId(t.id).forEach((e=>{De(e.userData.src.materialAssignments,t.materialAssignments).forEach((t=>{this.applyMaterial(e,t)}))})):"prefab"===t.type&&this.findByAssetId(t.id).forEach((e=>{const t=e.userData.src;this.remove(t),this.materializeAndInitActor(t)}))}))))}get actorInstances(){return Array.from(this.materializedActors.values())}prefetchAssets(){return e(this,void 0,void 0,(function*(){const e=Array.from(new Set(this.dataProvider.getObjects().filter((e=>null!=e.assetId&&"asset_mesh"==e.type)).filter((e=>e.assetId))));yield Promise.all(e.map((e=>this.assetsService.getAsset(e.assetId).then((e=>e&&this.assetManagerService.getMesh(e))))))}))}init(){return e(this,void 0,void 0,(function*(){fe.clear(),yield this.prefetchAssets(),yield Promise.all(this.dataProvider.getObjects().map((e=>this.materialize(e)))),yield this.initActorsPostInit()}))}initActorsPostInit(t=this.actorInstances){const i=t.map((t=>e(this,void 0,void 0,(function*(){var e,i,a,r;const s=t.object.userData.src,n=yield this.assetsService.getAsset(s.assetId),o=Object.assign(Object.assign({},null!==(i=null===(e=null==n?void 0:n.actor)||void 0===e?void 0:e.params)&&void 0!==i?i:{}),null!==(a=s.actor.params)&&void 0!==a?a:{});for(const e of null!==(r=s.actor.innerParams)&&void 0!==r?r:[])yield this.applyActorComponentParams(t,e.path.slice(),e.params);const l=yield ye(o,t.constructor,this.assetsService,this.assetManagerService,this.materializedActors,this.renderingView,this.shaders);Object.assign(t,l);try{return yield this.actorProvider.initActor(t)}catch(e){console.error(`Failed to initiate actor (name="${s.name}", id=${s.id})`,e)}}))));return Promise.all(i)}applyActorComponentParams(t,i,a){return e(this,void 0,void 0,(function*(){const e=i.length,r=i.shift();if(0==e){const e=yield ye(a,null,this.assetsService,this.assetManagerService,this.materializedActors,this.renderingView,this.shaders);for(const[i,a]of Object.entries(e))null!=a&&(t[i]=a)}else null!=t[r]&&(yield this.applyActorComponentParams(t[r],i,a))}))}canAssetBeInstanced(t){return e(this,void 0,void 0,(function*(){if(!this._canBeInstancedCache.has(t.assetId)){const e=yield this.createFromAsset(t);if(null==e)return!1;const i=[];e.traverse((e=>{!J(e)&&e.isMesh&&i.push(e)}));const a=1==i.length&&0==i[0].children.length,r=!0;this._canBeInstancedCache.set(t.assetId,a&&r)}return this._canBeInstancedCache.get(t.assetId)}))}initWithInstancing(){return e(this,void 0,void 0,(function*(){const t=new L;for(const i of this.dataProvider.getObjects())yield be(i,((i,a,r)=>e(this,void 0,void 0,(function*(){var e,s;const n="asset_mesh"==i.type&&(yield this.canAssetBeInstanced(i))?i.assetId+JSON.stringify(null!==(e=i.materialAssignments)&&void 0!==e?e:[]):"other";"other"!==n&&a&&(null===(s=a.children)||void 0===s?void 0:s.length)>0&&a.children.splice(a.children.findIndex((e=>e.id===i.id)),1),"other"===n&&a||t.push(n,Object.assign(Object.assign({},i),{parentTransform:r}))}))));for(const[e,i]of t.entries())if("other"!==e&&i.length>0){const e=yield this.createFromAsset(i[0]);if(null==e)continue;const t=yield this.createInstancedMesh(i,e),a=new H;a.add(t),a.userData.src=i[0],e instanceof H&&(a.collisionShapes=e.collisionShapes),a.castShadow=!1,a.receiveShadow=!1,this.scene.add(a)}else yield Promise.all(i.map((e=>this.materialize(e))));yield this.initActorsPostInit()}))}createInstancedMesh(t,a){var r,n;return e(this,void 0,void 0,(function*(){const e=a.children.filter((e=>!J(e)))[0],o=yield this.assetsService.getAsset(t[0].assetId);yield this.applyMaterials(a,De(t[0].materialAssignments,o.materialAssignments)),e.updateMatrix();const l=e.geometry.clone().applyMatrix4(e.matrix),c=new i.InstancedMesh(l,e.material.clone(),t.length);c.material.side=i.FrontSide;for(let e=0;e<t.length;e++){const a=(new i.Matrix4).compose((new A).fromArray(t[e].position),(new v).setFromEuler((new s).fromArray(t[e].rotation)),(new A).fromArray(t[e].scale)),r=(new ae).copy(t[e].parentTransform).multiply(a);c.setMatrixAt(e,r)}return c.castShadow=null===(r=o.castShadow)||void 0===r||r,c.receiveShadow=null===(n=o.receiveShadow)||void 0===n||n,c}))}remove(e){if("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)}const t=this.sceneObjectMap.get(e.id);null==t||t.parent.remove(t),this.sceneObjectMap.delete(e.id),this.components.filter((t=>{var i;return(null===(i=t.object.userData.src)||void 0===i?void 0:i.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 U(this.scene,(t=>{var i;return(null===(i=t.userData.src)||void 0===i?void 0:i.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))))}applyMaterial(t,i){const a=[];return t.traverse((t=>e(this,void 0,void 0,(function*(){if(t instanceof d||t.isMesh)for(const e of ue(t.material))e.hasOwnProperty("color")&&a.push(t)})))),Promise.all(a.map((t=>e(this,void 0,void 0,(function*(){var e,a,r,s;if(t.material instanceof Array)for(let r=0;r<t.material.length;r++){const s=t.material[r];if(null==s.color)continue;const n="#"+s.color.getHexString(),o=s.name;if(n===i.color&&(s.name===i.name||null==i.name)||t.userData["originalColor_"+r]===i.color&&t.userData["originalMaterialName_"+r]===i.name){const s=yield this.assetsService.getAsset(i.materialId),l=t.material[r];s&&(t.material[r]=yield materialFromAsset(s,this.renderingView,this.assetsService,this.assetManagerService,this.shaders),t.userData["originalColor_"+r]=null!==(e=t.userData["originalColor_"+r])&&void 0!==e?e:n,t.userData["originalMaterialName_"+r]=null!==(a=t.userData["originalMaterialName_"+r])&&void 0!==a?a:o,this.inEditor&&this._originalMaterials.set(t.id+"#"+r,l))}}else{const e="#"+t.material.color.getHexString(),a=t.material.name;if(e===i.color&&(t.material.name===i.name||null==i.name)||t.userData.originalColor===i.color&&t.userData.originalName===i.name){const n=yield this.assetsService.getAsset(i.materialId),o=t.material;n&&(t.material=yield materialFromAsset(n,this.renderingView,this.assetsService,this.assetManagerService,this.shaders),t.userData.originalColor=null!==(r=t.userData.originalColor)&&void 0!==r?r:e,t.userData.originalMaterialName=null!==(s=t.userData.originalMaterialName)&&void 0!==s?s:a,this.inEditor&&this._originalMaterials.set(t.id,o))}}})))))}unapplyMaterials(t){t.traverse((t=>e(this,void 0,void 0,(function*(){var e,i;if(t instanceof d)if(t.material instanceof Array)for(let i=0;i<t.material.length;i++)t.material[i]=null!==(e=this._originalMaterials.get(t.id+"#"+i))&&void 0!==e?e:t.material[i];else t.material=null!==(i=this._originalMaterials.get(t.id))&&void 0!==i?i:t.material}))))}updateActors(t){this.actorTypes=t;const i=new Set(Object.values(T));U(this.scene,(e=>{var t,a;return(null===(t=e.userData.src)||void 0===t?void 0:t.id)&&"actor"===e.userData.src.type&&this.materializedActors.has(null===(a=e.userData.src)||void 0===a?void 0:a.id)&&!i.has(e.userData.src.actor.type)})).forEach((t=>e(this,void 0,void 0,(function*(){this.remove(t.userData.src),yield this.materializeAndInitActor(t.userData.src)}))))}updateShaders(e){this.shaders=e;for(const[e,t]of fe.entries())t.userData.customShaderName&&fe.delete(e);this.landscapeManagers.forEach((t=>t.updateShaders(e))),U(this.scene,(e=>!0)).forEach((e=>{e.traverse((e=>{var t,i;if(e instanceof d){if(null!=(null===(t=e.material.userData)||void 0===t?void 0:t.customShaderName)){const t=null===(i=function(e,t){if(t(e))return e;let i;return e.traverseAncestors((e=>{null==i&&t(e)&&(i=e)})),i}(e,(e=>{var t;return null!=(null===(t=e.userData.src)||void 0===t?void 0:t.id)})))||void 0===i?void 0:i.userData.src;null!=t&&this.update(t)}}}))}))}update(t){return e(this,void 0,void 0,(function*(){const e=this.sceneObjectMap.get(t.id);if(e){const a=this.findParent(t);if(null!=a&&a.uuid!=e.uuid?a.attach(e):console.error("Parent is wrong"),"prefab"!==t.type){this.unapplyMaterials(e);this.inEditor&&t.hidden&&!1?e.traverse((e=>{e instanceof d&&(e.material.wireframe=!0)})):e.traverse((e=>{e instanceof d&&(e.material.wireframe=!1)}))}if("asset_mesh"===t.type){const i=yield this.assetsService.getAsset(t.assetId);De(t.materialAssignments,i.materialAssignments).forEach((t=>this.applyMaterial(e,t)))}else"shape_mesh"===t.type&&this.applyMaterials(e,t.materialAssignments);if(e.position.fromArray(t.position),e.scale.fromArray(t.scale),e.rotation.fromArray(t.rotation),this.applyVertexMaterials(t,e),"light"==t.type)if("point"==t.light.type){const i=e;i.color=new r(t.light.point.color),i.intensity=t.light.point.intensity,i.decay=t.light.point.decay,i.castShadow=t.light.point.castShadow,i.distance=Math.max(t.light.point.distance,0)}else"directional"===t.light.type?this.applyDirectionalLight(t.light.directional):"ambient"===t.light.type&&this.applyDirectionalAmbientLight(e,t.light.ambient);else if("landscape"===t.shape)this.applyHeightMaps(e,t.landscape.heightMaps),this.inEditor&&this.landscapeManagers.filter((e=>e.source.id===t.id)).forEach((e=>e.queueRefreshScatter(this.renderingView.camera.position,!0,(e=>!0))));else if("global_fog"===t.type){const e=(this.scene.fog instanceof o?"density":"linear")!==t.fog.type;this.scene.fog=Ae(t.fog),e&&(i=this.scene).traverse((e=>{if(e instanceof d){const t=e.material;t instanceof N&&(i.fog instanceof n?(t.uniforms.fogFar.value=i.fog.far,t.uniforms.fogNear.value=i.fog.near):i.fog instanceof o&&(t.uniforms.density={value:i.fog.density}),t.needsUpdate=!0,t.uniformsNeedUpdate=!0)}})),this.fixFogColor()}else if("actor"===t.type){if(this.materializedActors.has(t.id)){const e=this.editorActorParamSnapshot.get(t.id);null!=e&&e===JSON.stringify(t.actor)||(console.log("Rematerializing actor because parameters changed"),this.remove(t),yield this.materializeAndInitActor(t))}}else if("shape_mesh"===t.type){const i=yield this.createMeshByShape(t.shape,e.material,t.shapeParams);e instanceof G&&(e.geometry=i.geometry,e.collisionShape=i.collisionShape)}("asset_mesh"===t.type||"shape_mesh"===t.type&&"landscape"!==t.shape)&&pe(e,t.castShadow,t.receiveShadow),t.name&&t.name.length>0&&(e.name=t.name),this.updated$.next({object:e,source:t})}else{const e=yield this.materializeAndInitActor(t);this.updated$.next({object:e,source:t})}var i}))}materializeAndInitActor(t,i=this.findParent(t)){return e(this,void 0,void 0,(function*(){const e=yield this.materialize(t,i);if("actor"===t.type){const e=this.materializedActors.get(t.id);null!=e?yield this.initActorsPostInit([e]):console.error(`Something went wrong when creating actor ${t.id}`)}return e}))}findParent(e){const t=this.dataProvider.getObjects().flatMap((t=>t.id===e.id?null:R(t,(t=>{var i;return null===(i=t.children)||void 0===i?void 0:i.some((t=>t.id===e.id))}),(()=>!0))))[0];return null==t?this.scene:null!=t?U(this.scene,(e=>{var i,a;return(null===(a=null===(i=e.userData)||void 0===i?void 0:i.src)||void 0===a?void 0:a.id)===t.id}),(e=>{var t;return null!=(null===(t=e.userData)||void 0===t?void 0:t.src)}))[0]:void 0}fixFogColor(){!0===this.renderingView.options.enableOutlines&&(this.scene.fog.color=new r(this.scene.fog.color).convertSRGBToLinear())}findMeshWithGeometry(e){let t;return e.traverse((e=>{e instanceof d&&e.geometry&&(t=e)})),t}applyVertexMaterials(e,t){if(null==e.vertexMaterials||0===e.vertexMaterials.length)return;const i=k(e.vertexMaterials,(e=>e.m));t.traverse((e=>{if(e instanceof d){const t=ee(e,!1);if(null!=t){for(let e=0;e<t.array.length;e++)t.setX(e,0);t.needsUpdate=!0}}}));const a=new Set;for(const[e,r]of i.entries()){const i=null!=e?t.getObjectByName(e):this.findMeshWithGeometry(t);let s=!1;if(null==i)return void console.warn(`Failed to apply vertex materials on mesh with name "${e}"`);const n=ee(i,!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]),s=!0;s&&a.add(e)}this.inEditor&&this.landscapeManagers.filter((t=>t.source.id===e.id)).forEach((e=>e.queueRefreshScatter(this.renderingView.camera.position,!0,(e=>a.has(e.name)))))}materialize(t,i,r=!1){var s,n,o,c;return e(this,void 0,void 0,(function*(){let e;switch(t.type){case"asset_mesh":e=yield this.createFromAsset(t);break;case"shape_mesh":e=yield this.createFromShape(t);break;case"light":e=yield this.createLight(t);break;case"particles":e=yield this.createParticleSystem(t),t.collisionDetection=!1;break;case"global_fog":this.scene.fog=Ae(t.fog),this.fixFogColor(),e=new l;break;case"actor":e=yield this.createFromActor(t);break;case"group":e=new l;break;case"prefab":e=yield this.createFromPrefabAsset(t);break;default:throw console.log(t),new Error("unknown type "+t.type)}if(null!=e){if(t.name&&t.name.length>0&&(e.name=t.name),e.position.fromArray(t.position),e.scale.fromArray(t.scale),e.rotation.fromArray(t.rotation),r||(e.userData.src=t),!this.inEditor){const i=null!==(s=t.components)&&void 0!==s?s:[],a=null!=t.assetId&&null!==(o=null===(n=yield this.assetsService.getAsset(t.assetId))||void 0===n?void 0:n.components)&&void 0!==o?o:[];i.push(...a),i.length>0?e.userData.componentRefs=yield Promise.all(i.map(((i,a)=>this.createComponent(e,t,i,a)))):"asset_mesh"==t.type&&function(e){e.traverse((e=>{e.matrixAutoUpdate=!1,e.matrixWorldNeedsUpdate=!1}));const t=e.updateMatrixWorld;e.updateMatrixWorld=function(){t.apply(e),e.updateMatrixWorld=function(){}}}(e)}if(this.inEditor){let t=null;e instanceof G&&(t=function(e){if(e instanceof W)return new d(new a(...e.offset.toArray()),Se);return null}(e.collisionShape)),null!=t&&(t.layers.disable(0),t.layers.enable(18),t.scale.multiplyScalar(1.1),e.add(t))}return this.objectMap.set(e.uuid,t),this.sceneObjectMap.set(t.id,e),null==i?this.scene.add(e):null==i||i.add(e),null===(c=t.children)||void 0===c||c.forEach((t=>{this.materialize(t,e,r)})),e}}))}createComponent(t,i,a,r){return e(this,void 0,void 0,(function*(){const e=new me[a.path+"/"+a.className],s=i.id+r;e.id=s,e.object=t;for(const t of a.params)null!=t.value&&(e[t.name]=t.value);return this.components.push(e),s}))}createFromActor(t){var i,a,r;return e(this,void 0,void 0,(function*(){const e=null!==(a=null===(i=this.actorTypes.find((e=>{var i;return e.name===(null===(i=t.actor)||void 0===i?void 0:i.type)})))||void 0===i?void 0:i.type)&&void 0!==a?a:T[null===(r=t.actor)||void 0===r?void 0:r.type];if(null==e)return null;this.inEditor&&this.editorActorParamSnapshot.set(t.id,JSON.stringify(t.actor));const n=yield this.actorProvider.create(e,(new A).fromArray(t.position),(new s).fromArray(t.rotation),!0);return this.materializedActors.set(t.id,n),null==n?void 0:n.object}))}cleanup(){this.materializedActors.clear()}createFromShape(t){var i,a,s;return e(this,void 0,void 0,(function*(){const e=this.inEditor&&t.hidden;let n;if("landscape"==t.shape)n=this.createLandscape(t);else{let s=new m({name:"Default",color:new r("#aaaaaa"),visible:this.inEditor||!t.hidden,wireframe:e});const o=yield this.createMeshByShape(t.shape,s,t.shapeParams);o.castShadow=null===(i=t.castShadow)||void 0===i||i,o.receiveShadow=null!==(a=t.castShadow)&&void 0!==a&&a,t.collisionDetection||(o.collisionShape=null),n=o,this._originalMaterials.set(n.id,o.material),n.traverse((e=>{}))}return e||((null!==(s=t.materialAssignments)&&void 0!==s?s:[]).filter((e=>null!=e.materialId)).forEach((e=>this.applyMaterial(n,e))),this.applyVertexMaterials(t,n)),n}))}createLandscape(e){var t;if(null==(null===(t=e.landscape)||void 0===t?void 0:t.options))return console.error(`No landscape options exist on scene object ${e.id} ${e.name}`),new l;const i=X(e.landscape.options);this.applyHeightMaps(i,e.landscape.heightMaps,!0);const a=new Y(e,this.renderingView,i,this.assetManagerService,this.assetsService,this.shaders,(t=>{var i;(null!==(i=e.materialAssignments)&&void 0!==i?i:[]).filter((e=>null!=e.materialId)).forEach((e=>this.applyMaterial(t,e)))}));return this.landscapeManagers.push(a),a.refreshGeometry(),i}applyHeightMaps(e,t,i=!1){const a=new q(e.sections);for(const e of null!=t?t:[]){const t=a.find(e.x,e.y);if(!t)return;const i=t.geometry.getAttribute("position");for(const t of e.points)i.setY(t.i,t.y);i.needsUpdate=!0}const r=e.sections;r.forEach((e=>{e.geometry.computeBoundsTree(),e.geometry.computeVertexNormals()})),this.inEditor&&!i||setTimeout((()=>Z(r)),50)}createMeshByShape(t,i,a={}){return e(this,void 0,void 0,(function*(){if("landscape"!==t&&ce.includes(t)){const r=yield function(t){return e(this,void 0,void 0,(function*(){const e={};for(const[i,a]of Object.entries(t)){const t=yield we(a,null,null,null);null!=t&&(e[i]=t)}return e}))}(null!=a?a:{});return"cylinder"==t&&r.openEnded,new G(le[t].geometry(r),i,le[t].collision(r))}throw new Error(`Unsupported shape '${t}'`)}))}createFromAsset(t){var i,a,r,s;return e(this,void 0,void 0,(function*(){const e=yield this.assetsService.getAsset(t.assetId);if(null==e)return void console.warn(`Can not find asset with id ${t.assetId} and name ${t.name}`);let{scene:n}=yield this.assetManagerService.getMesh(e);De(t.materialAssignments,e.materialAssignments).forEach((e=>this.applyMaterial(n,e)));const o=null===(a=null!==(i=t.receiveShadow)&&void 0!==i?i:!!e.receiveShadow)||void 0===a||a,l=null!==(s=null!==(r=t.castShadow)&&void 0!==r?r:!!e.castShadow)&&void 0!==s&&s;return n.receiveShadow=o,pe(n,l,o),t.collisionDetection||(n.collisionShapes=[]),this.applyVertexMaterials(t,n),n.traverse((e=>{e instanceof d&&"computeBoundsTree"in e.geometry&&e.geometry.computeBoundsTree()})),n}))}createFromPrefabAsset(t){return e(this,void 0,void 0,(function*(){const e=yield this.assetsService.getAsset(t.assetId);if(null==e)return void console.warn(`Can not find asset with id ${t.assetId} and name ${t.name}`);const i=new l;return e.prefab.objects.filter((e=>"global_fog"!==e.type)).forEach((e=>this.materialize(e,i,!0))),i}))}createParticleSystem(t){return e(this,void 0,void 0,(function*(){const e=yield this.assetsService.getAsset(t.assetId),a=new p;return yield b.fromJSONAsync(e.particleSystem,i).then((e=>{const t=new M(a,i);e.addRenderer(t),this.renderingView.onLoop((t=>e.update()))})),a}))}createLight(t){var i;return e(this,void 0,void 0,(function*(){if("point"===t.light.type){const e=new f(t.light.point.color,t.light.point.intensity,t.light.point.distance,t.light.point.decay);if(e.castShadow=null===(i=t.light.point.castShadow)||void 0===i||i,this.inEditor){const t=new g(.3,10,10),i=new m({color:new r(16771709)}),a=new d(t,i);e.add(a)}return e}return"directional"===t.light.type?(this.applyDirectionalLight(t.light.directional),new l):"ambient"===t.light.type?(this.applyDirectionalAmbientLight(null,t.light.ambient),new l):void 0}))}applyDirectionalAmbientLight(e,t){const i=this.scene.children.find((e=>e.name===de));null!=i?(i.intensity=t.intensity,i.color.set(t.color),i.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()}}function pe(e,t,i){e.castShadow=t,e.receiveShadow=i,e.traverse((e=>{e.castShadow=t,e.receiveShadow=i}))}const fe=new Map,ve=new h({color:16711935}),ge=new Map;export function materialFromAsset(t,a,s,n,o,l=!0){var c,d,h,m,p,f,v,g,y,w,A;return e(this,void 0,void 0,(function*(){const e=JSON.stringify(t.material);if(l&&fe.has(e))return fe.get(e);const S={opacity:t.material.params.opacity,map:null,emissive:null!==(c=t.material.params.emissive)&&void 0!==c?c:null,metalness:null!==(d=t.material.params.metalness)&&void 0!==d?d:0,flatShading:null!==(h=t.material.params.flatShading)&&void 0!==h&&h,color:new r(t.material.params.color).convertSRGBToLinear(),transparent:null!=t.material.params.opacity&&t.material.params.opacity<1},b={};if(null!=t.material.params.map){const e=t.material.params.map,i=yield s.getAsset(e);null!=i&&(S.map=yield n.getTexture(i))}let M;switch(t.material.type){case"phong":M=new u(Object.assign(Object.assign({},S),b));break;case"water":M=te(S,a);break;case"grassFoliage":M=Q({color:S.color,map:S.map},a);break;case"grass":M=K(Object.assign(Object.assign({},S),{colorTwo:new r(t.material.params.colorTwo),colorThree:new r(t.material.params.colorThree)}),a);break;case"standard":case"lambert":case"shader":const e=null!==(m={standard:ne,lambert:oe}[t.material.type])&&void 0!==m?m:null===(p=o.find((e=>e.name==t.material.shader)))||void 0===p?void 0:p.type;if(e){const i=new e,r=yield ye(null!==(v=null===(f=t.material)||void 0===f?void 0:f.shaderParams)&&void 0!==v?v:{},e,s,n,null,a,o);Object.assign(i,r);try{M=i.build()}catch(e){console.log("Shader runtime error: "+e),ge.has(t.material.shader)||ge.set(t.material.shader,ve.clone()),M=ge.get(t.material.shader)}M.userData.customShaderName=t.material.shader}else console.warn("Missing shader implementation with name "+t.material.shader),M=ve;break;default:throw new Error("Unsupported material type"+t.material.type)}return null==a||a.csm.setupMaterial(M),l&&fe.set(e,M),M.side=null!==(y=null!==(g=t.material.side)&&void 0!==g?g:M.side)&&void 0!==y?y:i.FrontSide,M.transparent=null!==(A=null!==(w=t.material.transparent)&&void 0!==w?w:S.transparent)&&void 0!==A&&A||M.transparent,M}))}function ye(t,i,a,r,s,n,o){return e(this,void 0,void 0,(function*(){const e={};for(const[i,l]of Object.entries(t)){const t=yield we(l,a,r,s,n,o);null!=t&&(e[i]=t)}return e}))}function we(t,i,a,n,o,l){return e(this,void 0,void 0,(function*(){if(null==t||null==t.value||""==t.value)return null;const e=t.value;switch(t.type){case ie.Number:case ie.FloatNode:let c="string"==typeof e?parseFloat(e):e;return t.type===ie.FloatNode?x(c):c;case ie.Texture:return yield a.getTexture(yield i.getAsset(e));case ie.Sampler2DNode:return F(yield a.getTexture(yield i.getAsset(e)));case ie.Boolean:return e;case ie.BooleanNode:return D(e);case ie.Vector2:case ie.Vec2Node:if("object"==typeof e){const i=e instanceof Array?(new w).fromArray(e):new w(i.x,i.y);return t.type===ie.Vec2Node?V(i):i}return null;case ie.Vector3:case ie.Vec3Node:if("object"==typeof e){const i=e instanceof Array?(new A).fromArray(e):new A(i.x,i.y,i.z);return t.type===ie.Vec3Node?z(i):i}return null;case ie.Color:case ie.RgbNode:const d=new r(e).convertSRGBToLinear();return t.type===ie.RgbNode?j(d):d;case ie.String:return e;case ie.BaseActor:const h=e;return null==n&&console.warn("Class parameters can not be prepared as actors are not passed in"),null==n?void 0:n.get(h);case ie.Euler:const u=e;return(new s).fromArray(u);case ie.Object3D:return(yield a.getMesh(yield i.getAsset(e))).scene;case ie.Material:return yield materialFromAsset(yield i.getAsset(e),o,i,a,l);case ie.AudioBuffer:return yield a.getAudio(yield i.getAsset(e))}return null}))}function Ae(e){var t,i;return"linear"===e.type?new n(new r(e.color),null!==(t=e.near)&&void 0!==t?t:100,null!==(i=e.far)&&void 0!==i?i:1e3):"density"===e.type?new o(e.color,e.density):void console.warn("Invalid fog type",e)}const Se=new m({color:4229780});function be(t,a,r,n){var o;return e(this,void 0,void 0,(function*(){null==n&&(n=(new ae).identity()),yield a(t,r,n);const e=n.clone().multiply(function(e,t){return t.compose((new A).fromArray(e.position),(new v).setFromEuler((new s).fromArray(e.rotation)),(new A).fromArray(e.scale))}(t,new i.Matrix4));return Promise.all((null!==(o=t.children)&&void 0!==o?o:[]).map((i=>be(i,a,t,e))))}))}function Me(e){const t=e.constructor.prototype;return t instanceof Number||e===Number?ie.Number:t instanceof P||"function"==typeof e.prototype.isFloat?ie.FloatNode:t instanceof y||e===y||e.isTexture?ie.Texture:t instanceof se||e===O?ie.Sampler2DNode:t instanceof Boolean||e===Boolean?ie.Boolean:t instanceof I?ie.BooleanNode:t instanceof r||e==r?ie.Color:t instanceof E||"function"==typeof e.prototype.isRgb?ie.RgbNode:t instanceof w||e==w?ie.Vector2:t instanceof B||"function"==typeof e.prototype.isVec2?ie.Vec2Node:t instanceof A||e==A?ie.Vector3:t instanceof C||"function"==typeof e.prototype.isVec3?ie.Vec3Node:t instanceof String||e===String?ie.String:t instanceof re||e==re||e.prototype instanceof re||e.prototype==re?ie.BaseActor:t instanceof s||e==s?ie.Euler:t instanceof p||e==p?ie.Object3D:t instanceof c||e==c?ie.Material:t instanceof AudioBuffer||e==AudioBuffer?ie.AudioBuffer:void console.warn("Failed to map parameter type to serialized version",{type:e})}export function prepareCustomParams(e,t,i={}){return Object.fromEntries(e.map((e=>{var a,r,s;return[e.name,{type:Me(e.type),value:null!==(s=null!==(r=null===(a=t[e.name])||void 0===a?void 0:a.value)&&void 0!==r?r:i[e.name])&&void 0!==s?s:xe.get(Me(e.type))}]})))}export function prepareCustomParamsFromType(e,t,i=null){const a=$(e);if(0===a.length)return{};let r;null!=i?he(i,(()=>{r=i.get(e)})):r=new e;const s={};for(const e of a){const t=r[e.name];if(null!=t){const i=serializeCustomParameter(e.type,t);null!=i&&(s[e.name]=i)}}return prepareCustomParams(a,t,s)}export function serializeCustomParameter(e,t){function i(){console.error("Failed to serialize value",{type:e,value:t})}switch(e){case Number:case Boolean:return t;case w:return t instanceof w?t.toArray():void i();case A:return t instanceof A?t.toArray():void i();case S:return t instanceof S?t.toArray():void i();case r:return t instanceof r?"#"+t.getHexString():"string"==typeof t?t:"number"==typeof t?"#"+new r(t).getHexString():void i();case String:return t;case s:return t instanceof s?t.toArray():void i()}}function De(e,t){return function(e,t,i){const a=[],r=new Set;for(const s of[...null!=e?e:[],...null!=t?t:[]]){const e=i(s);r.has(e)||(r.add(e),a.push(s))}return a}((null!=e?e:[]).filter((e=>Ie(e.materialId))),(null!=t?t:[]).filter((e=>Ie(e.materialId))),(e=>e.color+e.name))}function Ie(e){return"null"!=e&&null!=e}const xe=new Map([[ie.RgbNode,"#000000"],[ie.Color,"#000000"],[ie.Vector4,[0,0,0,0]],[ie.Vec4Node,[0,0,0,0]],[ie.Vector3,[0,0,0]],[ie.Vec3Node,[0,0,0]],[ie.Vector2,[0,0]],[ie.Vec2Node,[0,0]],[ie.Euler,[0,0,0,"XYZ"]]]);
|
2
2
|
/*
|
3
3
|
* Copyright (©) 2023. All rights reserved.
|
4
4
|
* See the LICENSE.md file for details.
|
package/dist/scene/model.d.ts
CHANGED
@@ -32,8 +32,11 @@ export type CustomParamValue = {
|
|
32
32
|
type: SerializedParamType;
|
33
33
|
value: unknown;
|
34
34
|
};
|
35
|
+
export type WrapMode = 'clamp' | 'repeat' | 'mirror';
|
35
36
|
export type TextureSettings = {
|
36
37
|
flipY?: boolean;
|
38
|
+
wrapS?: WrapMode;
|
39
|
+
wrapT?: WrapMode;
|
37
40
|
};
|
38
41
|
export interface Asset {
|
39
42
|
id: string;
|
@@ -96,6 +96,30 @@ declare class TextDefinition implements ShapeDefinition {
|
|
96
96
|
geometry(params?: ParamsType<this>): TextGeometry;
|
97
97
|
collision(params?: ParamsType<this>): ConvexPolyhedronCollisionShape;
|
98
98
|
}
|
99
|
+
declare class StairsDefinition implements ShapeDefinition {
|
100
|
+
parameters: {
|
101
|
+
floating: ShapeParameterDef<boolean>;
|
102
|
+
width: ShapeParameterDef<number>;
|
103
|
+
height: ShapeParameterDef<number>;
|
104
|
+
depth: ShapeParameterDef<number>;
|
105
|
+
steps: ShapeParameterDef<number>;
|
106
|
+
};
|
107
|
+
geometry(params?: ParamsType<this>): BufferGeometry;
|
108
|
+
collision(params?: ParamsType<this>): TrimeshCollisionShape;
|
109
|
+
}
|
110
|
+
declare class StairsSpiralDefinition implements ShapeDefinition {
|
111
|
+
parameters: {
|
112
|
+
floating: ShapeParameterDef<boolean>;
|
113
|
+
width: ShapeParameterDef<number>;
|
114
|
+
height: ShapeParameterDef<number>;
|
115
|
+
steps: ShapeParameterDef<number>;
|
116
|
+
degrees: ShapeParameterDef<number>;
|
117
|
+
radius: ShapeParameterDef<number>;
|
118
|
+
flipped: ShapeParameterDef<boolean>;
|
119
|
+
};
|
120
|
+
geometry(params?: ParamsType<this>): BufferGeometry;
|
121
|
+
collision(params?: ParamsType<this>): TrimeshCollisionShape;
|
122
|
+
}
|
99
123
|
declare const shapes: {
|
100
124
|
box: {
|
101
125
|
geometry(): BoxGeometry;
|
@@ -114,8 +138,10 @@ declare const shapes: {
|
|
114
138
|
capsule: CapsuleDefinition;
|
115
139
|
roundedBox: RoundedBoxDefinition;
|
116
140
|
text: TextDefinition;
|
141
|
+
stairs: StairsDefinition;
|
142
|
+
stairsSpiral: StairsSpiralDefinition;
|
117
143
|
};
|
118
144
|
export type LibraryShapeType = keyof typeof shapes;
|
119
145
|
export declare const ShapeLibrary: Record<LibraryShapeType, ShapeDefinition>;
|
120
|
-
export declare const ShapeLibraryKeys: ("sphere" | "box" | "ramp" | "plane" | "cylinder" | "torus" | "cone" | "circle" | "capsule" | "roundedBox" | "text")[];
|
146
|
+
export declare const ShapeLibraryKeys: ("sphere" | "box" | "ramp" | "plane" | "cylinder" | "torus" | "cone" | "circle" | "capsule" | "roundedBox" | "text" | "stairs" | "stairsSpiral")[];
|
121
147
|
export {};
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import{BoxCollisionShape as e,CapsuleCollisionShape as t,ConeCollisionShape as r,ConvexPolyhedronCollisionShape as s,CylinderCollisionShape as n,PlaneCollisionShape as
|
1
|
+
import{BoxCollisionShape as e,CapsuleCollisionShape as t,ConeCollisionShape as r,ConvexPolyhedronCollisionShape as s,CylinderCollisionShape as n,PlaneCollisionShape as i,SphereCollisionShape as o,TrimeshCollisionShape as a}from"../collision/collision-shape";import{BoxGeometry as m,PlaneGeometry as h,Vector3 as l,CylinderGeometry as c,SphereGeometry as g,Euler as p,TorusGeometry as u,ConeGeometry as d,CircleGeometry as w,CapsuleGeometry as y}from"three";import{rampGeometry as S}from"./ramp-geometry";import{RoundedBoxGeometry as f}from"three/examples/jsm/geometries/RoundedBoxGeometry";import{TextGeometry as x}from"three/examples/jsm/geometries/TextGeometry";import{Font as b}from"three/examples/jsm/loaders/FontLoader";import*as j from"three/examples/fonts/helvetiker_regular.typeface.json";import{createSpiralStairsGeometry as L,createStairsGeometry as z}from"./stairs-geometry";function E(e){return{type:"number",default:e}}function k(e){return{type:"number",default:e,float:!0}}function v(e){return{type:"boolean",default:e}}function B(e,t){const r={};for(let s in t)null==e[s]?r[s]=t[s].default:r[s]=e[s];return r}const G={box:{geometry:()=>new m(1,1,1),collision:()=>new e(new l(1,1,1))},ramp:{geometry:()=>S(),collision:()=>new s(S())},plane:new class{constructor(){this.parameters={widthSegments:E(1),heightSegments:E(1)}}geometry(e={}){const t=B(e,this.parameters);return new h(1,1,t.widthSegments,t.heightSegments)}collision(e={}){return new i(1,1)}},cylinder:new class{constructor(){this.parameters={radialSegments:E(12),heightSegments:E(1),openEnded:v(!1)}}geometry(e={}){const t=B(e,this.parameters);return new c(.5,.5,1,t.radialSegments,t.heightSegments,t.openEnded)}collision(e={}){const t=B(e,this.parameters);return new n(.5,.5,1,t.radialSegments,new p(0,1,0))}},sphere:new class{constructor(){this.parameters={withSegments:E(12),heightSegments:E(12)}}geometry(e={}){const t=B(e,this.parameters);return new g(.5,t.withSegments,t.heightSegments)}collision(){return new o(.5)}},torus:new class{constructor(){this.parameters={radius:k(1),tube:k(.3),radialSegments:E(16),tubularSegments:E(50)}}geometry(e={}){const t=B(e,this.parameters);return new u(t.radius,t.tube,t.radialSegments,t.tubularSegments)}collision(e={}){return new a(this.geometry(e))}},cone:new class{constructor(){this.parameters={radialSegments:E(12),heightSegments:E(1),openEnded:v(!1)}}geometry(e={}){const t=B(e,this.parameters);return new d(.5,1,t.radialSegments,t.heightSegments)}collision(e={}){return new r(1,.5)}},circle:new class{constructor(){this.parameters={segments:E(32)}}geometry(e={}){const t=B(e,this.parameters);return new w(.5,t.segments)}collision(e={}){return new a(this.geometry(e))}},capsule:new class{constructor(){this.parameters={radius:k(.5),length:k(1),capSegments:E(8),radialSegments:E(32)}}geometry(e={}){const t=B(e,this.parameters);return new y(t.radius,t.length,t.capSegments,t.radialSegments)}collision(e={}){const r=B(e,this.parameters);return new t(r.length,r.radius)}},roundedBox:new class{constructor(){this.parameters={width:k(1),height:k(1),depth:k(1),segments:E(4),radius:k(.1)}}geometry(e={}){const t=B(e,this.parameters);return new f(t.width,t.height,t.depth,t.segments,t.radius)}collision(t={}){const r=B(t,this.parameters);return new e(new l(r.width,r.height,r.depth))}},text:new class{constructor(){var e;this.parameters={text:(e="Text",{type:"string",default:e}),size:k(1),depth:k(.2)}}geometry(e={}){const t=B(e,this.parameters);return new x(t.text,{font:new b(j),size:t.size,height:t.depth})}collision(e={}){return B(e,this.parameters).text.trim().length,null}},stairs:new class{constructor(){this.parameters={floating:v(!1),width:k(1),height:k(.25),depth:k(.25),steps:E(4)}}geometry(e={}){const t=B(e,this.parameters);return z(t.floating,t.steps,t.depth,t.height,t.width)}collision(e={}){return new a(this.geometry(e))}},stairsSpiral:new class{constructor(){this.parameters={floating:v(!1),width:k(1),height:k(.25),steps:E(4),degrees:E(90),radius:k(.2),flipped:v(!1)}}geometry(e={}){const t=B(e,this.parameters);return L(t.floating,t.steps,t.height,t.width,t.degrees,t.radius,t.flipped)}collision(e={}){return new a(this.geometry(e))}}};export const ShapeLibrary=G;export const ShapeLibraryKeys=Object.keys(ShapeLibrary);
|
2
2
|
/*
|
3
3
|
* Copyright (©) 2023. All rights reserved.
|
4
4
|
* See the LICENSE.md file for details.
|
@@ -0,0 +1,3 @@
|
|
1
|
+
import * as THREE from "three";
|
2
|
+
export declare function createSpiralStairsGeometry(floating: boolean, numSteps: number, stepHeight: number, stepWidth: number, degrees: number, innerRadius: number, flipped: boolean): THREE.BufferGeometry;
|
3
|
+
export declare function createStairsGeometry(floating: boolean, numSteps: number, stepSize: number, stepHeight: number, stepWidth: number): THREE.BufferGeometry;
|
@@ -0,0 +1,5 @@
|
|
1
|
+
import*as t from"three";export function createSpiralStairsGeometry(e,o,r,s,n,a,l){const c=[];for(let i=0;i<o;i++){let f=i*r;const u=t.MathUtils.degToRad(Math.abs(n)),h=i/o,m=(i+1)/o,p=Math.cos(h*u)*a,M=Math.cos(h*u)*(a+s),b=Math.cos(m*u)*a,x=Math.cos(m*u)*(a+s),g=Math.sin(h*u)*a,w=Math.sin(h*u)*(a+s),y=Math.sin(m*u)*a,A=Math.sin(m*u)*(a+s);let B;if(e)B=[p,f,g,M,f,w,p,f+r,g,M,f+r,w,b,f,y,x,f,A,b,f+r,y,x,f+r,A];else{const t=0;B=[p,f,g,M,f,w,p,f+r,g,M,f+r,w,b,t,y,x,t,A,b,t+r*(i+1),y,x,t+r*(i+1),A,p,t,g,M,t,w]}const G=0,N=[G,G+2,G+1,G+1,G+2,G+3,G,G+4,G+2,G+2,G+4,G+6,G+5,G+1,G+3,G+5,G+3,G+7,...e||i===o-1?[G+6,G+4,G+5,G+6,G+5,G+7]:[],G+3,G+2,G+6,G+7,G+3,G+6,...!e&&i>0?[G,G+8,G+4,G+5,G+9,G+1]:[],...e?[G,G+1,G+4,G+1,G+5,G+4]:[G+8,G+9,G+4,G+9,G+5,G+4]];if(l)for(let t=0;t<N.length;t+=3){let e=N[t];N[t]=N[t+2],N[t+2]=e}for(let t=0;t<N.length;t++)c.push(B[3*N[t]],B[3*N[t]+1],B[3*N[t]+2])}const i=new t.BufferGeometry;return i.setAttribute("position",new t.Float32BufferAttribute(c,3)),i.computeVertexNormals(),l&&(i.scale(-1,1,1),i.computeVertexNormals()),i}export function createStairsGeometry(e,o,r,s,n){const a=[];for(let t=0;t<o;t++){let l=0,c=t*s;const i=t*r;let f;if(e)f=[l,c+0*s,i,l+n,c+0*s,i,l,c+s,i,l+n,c+s,i,l,c+0*s,i+r,l+n,c+0*s,i+r,l,c+s,i+r,l+n,c+s,i+r];else{const e=0;f=[l,c,i,l+n,c,i,l,c+s,i,l+n,c+s,i,l,e,i+r,l+n,e,i+r,l,e+s*(t+1),i+r,l+n,e+s*(t+1),i+r,l,e,i,l+n,e,i]}const u=0,h=[u,u+2,u+1,u+1,u+2,u+3,u,u+4,u+2,u+2,u+4,u+6,u+5,u+1,u+3,u+5,u+3,u+7,...e||t===o-1?[u+6,u+4,u+5,u+6,u+5,u+7]:[],...!e&&t>0?[u,u+8,u+4,u+5,u+9,u+1]:[],u+3,u+2,u+6,u+7,u+3,u+6,...e?[u,u+1,u+4,u+1,u+5,u+4]:[u+8,u+9,u+4,u+9,u+5,u+4]];for(let t=0;t<h.length;t++)a.push(f[3*h[t]],f[3*h[t]+1],f[3*h[t]+2])}const l=new t.BufferGeometry;return l.setAttribute("position",new t.Float32BufferAttribute(a,3)),l.computeVertexNormals(),l}
|
2
|
+
/*
|
3
|
+
* Copyright (©) 2023. All rights reserved.
|
4
|
+
* See the LICENSE.md file for details.
|
5
|
+
*/
|