@ev-ry/fx 0.1.0-rc.2 → 0.1.0-rc.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,160 +1,164 @@
1
- import {imageMotionPixels} from './motion-envelope.js';
2
- import {loadImageRaster} from './image-source.js';
3
- import {rasterToTextureMesh} from './raster-texture-mesh.js';
4
- import {attachRasterTexture,applyRasterSamplingShader} from './raster-texture-material.js';
5
- import {textDivisionsForSize,createProjectedTextSize} from './text-mesh-density.js';
6
- import {TriangleEffect} from './triangle-effect.js';
7
- import {normalizeTextEffectOptions} from './text-effect-options.js';
8
- import {textEditMotions} from './text-edit-motions.js';
9
-
10
- export const IMAGE_EFFECT_MODES=Object.freeze(Object.keys(textEditMotions));
11
- // Images share motion recipes with text, but preserve source color by default.
12
- export function normalizeImageEffectOptions(settings={},mode='dust-wind'){
13
- const normalized=normalizeTextEffectOptions({formation:-1,...settings},mode);
14
- return Object.freeze({...normalized,recipe:Object.freeze({...normalized.recipe,glow:settings.recipe?.glow??0})});
15
- }
16
-
17
- // Images use twice the text-derived rows: half-size cells, with a bounded grid.
18
- export function imageDivisions(width,height,displayHeight,current=null,maxTriangles=80000){
19
- let rows=2*textDivisionsForSize(displayHeight,current===null?null:current/2);
20
- while(rows>1&&2*rows*Math.max(1,Math.round(width*rows/height))>maxTriangles)rows--;
21
- if(2*rows*Math.max(1,Math.round(width*rows/height))>maxTriangles)throw Error('Image aspect ratio exceeds the triangle budget');
22
- return rows;
23
- }
24
-
25
- const vertexShader=`
26
- attribute vec2 rasterUV;
27
- attribute vec2 glyphOffset;
28
- varying vec2 vRasterUV;
29
- void main(){
30
- vRasterUV=rasterUV;
31
- vec3 p=position+vec3(glyphOffset,0.0);
32
- /* THD_TRIANGLE_MOTION */
33
- gl_Position=projectionMatrix*modelViewMatrix*vec4(p,1.0);
34
- }`;
35
- const fragmentShader=`
36
- uniform sampler2D rasterMap;
37
- varying vec2 vRasterUV;
38
- void main(){
39
- vec4 rasterSample=texture2D(rasterMap,vRasterUV);
40
- float coverage=rasterSample.a;
41
- gl_FragColor=vec4(rasterSample.rgb, coverage);
42
- #include <colorspace_fragment>
43
- }`;
44
-
45
- // One image object in an existing runtime. No renderer, input or font ownership.
46
- export function createImageSurface(runtime,{source,width=6,height=3.8,effect='dust-wind',settings={},direction='ltr'}={}){
47
- if(runtime.disposed)throw Error('Spatial runtime disposed');
48
- if(!Number.isFinite(width)||!Number.isFinite(height)||width<=0||height<=0)throw RangeError('Positive image bounds required');
49
- const validMode=mode=>{if(!IMAGE_EFFECT_MODES.includes(mode))throw TypeError('Unsupported image effect');};
50
- const validDirection=value=>{if(!['ltr','rtl'].includes(value))throw TypeError('Invalid direction');};
51
- validMode(effect);validDirection(direction);
52
- const {THREE}=runtime,document=runtime.renderer.domElement.ownerDocument,object=new THREE.Group();
53
- const motion=new TriangleEffect(THREE,{mode:effect,settings:normalizeImageEffectOptions(settings,effect)}),measure=createProjectedTextSize(THREE);
54
- let requestedSettings=settings,entryMode=effect,entrySettings=motion.settings;
55
- let disposed=false,asset=null,controller=null,revision=0,loading=false,error=null,state='empty',intent=null;
56
- let displaySize=null;
57
- let lastSeed=null,builds=0,loadMs=0,buildMs=0;
58
- const view={sharedGroups:new Map(),version:0};
59
- function releaseAsset(){
60
- motion.cancel();motion.uniforms.dustMaskOnly.value=0;motion.materials.clear();view.sharedGroups.clear();
61
- if(asset){object.remove(asset.mesh);asset.mesh.geometry.dispose();asset.mesh.material.dispose();asset.releaseTexture();asset=null;}
62
- }
63
- function geometry(raster,rows,w,h,filterRadius,aspect){
64
- const mesh=rasterToTextureMesh(raster.rgba,raster.width,raster.height,rows,{filterRadius,aspect}),positions=new Float32Array(mesh.triangleCount*9);
65
- for(let i=0;i<mesh.coordinates.length;i+=2){const at=i/2*3;positions[at]=(mesh.coordinates[i]/raster.width-.5)*w;positions[at+1]=(.5-mesh.coordinates[i+1]/raster.height)*h;}
66
- const result=new THREE.BufferGeometry();result.setAttribute('position',new THREE.BufferAttribute(positions,3));
67
- result.setAttribute('rasterUV',new THREE.BufferAttribute(mesh.uvs,2));result.computeBoundingBox();
68
- return result;
69
- }
70
- function meshSettings(raster,h,current=null){
71
- const displayHeight=displaySize?.height??measure(object,runtime.camera,runtime.renderer.domElement,h);
72
- const aspect=displaySize?displaySize.width/displaySize.height:raster.width/raster.height;
73
- // Mipmap/bilinear filtering sees beyond the original opaque texel. Retain
74
- // that support too, especially when a wide image is displayed very small.
75
- const ratio=raster.height/Math.max(1,displayHeight*(runtime.renderer.getPixelRatio?.()||1));
76
- const filterRadius=displayHeight>0?Math.max(.5,2**Math.ceil(Math.log2(Math.max(1,ratio)))):.5;
77
- return {rows:imageDivisions(aspect,1,displayHeight,current),filterRadius,aspect};
78
- }
79
- function resizeMesh(){
80
- if(!asset||motion.active)return;
81
- const next=meshSettings(asset.raster,asset.h,asset.rows);if(next.rows===asset.rows&&next.filterRadius===asset.filterRadius&&next.aspect===asset.aspect)return;
82
- const start=performance.now(),replacement=geometry(asset.raster,next.rows,asset.w,asset.h,next.filterRadius,next.aspect);
83
- asset.mesh.geometry.dispose();asset.mesh.geometry=replacement;asset.rows=next.rows;asset.aspect=next.aspect;asset.filterRadius=next.filterRadius;view.version++;builds++;buildMs=performance.now()-start;
84
- motion.decorate(view);
85
- }
86
- async function setSource(value,{preserveMotion=false,bounds=null}={}){
87
- if(disposed)throw Error('Image surface disposed');
88
- const token=++revision;controller?.abort();controller=new AbortController();loading=true;error=null;
89
- try{
90
- const raster=await loadImageRaster(value,{document,signal:controller.signal,maxSide:Math.min(2048,runtime.renderer.capabilities?.maxTextureSize||2048)});
91
- if(disposed||token!==revision)return false;
92
- if(bounds){width=bounds.width;height=bounds.height;}
93
- const start=performance.now(),factor=Math.min(width/raster.width,height/raster.height),w=raster.width*factor,h=raster.height*factor;
94
- const {rows,filterRadius,aspect}=meshSettings(raster,h),g=geometry(raster,rows,w,h,filterRadius,aspect);
95
- const material=new THREE.ShaderMaterial({vertexShader,fragmentShader,side:THREE.DoubleSide,transparent:true,depthWrite:false,toneMapped:false,extensions:{derivatives:true}});
96
- applyRasterSamplingShader(material);
97
- material.defaultAttributeValues.glyphOffset=[0,0];
98
- const releaseTexture=attachRasterTexture(THREE,material,raster),mesh=new THREE.Mesh(g,material);mesh.frustumCulled=false;
99
- // Replacement of DOM pixels/size must not restart an existing effect or
100
- // reveal an image that has already completed its exit.
101
- const previousState=state,job=motion.jobs[0],seed=lastSeed;
102
- const previousIntent=preserveMotion?(intent??(motion.active&&job?{departing:motion.departing,fromAge:job.fromAge,started:job.started,seed}:null)):null;
103
- releaseAsset();asset={raster,w,h,rows,filterRadius,aspect,mesh,releaseTexture};object.add(mesh);view.sharedGroups.set('image',{mesh});view.version++;
104
- view.motionFrame={x:-w/2,y:-h/2,width:w,height:h};
105
- state=preserveMotion?previousState:'visible';intent=previousIntent;lastSeed=preserveMotion?seed:null;mesh.visible=state!=='hidden';builds++;loadMs=raster.decodeMs;buildMs=performance.now()-start;runtime.requestRender();return true;
106
- }catch(e){if(disposed||token!==revision||e.name==='AbortError')return false;error=e.message;throw e;}
107
- finally{if(token===revision){loading=false;runtime.requestRender();}}
108
- }
109
- function play(departing){
110
- if(disposed||!asset)return false;
111
- if(departing&&(state==='hidden'||state==='leaving'||intent?.departing))return false;
112
- const fromAge=state==='entering'&&motion.jobs[0]?Math.max(0,Math.min(1,((motion.lastTime??motion.jobs[0].started)-motion.jobs[0].started)/motion.duration)):1;
113
- intent={departing,fromAge};runtime.requestRender();return true;
114
- }
115
- const control={object,
116
- frame(now,reducedMotion){
117
- if(disposed||!asset)return;
118
- resizeMesh();
119
- if(intent){
120
- const {departing,fromAge,started,seed}=intent;intent=null;asset.mesh.visible=true;state=departing?'leaving':'entering';
121
- const exitMode=entrySettings.exitEffect==='same'?entryMode:entrySettings.exitEffect;
122
- const activeMode=departing?exitMode:entryMode;
123
- motion.setMode(activeMode);motion.configure(normalizeImageEffectOptions(requestedSettings,activeMode));
124
- if(seed!==undefined)lastSeed=seed;else if(!departing||lastSeed===null)lastSeed=motion.randomSeed();
125
- // DOM image coordinates are source pixels, unlike the spatial runtime's
126
- // world units. Keep travel at a stable 32 CSS px for DOM presentations.
127
- const shownHeight=displaySize?.height??measure(object,runtime.camera,runtime.renderer.domElement,asset.h);
128
- const shownWidth=displaySize?.width??shownHeight*asset.w/asset.h;
129
- const motionUnit=shownHeight>0?imageMotionPixels(shownWidth,shownHeight)*asset.h/shownHeight:.65;
130
- motion.playRegion(view,{x:-asset.w/2,y:-asset.h/2,width:asset.w,height:asset.h,direction},motionUnit,started??now,{departing,seed:lastSeed,fromAge:departing&&exitMode!==entryMode?1:fromAge});
131
- }
132
- if(motion.active){
133
- motion.step(now,reducedMotion);
134
- // This surface reuses one material for both directions. Refresh its
135
- // dynamic clock/mask uniforms on the first draw after a transition too.
136
- asset.mesh.material.uniformsNeedUpdate=true;
137
- // Upload this frame's region data before reusing the same sampler.
138
- runtime.renderer.initTexture?.(motion.uniforms.dustData.value);
139
- if(motion.active)runtime.requestRender();
140
- else{state=motion.departing?'hidden':'visible';asset.mesh.visible=!motion.departing;}
141
- }
142
- resizeMesh();
143
- },
144
- invalidate(){runtime.requestRender();},
145
- destroy(){if(disposed)return;disposed=true;revision++;controller?.abort();unregister();releaseAsset();motion.dispose();state='disposed';},
146
- };
147
- const unregister=runtime.register(control);
148
- return {object,ready:source===undefined?Promise.resolve(false):setSource(source),setSource,
149
- enter:()=>play(false),exit:()=>play(true),
150
- setEffect(mode,options={}){validMode(mode);const next=normalizeImageEffectOptions(options,mode);if(disposed)return;requestedSettings=options;entryMode=mode;entrySettings=next;motion.setMode(mode);motion.configure(next);motion.cancel();motion.uniforms.dustMaskOnly.value=0;intent=null;if(asset){asset.mesh.visible=true;state='visible';}runtime.requestRender();},
151
- setDisplaySize(width,height){if(!(Number.isFinite(width)&&Number.isFinite(height)&&width>0&&height>0))throw RangeError('Positive display dimensions required');displaySize={width,height};},
152
- setDirection(value){validDirection(value);direction=value;},
153
- show(){if(disposed||!asset)return;intent=null;motion.cancel();motion.uniforms.dustMaskOnly.value=0;asset.mesh.visible=true;state='visible';runtime.requestRender();},
154
- frame:control.frame,destroy:control.destroy,
155
- stats(){const raster=asset?.raster,g=asset?.mesh.geometry;return {disposed,loading,error,state,active:motion.active,mode:entryMode,activeMode:motion.mode,exitMode:entrySettings.exitEffect==='same'?entryMode:entrySettings.exitEffect,duration:motion.duration,settings:{...motion.settings,recipe:{...motion.settings.recipe}},direction,motionFrame:asset?{...view.motionFrame}:null,divisions:asset?.rows,gridAspect:asset?.aspect,gridColumns:asset?Math.max(1,Math.round(asset.rows*asset.aspect)):0,triangles:g?g.getAttribute('position').count/3:0,builds,loadMs,buildMs,rasterBytes:raster?.rgba.byteLength||0,estimatedTextureBytes:raster?Math.ceil(raster.rgba.byteLength*4/3):0,geometryBytes:g?Object.values(g.attributes).reduce((n,a)=>n+a.array.byteLength,0):0,source:raster?{type:raster.type,width:raster.width,height:raster.height,originalWidth:raster.originalWidth,originalHeight:raster.originalHeight}:null};},
156
- };
157
- }
1
+ import {imageMotionPixels} from './motion-envelope.js';
2
+ import {loadImageRaster} from './image-source.js';
3
+ import {rasterToTextureMesh} from './raster-texture-mesh.js';
4
+ import {attachRasterTexture,applyRasterSamplingShader} from './raster-texture-material.js';
5
+ import {textDivisionsForSize,createProjectedTextSize} from './text-mesh-density.js';
6
+ import {TriangleEffect} from './triangle-effect.js';
7
+ import {normalizeTextEffectOptions} from './text-effect-options.js';
8
+ import {textEditMotions} from './text-edit-motions.js';
9
+
10
+ export const IMAGE_EFFECT_MODES=Object.freeze(Object.keys(textEditMotions));
11
+ // Images share motion recipes with text, but preserve source color by default.
12
+ export function normalizeImageEffectOptions(settings={},mode='dust-wind'){
13
+ const normalized=normalizeTextEffectOptions({formation:-1,...settings},mode);
14
+ return Object.freeze({...normalized,recipe:Object.freeze({...normalized.recipe,glow:settings.recipe?.glow??0})});
15
+ }
16
+
17
+ // Images use twice the text-derived rows: half-size cells, with a bounded grid.
18
+ export function imageDivisions(width,height,displayHeight,current=null,maxTriangles=80000){
19
+ let rows=2*textDivisionsForSize(displayHeight,current===null?null:current/2);
20
+ while(rows>1&&2*rows*Math.max(1,Math.round(width*rows/height))>maxTriangles)rows--;
21
+ if(2*rows*Math.max(1,Math.round(width*rows/height))>maxTriangles)throw Error('Image aspect ratio exceeds the triangle budget');
22
+ return rows;
23
+ }
24
+
25
+ const vertexShader=`
26
+ attribute vec2 rasterUV;
27
+ attribute vec2 glyphOffset;
28
+ varying vec2 vRasterUV;
29
+ void main(){
30
+ vRasterUV=rasterUV;
31
+ vec3 p=position+vec3(glyphOffset,0.0);
32
+ /* THD_TRIANGLE_MOTION */
33
+ gl_Position=projectionMatrix*modelViewMatrix*vec4(p,1.0);
34
+ }`;
35
+ const fragmentShader=`
36
+ uniform float presentationOpacity;
37
+ uniform sampler2D rasterMap;
38
+ varying vec2 vRasterUV;
39
+ void main(){
40
+ vec4 rasterSample=texture2D(rasterMap,vRasterUV);
41
+ float coverage=rasterSample.a;
42
+ gl_FragColor=vec4(rasterSample.rgb, coverage);
43
+ #include <colorspace_fragment>
44
+ gl_FragColor.a *= presentationOpacity;
45
+ }`;
46
+
47
+ // One image object in an existing runtime. No renderer, input or font ownership.
48
+ export function createImageSurface(runtime,{source,width=6,height=3.8,effect='dust-wind',settings={},direction='ltr'}={}){
49
+ if(runtime.disposed)throw Error('Spatial runtime disposed');
50
+ if(!Number.isFinite(width)||!Number.isFinite(height)||width<=0||height<=0)throw RangeError('Positive image bounds required');
51
+ const validMode=mode=>{if(!IMAGE_EFFECT_MODES.includes(mode))throw TypeError('Unsupported image effect');};
52
+ const validDirection=value=>{if(!['ltr','rtl'].includes(value))throw TypeError('Invalid direction');};
53
+ validMode(effect);validDirection(direction);
54
+ const {THREE}=runtime,document=runtime.renderer.domElement.ownerDocument,object=new THREE.Group();
55
+ const motion=new TriangleEffect(THREE,{mode:effect,settings:normalizeImageEffectOptions(settings,effect)}),measure=createProjectedTextSize(THREE);
56
+ let requestedSettings=settings,entryMode=effect,entrySettings=motion.settings;
57
+ let disposed=false,asset=null,controller=null,revision=0,loading=false,error=null,state='empty',intent=null;
58
+ let displaySize=null;
59
+ let lastSeed=null,builds=0,loadMs=0,buildMs=0;
60
+ const view={sharedGroups:new Map(),version:0};
61
+ function releaseAsset(){
62
+ motion.cancel();motion.uniforms.dustMaskOnly.value=0;motion.materials.clear();view.sharedGroups.clear();
63
+ if(asset){object.remove(asset.mesh);asset.mesh.geometry.dispose();asset.mesh.material.dispose();asset.releaseTexture();asset=null;}
64
+ }
65
+ function geometry(raster,rows,w,h,filterRadius,aspect){
66
+ const mesh=rasterToTextureMesh(raster.rgba,raster.width,raster.height,rows,{filterRadius,aspect}),positions=new Float32Array(mesh.triangleCount*9);
67
+ for(let i=0;i<mesh.coordinates.length;i+=2){const at=i/2*3;positions[at]=(mesh.coordinates[i]/raster.width-.5)*w;positions[at+1]=(.5-mesh.coordinates[i+1]/raster.height)*h;}
68
+ const result=new THREE.BufferGeometry();result.setAttribute('position',new THREE.BufferAttribute(positions,3));
69
+ result.setAttribute('rasterUV',new THREE.BufferAttribute(mesh.uvs,2));result.computeBoundingBox();
70
+ return result;
71
+ }
72
+ function meshSettings(raster,h,current=null){
73
+ const displayHeight=displaySize?.height??measure(object,runtime.camera,runtime.renderer.domElement,h);
74
+ const aspect=displaySize?displaySize.width/displaySize.height:raster.width/raster.height;
75
+ // Mipmap/bilinear filtering sees beyond the original opaque texel. Retain
76
+ // that support too, especially when a wide image is displayed very small.
77
+ const ratio=raster.height/Math.max(1,displayHeight*(runtime.renderer.getPixelRatio?.()||1));
78
+ const filterRadius=displayHeight>0?Math.max(.5,2**Math.ceil(Math.log2(Math.max(1,ratio)))):.5;
79
+ return {rows:imageDivisions(aspect,1,displayHeight,current),filterRadius,aspect};
80
+ }
81
+ function resizeMesh(){
82
+ if(!asset||motion.active)return;
83
+ const next=meshSettings(asset.raster,asset.h,asset.rows);if(next.rows===asset.rows&&next.filterRadius===asset.filterRadius&&next.aspect===asset.aspect)return;
84
+ const start=performance.now(),replacement=geometry(asset.raster,next.rows,asset.w,asset.h,next.filterRadius,next.aspect);
85
+ asset.mesh.geometry.dispose();asset.mesh.geometry=replacement;asset.rows=next.rows;asset.aspect=next.aspect;asset.filterRadius=next.filterRadius;view.version++;builds++;buildMs=performance.now()-start;
86
+ motion.decorate(view);
87
+ }
88
+ async function setSource(value,{preserveMotion=false,bounds=null}={}){
89
+ if(disposed)throw Error('Image surface disposed');
90
+ const token=++revision;controller?.abort();controller=new AbortController();loading=true;error=null;
91
+ try{
92
+ const raster=await loadImageRaster(value,{document,signal:controller.signal,maxSide:Math.min(2048,runtime.renderer.capabilities?.maxTextureSize||2048)});
93
+ if(disposed||token!==revision)return false;
94
+ if(bounds){width=bounds.width;height=bounds.height;}
95
+ const start=performance.now(),factor=Math.min(width/raster.width,height/raster.height),w=raster.width*factor,h=raster.height*factor;
96
+ const {rows,filterRadius,aspect}=meshSettings(raster,h),g=geometry(raster,rows,w,h,filterRadius,aspect);
97
+ const material=new THREE.ShaderMaterial({vertexShader,fragmentShader,side:THREE.DoubleSide,transparent:true,depthWrite:false,toneMapped:false,extensions:{derivatives:true}});
98
+ material.uniforms.presentationOpacity={value:1};
99
+ applyRasterSamplingShader(material);
100
+ material.defaultAttributeValues.glyphOffset=[0,0];
101
+ const releaseTexture=attachRasterTexture(THREE,material,raster),mesh=new THREE.Mesh(g,material);mesh.frustumCulled=false;
102
+ // Replacement of DOM pixels/size must not restart an existing effect or
103
+ // reveal an image that has already completed its exit.
104
+ const previousState=state,job=motion.jobs[0],seed=lastSeed;
105
+ const previousIntent=preserveMotion?(intent??(motion.active&&job?{departing:motion.departing,fromAge:job.fromAge,started:job.started,seed}:null)):null;
106
+ releaseAsset();asset={raster,w,h,rows,filterRadius,aspect,mesh,releaseTexture};object.add(mesh);view.sharedGroups.set('image',{mesh});view.version++;
107
+ view.motionFrame={x:-w/2,y:-h/2,width:w,height:h};
108
+ state=preserveMotion?previousState:'visible';intent=previousIntent;lastSeed=preserveMotion?seed:null;mesh.visible=state!=='hidden';builds++;loadMs=raster.decodeMs;buildMs=performance.now()-start;runtime.requestRender();return true;
109
+ }catch(e){if(disposed||token!==revision||e.name==='AbortError')return false;error=e.message;throw e;}
110
+ finally{if(token===revision){loading=false;runtime.requestRender();}}
111
+ }
112
+ function play(departing,started){
113
+ if(disposed||!asset)return false;
114
+ if(departing&&(state==='hidden'||state==='leaving'||intent?.departing))return false;
115
+ const fromAge=state==='entering'&&motion.jobs[0]?Math.max(0,Math.min(1,((motion.lastTime??motion.jobs[0].started)-motion.jobs[0].started)/motion.duration)):1;
116
+ intent={departing,fromAge,started};runtime.requestRender();return true;
117
+ }
118
+ const control={object,
119
+ frame(now,reducedMotion){
120
+ if(disposed||!asset)return;
121
+ resizeMesh();
122
+ if(intent){
123
+ const {departing,fromAge,started,seed}=intent;intent=null;asset.mesh.visible=true;state=departing?'leaving':'entering';
124
+ const exitMode=entrySettings.exitEffect==='same'?entryMode:entrySettings.exitEffect;
125
+ const activeMode=departing?exitMode:entryMode;
126
+ motion.setMode(activeMode);motion.configure(normalizeImageEffectOptions(requestedSettings,activeMode));
127
+ if(seed!==undefined)lastSeed=seed;else if(!departing||lastSeed===null)lastSeed=motion.randomSeed();
128
+ // DOM image coordinates are source pixels, unlike the spatial runtime's
129
+ // world units. Keep travel at a stable 32 CSS px for DOM presentations.
130
+ const shownHeight=displaySize?.height??measure(object,runtime.camera,runtime.renderer.domElement,asset.h);
131
+ const shownWidth=displaySize?.width??shownHeight*asset.w/asset.h;
132
+ const motionUnit=shownHeight>0?imageMotionPixels(shownWidth,shownHeight)*asset.h/shownHeight:.65;
133
+ motion.playRegion(view,{x:-asset.w/2,y:-asset.h/2,width:asset.w,height:asset.h,direction},motionUnit,started??now,{departing,seed:lastSeed,fromAge:departing&&exitMode!==entryMode?1:fromAge});
134
+ }
135
+ if(motion.active){
136
+ motion.step(now,reducedMotion);
137
+ // This surface reuses one material for both directions. Refresh its
138
+ // dynamic clock/mask uniforms on the first draw after a transition too.
139
+ asset.mesh.material.uniformsNeedUpdate=true;
140
+ // Upload this frame's region data before reusing the same sampler.
141
+ runtime.renderer.initTexture?.(motion.uniforms.dustData.value);
142
+ if(motion.active)runtime.requestRender();
143
+ else{state=motion.departing?'hidden':'visible';asset.mesh.visible=!motion.departing;}
144
+ }
145
+ resizeMesh();
146
+ },
147
+ invalidate(){runtime.requestRender();},
148
+ destroy(){if(disposed)return;disposed=true;revision++;controller?.abort();unregister();releaseAsset();motion.dispose();state='disposed';},
149
+ };
150
+ const unregister=runtime.register(control);
151
+ return {object,ready:source===undefined?Promise.resolve(false):setSource(source),setSource,
152
+ enter:started=>play(false,started),exit:started=>play(true,started),
153
+ setEffect(mode,options={}){validMode(mode);const next=normalizeImageEffectOptions(options,mode);if(disposed)return;requestedSettings=options;entryMode=mode;entrySettings=next;motion.setMode(mode);motion.configure(next);motion.cancel();motion.uniforms.dustMaskOnly.value=0;intent=null;if(asset){asset.mesh.visible=true;state='visible';}runtime.requestRender();},
154
+ setDisplaySize(width,height){if(!(Number.isFinite(width)&&Number.isFinite(height)&&width>0&&height>0))throw RangeError('Positive display dimensions required');displaySize={width,height};},
155
+ setDirection(value){validDirection(value);direction=value;},
156
+ setPresentationOpacity(value){if(asset)asset.mesh.material.uniforms.presentationOpacity.value=value;},
157
+ show(){if(disposed||!asset)return;intent=null;motion.cancel();motion.uniforms.dustMaskOnly.value=0;asset.mesh.visible=true;state='visible';runtime.requestRender();},
158
+ frame:control.frame,destroy:control.destroy,
159
+ stats(){const raster=asset?.raster,g=asset?.mesh.geometry;return {disposed,loading,error,state,active:motion.active,mode:entryMode,activeMode:motion.mode,exitMode:entrySettings.exitEffect==='same'?entryMode:entrySettings.exitEffect,duration:motion.duration,settings:{...motion.settings,recipe:{...motion.settings.recipe}},direction,motionFrame:asset?{...view.motionFrame}:null,divisions:asset?.rows,gridAspect:asset?.aspect,gridColumns:asset?Math.max(1,Math.round(asset.rows*asset.aspect)):0,triangles:g?g.getAttribute('position').count/3:0,builds,loadMs,buildMs,rasterBytes:raster?.rgba.byteLength||0,estimatedTextureBytes:raster?Math.ceil(raster.rgba.byteLength*4/3):0,geometryBytes:g?Object.values(g.attributes).reduce((n,a)=>n+a.array.byteLength,0):0,source:raster?{type:raster.type,width:raster.width,height:raster.height,originalWidth:raster.originalWidth,originalHeight:raster.originalHeight}:null};},
160
+ };
161
+ }
158
162
 
