@@ -20,8 +20,14 @@ const videos = {
2020 url : '../../assets/video/Video_360°._Timelapse._Bled_Lake_in_Slovenia..webm.720p.vp9.webm' ,
2121 mode : '360' ,
2222 } ,
23+ webcam : {
24+ url : '' ,
25+ mode : 'mirror' ,
26+ } ,
2327} as const ;
2428
29+ const videoinfo = document . getElementById ( 'videoinfo' ) as HTMLPreElement ;
30+
2531const canvas = document . querySelector ( 'canvas' ) as HTMLCanvasElement ;
2632const context = canvas . getContext ( 'webgpu' ) ;
2733const presentationFormat = navigator . gpu . getPreferredCanvasFormat ( ) ;
@@ -90,7 +96,21 @@ let canReadVideo = false;
9096
9197async function playVideo ( videoName : keyof typeof videos ) {
9298 canReadVideo = false ;
93- video . src = videos [ videoName ] . url ;
99+
100+ if ( video . srcObject ) {
101+ ( video . srcObject as MediaStream )
102+ . getTracks ( )
103+ . forEach ( ( track ) => track . stop ( ) ) ;
104+ video . srcObject = null ;
105+ }
106+
107+ if ( videoName === 'webcam' ) {
108+ video . srcObject = await navigator . mediaDevices . getUserMedia ( {
109+ video : true ,
110+ } ) ;
111+ } else {
112+ video . src = videos [ videoName ] . url ;
113+ }
94114 await video . play ( ) ;
95115 canReadVideo = true ;
96116}
@@ -120,8 +140,22 @@ function drawVideo() {
120140 const maxSize = device . limits . maxTextureDimension2D ;
121141 canvas . width = Math . min ( Math . max ( 1 , canvas . offsetWidth ) , maxSize ) ;
122142 canvas . height = Math . min ( Math . max ( 1 , canvas . offsetHeight ) , maxSize ) ;
123- const externalTextureSource =
124- settings . videoSource === 'videoFrame' ? new VideoFrame ( video ) : video ;
143+ const videoFrame =
144+ settings . videoSource === 'videoFrame' ? new VideoFrame ( video ) : null ;
145+ const externalTextureSource = videoFrame ?? video ;
146+ videoinfo . textContent = `HTMLVideoElement: ${ video . videoWidth } x${ video . videoHeight } ` ;
147+ if ( videoFrame ) {
148+ videoinfo . textContent +=
149+ '\nVideoFrame: ' +
150+ JSON . stringify (
151+ {
152+ codedRect : videoFrame . codedRect ,
153+ visibleRect : videoFrame . visibleRect ,
154+ } ,
155+ undefined ,
156+ 2
157+ ) ;
158+ }
125159
126160 const mode = videos [ settings . video ] . mode ;
127161 const pipeline = mode === '360' ? video360Pipeline : videoCoverPipeline ;
@@ -182,6 +216,9 @@ function drawVideo() {
182216 combinedAspect > 1 ? [ 1 / combinedAspect , 1 ] : [ 1 , combinedAspect ] ,
183217 mat
184218 ) ;
219+ if ( mode === 'mirror' ) {
220+ mat3 . scale ( mat , [ - 1 , 1 ] , mat ) ;
221+ }
185222 mat3 . translate ( mat , [ - 0.5 , - 0.5 ] , mat ) ;
186223 device . queue . writeBuffer ( uniformBuffer , 0 , mat ) ;
187224 }
0 commit comments