1
- #[ macro_use] mod utils;
1
+ #[ macro_use]
2
+ mod utils;
2
3
3
4
use futures:: { future, Future } ;
4
5
use js_sys:: Promise ;
6
+ use std:: cell:: RefCell ;
7
+ use std:: collections:: HashMap ;
8
+ use std:: rc:: Rc ;
9
+ use utils:: set_panic_hook;
5
10
use wasm_bindgen:: prelude:: * ;
6
11
use wasm_bindgen:: prelude:: * ;
7
12
use wasm_bindgen:: JsCast ;
8
13
use wasm_bindgen_futures:: future_to_promise;
9
14
use wasm_bindgen_futures:: JsFuture ;
10
15
use web_sys:: * ;
11
- use std:: cell:: RefCell ;
12
- use std:: collections:: HashMap ;
13
- use std:: rc:: Rc ;
14
- use utils:: set_panic_hook;
15
-
16
16
17
17
// A macro to provide `println!(..)`-style syntax for `console.log` logging.
18
18
macro_rules! log {
@@ -21,54 +21,50 @@ macro_rules! log {
21
21
}
22
22
}
23
23
24
-
25
24
fn request_animation_frame ( session : & XrSession , f : & Closure < dyn FnMut ( f64 , XrFrame ) > ) -> i32 {
26
25
// This turns the Closure into a js_sys::Function
27
26
// See https://rustwasm.github.io/wasm-bindgen/api/wasm_bindgen/closure/struct.Closure.html#casting-a-closure-to-a-js_sysfunction
28
- session
29
- . request_animation_frame ( f. as_ref ( ) . unchecked_ref ( ) )
27
+ session. request_animation_frame ( f. as_ref ( ) . unchecked_ref ( ) )
30
28
}
31
29
32
-
33
30
pub fn create_webgl_context ( xr_mode : bool ) -> Result < WebGl2RenderingContext , JsValue > {
34
31
let canvas = web_sys:: window ( )
35
- . unwrap ( )
36
- . document ( )
37
- . unwrap ( )
38
- . get_element_by_id ( "canvas" )
39
- . unwrap ( )
40
- . dyn_into :: < HtmlCanvasElement > ( )
41
- . unwrap ( ) ;
32
+ . unwrap ( )
33
+ . document ( )
34
+ . unwrap ( )
35
+ . get_element_by_id ( "canvas" )
36
+ . unwrap ( )
37
+ . dyn_into :: < HtmlCanvasElement > ( )
38
+ . unwrap ( ) ;
42
39
43
40
let gl: WebGl2RenderingContext = if xr_mode {
44
41
let mut gl_attribs = HashMap :: new ( ) ;
45
42
gl_attribs. insert ( String :: from ( "xrCompatible" ) , true ) ;
46
43
let js_gl_attribs = JsValue :: from_serde ( & gl_attribs) . unwrap ( ) ;
47
44
48
- canvas. get_context_with_context_options ( "webgl2" , & js_gl_attribs) ?. unwrap ( ) . dyn_into ( ) ?
49
- }
50
- else {
45
+ canvas
46
+ . get_context_with_context_options ( "webgl2" , & js_gl_attribs) ?
47
+ . unwrap ( )
48
+ . dyn_into ( ) ?
49
+ } else {
51
50
canvas. get_context ( "webgl2" ) ?. unwrap ( ) . dyn_into ( ) ?
52
51
} ;
53
52
54
53
Ok ( gl)
55
54
}
56
55
57
-
58
56
#[ wasm_bindgen]
59
57
pub struct XrApp {
60
58
session : Rc < RefCell < Option < XrSession > > > ,
61
- gl : Rc < WebGl2RenderingContext >
59
+ gl : Rc < WebGl2RenderingContext > ,
62
60
}
63
61
64
-
65
62
#[ wasm_bindgen]
66
63
impl XrApp {
67
-
68
64
#[ wasm_bindgen( constructor) ]
69
65
pub fn new ( ) -> XrApp {
70
66
set_panic_hook ( ) ;
71
-
67
+
72
68
let session = Rc :: new ( RefCell :: new ( None ) ) ;
73
69
74
70
let xr_mode = true ;
@@ -83,7 +79,7 @@ impl XrApp {
83
79
let gpu = navigator. gpu ( ) ;
84
80
let xr = navigator. xr ( ) ;
85
81
let session_mode = XrSessionMode :: Inline ;
86
- let session_supported_promise = xr. is_session_supported ( session_mode) ;
82
+ let session_supported_promise = xr. is_session_supported ( session_mode) ;
87
83
88
84
// Note: &self is on the stack so we can't use it in a future (which will
89
85
// run after the &self reference is out or scope). Clone ref to the parts
@@ -93,17 +89,18 @@ impl XrApp {
93
89
let gl = self . gl . clone ( ) ;
94
90
95
91
let future_ = async move {
96
- let supports_session = wasm_bindgen_futures:: JsFuture :: from ( session_supported_promise) . await ;
92
+ let supports_session =
93
+ wasm_bindgen_futures:: JsFuture :: from ( session_supported_promise) . await ;
97
94
let supports_session = supports_session. unwrap ( ) ;
98
95
if supports_session == false {
99
96
log ! ( "XR session not supported" ) ;
100
97
return Ok ( JsValue :: from ( "XR session not supported" ) ) ;
101
98
}
102
-
99
+
103
100
let xr_session_promise = xr. request_session ( session_mode) ;
104
101
let xr_session = wasm_bindgen_futures:: JsFuture :: from ( xr_session_promise) . await ;
105
102
let xr_session: XrSession = xr_session. unwrap ( ) . into ( ) ;
106
-
103
+
107
104
let xr_gl_layer = XrWebGlLayer :: new_with_web_gl2_rendering_context ( & xr_session, & gl) ?;
108
105
let mut render_state_init = XrRenderStateInit :: new ( ) ;
109
106
render_state_init. base_layer ( Some ( & xr_gl_layer) ) ;
@@ -140,11 +137,14 @@ impl XrApp {
140
137
// Schedule ourself for another requestAnimationFrame callback.
141
138
// TODO: WebXR Samples call this at top of request_animation_frame - should this be moved?
142
139
request_animation_frame ( & sess, f. borrow ( ) . as_ref ( ) . unwrap ( ) ) ;
143
-
144
140
} ) as Box < dyn FnMut ( f64 , XrFrame ) > ) ) ;
145
141
146
142
let session: & Option < XrSession > = & self . session . borrow ( ) ;
147
- let sess: & XrSession = if let Some ( sess) = session { sess } else { return ( ) } ;
143
+ let sess: & XrSession = if let Some ( sess) = session {
144
+ sess
145
+ } else {
146
+ return ( ) ;
147
+ } ;
148
148
149
149
request_animation_frame ( sess, g. borrow ( ) . as_ref ( ) . unwrap ( ) ) ;
150
150
}
0 commit comments