159
163
 
160
164
 
@@ -1,10 +1,10 @@
1
- import {createViewportRenderOwner} from './viewport-render-owner.js';
2
-
3
- // Shared GPU ownership, independent of component factories. Local 2D
4
- // presentation canvases preserve DOM stacking and clipping of each surface.
5
- export function createRenderOwner(THREE,document,{presentation='local',...options}={}){
6
- if(presentation==='viewport')return createViewportRenderOwner(THREE,document,options);
7
- if(presentation!=='local')throw TypeError('Invalid canvas presentation');
1
+ import {createViewportRenderOwner} from './viewport-render-owner.js';
2
+
3
+ // Shared GPU ownership, independent of component factories. Local 2D
4
+ // presentation canvases preserve DOM stacking and clipping of each surface.
5
+ export function createRenderOwner(THREE,document,{presentation='local',...options}={}){
6
+ if(presentation==='viewport')return createViewportRenderOwner(THREE,document,options);
7
+ if(presentation!=='local')throw TypeError('Invalid canvas presentation');
8
8
  if(!THREE?.WebGLRenderer||!document?.defaultView)throw TypeError('THREE and a window document required');
9
9
  let renderer=null,disposed=false,lost=false,w=0,h=0,dpr=0,copies=0,copyMs=0,renderMs=0,created=0;
10
10
  const leases=new Set(),handles=new Set();
@@ -29,11 +29,16 @@ export function createRenderOwner(THREE,document,{presentation='local',...option
29
29
  }
30
30
  render(scene,camera){
31
31
  ensure();if(this.dead)throw Error('Presentation disposed');
32
- if(dpr!==this.ratio){renderer.setPixelRatio(this.ratio);dpr=this.ratio;}
33
- if(w!==this.width||h!==this.height){renderer.setSize(this.width,this.height,false);w=this.width;h=this.height;}
32
+ // Keep a high-water scratch buffer: alternating differently sized leases
33
+ // must not reallocate the GPU drawing buffer on every text frame.
34
+ const pw=this.domElement.width,ph=this.domElement.height;
35
+ if(pw>w||ph>h){w=Math.max(w,pw);h=Math.max(h,ph);renderer.setSize(w,h,false);}
36
+ renderer.setScissorTest(false);renderer.clear(true,true,true);
37
+ renderer.setViewport(0,0,pw,ph);
38
+ renderer.setScissor(0,0,pw,ph);renderer.setScissorTest(true);
34
39
  const t=performance.now();renderer.render(scene,camera);renderMs+=performance.now()-t;
35
40
  const start=performance.now();this.context.clearRect(0,0,this.domElement.width,this.domElement.height);
36
- this.context.drawImage(renderer.domElement,0,0);copyMs+=performance.now()-start;copies++;
41
+ this.context.drawImage(renderer.domElement,0,h-ph,pw,ph,0,0,pw,ph);copyMs+=performance.now()-start;copies++;
37
42
  }
38
43
  dispose(){if(this.dead)return;this.dead=true;leases.delete(this);this.domElement.width=this.domElement.height=0;}
39
44
  forceContextLoss(){} // A lease cannot destroy a peer's context.
@@ -44,8 +49,8 @@ export function createRenderOwner(THREE,document,{presentation='local',...option
44
49
  const control=factory(host,Namespace,options),destroy=control.destroy;
45
50
  handles.add(control);control.destroy=()=>{handles.delete(control);destroy();};return control;
46
51
  }
47
- return {create,
48
- refresh(){if(disposed)throw Error('Shared owner disposed');for(const control of handles)control.refresh?.();},
52
+ return {create,
53
+ refresh(){if(disposed)throw Error('Shared owner disposed');for(const control of handles)control.refresh?.();},
49
54
  stats:()=>({disposed,lost,contexts:renderer&&!disposed?1:0,created,controls:handles.size,leases:leases.size,copies,copyMs,renderMs,geometries:renderer?.info.memory.geometries||0}),
50
55
  destroy(){
51
56
  if(disposed)return;disposed=true;
@@ -33,6 +33,13 @@ export const textMotionPrimitives=`
33
33
  float delay=clamp(order,0.0,1.0)*spread;
34
34
  return clamp((t-delay)/max(1.0-delay,0.001),0.0,1.0);
35
35
  }
36
+ // Continuous per-particle appearance: sparse at first, progressively denser.
37
+ // Use the existing reversible motion phase so exit follows the same envelope.
38
+ float thdParticleAppearance(float t,float order){
39
+ float start=0.55*sqrt(clamp(order,0.0,1.0));
40
+ float fade=clamp((t-start)/0.25,0.0,1.0);
41
+ return fade*fade;
42
+ }
36
43
  vec2 thdTurn(vec2 v,float angle){
37
44
  return vec2(v.x*cos(angle)-v.y*sin(angle),v.x*sin(angle)+v.y*cos(angle));
38
45
  }
@@ -139,8 +139,9 @@ export class TriangleEffect {
139
139
  pace=clamp(mix(0.5,pace,dustChaos),0.0,1.0);
140
140
  }
141
141
  float remain=1.0-t;
142
- dustOpacity=smoothstep(0.0,0.1,t);
142
+ dustOpacity=1.0;
143
143
  ${textEffectApplicationShader}
144
+ dustOpacity*=thdParticleAppearance(t,fract(seed*17.17+drift*3.13));
144
145
  p.z=thdFrontDepth(position.z,p.z);
145
146
  break;
146
147
  }