1- import React , { useRef } from 'react'
1+ import React from 'react'
22import { NeedleEngine } from './NeedleEngine' ;
3- import { GameObject , onStart , Rigidbody } from '@needle-tools/engine' ;
4- import { Vector3 } from 'three' ;
3+ import { NeedleXRSession , onStart , WebXR } from '@needle-tools/engine' ;
54
6- function randFloat ( min : number , max : number ) {
7- return Math . random ( ) * ( max - min ) + min ;
8- }
95
106const App = ( ) => {
117
12- // Fetch rigidbodies from needle
13- const bodies : Rigidbody [ ] = [ ] ;
14- onStart ( _context => {
15- bodies . push ( ...GameObject . findObjectsOfType ( Rigidbody ) ) ;
16- } )
17-
18- const performJump = ( ) => {
19- const vec = new Vector3 ( 0 , 1 , 0 ) ;
20- bodies . forEach ( ( body ) => {
21- vec . set ( 0 , randFloat ( 0.5 , 5 ) , 0 ) ;
22- body . applyImpulse ( vec ) ;
8+ const startWebXR = ( ) => {
9+ return onStart ( ctx => {
10+ const webxr = ctx . scene . getOrAddComponent ( WebXR ) ;
11+ webxr . autoPlace = false ; // disable auto placement, we want the user to tap to place the scene
12+ webxr . usePlacementReticle = true ; // show the placement reticle to help the user find surfaces to place the scene
13+ webxr . usePlacementAdjustment = true ; // allow the user to adjust the position, rotation and scale of the scene with touch after placing
14+ webxr . arScale = 5 ; // scale of the user in AR (larger values make the scene smaller in AR)
15+ webxr . enterAR ( ) ; // start AR session
2316 } )
2417 } ;
2518
19+ const clickButton = ( ) => {
20+ const session = NeedleXRSession . active ;
21+ if ( session ) {
22+ const newScale = Math . random ( ) * 2 + 0.5 ;
23+ session . rig ?. gameObject . scale . set ( newScale , newScale , newScale ) ;
24+ }
25+ }
26+
2627 return (
2728 < >
2829 < div className = "top" >
@@ -34,16 +35,12 @@ const App = () => {
3435 </ div >
3536 < div id = "container" className = "needle-container" >
3637 < NeedleEngine style = { { position : "relative" , display : "flex" } } loading-style = "light" >
37- < div style = { { width : "100%" , heigth : "100%" } } >
38- < button type = "button" onClick = { performJump } className = "jump-button" > Squish!</ button >
39- </ div >
38+ < button className = "desktop xrbutton" type = "button" onClick = { startWebXR } >
39+ Start WebXR
40+ </ button >
41+ < h2 className = "ar xrbutton" onClick = { clickButton } > Hello AR</ h2 >
4042 </ NeedleEngine >
4143 </ div >
42- < div className = "bottom" >
43- The canvas can be transparent allowing for a seamless experience.
44- < br />
45- Visit < a href = "https://needle.tools" target = "_blank" > needle.tools</ a > for more information.
46- </ div >
4744 </ >
4845 ) ;
4946} ;
0 commit comments