@hology/core 0.0.186 → 0.0.188
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/effects/vfx/vfx-materializer.d.ts +1 -1
- package/dist/gameplay/actors/builtin/components/character/character-movement.d.ts +2 -0
- package/dist/gameplay/actors/builtin/components/character/character-movement.js +1 -1
- package/dist/gameplay/actors/builtin/index.d.ts +3 -0
- package/dist/gameplay/actors/builtin/index.js +1 -1
- package/dist/gameplay/actors/camera/third-person-camera-component.d.ts +6 -0
- package/dist/gameplay/actors/camera/third-person-camera-component.js +1 -1
- package/dist/gameplay/services/render.d.ts +1 -1
- package/dist/gameplay/services/render.js +1 -1
- package/dist/rendering/fog/fog-volume-actor.d.ts +4 -0
- package/dist/rendering/fog/fog-volume-actor.js +1 -1
- package/dist/rendering/fog/fog-volume-object.d.ts +13 -0
- package/dist/rendering/fog/volumetric-fog-pass.js +1 -1
- package/dist/rendering/light-probes/light-probe-volume-actor.d.ts +14 -0
- package/dist/rendering/light-probes/light-probe-volume-actor.js +4 -0
- package/dist/rendering/light-probes/light-volume-capture.d.ts +16 -0
- package/dist/rendering/light-probes/light-volume-capture.js +4 -0
- package/dist/rendering/outline-effect.js +1 -1
- package/dist/rendering.d.ts +13 -1
- package/dist/rendering.js +1 -1
- package/dist/scene/asset-resource-loader.js +1 -1
- package/dist/scene/materializer.d.ts +11 -2
- package/dist/scene/materializer.js +1 -1
- package/dist/scene/model.d.ts +2 -2
- package/dist/scene/runtime-backend-service.js +1 -1
- package/dist/scene/storage/storage.d.ts +3 -2
- package/dist/scene/storage/storage.js +1 -1
- package/dist/shader/builtin/index.js +1 -1
- package/dist/shader/builtin/lambert-shader.d.ts +1 -3
- package/dist/shader/builtin/lambert-shader.js +1 -1
- package/dist/shader/builtin/standard-shader.d.ts +1 -0
- package/dist/shader/builtin/standard-shader.js +1 -1
- package/dist/shader/builtin/toon-shader.d.ts +1 -0
- package/dist/shader/builtin/toon-shader.js +1 -1
- package/dist/shader/builtin/unlit-shader.d.ts +1 -4
- package/dist/shader/builtin/unlit-shader.js +1 -1
- package/dist/shader/color-layer.d.ts +6 -1
- package/dist/shader/color-layer.js +1 -1
- package/dist/shader/shader.d.ts +1 -0
- package/dist/shader/sprite-shader.d.ts +21 -3
- package/dist/shader/sprite-shader.js +1 -1
- package/dist/shader-nodes/dither.d.ts +8 -0
- package/dist/shader-nodes/dither.js +4 -0
- package/dist/shader-nodes/index.d.ts +1 -0
- package/dist/shader-nodes/index.js +1 -1
- package/dist/shader-nodes/pom.d.ts +15 -1
- package/dist/shader-nodes/pom.js +1 -1
- package/dist/utils/three/transform-controls.d.ts +2 -2
- package/dist/utils/three/traverse.d.ts +1 -1
- package/package.json +2 -2
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -14,7 +14,7 @@ export type VfxAssetProvider = {
|
|
|
14
14
|
getMaterial(assetId: string): Promise<THREE.Material>;
|
|
15
15
|
};
|
|
16
16
|
export declare function materializeVfx(asset: VfxAsset, target: Object3D, assetProvider: VfxAssetProvider, view: ViewController, physics: PhysicsSystem, shaderProvider: ShaderProvider, assetsService: AssetsProvider, assetManagerService: AssetResourceLoader): Promise<{
|
|
17
|
-
container: THREE.Object3D<THREE.Object3DEventMap>;
|
|
17
|
+
container: THREE.Object3D<THREE.Object3DEventMap, Event>;
|
|
18
18
|
system: System;
|
|
19
19
|
dispose: () => void;
|
|
20
20
|
}>;
|
|
@@ -61,6 +61,8 @@ export declare class CharacterMovementComponent extends ActorComponent {
|
|
|
61
61
|
rotateToMovementDirection: boolean;
|
|
62
62
|
/** Makes the character rotate smoothly to the desired direction */
|
|
63
63
|
smoothRotation: boolean;
|
|
64
|
+
rotationSpeed: number;
|
|
65
|
+
maxRotationSpeed: number;
|
|
64
66
|
private impulse;
|
|
65
67
|
/**
|
|
66
68
|
* The damping factor used to slow down impulses over time
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{__decorate as t,__metadata as e}from"tslib";import i from"@dimforge/rapier3d-compat";import{takeUntil as o}from"rxjs";import*as s from"three";import{ArrowHelper as n,MathUtils as r,Vector3 as a}from"three";import{RootMotionClip as c}from"../../../../../gameplay/animation/root-motion.js";import{ActionInput as
|
|
1
|
+
import{__decorate as t,__metadata as e}from"tslib";import i from"@dimforge/rapier3d-compat";import{takeUntil as o}from"rxjs";import*as s from"three";import{ArrowHelper as n,MathUtils as r,Vector3 as a}from"three";import{RootMotionClip as c}from"../../../../../gameplay/animation/root-motion.js";import{ActionInput as h,AxisInput as l,RotationInput as p}from"../../../../../gameplay/input/index.js";import{PhysicsSystem as m,RayTestResult as u}from"../../../../../gameplay/services/physics/physics-system.js";import{CapsuleCollisionShape as d}from"../../../../../scene/collision/collision-shape.js";import{PhysicsBodyType as y}from"../../../../services/physics/physics-system.js";import{ActorComponent as g,Component as f}from"../../../component.js";import{CharacterMovementMode as S}from"./modes.js";import{inject as M}from"../../../../../gameplay/inject.js";const v=new a,w=new a,x=1/30,I=131070;let T=class extends g{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)}set offset(t){this.cc.setOffset(t)}get offset(){return this.cc.offset()}set normalNudgeFactor(t){this.cc.setNormalNudgeFactor(t)}get normalNudgeFactor(){return this.cc.normalNudgeFactor()}constructor(){super(),this.directionInput=new l,this.jumpInput=new h,this.sprintInput=new h,this.rotationInput=new p,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=r.degToRad(70),this.maxSlopeClimbAngle=r.degToRad(70),this.applyImpulsesToDynamicBodies=!0,this.characterCollision=!1,this.enabled=!0,this.velocity=new a,this.mode=S.walking,this.isSprinting=!1,this.pressedJump=!1,this.rayTestResult=new u,this.physicsSystem=M(m),this.resetRootMotion=!1,this.rotateToMovementDirection=!1,this.smoothRotation=!0,this.rotationSpeed=20,this.maxRotationSpeed=32,this.impulse=new a,this.impulseDamping=2;const t=this.cc=this.physicsSystem.getCharacterController(.1);t.enableSnapToGround(.1),t.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:y.kinematic,continousCollisionDetection:!1,friction:0,restitution:.5,ignoreForNavMesh:!0}),this.rotationInput.rotation.copy(this.actor.rotation);let e=this.rotationInput.rotation.y;const i=new a,n=new a,h=new a,l=new a,p=new a,m=new a,u=new a;let d=0,g=null;const f=new a,M=new a,T=new a,j=new a,R=new a,C=this.characterCollision?null:I;let D=this.rotateToMovementDirection;this.physicsSystem.beforeStep.pipe(o(this.disposed)).subscribe(o=>{if(this.checkGrounded(o),!this.enabled)return;if(null!=this.rootMotionAction){if(this.rootMotionAction.getClip()instanceof c){const t=this.rootMotionInterpolant;this.resetRootMotion&&(f.fromArray(t.evaluate(0)),this.resetRootMotion=!1),j.fromArray(t.evaluate(this.rootMotionAction.time)),M.subVectors(j,f),f.copy(j),this.rootMotionAction.getRoot().getWorldScale(T),M.multiply(T)}}o>x&&(o=x);const y=null!=this.rootMotionAction&&this.rootMotionAction.enabled&&0!=M.length();this.pressedJump=this.jumpInput.activated,this.isSprinting=this.sprintInput.activated;const I=D!==this.rotateToMovementDirection;let q=r.euclideanModulo(this.rotationInput.rotation.y-e+Math.PI,2*Math.PI)-Math.PI;I&&(this.actor.object.quaternion.setFromEuler(F.set(0,this.rotationInput.rotation.y,0)),D?this.actor.object.quaternion.setFromEuler(F.set(0,this.rotationInput.rotation.y,0)):(q=0,e=this.rotationInput.rotation.y),D=this.rotateToMovementDirection),h.copy(this.actor.position),l.set(-this.directionInput.vector.x,0,this.directionInput.vector.y).normalize();const H=!this.rotateToMovementDirection&&l.z<0?this.maxSpeedBackwards:this.isSprinting?this.maxSpeedSprint:this.maxSpeed;if(u.set(0,0,0),this.rotateToMovementDirection){if(l.lengthSq()>0&&this.mode!=S.falling){const t=this.smoothRotation,i=F.setFromQuaternion(this.actor.object.quaternion,"YXZ").y,s=Math.atan2(l.x,l.z),n=.99*this.rotationInput.rotation.y+s;if(t){const t=1+4*(1-r.clamp(.5*l.dot(p)+.5,.001,1)),e=r.euclideanModulo(n-i+Math.PI,2*Math.PI)-Math.PI,s=r.clamp(e*this.rotationSpeed*o*t,-this.maxRotationSpeed*o,this.maxRotationSpeed*o);this.actor.object.quaternion.setFromEuler(F.set(0,i+s,0))}else this.actor.object.quaternion.setFromEuler(F.set(0,n,0));p.lerp(l,o*this.rotationSpeed),e+=q,u.copy(this.actor.object.getWorldDirection(E).normalize())}}else e+=q,this.actor.object.quaternion.multiply(z.setFromEuler(F.set(0,q,0))),l.lengthSq()>0&&u.copy(l).applyQuaternion(this.actor.object.quaternion).normalize();if(R.set(0,0,0),this.rayTestResult.hasHit&&null!=this.rayTestResult.actor&&this.physicsSystem.getLinearVelocity(this.rayTestResult.actor,R),this.mode===S.walking?(0!==u.lengthSq()?(d=Math.min(H,d),d=r.lerp(d,H,4*o)):d=0,m.copy(u).multiplyScalar(d),this.pressedJump&&(this.mode=S.falling,this.velocity.copy(m),this.velocity.y=this.jumpVelocity),m.y=o*this.getEffectiveGravity(),m.add(R)):this.mode===S.falling&&(this.pressedJump&&this.jumpInAir&&(this.mode=S.falling,this.velocity.copy(m),this.velocity.y=this.jumpVelocity),this.velocity.y+=o*this.getEffectiveGravity(),m.copy(this.velocity),m.addScaledVector(u,this.fallingMovementControl),this.fallingReorientation&&m.applyAxisAngle(new a(0,1,0),q)),this.impulse.lengthSq()>.1){const t=Math.min(1,this.impulse.length()/5);m.x=r.lerp(m.x,this.impulse.x,t),m.z=r.lerp(m.z,this.impulse.z,t),m.y+=this.impulse.y;const e=Math.exp(-this.impulseDamping*o);this.impulse.x*=e,this.impulse.y>0?this.impulse.y+=o*this.getEffectiveGravity():this.impulse.y*=e,this.impulse.z*=e}else this.impulse.set(0,0,0);if(n.copy(m).normalize(),i.copy(m),m.length()>0||!this.isGrounded||y){if(y?(M.applyQuaternion(this.actor.quaternion),M.y+=o*this.getEffectiveGravity(),v.copy(M)):v.copy(m).multiplyScalar(o),this.isGrounded&&this.mode===S.walking&&(this.rayTestResult.distance>t.offset()||(v.y=Math.max(0,R.y*o)),this.physicsSystem.getActorComputedMovement(this.actor,t,v,C),t.computedCollision(0,G),null!=G&&null!=G.normal1)){const e=(new s.Vector3).copy(G.normal1);Math.acos(e.dot(b))>t.maxSlopeClimbAngle()&&(v.y=.016*this.getEffectiveGravity()*.5)}w.copy(this.physicsSystem.getActorComputedMovement(this.actor,t,v,C))}else w.set(0,0,0);this.physicsSystem.setNextKinematicTranslation(this.actor,w);let k=function(t){if(t.numComputedCollisions()>0){const e=t.computedCollision(0);A.x=e.normal2.x,A.y=e.normal2.y,A.z=e.normal2.z;const i=A.angleTo(b);A.x=e.normal1.x,A.y=e.normal1.y,A.z=e.normal1.z;const o=A.angleTo(b);return!(i<100)&&o>t.minSlopeSlideAngle()}return!1}(t);y||this.isGrounded&&!k||this.mode!==S.falling&&(null==g?g=performance.now():performance.now()-g>100&&(this.mode=S.falling,this.velocity.copy(i))),this.isGrounded&&this.velocity.y<=0&&(this.mode,S.falling,this.mode=S.walking,this.velocity.y=0,g=null),this.mode,S.walking,this.horizontalSpeed=d})}applyImpulse(t){this.impulse.add(t)}debugDirection(){const t=new n(v,this.actor.position,1,65280);this.actor.object.parent.add(t),setTimeout(()=>{t.removeFromParent()},30);const e=new n(w,this.actor.position,1,16711680);this.actor.object.parent.add(e),setTimeout(()=>{e.removeFromParent()},30)}setRootMotionAction(t){const e=t?.getClip();if(e instanceof c){this.rootMotionAction=t,this.resetRootMotion=!0;const i=[];this.rootMotionInterpolant=e.motionTrack.InterpolantFactoryMethodSmooth(i)}}getWallDirection(t,e){const i=t.clone().negate().cross(b);return i.dot(e)<0?i.negate():i}moveTo(t){this.actor.position.copy(t),this.physicsSystem.updateActorTransform(this.actor)}getEffectiveGravity(){return this.gravityOverride??this.physicsSystem.getGravity().y}checkGrounded(t){this.colliderHeight,this.colliderRadius;D.y=-.05,this.physicsSystem.rayTest(j.addVectors(this.actor.position,C.set(0,this.offset,0)),R.addVectors(this.actor.position,D),this.rayTestResult,{excludeActor:this.actor,excludeTriggers:!0})}get isGrounded(){return this.rayTestResult.hasHit||this.cc.computedGrounded()}createCollisionShape(){const t=new d(this.colliderHeight,this.colliderRadius);return t.offset.y=this.colliderRadius+this.colliderHeight/2+this.offset,t.collisionGroup=I,t}step(t){}performMovement(t){}arrowHelper(t,e,i){const o=new n(t.clone().normalize(),e,1,i);this.actor.object.parent.add(o),setTimeout(()=>{o.removeFromParent()},30)}};T=t([f({inEditor:!1}),e("design:paramtypes",[])],T);export{T as CharacterMovementComponent};const b=new a(0,1,0),A=new a;const j=new a,R=new a,C=new a(0,1,0),D=new a(0,-.1,0),G=(new a(0,-1,0),new i.CharacterCollision),z=new s.Quaternion,F=new s.Euler,E=new s.Vector3;/*
|
|
2
2
|
* Copyright (©) 2026 Hology Interactive AB. All rights reserved.
|
|
3
3
|
* See the LICENSE.md file for details.
|
|
4
4
|
*/
|
|
@@ -5,6 +5,7 @@ import { PositionalAudioActor } from './positional-audio-actor.js';
|
|
|
5
5
|
import { SpawnPoint } from './spawn-point.js';
|
|
6
6
|
import { TriggerVolume } from './trigger-volume.js';
|
|
7
7
|
import { FogVolume } from '../../../rendering/fog/fog-volume-actor.js';
|
|
8
|
+
import { LightProbeVolume } from '../../../rendering/light-probes/light-probe-volume-actor.js';
|
|
8
9
|
export declare const builtInActors: {
|
|
9
10
|
Camera: typeof CameraActor;
|
|
10
11
|
SpawnPoint: typeof SpawnPoint;
|
|
@@ -13,6 +14,8 @@ export declare const builtInActors: {
|
|
|
13
14
|
NavMesh: typeof NavMeshActor;
|
|
14
15
|
PostProcessVolume: typeof PostProcessVolume;
|
|
15
16
|
FogVolume: typeof FogVolume;
|
|
17
|
+
LightProbeVolume: typeof LightProbeVolume;
|
|
16
18
|
};
|
|
17
19
|
export default builtInActors;
|
|
20
|
+
export declare const experimentalActors: string[];
|
|
18
21
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{PostProcessVolume as o}from"./post-process-volume-actor.js";import{CameraActor as r}from"./camera-actor.js";import t from"./navmesh-actor.js";import{PositionalAudioActor as
|
|
1
|
+
import{PostProcessVolume as o}from"./post-process-volume-actor.js";import{CameraActor as r}from"./camera-actor.js";import t from"./navmesh-actor.js";import{PositionalAudioActor as e}from"./positional-audio-actor.js";import{SpawnPoint as m}from"./spawn-point.js";import{TriggerVolume as i}from"./trigger-volume.js";import{FogVolume as s}from"../../../rendering/fog/fog-volume-actor.js";import{LightProbeVolume as p}from"../../../rendering/light-probes/light-probe-volume-actor.js";export const builtInActors={Camera:r,SpawnPoint:m,TriggerVolume:i,PositionalAudio:e,NavMesh:t,PostProcessVolume:o,FogVolume:s,LightProbeVolume:p};export default builtInActors;export const experimentalActors=["LightProbeVolume"];/*
|
|
2
2
|
* Copyright (©) 2026 Hology Interactive AB. All rights reserved.
|
|
3
3
|
* See the LICENSE.md file for details.
|
|
4
4
|
*/
|
|
@@ -14,6 +14,8 @@ export declare class ThirdPersonCameraComponent extends ActorComponent {
|
|
|
14
14
|
viewAngle: number;
|
|
15
15
|
collision: boolean;
|
|
16
16
|
collisionSphereRadius: number;
|
|
17
|
+
smoothCamera: boolean;
|
|
18
|
+
smoothSpeed: number;
|
|
17
19
|
camera: PerspectiveCamera;
|
|
18
20
|
distance: number;
|
|
19
21
|
minDistance: number;
|
|
@@ -51,6 +53,10 @@ export declare class ThirdPersonCameraComponent extends ActorComponent {
|
|
|
51
53
|
private getLookAtPosition;
|
|
52
54
|
private updateCameraPosition;
|
|
53
55
|
private prevLookAt;
|
|
56
|
+
private smoothedLookAt;
|
|
57
|
+
private smoothedRotX;
|
|
58
|
+
private smoothedRotY;
|
|
59
|
+
private smoothZoom;
|
|
54
60
|
}
|
|
55
61
|
/** @deprecated Use ThirdPersonCameraComponent. No difference just renamed */
|
|
56
62
|
export declare class ThirdPartyCameraComponent extends ThirdPersonCameraComponent {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{__decorate as t,__metadata as e}from"tslib";import{ActorComponent as
|
|
1
|
+
import{__decorate as t,__metadata as e}from"tslib";import{ActorComponent as o,Component as i}from"../component.js";import{Vector3 as s,MathUtils as h,PerspectiveCamera as n,Object3D as r}from"three";import{ViewController as a}from"../../services/render.js";import{DecimalInput as c,RestrictedRotationInput as d}from"../../input/index.js";import{PhysicsSystem as m}from"../../services/physics/physics-system.js";import{Parameter as p}from"../../../shader/parameter.js";import{World as l}from"../../services/world.js";import{inject as u}from"../../inject.js";import{ease as f}from"@hology/nebula";const v=void 0!==window&&/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);let k=class extends o{constructor(){super(),this.viewController=u(a),this.physicsSystem=u(m),this.aspect=this.viewController.htmlElement.clientWidth/this.viewController.htmlElement.clientHeight,this.near=.5,this.far=500,this.viewAngle=v?30:45,this.collision=!0,this.collisionSphereRadius=.25,this.smoothCamera=!1,this.smoothSpeed=10,this.camera=new n(this.viewAngle,this.aspect,this.near,this.far),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=3,this.restrictedDistance=Math.max(this.distance,this.maxDistance),this.rotationInput=new d(-Math.PI/4,Math.PI/2-.7),this.zoomInput=new c(1,0,1),this.offset=new s,this.lookAtOffset=new s(this.offsetX,0,this.offsetZ),this.fixedBehind=!0,this.world=u(l),this.activated=!1,this.isMouseLocked=!1,this.prevFixedBehind=!1,this.blendDurationLeft=0,this.canvas=null,this.pointerLockInactivatedAt=null,this.onMouseDown=t=>{this.isMouseLocked||"mouse"!==t.pointerType||this.hideCursor()},this.onKeyDown=t=>{"Escape"===t.key&&this.showCursor()},this.onPointerLockChange=()=>{null!=document.pointerLockElement||null!=document.mozPointerLockElement||this.showCursor()},this.prevLookAt=new s,this.smoothedLookAt=new s,this.smoothedRotX=0,this.smoothedRotY=0,this.smoothZoom=0}async onInit(){this.prevFixedBehind=this.fixedBehind,this.world.scene.add(this.camera),this.rotationInput.rotation.copy(this.actor.rotation),this.smoothedRotX=this.rotationInput.rotation.x,this.smoothedRotY=this.rotationInput.rotation.y,this.restrictedDistance=Math.max(this.distance,this.maxDistance),this.lookAtOffset.set(this.offsetX,0,this.offsetZ),this.smoothZoom=this.zoomInput.value,this.autoActivate&&this.activate()}activate(){this.activated=!0,this.viewController.setCamera(this.camera);const t=this.element;null!=document.body.requestPointerLock&&(t.addEventListener("pointerdown",this.onMouseDown),t.addEventListener("keydown",this.onKeyDown),document.addEventListener("pointerlockchange",this.onPointerLockChange,!1),this.disposed.subscribe(()=>{t.removeEventListener("pointerdown",this.onMouseDown),t.removeEventListener("keydown",this.onKeyDown),document.removeEventListener("pointerlockchange",this.onPointerLockChange,!1)}))}onLateUpdate(t){this.activated&&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.canvas.requestPointerLock(),this.isMouseLocked=!0))}showCursor(){this.pointerLockInactivatedAt=performance.now(),this.element.style.cursor="default",window.document.exitPointerLock(),this.isMouseLocked=!1}setFromRotation(t){this.lookAtOffset.set(this.offsetX,0,this.offsetZ),this.fixedBehind!==this.prevFixedBehind?(this.blendDurationLeft=1,this.prevFixedBehind=this.fixedBehind):this.blendDurationLeft>0&&(this.blendDurationLeft-=t),this.collision&&this.checkForCollision(t);let e=this.zoomInput.value,o=this.rotationInput.rotation.x,i=this.rotationInput.rotation.y;if(this.smoothCamera){const s=1-Math.exp(-this.smoothSpeed*t*2);this.smoothedRotX=h.lerp(this.smoothedRotX,o,s),this.smoothedRotY=h.lerp(this.smoothedRotY,i,s),o=this.smoothedRotX,i=this.smoothedRotY,this.smoothZoom=h.lerp(this.smoothZoom,e,.4*s),e=this.smoothZoom}else this.smoothedRotX=o,this.smoothedRotY=i;const s=h.clamp(Math.min(this.restrictedDistance,this.distance),Math.min(this.minDistance,this.restrictedDistance),Math.max(this.distance*e,this.minDistance)),n=Math.cos(o)*s,r=this.fixedBehind?0:i;this.offset.x=Math.sin(-r)*n,this.offset.y=Math.sin(o)*s+2,this.offset.z=Math.cos(-r)*-n,this.fixedBehind&&this.offset.add(this.lookAtOffset),this.updateCameraPosition(t)}checkForCollision(t){const e=this.getLookAtPosition(),o=this.camera.getWorldPosition(A),i=x.subVectors(o,e),s=i.length();if(s<.001)return;i.divideScalar(s);const n=this.physicsSystem.sphereCast(e,this.collisionSphereRadius,i,this.distance,void 0,{excludeActor:this.actor,excludeTriggers:!0,collisionFilter:-2});if(n.hasHit){const e=Math.max(this.minDistance,n.distance-.05);e<this.restrictedDistance?this.restrictedDistance=e:this.restrictedDistance=h.lerp(this.restrictedDistance,e,h.clamp(this.bounceBackSpeed*t,0,1))}else this.restrictedDistance=h.lerp(this.restrictedDistance,this.distance,h.clamp(this.bounceBackSpeed*t,0,1))}getLookAtPosition(){const t=L;return t.set(0,0,0),t.y=this.height,this.fixedBehind&&t.add(this.lookAtOffset),t.applyMatrix4(this.actor.object.matrixWorld),t}updateCameraPosition(t){this.actor.object.updateWorldMatrix(!0,!1),this.fixedBehind?(y.position.set(this.offset.x,this.offset.y,this.offset.z),y.rotation.set(0,0,0),y.scale.set(1,1,1),y.applyMatrix4(this.actor.object.matrix)):y.position.copy(this.actor.position).add(this.offset);const e=f.easeInOutCubic(1-this.blendDurationLeft);if(this.blendDurationLeft>0){const t=this.getLookAtPosition();this.prevLookAt.lerp(t,e),this.camera.lookAt(this.prevLookAt),this.camera.position.lerp(y.position,e),this.smoothedLookAt.copy(this.prevLookAt)}else{const e=this.getLookAtPosition();if(this.smoothCamera){const o=1-Math.exp(-this.smoothSpeed*t);this.smoothedLookAt.distanceToSquared(e)>1?this.smoothedLookAt.copy(e):this.smoothedLookAt.lerp(e,o)}else this.smoothedLookAt.copy(e);w.subVectors(y.position,e),this.camera.position.copy(this.smoothedLookAt).add(w),this.camera.lookAt(this.smoothedLookAt),this.prevLookAt.copy(this.smoothedLookAt)}}};t([p(),e("design:type",Number)],k.prototype,"near",void 0),t([p(),e("design:type",Number)],k.prototype,"far",void 0),t([p(),e("design:type",Number)],k.prototype,"viewAngle",void 0),t([p(),e("design:type",Boolean)],k.prototype,"collision",void 0),t([p(),e("design:type",Number)],k.prototype,"collisionSphereRadius",void 0),t([p(),e("design:type",Boolean)],k.prototype,"smoothCamera",void 0),t([p(),e("design:type",Number)],k.prototype,"smoothSpeed",void 0),k=t([i(),e("design:paramtypes",[])],k);export{k as ThirdPersonCameraComponent};const y=new r,L=new s,w=new s,x=new s,A=(new s,new s);export class ThirdPartyCameraComponent extends k{}/*
|
|
2
2
|
* Copyright (©) 2026 Hology Interactive AB. All rights reserved.
|
|
3
3
|
* See the LICENSE.md file for details.
|
|
4
4
|
*/
|
|
@@ -30,7 +30,7 @@ export declare class ViewController {
|
|
|
30
30
|
getMuted(): boolean;
|
|
31
31
|
private outlined;
|
|
32
32
|
setOutlined(objects: THREE.Object3D[]): void;
|
|
33
|
-
getOutlined(): THREE.Object3D<THREE.Object3DEventMap>[];
|
|
33
|
+
getOutlined(): THREE.Object3D<THREE.Object3DEventMap, Event>[];
|
|
34
34
|
addOutlined(object: THREE.Object3D): void;
|
|
35
35
|
removeOutlined(object: THREE.Object3D): void;
|
|
36
36
|
setOutlineColor(color: THREE.Color): void;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{__decorate as e,__metadata as t}from"tslib";import{Service as i}from"typedi";import{Camera as s}from"three";import{RenderingView as n}from"../../rendering.js";import{BehaviorSubject as r,Observable as o,Subject as a,takeUntil as h}from"rxjs";import*as u from"three";let d=class{constructor(e){this.view=e,this.tick=new a,this.lateTick=new a,this.audioListener=new u.AudioListener,this.pausedChanged=new r(!1),this.mutedChanged=new r(!1),this._muted=!1,this.activeTimers=new Map,this.nextTimerId=0,this.outlined=[],e.onLoop(e=>{this.tick.next(e),this.lateTick.next(e)}),e.camera.add(this.audioListener),window.hology_view=this}set fpsCap(e){this.view.fpsCap=e}get fpsCap(){return this.view.fpsCap}set showStats(e){this.view.showStats=e}get showStats(){return this.view.showStats}addPostProcessVolume(e){this.view.addPostProcessVolume(e)}removePostProcessVolume(e){this.view.removePostProcessVolume(e)}onUpdate(e){return null!=e&&this.tick.pipe(h(e.disposed)),this.tick}onLateUpdate(e){return null!=e&&this.lateTick.pipe(h(e.disposed)),this.lateTick}setCamera(e){const t=e instanceof s?e:e.camera.instance;this.view.setCamera(t),t.add(this.audioListener)}getCamera(){return this.view.camera}setMuted(e){this._muted=e,this.audioListener.gain.gain.setValueAtTime(e?0:1,this.audioListener.context.currentTime),this.mutedChanged.next(e)}getMuted(){return this._muted}setOutlined(e){this.outlined.length=0;const t=this.outlined;for(let i=0;i<e.length;i++)t[i]=e[i];this.view.setSelectedObjects(t),this.view.setEnableOutlines(e.length>0)}getOutlined(){return this.outlined}addOutlined(e){this.outlined.includes(e)||(this.outlined.push(e),this.view.setSelectedObjects(this.outlined),this.view.setEnableOutlines(!0))}removeOutlined(e){const t=this.outlined.indexOf(e);-1!==t&&(this.outlined.splice(t,1),this.view.setSelectedObjects(this.outlined))}setOutlineColor(e){this.view.outlinePass?.visibleEdgeColor.copy(e)}getOutlineColor(){return this.view.outlinePass?.visibleEdgeColor}setOutlineThickness(e){this.view.outlinePass.edgeThickness=e}getOutlineThickness(){return this.view.outlinePass.edgeThickness}get htmlElement(){return this.view.container}get paused(){return this.view.paused}set paused(e){e!=this.paused&&(e?this.pauseRendering():this.unpauseRendering())}pauseRendering(){this.view.paused=!0,this.pausedChanged.next(this.view.paused);for(const[e,t]of this.activeTimers){clearTimeout(t.timeoutId);const e=Date.now()-t.startTime;t.remainingMs=Math.max(0,t.remainingMs-e)}}unpauseRendering(){this.view.paused=!1,this.pausedChanged.next(this.view.paused);for(const[e,t]of this.activeTimers)t.remainingMs>0&&(t.timeoutId=setTimeout(()=>{t.subscriber.next(),t.subscriber.complete(),this.activeTimers.delete(e)},t.remainingMs),t.startTime=Date.now())}dispose(){this.view.running&&this.view.stop(),this.audioListener.removeFromParent(),this.tick.complete(),this.lateTick.complete(),this.paused=!0}createTimer(e){const t=this.nextTimerId++;return new o(i=>{const s=Date.now(),n=setTimeout(()=>{i.next(),i.complete(),this.activeTimers.delete(t)},e);return this.activeTimers.set(t,{timeoutId:n,remainingMs:e,startTime:s,subscriber:i}),()=>{clearTimeout(n),this.activeTimers.delete(t)}})}getScreenPosition(e,t=
|
|
1
|
+
import{__decorate as e,__metadata as t}from"tslib";import{Service as i}from"typedi";import{Camera as s}from"three";import{RenderingView as n}from"../../rendering.js";import{BehaviorSubject as r,Observable as o,Subject as a,takeUntil as h}from"rxjs";import*as u from"three";let d=class{constructor(e){this.view=e,this.tick=new a,this.lateTick=new a,this.audioListener=new u.AudioListener,this.pausedChanged=new r(!1),this.mutedChanged=new r(!1),this._muted=!1,this.activeTimers=new Map,this.nextTimerId=0,this.outlined=[],e.onLoop(e=>{this.tick.next(e),this.lateTick.next(e)}),e.camera.add(this.audioListener),window.hology_view=this}set fpsCap(e){this.view.fpsCap=e}get fpsCap(){return this.view.fpsCap}set showStats(e){this.view.showStats=e}get showStats(){return this.view.showStats}setLightVolume(e){this.view.lightVolume=e}getLightVolume(){return this.view.lightVolume}addPostProcessVolume(e){this.view.addPostProcessVolume(e)}removePostProcessVolume(e){this.view.removePostProcessVolume(e)}onUpdate(e){return null!=e&&this.tick.pipe(h(e.disposed)),this.tick}onLateUpdate(e){return null!=e&&this.lateTick.pipe(h(e.disposed)),this.lateTick}setCamera(e){const t=e instanceof s?e:e.camera.instance;this.view.setCamera(t),t.add(this.audioListener)}getCamera(){return this.view.camera}setMuted(e){this._muted=e,this.audioListener.gain.gain.setValueAtTime(e?0:1,this.audioListener.context.currentTime),this.mutedChanged.next(e)}getMuted(){return this._muted}setOutlined(e){this.outlined.length=0;const t=this.outlined;for(let i=0;i<e.length;i++)t[i]=e[i];this.view.setSelectedObjects(t),this.view.setEnableOutlines(e.length>0)}getOutlined(){return this.outlined}addOutlined(e){this.outlined.includes(e)||(this.outlined.push(e),this.view.setSelectedObjects(this.outlined),this.view.setEnableOutlines(!0))}removeOutlined(e){const t=this.outlined.indexOf(e);-1!==t&&(this.outlined.splice(t,1),this.view.setSelectedObjects(this.outlined))}setOutlineColor(e){this.view.outlinePass?.visibleEdgeColor.copy(e)}getOutlineColor(){return this.view.outlinePass?.visibleEdgeColor}setOutlineThickness(e){this.view.outlinePass.edgeThickness=e}getOutlineThickness(){return this.view.outlinePass.edgeThickness}get htmlElement(){return this.view.container}get paused(){return this.view.paused}set paused(e){e!=this.paused&&(e?this.pauseRendering():this.unpauseRendering())}pauseRendering(){this.view.paused=!0,this.pausedChanged.next(this.view.paused);for(const[e,t]of this.activeTimers){clearTimeout(t.timeoutId);const e=Date.now()-t.startTime;t.remainingMs=Math.max(0,t.remainingMs-e)}}unpauseRendering(){this.view.paused=!1,this.pausedChanged.next(this.view.paused);for(const[e,t]of this.activeTimers)t.remainingMs>0&&(t.timeoutId=setTimeout(()=>{t.subscriber.next(),t.subscriber.complete(),this.activeTimers.delete(e)},t.remainingMs),t.startTime=Date.now())}dispose(){this.view.running&&this.view.stop(),this.audioListener.removeFromParent(),this.tick.complete(),this.lateTick.complete(),this.paused=!0}createTimer(e){const t=this.nextTimerId++;return new o(i=>{const s=Date.now(),n=setTimeout(()=>{i.next(),i.complete(),this.activeTimers.delete(t)},e);return this.activeTimers.set(t,{timeoutId:n,remainingMs:e,startTime:s,subscriber:i}),()=>{clearTimeout(n),this.activeTimers.delete(t)}})}getScreenPosition(e,t=m){const i=this.getCamera();return l.multiplyMatrices(i.projectionMatrix,i.matrixWorldInverse),i instanceof u.PerspectiveCamera&&(c.setFromProjectionMatrix(l),!c.containsPoint(e))?null:(t.copy(e),t.project(i),t.x=(t.x+1)/2*this.htmlElement.clientWidth,t.y=(1-t.y)/2*this.htmlElement.clientHeight,t)}};d=e([i(),t("design:paramtypes",[n])],d);export{d as ViewController};const l=new u.Matrix4,m=new u.Vector3,c=new u.Frustum;/*
|
|
2
2
|
* Copyright (©) 2026 Hology Interactive AB. All rights reserved.
|
|
3
3
|
* See the LICENSE.md file for details.
|
|
4
4
|
*/
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Color } from "three";
|
|
2
2
|
import { BaseActor } from "../../gameplay";
|
|
3
|
+
import { ScatteringMode } from "./fog-volume-object";
|
|
3
4
|
export declare class FogVolume extends BaseActor {
|
|
4
5
|
baseDensity: number;
|
|
5
6
|
heightFalloff: number;
|
|
@@ -7,6 +8,9 @@ export declare class FogVolume extends BaseActor {
|
|
|
7
8
|
startDistance: number;
|
|
8
9
|
scatteringDistribution: number;
|
|
9
10
|
albedo: Color;
|
|
11
|
+
emissive: Color;
|
|
12
|
+
ambientIntensity: number;
|
|
13
|
+
scatteringMode: ScatteringMode;
|
|
10
14
|
private sprite;
|
|
11
15
|
onInit(): Promise<void> | void;
|
|
12
16
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{__decorate as
|
|
1
|
+
import{__decorate as t,__metadata as e}from"tslib";import{Color as i}from"three";import{Actor as o,attach as s,BaseActor as r,Parameter as n}from"../../gameplay";import{FogVolumeObject as a}from"./fog-volume-object";import{EditorSpriteComponent as l}from"../../gameplay/actors/builtin/components/editor-sprite-component";let h=class extends r{constructor(){super(...arguments),this.baseDensity=.5,this.heightFalloff=1,this.heightOffset=0,this.startDistance=3,this.scatteringDistribution=0,this.albedo=new i(1,1,.6),this.emissive=new i(0,0,0),this.ambientIntensity=1,this.scatteringMode="volumetric",this.sprite=s(l,{file:"assets/cloud-icon.webp"})}onInit(){this.object.add(new a(this))}};t([n({range:[0,5],stepSize:.01,help:"The density (or thickness) of fog."}),e("design:type",Number)],h.prototype,"baseDensity",void 0),t([n(),e("design:type",Number)],h.prototype,"heightFalloff",void 0),t([n(),e("design:type",Number)],h.prototype,"heightOffset",void 0),t([n(),e("design:type",Number)],h.prototype,"startDistance",void 0),t([n({range:[-1,1],help:"\n Controls the direction in which light scatters inside the fog.\n\n - Positive values (> 0): More light is scattered forward, making beams and shafts more visible when looking toward the directional light source. (for dramatic god rays).\n - Zero: Even scattering in all directions—fog appears uniform.\n - Negative values (< 0): Fog is brighter when looking away from the directional light source.\n "}),e("design:type",Number)],h.prototype,"scatteringDistribution",void 0),t([n(),e("design:type",i)],h.prototype,"albedo",void 0),t([n({help:"Self-illumination color - makes fog visible even without light sources. Useful for stylized fog that should always be visible."}),e("design:type",i)],h.prototype,"emissive",void 0),t([n({range:[0,2],stepSize:.1,help:"Multiplier for ambient light contribution. Higher values make fog more visible in shadows. Set to 0 for god rays that only appear in direct light."}),e("design:type",Number)],h.prototype,"ambientIntensity",void 0),t([n({options:[{name:"Volumetric",value:"volumetric"},{name:"Light shaft",value:"lightShaft"}],help:"\n Controls how fog interacts with lighting:\n \n - volumetric: Standard fog. Occludes background, can appear dark in shadows. Good for realistic fog.\n - lightShaft: God rays/light scattering. Only visible where light hits (shadow map). Good for dramatic light shafts.\n "}),e("design:type",String)],h.prototype,"scatteringMode",void 0),h=t([o()],h);export{h as FogVolume};/*
|
|
2
2
|
* Copyright (©) 2026 Hology Interactive AB. All rights reserved.
|
|
3
3
|
* See the LICENSE.md file for details.
|
|
4
4
|
*/
|
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import { Color, Object3D } from 'three';
|
|
2
|
+
/**
|
|
3
|
+
* Controls how fog affects the scene:
|
|
4
|
+
* - "volumetric": Standard fog. Fog always has opacity based on density, can appear dark in shadows.
|
|
5
|
+
* - "lightShaft": God rays/light scattering. Fog only visible where directional light hits (using shadow map).
|
|
6
|
+
* Invisible in shadows. No ambient/emissive contribution.
|
|
7
|
+
*/
|
|
8
|
+
export type ScatteringMode = "volumetric" | "lightShaft";
|
|
2
9
|
export type FogVolumeSettings = {
|
|
3
10
|
object: Object3D;
|
|
4
11
|
baseDensity: number;
|
|
@@ -7,5 +14,11 @@ export type FogVolumeSettings = {
|
|
|
7
14
|
startDistance: number;
|
|
8
15
|
scatteringDistribution: number;
|
|
9
16
|
albedo: Color;
|
|
17
|
+
/** Self-illumination color - makes fog visible even without light sources */
|
|
18
|
+
emissive: Color;
|
|
19
|
+
/** Multiplier for how much ambient light affects the fog (default: 1.0) */
|
|
20
|
+
ambientIntensity: number;
|
|
21
|
+
/** Scattering mode: "volumetric" for standard fog, "lightShaft" for god rays */
|
|
22
|
+
scatteringMode: ScatteringMode;
|
|
10
23
|
};
|
|
11
24
|
//# sourceMappingURL=fog-volume-object.d.ts.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import*as t from"three";import{Matrix4 as e,PerspectiveCamera as n,PointLight as i,Vector3 as o,WebGLRenderTarget as a}from"three";import{FullScreenQuad as r,Pass as s}from"three/examples/jsm/Addons.js";import{ambientLightName as l}from"../../scene/sky.js";import{FogVolumeObject as c}from"./fog-volume-object";const h=new o;export class VolumetricFogPass extends s{constructor(n){super(),this.resolution=n,this.fsQuad=new r(null),this.downSampleRatio=4,this._oldClearColor=new t.Color,this.oldClearAlpha=1,this.volumesCache=new WeakMap,this._viewProjection=new e,this._invViewProjection=new e;const i={minFilter:t.LinearFilter,magFilter:t.LinearFilter,format:t.RGBAFormat,depthBuffer:!1,stencilBuffer:!1,type:t.HalfFloatType},s=Math.round(this.resolution.x/this.downSampleRatio),l=Math.round(this.resolution.y/this.downSampleRatio);this.renderTargetFog=new a(s,l,i),this.renderTargetFog.texture.generateMipmaps=!1,this.renderTargetBlur=new a(s,l,i),this.renderTargetBlur.texture.generateMipmaps=!1,this.material=new t.ShaderMaterial({defines:{PERSPECTIVE_CAMERA:1,NUM_FOG_VOLUMES:0,MAX_POINT_LIGHTS:8,MAX_SPOT_LIGHTS:2,VOLUMETRIC:1},uniforms:{tDepth:{value:null},tDiffuse:{value:null},cameraPos:{value:new o},cameraMatrix:{value:new e},invProjection:{value:new e},fogVolumes:{value:[]},stepCount:{value:30},cameraNear:{value:null},cameraFar:{value:null},pointLights:{value:new Array(8).fill(null).map(()=>({color:new o(0,0,0),distance:0,position:new o}))},activePointLightCount:{value:0},spotLights:{value:new Array(2).fill(null).map(()=>({position:new o,direction:new o,color:new o,distance:0,decay:0,coneCos:0,penumbraCos:0}))},activeSpotLightCount:{value:0},directionalLight:{value:{direction:new o(0,-1,1).normalize(),color:new o(1,1,1)}},ambientLight:{value:new o(1,1,1).multiplyScalar(1.35)},directionalShadowMatrix:{value:new e},directionalShadowMap:{value:null}},vertexShader:"\n varying vec2 vUv;\n void main() {\n vUv = uv;\n gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);\n }\n ",fragmentShader:"\n #define MAX_STEP_COUNT 64\n\n struct IncidentLight {\n vec3 color;\n vec3 direction;\n bool visible;\n };\n\n #if MAX_POINT_LIGHTS > 0\n struct PointLight {\n\t\t\tvec3 position;\n vec3 color;\n float distance;\n\t\t};\n #endif\n\n #if MAX_SPOT_LIGHTS > 0\n struct SpotLight {\n vec3 position;\n vec3 direction;\n vec3 color;\n float distance;\n float decay;\n float coneCos;\n float penumbraCos;\n };\n uniform SpotLight spotLights[ MAX_SPOT_LIGHTS ];\n uniform int activeSpotLightCount;\n float getSpotAttenuation( const in float coneCosine, const in float penumbraCosine, const in float angleCosine ) {\n return smoothstep( coneCosine, penumbraCosine, angleCosine );\n }\n #ifndef saturate\n #define saturate( a ) clamp( a, 0.0, 1.0 )\n #endif\n float pow2( const in float x ) { return x*x; }\n vec3 pow2( const in vec3 x ) { return x*x; }\n float pow4( const in float x ) { float x2 = x*x; return x2*x2; }\n float getDistanceAttenuation( const in float lightDistance, const in float cutoffDistance, const in float decayExponent ) {\n // based upon Frostbite 3 Moving to Physically-based Rendering\n // page 32, equation 26: E[window1]\n // https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf\n float distanceFalloff = 1.0 / max( pow( lightDistance, decayExponent ), 0.01 );\n if ( cutoffDistance > 0.0 ) {\n distanceFalloff *= pow2( saturate( 1.0 - pow4( lightDistance / cutoffDistance ) ) );\n }\n return distanceFalloff;\n }\n void getSpotLightInfo( const in SpotLight spotLight, const in vec3 geometryPosition, out IncidentLight light ) {\n vec3 lVector = spotLight.position - geometryPosition;\n light.direction = normalize( lVector );\n float angleCos = dot( light.direction, spotLight.direction );\n float spotAttenuation = getSpotAttenuation( spotLight.coneCos, spotLight.penumbraCos, angleCos );\n if ( spotAttenuation > 0.0 ) {\n float lightDistance = length( lVector );\n light.color = spotLight.color * spotAttenuation;\n light.color *= getDistanceAttenuation( lightDistance, spotLight.distance, spotLight.decay );\n light.visible = ( light.color != vec3( 0.0 ) );\n } else {\n light.color = vec3( 0.0 );\n light.visible = false;\n }\n }\n vec3 calcSpotLight(vec3 p, vec3 rayDir, float g, SpotLight spotLight) {\n IncidentLight directLight;\n getSpotLightInfo(spotLight, p, directLight);\n return directLight.color * 3.14;\n }\n #endif\n\n struct DirectionalLight {\n vec3 color; // pre multiplied with intensity\n vec3 direction; // should point *from the light*, normalized\n };\n\n struct FogVolume {\n vec3 sphereCenter;\n float sphereRadius;\n float baseDensity;\n float heightFalloff;\n float heightOffset;\n float scatteringDistribution;\n float startDistance;\n vec3 albedo;\n };\n\n varying vec2 vUv;\n uniform sampler2D tDiffuse;\n uniform vec3 cameraPos;\n\n uniform float stepCount;\n uniform float nearMinDistance;\n\n uniform mat4 cameraMatrix;\n uniform mat4 invProjection;\n uniform sampler2D tDepth;\n\t\tuniform float cameraNear;\n\t\tuniform float cameraFar;\n \n uniform DirectionalLight directionalLight;\n\n uniform mat4 directionalShadowMatrix;\n uniform sampler2D directionalShadowMap;\n\n uniform vec3 ambientLight;\n\n #if NUM_FOG_VOLUMES > 0\n uniform FogVolume fogVolumes[ NUM_FOG_VOLUMES ];\n #endif\n #if MAX_POINT_LIGHTS > 0\n uniform PointLight pointLights[ MAX_POINT_LIGHTS ];\n uniform int activePointLightCount;\n #endif\n\n #include <packing>\n\n // Ray-sphere intersection\n bool intersectSphere(vec3 rayOrigin, vec3 rayDir, vec3 sphereCenter, float sphereRadius, out float t0, out float t1) {\n // Vector from ray origin to sphere center\n vec3 L = rayOrigin - sphereCenter;\n float a = dot(rayDir, rayDir); // should be 1 if rayDir is normalized\n float b = 2.0 * dot(rayDir, L);\n float c = dot(L, L) - sphereRadius * sphereRadius;\n \n float discriminant = b * b - 4.0 * a * c;\n if (discriminant < 0.0) {\n return false; // no intersection\n }\n \n float sqrtD = sqrt(discriminant);\n t0 = (-b - sqrtD) / (2.0 * a);\n t1 = (-b + sqrtD) / (2.0 * a);\n \n // Make sure t0 <= t1\n if (t0 > t1) {\n float tmp = t0;\n t0 = t1;\n t1 = tmp;\n }\n return true;\n }\n\n float getLinearDepth( const in vec2 uv ) {\n\n float fragCoordZ = texture2D( tDepth, uv ).x;\n return perspectiveDepthToViewZ( fragCoordZ, cameraNear, cameraFar );\n\n\t\t}\n\n float linearizeDepth(float depth) {\n float z = depth * 2.0 - 1.0; // back to NDC (-1..1)\n return (2.0 * cameraNear * cameraFar) / (cameraFar + cameraNear - z * (cameraFar - cameraNear));\n }\n\n\n\n float texture2DCompare( sampler2D depths, vec2 uv, float compare ) {\n return step( compare, unpackRGBAToDepth( texture2D( depths, uv ) ) );\n }\n\n vec3 calcDirectionalLight(vec3 p, vec3 rayDir, float g, DirectionalLight dirLight) {\n vec3 toLight = dirLight.direction; // direction from sample point to light\n float cosTheta = dot(toLight, -rayDir); // angle between view ray and light\n cosTheta = clamp(cosTheta, -1.0, 1.0); // keep it safe\n\n // phase function (Henyey-Greenstein or isotropic)\n float denom = 1.0 + g*g - 2.0*g*cosTheta;\n float phase = (1.0 - g*g) / (4.0 * 3.14159265 * pow(denom, 1.5));\n phase *= 3.0 * 3.14159265; // scale to total energy\n\n vec4 shadowCoord = directionalShadowMatrix * vec4(p, 1.0);\n shadowCoord.xyz /= shadowCoord.w; // perspective divide\n\n float bias = 0.0; \n float inShadow = texture2DCompare(directionalShadowMap, shadowCoord.xy, shadowCoord.z - bias);\n\n return dirLight.color * phase * inShadow;\n }\n\n vec3 calcPointLight(vec3 p, vec3 rayDir, float g, PointLight pointLight) {\n vec3 lightPos = pointLight.position;\n vec3 lightColor = pointLight.color;\n float lightRadius = pointLight.distance;\n\n vec3 L = normalize(lightPos - p);\n float lightDist = length(lightPos - p);\n \n float attenuation = clamp(1.0 - lightDist / lightRadius, 0.0, 1.0);\n attenuation *= attenuation; // smoother falloff\n attenuation /= (1.0 + lightDist*lightDist); // inverse-square style\n\n attenuation *= 4.0 * 3.14159; // Manually scaled to match point light intensity\n // float attenuation = 1.0 / (4.0 * 3.14159 * lightDist * lightDist);\n\n\n float cosTheta = dot(L, -rayDir); // angle between light and view ray\n float phase = (1.0 - g*g) / pow(1.0 + g*g - 2.0*g*cosTheta, 1.5);\n phase *= 0.25 / 3.14159; // normalize\n\n // Intensity is part of color\n vec3 light = lightColor * attenuation * phase;\n return light;\n }\n\n float random01(vec2 p) {\n return fract(sin(dot(p, vec2(41.0, 289.0))) * 45758.5453);\n }\n\n float hash(vec3 p) { return fract(sin(dot(p, vec3(12.9898,78.233,37.719))) * 43758.5453); }\n\n // Compute perceived brightness of a color (luminosity, Rec. 709)\n float luminosity(vec3 color) {\n return dot(color, vec3(0.2126, 0.7152, 0.0722));\n }\n \n void main() {\n // reconstruct ray in world space\n vec2 ndc = vUv * 2.0 - 1.0;\n vec4 nearPoint = invProjection * vec4(ndc, -1.0, 1.0);\n nearPoint /= nearPoint.w;\n vec4 farPoint = invProjection * vec4(ndc, 1.0, 1.0);\n farPoint /= farPoint.w;\n\n vec3 rayOrigin = cameraPos;\n vec3 rayDir = normalize(farPoint.xyz - cameraPos);\n\n float sceneDepth = -getLinearDepth(vUv); // in view space\n\n // vec4 scenePosView = invProjection * vec4(ndc, sceneDepth * 2.0 - 1.0, 1.0);\n // scenePosView /= scenePosView.w;\n\n // distance from camera to surface\n // float sceneDist = length(scenePosView.xyz - cameraPosition);\n\n float transmittance = 1.0;\n vec3 inscatter = vec3(0.0);\n float random = random01(vUv);\n\n\n #if NUM_FOG_VOLUMES > 0\n for (int v = 0; v < NUM_FOG_VOLUMES; v++) {\n FogVolume volume = fogVolumes[v];\n vec3 sphereCenter = volume.sphereCenter;\n float sphereRadius = volume.sphereRadius;\n float heightFalloff = volume.heightFalloff;\n float heightOffset = volume.heightOffset;\n float baseDensity = volume.baseDensity;\n float startDistance = volume.startDistance;\n float g = volume.scatteringDistribution;\n\n float t0, t1;\n if (!intersectSphere(rayOrigin, rayDir, sphereCenter, sphereRadius, t0, t1) || t1 <= 0.0) {\n continue;\n }\n // if camera inside sphere\n t0 = max(t0, 0.0);\n\n // An alternative method to get stepSize but is less dynamic\n // float stepSize = (t1 - t0) / float(MAX_STEP_COUNT);\n\n float maxStepSize = 0.3; // adjust for visual quality\n float distance = t1 - t0;\n int stepCount = int(ceil(distance / maxStepSize));\n stepCount = min(stepCount, MAX_STEP_COUNT); // optional clamp for performance\n float stepSize = distance / float(stepCount);\n \n // Add a slight random offset to reduce banding \n float jitterAmount = 1.0;\n float rayStartOffset = random * stepSize * jitterAmount;\n t0 += rayStartOffset;\n\n for (int i = 0; i < stepCount; i++) {\n\n float t = t0 + float(i) * stepSize;\n vec3 p = rayOrigin + rayDir * t;\n\n float geomDelta = sceneDepth - t; // how far fog sample is from hitting geometry\n float fade = clamp(geomDelta, 0.0, 1.0); \n // float fade = clamp((sceneDepth - t) / 0.1, 0.0, 1.0);\n\n float closeFade = smoothstep(0.0, 0.3, (t - startDistance) / t);\n float edgeFade = clamp(sphereRadius - length(p - sphereCenter), 0.0, 1.0);\n\n // density with exponential height falloff\n float h = p.y - (sphereCenter.y + heightOffset);\n float heightFactor = clamp(exp(-h * heightFalloff), 0.0, 1.0);\n float density = baseDensity * heightFactor * fade * closeFade * edgeFade;\n\n vec3 light = vec3(0.0); // simple white light\n #if VOLUMETRIC == 1\n\n #if MAX_POINT_LIGHTS > 0\n for (int pi = 0; pi < activePointLightCount; pi++) {\n light += calcPointLight(p, rayDir, g, pointLights[pi]);\n } \n #endif\n\n #if MAX_SPOT_LIGHTS > 0\n for (int pi = 0; pi < activeSpotLightCount; pi++) {\n light += calcSpotLight(p, rayDir, g, spotLights[pi]);\n } \n #endif\n\n light += calcDirectionalLight(p, rayDir, g, directionalLight);\n\n // Ambient \n light += ambientLight / 12.0;\n\n // Use luminosity to reduce transparency when dark to avoid dark fog\n transmittance *= exp(-density * stepSize * luminosity(light));\n #else\n light = vec3(1.0);\n transmittance *= exp(-density * stepSize);\n #endif\n\n light *= volume.albedo;\n inscatter += transmittance * light * density * stepSize;\n }\n }\n #endif\n\n float alpha = 1.0 - transmittance;\n // inscatter += ambientLight * alpha;\n\n vec3 color = inscatter;\n\n gl_FragColor = vec4(color, alpha);\n }\n "}),this.material.onBeforeCompile=t=>{},this.materialBlur=new t.ShaderMaterial({uniforms:{tInput:{value:null},tDepth:{value:null},texelSize:{value:new t.Vector2(1,1).divideScalar(1e3)},direction:{value:new t.Vector2(1,0)},depthSigma:{value:2},blurSigma:{value:2}},vertexShader:"\n varying vec2 vUv;\n void main() {\n vUv = uv;\n gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);\n }\n ",fragmentShader:"\n uniform sampler2D tInput;\n uniform sampler2D tDepth;\n uniform vec2 texelSize;\n uniform vec2 direction;\n uniform float depthSigma;\n uniform float blurSigma;\n varying vec2 vUv;\n\n #include <packing>\n\n void main() {\n // float centerDepth = texture2D(tDepth, vUv).r;\n\n float centerDepth = unpackRGBAToDepth( texture2D( tDepth, vUv ) );\n vec4 center = texture2D(tInput, vUv);\n vec4 centerColor = center;\n\n vec4 sum = vec4(0.0);\n float wsum = 0.0;\n\n // int radius = int(ceil(2.0 * blurSigma));\n\n for (int i = -5; i <= 5; i++) {\n // if (i > radius || i < -radius) continue;\n\n vec2 offset = direction * float(i) * texelSize; // this should be based on resolution\n vec2 uv = vUv + offset;\n\n vec4 sampleColor = texture2D(tInput, uv);\n float sampleDepth = unpackRGBAToDepth( texture2D( tDepth, uv ) );\n\n float spatialWeight = exp(-0.5 * (float(i) * float(i)) / (blurSigma * blurSigma));\n float depthDiff = abs(sampleDepth - centerDepth) * 100.0;\n float depthWeight = exp(-0.5 * (depthDiff * depthDiff) / (depthSigma * depthSigma));\n\n float w = spatialWeight * depthWeight;\n\n sum += sampleColor * w;\n wsum += w;\n }\n\n gl_FragColor = sum / wsum;\n }\n ",transparent:!0}),this.materialComposite=new t.ShaderMaterial({uniforms:{tDiffuse:{value:null},tFog:{value:null}},vertexShader:"\n varying vec2 vUv;\n void main() {\n vUv = uv;\n gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);\n }\n ",fragmentShader:"\n uniform sampler2D tDiffuse;\n uniform sampler2D tFog;\n varying vec2 vUv;\n void main() {\n vec4 sceneColor = texture2D(tDiffuse, vUv);\n vec4 fogColor = texture2D(tFog, vUv);\n // Composite: fogColor.a is the fog alpha, blend over scene\n // vec3 color = mix(sceneColor.rgb, fogColor.rgb, fogColor.a);\n // Not sure why multipling alpha like this helps but it is necessary to avoid a dark\n // borders around the fog\n gl_FragColor = vec4(fogColor.rgb + (1.0 - fogColor.a * fogColor.a) * sceneColor.rgb, 1.0);\n }\n ",transparent:!0})}render(t,e,n){t.getClearColor(this._oldClearColor),this.oldClearAlpha=t.getClearAlpha();const i=t.autoClear;t.autoClear=!1,t.setClearColor(16777215,0),this.fsQuad.material=this.material,t.setRenderTarget(this.renderTargetFog),t.clear(),this.fsQuad.render(t),this.materialBlur.uniforms.tInput.value=this.renderTargetFog.texture,this.materialBlur.uniforms.direction.value=u,this.fsQuad.material=this.materialBlur,t.setRenderTarget(this.renderTargetBlur),t.clear(),this.fsQuad.render(t),this.materialBlur.uniforms.tInput.value=this.renderTargetBlur.texture,this.materialBlur.uniforms.direction.value=f,t.setRenderTarget(this.renderTargetFog),t.clear(),this.fsQuad.render(t),this.materialComposite.uniforms.tDiffuse.value=n.texture,this.materialComposite.uniforms.tFog.value=this.renderTargetFog.texture,this.fsQuad.material=this.materialComposite,this.renderToScreen?(t.setRenderTarget(null),this.fsQuad.render(t)):(t.setRenderTarget(e),this.clear&&t.clear(),this.fsQuad.render(t)),t.setClearColor(this._oldClearColor,this.oldClearAlpha),t.autoClear=i}setSize(t,e){let n=Math.round(t/this.downSampleRatio),i=Math.round(e/this.downSampleRatio);this.renderTargetFog.setSize(n,i),this.renderTargetBlur.setSize(n,i),this.materialBlur.uniforms.texelSize.value.set(1/n,1/i)}set volumetric(t){this.material.defines.VOLUMETRIC=t?1:0}get volumetric(){return!!this.material.defines.VOLUMETRIC}getVolumeUniforms(t){let e=this.volumesCache.get(t);return null==e&&(e={albedo:new o,baseDensity:t.baseDensity,heightFalloff:t.heightFalloff,heightOffset:t.heightOffset,startDistance:t.startDistance??3,scatteringDistribution:t.scatteringDistribution,sphereRadius:1,sphereCenter:new o},this.volumesCache.set(t,e)),t.object.getWorldPosition(e.sphereCenter),t.object.getWorldScale(h),e.sphereRadius=5*h.x,e.albedo.set(t.albedo.r,t.albedo.g,t.albedo.b),e.baseDensity=t.baseDensity,e.heightFalloff=t.heightFalloff,e.heightOffset=t.heightOffset,e.scatteringDistribution=t.scatteringDistribution,e.startDistance=t.startDistance,e}getPointLightUniforms(t){const e={position:new o,color:new o,distance:0};return t.getWorldPosition(e.position),e.color.set(t.color.r,t.color.g,t.color.b).multiplyScalar(t.intensity*(t.userData?.volumetricIntensity??1)),e.distance=void 0!==t.distance&&t.distance>0?t.distance:1,e}getSpotLightUniforms(t){const e={position:new o,direction:new o,color:new o,distance:0,coneCos:0,penumbraCos:0,decay:1};return t.getWorldPosition(e.position),e.direction.setFromMatrixPosition(t.matrixWorld),h.setFromMatrixPosition(t.target.matrixWorld),e.direction.sub(h),e.color.set(t.color.r,t.color.g,t.color.b).multiplyScalar(t.intensity*(t.userData?.volumetricIntensity??1)),e.distance=void 0!==t.distance&&t.distance>0?t.distance:0,e.coneCos=Math.cos(t.angle),e.penumbraCos=Math.cos(t.angle*(1-t.penumbra)),e.decay=void 0!==t.decay?t.decay:1,e}updateArrayUniforms(e){const n=this.material.uniforms.fogVolumes.value;let o=0;const a=this.material.uniforms.pointLights.value;let r=0;const s=this.material.uniforms.spotLights.value;let l=0;e.traverseVisible(e=>{if(e instanceof c){const t=e.volume,i=this.getVolumeUniforms(t);n[o]=i,o++}else if(e instanceof i){if(r<a.length){if(0===e.userData.volumetricIntensity)return;const t=this.getPointLightUniforms(e);a[r].position.copy(t.position),a[r].color.copy(t.color),a[r].distance=t.distance,r++}}else if(e instanceof t.SpotLight&&l<s.length){if(0===e.userData.volumetricIntensity)return;const t=this.getSpotLightUniforms(e);s[l].position.copy(t.position),s[l].direction.copy(t.direction),s[l].color.copy(t.color),s[l].distance=t.distance,s[l].coneCos=t.coneCos,s[l].penumbraCos=t.penumbraCos,s[l].decay=t.decay,l++}}),this.material.uniforms.activePointLightCount.value=r,this.material.uniforms.activeSpotLightCount.value=l,this.material.defines.NUM_FOG_VOLUMES!==o&&(this.material.needsUpdate=!0,this.material.uniformsNeedUpdate=!0),this.material.defines.NUM_FOG_VOLUMES=o}get uniforms(){return this.material.uniforms}update(t,e,i,a){if(this.updateArrayUniforms(a),this.enabled=0!=this.material.defines.NUM_FOG_VOLUMES,!this.enabled)return;null==this.uniforms.cameraPosition&&(this.uniforms.cameraPos={value:new o}),this.uniforms.cameraPos.value.copy(t.position),this._viewProjection.multiplyMatrices(t.projectionMatrix,t.matrixWorldInverse),this.uniforms.invProjection.value.copy(this._invViewProjection.copy(this._viewProjection).invert()),this.uniforms.tDepth.value=e.depthTexture,this.materialBlur.uniforms.tDepth.value=e.depthTexture,t instanceof n&&(this.uniforms.cameraNear.value=t.near,this.uniforms.cameraFar.value=t.far),this.uniforms.directionalLight.value.direction=i.lightDirection,this.uniforms.directionalLight.value.color.set(1,1,1).multiplyScalar(i.lightIntensity);const r=i.lights[0];r&&r.shadow&&r.castShadow&&r.visible&&(this.uniforms.directionalShadowMap.value=r.shadow.map?.texture,this.uniforms.directionalShadowMatrix.value=r.shadow.matrix),r&&!r.visible?this.uniforms.directionalLight.value.color.set(0,0,0):this.uniforms.directionalLight.value.color.set(r.color.r,r.color.g,r.color.b).multiplyScalar(r.intensity*(r.userData?.volumetricIntensity??1));const s=a.children.find(t=>t.name===l);null!=s&&s.visible?this.uniforms.ambientLight.value.set(s.color.r,s.color.g,s.color.b).multiplyScalar(s.intensity*(s.userData?.volumetricIntensity??1)):this.uniforms.ambientLight.value.set(0,0,0)}}const u=new t.Vector2(1,0),f=new t.Vector2(0,1);/*
|
|
1
|
+
import*as t from"three";import{Matrix4 as e,PerspectiveCamera as n,PointLight as i,Vector3 as o,WebGLRenderTarget as a}from"three";import{FullScreenQuad as r,Pass as s}from"three/examples/jsm/Addons.js";import{ambientLightName as l}from"../../scene/sky.js";import{FogVolumeObject as c}from"./fog-volume-object";const h=new o;export class VolumetricFogPass extends s{constructor(n){super(),this.resolution=n,this.fsQuad=new r(null),this.downSampleRatio=4,this._oldClearColor=new t.Color,this.oldClearAlpha=1,this.volumesCache=new WeakMap,this._viewProjection=new e,this._invViewProjection=new e;const i={minFilter:t.LinearFilter,magFilter:t.LinearFilter,format:t.RGBAFormat,depthBuffer:!1,stencilBuffer:!1,type:t.HalfFloatType},s=Math.round(this.resolution.x/this.downSampleRatio),l=Math.round(this.resolution.y/this.downSampleRatio);this.renderTargetFog=new a(s,l,i),this.renderTargetFog.texture.generateMipmaps=!1,this.renderTargetBlur=new a(s,l,i),this.renderTargetBlur.texture.generateMipmaps=!1,this.material=new t.ShaderMaterial({defines:{PERSPECTIVE_CAMERA:1,NUM_FOG_VOLUMES:0,MAX_POINT_LIGHTS:8,MAX_SPOT_LIGHTS:2,VOLUMETRIC:1,SHADOW_FAR:100},uniforms:{tDepth:{value:null},tDiffuse:{value:null},cameraPos:{value:new o},cameraMatrix:{value:new e},invProjection:{value:new e},fogVolumes:{value:[]},stepCount:{value:30},cameraNear:{value:null},cameraFar:{value:null},pointLights:{value:new Array(8).fill(null).map(()=>({color:new o(0,0,0),distance:0,position:new o}))},activePointLightCount:{value:0},spotLights:{value:new Array(2).fill(null).map(()=>({position:new o,direction:new o,color:new o,distance:0,decay:0,coneCos:0,penumbraCos:0}))},activeSpotLightCount:{value:0},directionalLight:{value:{direction:new o(0,-1,1).normalize(),color:new o(1,1,1)}},ambientLight:{value:new o(1,1,1).multiplyScalar(1.35)},directionalShadowMatrix:{value:new e},directionalShadowMap:{value:null}},vertexShader:"\n varying vec2 vUv;\n void main() {\n vUv = uv;\n gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);\n }\n ",fragmentShader:"\n #define MAX_STEP_COUNT 64\n\n struct IncidentLight {\n vec3 color;\n vec3 direction;\n bool visible;\n };\n\n #if MAX_POINT_LIGHTS > 0\n struct PointLight {\n\t\t\tvec3 position;\n vec3 color;\n float distance;\n\t\t};\n #endif\n\n #if MAX_SPOT_LIGHTS > 0\n struct SpotLight {\n vec3 position;\n vec3 direction;\n vec3 color;\n float distance;\n float decay;\n float coneCos;\n float penumbraCos;\n };\n uniform SpotLight spotLights[ MAX_SPOT_LIGHTS ];\n uniform int activeSpotLightCount;\n float getSpotAttenuation( const in float coneCosine, const in float penumbraCosine, const in float angleCosine ) {\n return smoothstep( coneCosine, penumbraCosine, angleCosine );\n }\n #ifndef saturate\n #define saturate( a ) clamp( a, 0.0, 1.0 )\n #endif\n float pow2( const in float x ) { return x*x; }\n vec3 pow2( const in vec3 x ) { return x*x; }\n float pow4( const in float x ) { float x2 = x*x; return x2*x2; }\n float getDistanceAttenuation( const in float lightDistance, const in float cutoffDistance, const in float decayExponent ) {\n // based upon Frostbite 3 Moving to Physically-based Rendering\n // page 32, equation 26: E[window1]\n // https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf\n float distanceFalloff = 1.0 / max( pow( lightDistance, decayExponent ), 0.01 );\n if ( cutoffDistance > 0.0 ) {\n distanceFalloff *= pow2( saturate( 1.0 - pow4( lightDistance / cutoffDistance ) ) );\n }\n return distanceFalloff;\n }\n void getSpotLightInfo( const in SpotLight spotLight, const in vec3 geometryPosition, out IncidentLight light ) {\n vec3 lVector = spotLight.position - geometryPosition;\n light.direction = normalize( lVector );\n float angleCos = dot( light.direction, spotLight.direction );\n float spotAttenuation = getSpotAttenuation( spotLight.coneCos, spotLight.penumbraCos, angleCos );\n if ( spotAttenuation > 0.0 ) {\n float lightDistance = length( lVector );\n light.color = spotLight.color * spotAttenuation;\n light.color *= getDistanceAttenuation( lightDistance, spotLight.distance, spotLight.decay );\n light.visible = ( light.color != vec3( 0.0 ) );\n } else {\n light.color = vec3( 0.0 );\n light.visible = false;\n }\n }\n vec3 calcSpotLight(vec3 p, vec3 rayDir, float g, SpotLight spotLight) {\n IncidentLight directLight;\n getSpotLightInfo(spotLight, p, directLight);\n return directLight.color * 3.14;\n }\n #endif\n\n struct DirectionalLight {\n vec3 color; // pre multiplied with intensity\n vec3 direction; // should point *from the light*, normalized\n };\n\n struct FogVolume {\n vec3 sphereCenter;\n float sphereRadius;\n float baseDensity;\n float heightFalloff;\n float heightOffset;\n float scatteringDistribution;\n float startDistance;\n vec3 albedo;\n vec3 emissive; // Self-illumination color\n float ambientIntensity; // Multiplier for ambient light contribution\n float scatteringMode; // 0 = volumetric (occludes), 1 = lightShaft (god rays)\n };\n\n varying vec2 vUv;\n uniform sampler2D tDiffuse;\n uniform vec3 cameraPos;\n\n uniform float stepCount;\n uniform float nearMinDistance;\n\n uniform mat4 cameraMatrix;\n uniform mat4 invProjection;\n uniform sampler2D tDepth;\n\t\tuniform float cameraNear;\n\t\tuniform float cameraFar;\n \n uniform DirectionalLight directionalLight;\n\n uniform mat4 directionalShadowMatrix;\n uniform sampler2D directionalShadowMap;\n\n uniform vec3 ambientLight;\n\n #if NUM_FOG_VOLUMES > 0\n uniform FogVolume fogVolumes[ NUM_FOG_VOLUMES ];\n #endif\n #if MAX_POINT_LIGHTS > 0\n uniform PointLight pointLights[ MAX_POINT_LIGHTS ];\n uniform int activePointLightCount;\n #endif\n\n #include <packing>\n\n // Ray-sphere intersection\n bool intersectSphere(vec3 rayOrigin, vec3 rayDir, vec3 sphereCenter, float sphereRadius, out float t0, out float t1) {\n // Vector from ray origin to sphere center\n vec3 L = rayOrigin - sphereCenter;\n float a = dot(rayDir, rayDir); // should be 1 if rayDir is normalized\n float b = 2.0 * dot(rayDir, L);\n float c = dot(L, L) - sphereRadius * sphereRadius;\n \n float discriminant = b * b - 4.0 * a * c;\n if (discriminant < 0.0) {\n return false; // no intersection\n }\n \n float sqrtD = sqrt(discriminant);\n t0 = (-b - sqrtD) / (2.0 * a);\n t1 = (-b + sqrtD) / (2.0 * a);\n \n // Make sure t0 <= t1\n if (t0 > t1) {\n float tmp = t0;\n t0 = t1;\n t1 = tmp;\n }\n return true;\n }\n\n float getLinearDepth( const in vec2 uv ) {\n\n float fragCoordZ = texture2D( tDepth, uv ).x;\n return perspectiveDepthToViewZ( fragCoordZ, cameraNear, cameraFar );\n\n\t\t}\n\n float linearizeDepth(float depth) {\n float z = depth * 2.0 - 1.0; // back to NDC (-1..1)\n return (2.0 * cameraNear * cameraFar) / (cameraFar + cameraNear - z * (cameraFar - cameraNear));\n }\n\n\n\n float texture2DCompare( sampler2D depths, vec2 uv, float compare ) {\n return step( compare, unpackRGBAToDepth( texture2D( depths, uv ) ) );\n }\n\n vec3 calcDirectionalLight(vec3 p, vec3 rayDir, float g, DirectionalLight dirLight, out float outInShadow) {\n vec3 toLight = dirLight.direction; // direction from sample point to light\n float cosTheta = dot(toLight, -rayDir); // angle between view ray and light\n cosTheta = clamp(cosTheta, -1.0, 1.0); // keep it safe\n\n // phase function (Henyey-Greenstein or isotropic)\n float denom = 1.0 + g*g - 2.0*g*cosTheta;\n float phase = (1.0 - g*g) / (4.0 * 3.14159265 * pow(denom, 1.5));\n phase *= 3.0 * 3.14159265; // scale to total energy\n\n vec4 shadowCoord = directionalShadowMatrix * vec4(p, 1.0);\n shadowCoord.xyz /= shadowCoord.w; // perspective divide\n\n float bias = 0.0; \n outInShadow = texture2DCompare(directionalShadowMap, shadowCoord.xy, shadowCoord.z - bias);\n\n return dirLight.color * phase * outInShadow;\n }\n\n vec3 calcPointLight(vec3 p, vec3 rayDir, float g, PointLight pointLight) {\n vec3 lightPos = pointLight.position;\n vec3 lightColor = pointLight.color;\n float lightRadius = pointLight.distance;\n\n vec3 L = normalize(lightPos - p);\n float lightDist = length(lightPos - p);\n \n float attenuation = clamp(1.0 - lightDist / lightRadius, 0.0, 1.0);\n attenuation *= attenuation; // smoother falloff\n attenuation /= (1.0 + lightDist*lightDist); // inverse-square style\n\n attenuation *= 4.0 * 3.14159; // Manually scaled to match point light intensity\n // float attenuation = 1.0 / (4.0 * 3.14159 * lightDist * lightDist);\n\n\n float cosTheta = dot(L, -rayDir); // angle between light and view ray\n float phase = (1.0 - g*g) / pow(1.0 + g*g - 2.0*g*cosTheta, 1.5);\n phase *= 0.25 / 3.14159; // normalize\n\n // Intensity is part of color\n vec3 light = lightColor * attenuation * phase;\n return light;\n }\n\n float random01(vec2 p) {\n return fract(sin(dot(p, vec2(41.0, 289.0))) * 45758.5453);\n }\n\n float hash(vec3 p) { return fract(sin(dot(p, vec3(12.9898,78.233,37.719))) * 43758.5453); }\n\n // Compute perceived brightness of a color (luminosity, Rec. 709)\n float luminosity(vec3 color) {\n return dot(color, vec3(0.2126, 0.7152, 0.0722));\n }\n \n void main() {\n // reconstruct ray in world space\n vec2 ndc = vUv * 2.0 - 1.0;\n vec4 nearPoint = invProjection * vec4(ndc, -1.0, 1.0);\n nearPoint /= nearPoint.w;\n vec4 farPoint = invProjection * vec4(ndc, 1.0, 1.0);\n farPoint /= farPoint.w;\n\n vec3 rayOrigin = cameraPos;\n vec3 rayDir = normalize(farPoint.xyz - cameraPos);\n\n float sceneDepth = -getLinearDepth(vUv); // in view space\n\n // vec4 scenePosView = invProjection * vec4(ndc, sceneDepth * 2.0 - 1.0, 1.0);\n // scenePosView /= scenePosView.w;\n\n // distance from camera to surface\n // float sceneDist = length(scenePosView.xyz - cameraPosition);\n\n float transmittance = 1.0;\n vec3 inscatter = vec3(0.0);\n float random = random01(vUv);\n\n\n #if NUM_FOG_VOLUMES > 0\n for (int v = 0; v < NUM_FOG_VOLUMES; v++) {\n FogVolume volume = fogVolumes[v];\n vec3 sphereCenter = volume.sphereCenter;\n float sphereRadius = volume.sphereRadius;\n float heightFalloff = volume.heightFalloff;\n float heightOffset = volume.heightOffset;\n float baseDensity = volume.baseDensity;\n float startDistance = volume.startDistance;\n float g = volume.scatteringDistribution;\n bool isLightShaft = volume.scatteringMode > 0.5;\n\n float t0, t1;\n if (!intersectSphere(rayOrigin, rayDir, sphereCenter, sphereRadius, t0, t1) || t1 <= 0.0) {\n continue;\n }\n // if camera inside sphere\n t0 = max(t0, 0.0);\n\n // An alternative method to get stepSize but is less dynamic\n // float stepSize = (t1 - t0) / float(MAX_STEP_COUNT);\n\n float maxStepSize = 0.3; // adjust for visual quality\n float distance = t1 - t0;\n int stepCount = int(ceil(distance / maxStepSize));\n stepCount = min(stepCount, MAX_STEP_COUNT); // optional clamp for performance\n float stepSize = distance / float(stepCount);\n \n // Add a slight random offset to reduce banding \n float jitterAmount = 1.0;\n float rayStartOffset = random * stepSize * jitterAmount;\n t0 += rayStartOffset;\n\n for (int i = 0; i < stepCount; i++) {\n\n float t = t0 + float(i) * stepSize;\n vec3 p = rayOrigin + rayDir * t;\n\n float geomDelta = sceneDepth - t; // how far fog sample is from hitting geometry\n float fade = clamp(geomDelta, 0.0, 1.0); \n // float fade = clamp((sceneDepth - t) / 0.1, 0.0, 1.0);\n\n float closeFade = smoothstep(0.0, 0.3, (t - startDistance) / t);\n float edgeFade = clamp(sphereRadius - length(p - sphereCenter), 0.0, 1.0);\n\n // density with exponential height falloff\n float h = p.y - (sphereCenter.y + heightOffset);\n float heightFactor = clamp(exp(-h * heightFalloff), 0.0, 1.0);\n float density = baseDensity * heightFactor * fade * closeFade * edgeFade;\n\n vec3 light = vec3(0.0);\n float inDirectLight = 1.0; // 1.0 = in light, 0.0 = in shadow\n \n #if VOLUMETRIC == 1\n // Skip if we're too far away for directional light shadows\n // Or the entire volume will show instead of just the shafts\n if (isLightShaft && t > float(SHADOW_FAR)) {\n continue;\n }\n // Get directional light first - this also gives us the shadow value for god rays\n light += calcDirectionalLight(p, rayDir, g, directionalLight, inDirectLight);\n \n // For god rays mode, skip everything else if we're in shadow\n // For atmospheric mode, always include all lights\n if (!isLightShaft || inDirectLight > 0.5) {\n #if MAX_POINT_LIGHTS > 0\n for (int pi = 0; pi < activePointLightCount; pi++) {\n light += calcPointLight(p, rayDir, g, pointLights[pi]);\n } \n #endif\n\n #if MAX_SPOT_LIGHTS > 0\n for (int pi = 0; pi < activeSpotLightCount; pi++) {\n light += calcSpotLight(p, rayDir, g, spotLights[pi]);\n } \n #endif\n\n // Ambient and emissive only for atmospheric mode, not god rays\n if (!isLightShaft) {\n light += ambientLight * volume.ambientIntensity;\n light += volume.emissive;\n }\n }\n\n #else\n light = vec3(1.0);\n #endif\n\n light *= volume.albedo;\n \n // In god rays mode, only accumulate when in direct light\n // In atmospheric mode, always accumulate\n if (!isLightShaft || inDirectLight > 0.5) {\n transmittance *= exp(-density * stepSize);\n inscatter += transmittance * light * density * stepSize;\n }\n }\n }\n #endif\n\n float alpha = 1.0 - transmittance;\n\n gl_FragColor = vec4(inscatter, alpha);\n }\n "}),this.material.onBeforeCompile=t=>{},this.materialBlur=new t.ShaderMaterial({uniforms:{tInput:{value:null},tDepth:{value:null},texelSize:{value:new t.Vector2(1,1).divideScalar(1e3)},direction:{value:new t.Vector2(1,0)},depthSigma:{value:2},blurSigma:{value:2}},vertexShader:"\n varying vec2 vUv;\n void main() {\n vUv = uv;\n gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);\n }\n ",fragmentShader:"\n uniform sampler2D tInput;\n uniform sampler2D tDepth;\n uniform vec2 texelSize;\n uniform vec2 direction;\n uniform float depthSigma;\n uniform float blurSigma;\n varying vec2 vUv;\n\n #include <packing>\n\n void main() {\n // float centerDepth = texture2D(tDepth, vUv).r;\n\n float centerDepth = unpackRGBAToDepth( texture2D( tDepth, vUv ) );\n vec4 center = texture2D(tInput, vUv);\n vec4 centerColor = center;\n\n vec4 sum = vec4(0.0);\n float wsum = 0.0;\n\n // int radius = int(ceil(2.0 * blurSigma));\n\n for (int i = -5; i <= 5; i++) {\n // if (i > radius || i < -radius) continue;\n\n vec2 offset = direction * float(i) * texelSize; // this should be based on resolution\n vec2 uv = vUv + offset;\n\n vec4 sampleColor = texture2D(tInput, uv);\n float sampleDepth = unpackRGBAToDepth( texture2D( tDepth, uv ) );\n\n float spatialWeight = exp(-0.5 * (float(i) * float(i)) / (blurSigma * blurSigma));\n float depthDiff = abs(sampleDepth - centerDepth) * 100.0;\n float depthWeight = exp(-0.5 * (depthDiff * depthDiff) / (depthSigma * depthSigma));\n\n float w = spatialWeight * depthWeight;\n\n sum += sampleColor * w;\n wsum += w;\n }\n\n gl_FragColor = sum / wsum;\n }\n ",transparent:!0}),this.materialComposite=new t.ShaderMaterial({uniforms:{tDiffuse:{value:null},tFog:{value:null}},vertexShader:"\n varying vec2 vUv;\n void main() {\n vUv = uv;\n gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);\n }\n ",fragmentShader:"\n uniform sampler2D tDiffuse;\n uniform sampler2D tFog;\n varying vec2 vUv;\n void main() {\n vec4 sceneColor = texture2D(tDiffuse, vUv);\n vec4 fogColor = texture2D(tFog, vUv);\n // Composite: fogColor.a is the fog alpha, blend over scene\n // vec3 color = mix(sceneColor.rgb, fogColor.rgb, fogColor.a);\n // Not sure why multipling alpha like this helps but it is necessary to avoid a dark\n // borders around the fog\n gl_FragColor = vec4(fogColor.rgb + (1.0 - fogColor.a * fogColor.a) * sceneColor.rgb, 1.0);\n }\n ",transparent:!0})}render(t,e,n){t.getClearColor(this._oldClearColor),this.oldClearAlpha=t.getClearAlpha();const i=t.autoClear;t.autoClear=!1,t.setClearColor(16777215,0),this.fsQuad.material=this.material,t.setRenderTarget(this.renderTargetFog),t.clear(),this.fsQuad.render(t),this.materialBlur.uniforms.tInput.value=this.renderTargetFog.texture,this.materialBlur.uniforms.direction.value=u,this.fsQuad.material=this.materialBlur,t.setRenderTarget(this.renderTargetBlur),t.clear(),this.fsQuad.render(t),this.materialBlur.uniforms.tInput.value=this.renderTargetBlur.texture,this.materialBlur.uniforms.direction.value=f,t.setRenderTarget(this.renderTargetFog),t.clear(),this.fsQuad.render(t),this.materialComposite.uniforms.tDiffuse.value=n.texture,this.materialComposite.uniforms.tFog.value=this.renderTargetFog.texture,this.fsQuad.material=this.materialComposite,this.renderToScreen?(t.setRenderTarget(null),this.fsQuad.render(t)):(t.setRenderTarget(e),this.clear&&t.clear(),this.fsQuad.render(t)),t.setClearColor(this._oldClearColor,this.oldClearAlpha),t.autoClear=i}setSize(t,e){let n=Math.round(t/this.downSampleRatio),i=Math.round(e/this.downSampleRatio);this.renderTargetFog.setSize(n,i),this.renderTargetBlur.setSize(n,i),this.materialBlur.uniforms.texelSize.value.set(1/n,1/i)}set volumetric(t){this.material.defines.VOLUMETRIC=t?1:0}get volumetric(){return!!this.material.defines.VOLUMETRIC}getVolumeUniforms(t){let e=this.volumesCache.get(t);return null==e&&(e={albedo:new o,baseDensity:t.baseDensity,heightFalloff:t.heightFalloff,heightOffset:t.heightOffset,startDistance:t.startDistance??3,scatteringDistribution:t.scatteringDistribution,sphereRadius:1,sphereCenter:new o,emissive:new o,ambientIntensity:t.ambientIntensity??1,scatteringMode:"lightShaft"===t.scatteringMode?1:0},this.volumesCache.set(t,e)),t.object.getWorldPosition(e.sphereCenter),t.object.getWorldScale(h),e.sphereRadius=5*h.x,e.albedo.set(t.albedo.r,t.albedo.g,t.albedo.b),e.baseDensity=t.baseDensity,e.heightFalloff=t.heightFalloff,e.heightOffset=t.heightOffset,e.scatteringDistribution=t.scatteringDistribution,e.startDistance=t.startDistance,e.emissive.set(t.emissive.r,t.emissive.g,t.emissive.b),e.ambientIntensity=t.ambientIntensity??1,e.scatteringMode="lightShaft"===t.scatteringMode?1:0,e}getPointLightUniforms(t){const e={position:new o,color:new o,distance:0};return t.getWorldPosition(e.position),e.color.set(t.color.r,t.color.g,t.color.b).multiplyScalar(t.intensity*(t.userData?.volumetricIntensity??1)),e.distance=void 0!==t.distance&&t.distance>0?t.distance:1,e}getSpotLightUniforms(t){const e={position:new o,direction:new o,color:new o,distance:0,coneCos:0,penumbraCos:0,decay:1};return t.getWorldPosition(e.position),e.direction.setFromMatrixPosition(t.matrixWorld),h.setFromMatrixPosition(t.target.matrixWorld),e.direction.sub(h),e.color.set(t.color.r,t.color.g,t.color.b).multiplyScalar(t.intensity*(t.userData?.volumetricIntensity??1)),e.distance=void 0!==t.distance&&t.distance>0?t.distance:0,e.coneCos=Math.cos(t.angle),e.penumbraCos=Math.cos(t.angle*(1-t.penumbra)),e.decay=void 0!==t.decay?t.decay:1,e}updateArrayUniforms(e){const n=this.material.uniforms.fogVolumes.value;let o=0;const a=this.material.uniforms.pointLights.value;let r=0;const s=this.material.uniforms.spotLights.value;let l=0;e.traverseVisible(e=>{if(e instanceof c){const t=e.volume,i=this.getVolumeUniforms(t);n[o]=i,o++}else if(e instanceof i){if(r<a.length){if(0===e.userData.volumetricIntensity)return;const t=this.getPointLightUniforms(e);a[r].position.copy(t.position),a[r].color.copy(t.color),a[r].distance=t.distance,r++}}else if(e instanceof t.SpotLight&&l<s.length){if(0===e.userData.volumetricIntensity)return;const t=this.getSpotLightUniforms(e);s[l].position.copy(t.position),s[l].direction.copy(t.direction),s[l].color.copy(t.color),s[l].distance=t.distance,s[l].coneCos=t.coneCos,s[l].penumbraCos=t.penumbraCos,s[l].decay=t.decay,l++}}),this.material.uniforms.activePointLightCount.value=r,this.material.uniforms.activeSpotLightCount.value=l,this.material.defines.NUM_FOG_VOLUMES!==o&&(this.material.needsUpdate=!0,this.material.uniformsNeedUpdate=!0),this.material.defines.NUM_FOG_VOLUMES=o}get uniforms(){return this.material.uniforms}update(t,e,i,a){if(this.updateArrayUniforms(a),this.enabled=0!=this.material.defines.NUM_FOG_VOLUMES,!this.enabled)return;null==this.uniforms.cameraPosition&&(this.uniforms.cameraPos={value:new o}),this.uniforms.cameraPos.value.copy(t.position),this._viewProjection.multiplyMatrices(t.projectionMatrix,t.matrixWorldInverse),this.uniforms.invProjection.value.copy(this._invViewProjection.copy(this._viewProjection).invert()),this.uniforms.tDepth.value=e.depthTexture,this.materialBlur.uniforms.tDepth.value=e.depthTexture,t instanceof n&&(this.uniforms.cameraNear.value=t.near,this.uniforms.cameraFar.value=t.far),this.uniforms.directionalLight.value.direction=i.lightDirection,this.uniforms.directionalLight.value.color.set(1,1,1).multiplyScalar(i.lightIntensity);const r=i.lights[i.lights.length-1];r&&r.shadow&&r.castShadow&&r.visible&&(this.uniforms.directionalShadowMap.value=r.shadow.map?.texture,this.uniforms.directionalShadowMatrix.value=r.shadow.matrix),r&&!r.visible?this.uniforms.directionalLight.value.color.set(0,0,0):this.uniforms.directionalLight.value.color.set(r.color.r,r.color.g,r.color.b).multiplyScalar(r.intensity*(r.userData?.volumetricIntensity??1));const s=a.children.find(t=>t.name===l);null!=s&&s.visible?this.uniforms.ambientLight.value.set(s.color.r,s.color.g,s.color.b).multiplyScalar(s.intensity*(s.userData?.volumetricIntensity??1)):this.uniforms.ambientLight.value.set(0,0,0)}}const u=new t.Vector2(1,0),f=new t.Vector2(0,1);/*
|
|
2
2
|
* Copyright (©) 2026 Hology Interactive AB. All rights reserved.
|
|
3
3
|
* See the LICENSE.md file for details.
|
|
4
4
|
*/
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { BaseActor } from "../../gameplay/actors/actor.js";
|
|
2
|
+
import { Vector3 } from "three";
|
|
3
|
+
export declare class LightProbeVolume extends BaseActor {
|
|
4
|
+
gridResolution: Vector3;
|
|
5
|
+
gridSize: Vector3;
|
|
6
|
+
private world;
|
|
7
|
+
private view;
|
|
8
|
+
private removed;
|
|
9
|
+
private lightVolume;
|
|
10
|
+
onInit(): void;
|
|
11
|
+
bake(): Promise<void>;
|
|
12
|
+
onEndPlay(): void;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=light-probe-volume-actor.d.ts.map
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import{__decorate as e,__metadata as i}from"tslib";import{inject as t}from"../../gameplay/inject.js";import{Actor as o,BaseActor as r}from"../../gameplay/actors/actor.js";import{Vector3 as s}from"three";import{captureSHGrid as l}from"./light-volume-capture.js";import{Parameter as n,ViewController as m,World as h}from"../../gameplay/index.js";import{sleepDelay as d}from"../../utils/async.js";let g=class extends r{constructor(){super(...arguments),this.gridResolution=new s(8,3,8),this.gridSize=new s(30,2,30),this.world=t(h),this.view=t(m),this.removed=!1}onInit(){this.bake()}async bake(){if(await d(5e3),this.removed)return;const e=window.renderer;if(null==e)return void console.error("No renderer found for light volume capture");const i=this.position;i.y+=this.gridSize.y/2+.5,console.time("capture light volume");const t=await l({gridOrigin:i,gridResolution:this.gridResolution,gridSize:this.gridSize,renderer:e,scene:this.world.scene});console.timeEnd("capture light volume"),this.removed||(this.view.setLightVolume(t),this.lightVolume=t)}onEndPlay(){this.removed=!0,this.view.getLightVolume()===this.lightVolume&&(this.lightVolume?.shTexture.dispose(),this.view.setLightVolume(null))}};e([n(),i("design:type",Object)],g.prototype,"gridResolution",void 0),e([n(),i("design:type",Object)],g.prototype,"gridSize",void 0),g=e([o()],g);export{g as LightProbeVolume};/*
|
|
2
|
+
* Copyright (©) 2026 Hology Interactive AB. All rights reserved.
|
|
3
|
+
* See the LICENSE.md file for details.
|
|
4
|
+
*/
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Data3DTexture, Object3D, Vector3, WebGLRenderer } from "three";
|
|
2
|
+
export declare class LightVolume {
|
|
3
|
+
shTexture: Data3DTexture;
|
|
4
|
+
gridResolution: Vector3;
|
|
5
|
+
gridSize: Vector3;
|
|
6
|
+
gridOrigin: Vector3;
|
|
7
|
+
constructor(shTexture: Data3DTexture, gridResolution: Vector3, gridSize: Vector3, gridOrigin: Vector3);
|
|
8
|
+
}
|
|
9
|
+
export declare function captureSHGrid({ gridResolution, gridSize, gridOrigin, renderer, scene, }: {
|
|
10
|
+
gridResolution: Vector3;
|
|
11
|
+
gridSize: Vector3;
|
|
12
|
+
gridOrigin: Vector3;
|
|
13
|
+
renderer: WebGLRenderer;
|
|
14
|
+
scene: Object3D;
|
|
15
|
+
}): Promise<LightVolume>;
|
|
16
|
+
//# sourceMappingURL=light-volume-capture.d.ts.map
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import{LightProbeGenerator as e,LightProbeHelper as t}from"three/examples/jsm/Addons.js";import{CubeCamera as r,Data3DTexture as o,DataUtils as n,HalfFloatType as i,LinearFilter as s,RGBAFormat as a,Vector3 as c,WebGLCubeRenderTarget as l}from"three";export class LightVolume{constructor(e,t,r,o){this.shTexture=e,this.gridResolution=t,this.gridSize=r,this.gridOrigin=o}}export async function captureSHGrid({gridResolution:g,gridSize:d,gridOrigin:p,renderer:m,scene:f}){const h=g.x*g.y*g.z,u=Array.from({length:9},()=>new Float32Array(4*h)),y=Array.from({length:8},()=>{const e=new l(8);return{target:e,camera:new r(.2,100,e)}}),x=[];let w=0;for(let e=0;e<g.z;e++)for(let t=0;t<g.y;t++)for(let r=0;r<g.x;r++){const o=new c(p.x+r/(g.x-1)*d.x,p.y+t/(g.y-1)*d.y,p.z+e/(g.z-1)*d.z);x.push({position:o,index:w}),w++}const z=y.map(async(r,o)=>{for(let n=o;n<x.length;n+=8){const{position:o,index:i}=x[n];r.camera.position.copy(o),r.camera.update(m,f);const s=await e.fromCubeRenderTarget(m,r.target),a=s.sh;for(let e=0;e<9;e++){const t=a.coefficients[e];u[e][4*i+0]=t.x,u[e][4*i+1]=t.y,u[e][4*i+2]=t.z,u[e][4*i+3]=1}s.position.copy(o.clone());const c=new t(s,.05);f.add(c)}});await Promise.all(z),y.forEach(e=>e.target.dispose());const A=new Uint16Array(9*h*4);for(let e=0;e<9;e++){const t=u[e],r=e*h*4;for(let e=0;e<t.length;e++)A[r+e]=n.toHalfFloat(t[e])}const F=new o(A,g.x,g.y,9*g.z);return F.format=a,F.type=i,F.minFilter=s,F.magFilter=s,F.unpackAlignment=1,F.needsUpdate=!0,console.log("Captured SH grid into a single packed texture:",F),new LightVolume(F,g,d,p)}/*
|
|
2
|
+
* Copyright (©) 2026 Hology Interactive AB. All rights reserved.
|
|
3
|
+
* See the LICENSE.md file for details.
|
|
4
|
+
*/
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{BackSide as e,Color as t,ShaderMaterial as i,UniformsLib as n,UniformsUtils as a,Object3D as
|
|
1
|
+
import{BackSide as e,Color as t,ShaderMaterial as i,UniformsLib as n,UniformsUtils as a,Object3D as r,Mesh as l,InstancedMesh as o,BatchedMesh as s,Vector3 as u}from"three";import{glslFunction as c,NodeShaderMaterial as p,Vec4Node as d,uniforms as m,transformed as v,neg as f,uniformFloat as h,uniformVec3 as _,vec3 as g,rgba as x,vec4 as k,negVec3 as A,ifDefApply as y,BooleanExpression as w,select as D,attributes as b}from"three-shader-graph";export class OutlineEffect{constructor(i,n={}){this.applied=new WeakSet,this.enabled=!0;const a=void 0!==n.defaultThickness?n.defaultThickness:.003,r=((new t).fromArray(void 0!==n.defaultColor?n.defaultColor:[0,0,0]),void 0!==n.defaultAlpha?n.defaultAlpha:1),l=void 0!==n.defaultKeepAlive&&n.defaultKeepAlive,o={},s={};["#include <common>","#include <uv_pars_vertex>","#include <displacementmap_pars_vertex>","#include <fog_pars_vertex>","#include <morphtarget_pars_vertex>","#include <skinning_pars_vertex>","#include <logdepthbuf_pars_vertex>","#include <clipping_planes_pars_vertex>","uniform float outlineThickness;","vec4 calculateOutline( vec4 pos, vec3 normal, vec4 skinned ) {","\tfloat thickness = outlineThickness;","\tconst float ratio = 1.0;","\tvec4 pos2 = projectionMatrix * modelViewMatrix * vec4( skinned.xyz + normal, 1.0 );"," float viewDistance = length((modelViewMatrix * vec4(skinned.xyz, 1.0)).xyz);"," float scale = pow(viewDistance, 0.5); // sqrt, or any exponent","\tvec4 norm = normalize( pos - pos2 );","\treturn pos + norm * thickness * scale * ratio;","}","void main() {","\t#include <uv_vertex>","\t#include <beginnormal_vertex>","\t#include <morphnormal_vertex>","\t#include <skinbase_vertex>","\t#include <skinnormal_vertex>","\t#include <begin_vertex>","\t#include <morphtarget_vertex>","\t#include <skinning_vertex>","\t#include <displacementmap_vertex>","\t#include <project_vertex>","\tvec3 outlineNormal = - objectNormal;","\tgl_Position = calculateOutline( gl_Position, outlineNormal, vec4( transformed, 1.0 ) );","\t#include <logdepthbuf_vertex>","\t#include <clipping_planes_vertex>","\t#include <fog_vertex>","}"].join("\n"),["#include <common>","#include <fog_pars_fragment>","#include <logdepthbuf_pars_fragment>","#include <clipping_planes_pars_fragment>","uniform vec3 outlineColor;","uniform float outlineAlpha;","void main() {","\t#include <clipping_planes_fragment>","\t#include <logdepthbuf_fragment>","\tgl_FragColor = vec4( outlineColor, outlineAlpha );","\t#include <tonemapping_fragment>","\t#include <colorspace_fragment>","\t#include <fog_fragment>","\t#include <premultiplied_alpha_fragment>","}"].join("\n");function f(t){Array.isArray(t)&&(t=t[0]);const i=m.projectionMatrix.multiply(m.modelViewMatrix).multiplyVec(v.position);let n=v.normal;n=y("DOUBLE_SIDED",n,e=>D(new w("gl_FrontFacing"),e,e.multiplyScalar(-1)));const l=h("outlineThickness",a),o=_("outlineColor",new u(0,0,0)),s=h("outlineAlpha",r),f=c(d,{pos:i,normal:n,skinned:v.position,outlineThickness:l},"\n\t\t\t\tfloat thickness = outlineThickness;\n\t\t\t\tvec4 viewPos = modelViewMatrix * vec4(skinned.xyz, 1.0);\n\t\t\t\tfloat viewDistance = length(viewPos.xyz);\n\t\t\t\t// Clamp to safe domain\n\t\t\t\tviewDistance = max(viewDistance, 0.0001);\n\t\t\t\tfloat scale = sqrt(viewDistance) * 0.5;\n\t\t\t\tvec3 displaced = viewPos.xyz + normal * thickness * scale;\n\t\t\t\treturn projectionMatrix * vec4(displaced, 1.0);\n\t\t\t"),g=x(o,s),A=[g,k(0),k(0)],b="number"==typeof t.userData.mrtOutputs&&t.userData.mrtOutputs>1,M=new p({position:f,outputs:b?A:void 0,color:b?void 0:g});return M.side=e,M.depthTest=!0,M.depthWrite=!0,M.transparent=!0,M}this.createMaterial=f,this.getOutlineMaterial=function(e){const t=function(e){let t=o[e.uuid];return void 0===t&&(t={material:f(e),used:!0,keepAlive:l,count:0},o[e.uuid]=t),t.used=!0,t.material}(e);return s[t.uuid]=e,function(e,t){Array.isArray(t)&&(t=t[0]);if("invisible"===e.name)return;const i=t.userData.outlineParameters;e.fog=t.fog,e.toneMapped=t.toneMapped,e.premultipliedAlpha=t.premultipliedAlpha,e.displacementMap=t.displacementMap,void 0!==i?(!1===t.visible?e.visible=!1:e.visible=void 0===i.visible||i.visible,e.transparent=void 0!==i.alpha&&i.alpha<1||t.transparent,void 0!==i.keepAlive&&(o[t.uuid].keepAlive=i.keepAlive)):(e.transparent=t.transparent,e.visible=t.visible);!0!==t.wireframe&&!1!==t.depthTest||(e.visible=!1);t.clippingPlanes&&(e.clipping=!0,e.clippingPlanes=t.clippingPlanes,e.clipIntersection=t.clipIntersection,e.clipShadows=t.clipShadows);e.version=t.version}(t,e),t},this.updateUniforms=function(e,t){const i=t.userData.outlineParameters;e.uniforms.outlineAlpha.value=t.opacity,void 0!==i&&(void 0!==i.thickness&&(e.uniforms.outlineThickness.value=i.thickness),void 0!==i.color&&e.uniforms.outlineColor.value.fromArray(i.color),void 0!==i.alpha&&(e.uniforms.outlineAlpha.value=i.alpha)),t.displacementMap&&(e.uniforms.displacementMap.value=t.displacementMap,e.uniforms.displacementScale.value=t.displacementScale,e.uniforms.displacementBias.value=t.displacementBias)}}apply(e){if(!(e instanceof l)||e instanceof s||e instanceof o)return;if(this.applied.has(e))return;if(!function(e){if(Array.isArray(e.material))return e.material.some(e=>e&&e.userData&&e.userData.outlineParameters);if(e.material&&e.material.userData&&e.material.userData.outlineParameters)return!0;return!1}(e))return;this.applied.add(e);const t=e.onAfterRender;e.onAfterRender=(i,n,a,r,l,o)=>{t(i,n,a,r,l);let s=!1;if(Array.isArray(l)?s=l.some(e=>e&&e.userData&&e.userData.outlineParameters):l&&l.userData&&l.userData.outlineParameters&&(s=!0),!s)return;const u=e.material,c=this.getOutlineMaterial(u);e.material=c,this.updateUniforms(c,l),i.renderBufferDirect(a,n,r,c,e,o),e.material=u}}}/*
|
|
2
2
|
* Copyright (©) 2026 Hology Interactive AB. All rights reserved.
|
|
3
3
|
* See the LICENSE.md file for details.
|
|
4
4
|
*/
|
package/dist/rendering.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { CSM } from "three/examples/jsm/csm/CSM.js";
|
|
|
6
6
|
import { GTAOPass } from "three/examples/jsm/postprocessing/GTAOPass.js";
|
|
7
7
|
import { OutlinePass } from './utils/three/outline-pass.js';
|
|
8
8
|
import { type PostProcessSettings, type PostProcessVolume } from "./gameplay/actors/builtin/post-process-volume-actor.js";
|
|
9
|
+
import { LightVolume } from "./rendering/light-probes/light-volume-capture.js";
|
|
9
10
|
export type RenderingViewOptions = {
|
|
10
11
|
enableOutlines?: boolean;
|
|
11
12
|
enableXR?: boolean;
|
|
@@ -64,6 +65,8 @@ export declare class RenderingView {
|
|
|
64
65
|
private volumetricFogPass;
|
|
65
66
|
private fquadCopy;
|
|
66
67
|
private fquadCopyOpaque;
|
|
68
|
+
lightVolume: LightVolume;
|
|
69
|
+
lightProbeIntensity: number;
|
|
67
70
|
/**
|
|
68
71
|
* This should be used to multiply the AO with the opaque color.
|
|
69
72
|
*
|
|
@@ -99,7 +102,7 @@ export declare class RenderingView {
|
|
|
99
102
|
private createSceneColorRenderTarget;
|
|
100
103
|
private createGRenderTarget;
|
|
101
104
|
private setupEventListeners;
|
|
102
|
-
stop(): void;
|
|
105
|
+
stop(dispose?: boolean): void;
|
|
103
106
|
private onLoopCallbacks;
|
|
104
107
|
onLoop(onFrame: (deltaTime: number) => any): void;
|
|
105
108
|
removeOnLoop(onFrame: (deltaTime: number) => any): void;
|
|
@@ -111,11 +114,19 @@ export declare class RenderingView {
|
|
|
111
114
|
private setupCsm;
|
|
112
115
|
private gbufferMaterialCache;
|
|
113
116
|
private tbufferMaterialCache;
|
|
117
|
+
/**
|
|
118
|
+
* If the material such as a standard material has updated properties, then we need to update
|
|
119
|
+
* the corresponding g buffer material
|
|
120
|
+
* @param source
|
|
121
|
+
* @param target
|
|
122
|
+
*/
|
|
123
|
+
private updateMaterialProperties;
|
|
114
124
|
/**
|
|
115
125
|
* Updates uniform values from source material to target material.
|
|
116
126
|
* Optimized for hot path execution in rendering loop.
|
|
117
127
|
*/
|
|
118
128
|
private updateUniformValues;
|
|
129
|
+
private updateMaterialCommonProperties;
|
|
119
130
|
/**
|
|
120
131
|
* After rendering the opaque scene, we will also calculate lights which updates uniforms.
|
|
121
132
|
* These need to be present on the gbuffer materials so we need to copy them afterward.
|
|
@@ -162,6 +173,7 @@ export declare class RenderingView {
|
|
|
162
173
|
private initDepthUniform;
|
|
163
174
|
private initNormalUniform;
|
|
164
175
|
private initShadowUniform;
|
|
176
|
+
private initLightVolumeUniform;
|
|
165
177
|
private initResolutionUniform;
|
|
166
178
|
private initAoUniform;
|
|
167
179
|
private initCustomDepthMaterial;
|