1
- const modelRootURL = './' // Directory where 3D model lives
2
- const modelFile = 'tree.glb' // 3D model to spawn at tap
3
- const startScale = new BABYLON . Vector3 ( 0.01 , 0.01 , 0.01 ) // Initial scale value for our model
4
- const endScale = new BABYLON . Vector3 ( 0.05 , 0.05 , 0.05 ) // Ending scale value for our model
5
- const animationMillis = 750 // Animate over 0.75 seconds
1
+ /* globals BABYLON TWEEN XR8 XRExtras */
2
+
3
+ const modelRootURL = './' // Directory where 3D model lives
4
+ const modelFile = 'tree.glb' // 3D model to spawn at tap
5
+ const startScale = new BABYLON . Vector3 ( 0.1 , 0.1 , - 0.1 ) // Initial scale value for our model
6
+ const endScale = new BABYLON . Vector3 ( 2.0 , 2.0 , - 2.0 ) // Ending scale value for our model
7
+ const animationMillis = 750 // Animate over 0.75 seconds
6
8
7
9
let surface , engine , scene , camera
8
10
9
11
// Populates some object into an XR scene and sets the initial camera position.
10
- const initXrScene = ( { scene, camera } ) => {
11
- console . log ( 'initXrScene' )
12
-
13
- const directionalLight = new BABYLON . DirectionalLight ( "DirectionalLight" , new BABYLON . Vector3 ( 0 , - 1 , 1 ) , scene )
12
+ const initXrScene = ( ) => {
13
+ const directionalLight = new BABYLON . DirectionalLight (
14
+ 'DirectionalLight' ,
15
+ new BABYLON . Vector3 ( 0 , - 1 , 1 ) ,
16
+ scene
17
+ )
14
18
directionalLight . intensity = 1.0
15
19
16
20
const ground = BABYLON . Mesh . CreatePlane ( 'ground' , 100 , scene )
17
21
ground . rotation . x = Math . PI / 2
18
- ground . material = new BABYLON . StandardMaterial ( "groundMaterial" , scene )
22
+ ground . material = new BABYLON . StandardMaterial ( 'groundMaterial' , scene )
23
+ ground . material . diffuseColor = BABYLON . Color3 . Purple ( )
19
24
ground . material . alpha = 0
20
25
surface = ground
21
26
22
27
// Set the initial camera position relative to the scene we just laid out. This must be at a
23
28
// height greater than y=0.
24
- camera . position = new BABYLON . Vector3 ( 0 , 3 , 5 )
29
+ camera . position = new BABYLON . Vector3 ( 0 , 3 , - 5 )
25
30
}
26
31
27
32
const placeObjectTouchHandler = ( e ) => {
28
33
// console.log('placeObjectTouchHandler')
29
34
// Call XrController.recenter() when the canvas is tapped with two fingers. This resets the
30
35
// AR camera to the position specified by XrController.updateCameraProjectionMatrix() above.
31
- if ( e . touches . length == 2 ) {
36
+ if ( e . touches . length === 2 ) {
32
37
XR8 . XrController . recenter ( )
33
38
}
34
39
@@ -37,60 +42,52 @@ const placeObjectTouchHandler = (e) => {
37
42
}
38
43
39
44
// If the canvas is tapped with one finger and hits the "surface", spawn an object.
40
-
41
45
const pickResult = scene . pick ( e . touches [ 0 ] . clientX , e . touches [ 0 ] . clientY )
42
- if ( pickResult . hit && pickResult . pickedMesh == surface ) {
43
-
44
- const gltf = BABYLON . SceneLoader . LoadAssetContainer (
46
+ if ( pickResult . hit && pickResult . pickedMesh === surface ) {
47
+ BABYLON . SceneLoader . ImportMesh (
48
+ '' ,
45
49
modelRootURL ,
46
50
modelFile ,
47
51
scene ,
48
- function ( container ) { // onSuccess
49
- const scale = Object . assign ( { } , startScale )
50
- const yRot = Math . random ( ) * 360
51
- for ( i = 0 ; i < container . meshes . length ; i ++ ) {
52
- container . meshes [ i ] . _position . x = pickResult . pickedPoint . x
53
- container . meshes [ i ] . _position . z = pickResult . pickedPoint . z
54
- container . meshes [ i ] . _rotation . y = yRot
55
- container . meshes [ i ] . _scaling . x = scale . x
56
- container . meshes [ i ] . _scaling . y = scale . y
57
- container . meshes [ i ] . _scaling . z = scale . z
58
- }
59
- // Adds all elements to the scene
60
- container . addAllToScene ( )
52
+ ( newMeshes ) => { // onSuccess
53
+ const mesh = newMeshes [ 0 ]
54
+ mesh . scaling = new BABYLON . Vector3 ( startScale . x , startScale . y , startScale . z )
61
55
56
+ const yRot = Math . random ( ) * Math . PI
57
+ mesh . position = new BABYLON . Vector3 ( pickResult . pickedPoint . x , 0 , pickResult . pickedPoint . z )
58
+ mesh . rotation = new BABYLON . Vector3 ( 0 , yRot , 0 )
59
+
60
+ const scale = Object . assign ( { } , startScale )
62
61
new TWEEN . Tween ( scale )
63
- . to ( endScale , animationMillis )
64
- . easing ( TWEEN . Easing . Elastic . Out ) // Use an easing function to make the animation smooth.
65
- . onUpdate ( ( ) => {
66
- for ( i = 0 ; i < container . meshes . length ; i ++ ) {
67
- container . meshes [ i ] . _scaling . x = scale . x
68
- container . meshes [ i ] . _scaling . y = scale . y
69
- container . meshes [ i ] . _scaling . z = scale . z
70
- }
71
- } )
72
- . start ( ) // Start the tween immediately.
62
+ . to ( endScale , animationMillis )
63
+ . easing ( TWEEN . Easing . Elastic . Out ) // Use an easing function to make the animation smooth.
64
+ . onUpdate ( ( ) => {
65
+ mesh . scaling . x = scale . x
66
+ mesh . scaling . y = scale . y
67
+ mesh . scaling . z = scale . z
68
+ } )
69
+ . start ( ) // Start the tween immediately.
73
70
} ,
74
- function ( xhr ) { // onProgress
75
- console . log ( `${ ( xhr . loaded / xhr . total * 100 ) } % loaded` )
71
+ ( xhr ) => { // onProgress
72
+ console . log ( `${ ( xhr . loaded / xhr . total * 100 ) } % loaded` )
76
73
} ,
77
- function ( error ) { // onError
74
+ ( ) => { // onError
78
75
console . log ( 'Error loading model' )
79
- } ,
76
+ }
80
77
)
81
78
}
82
79
}
83
80
84
81
const startScene = ( ) => {
85
82
const canvas = document . getElementById ( 'renderCanvas' )
86
83
87
- engine = new BABYLON . Engine ( canvas , true , { stencil : true , preserveDrawingBuffer : true } , true )
84
+ engine = new BABYLON . Engine ( canvas , true , { stencil : true , preserveDrawingBuffer : true } )
88
85
engine . enableOfflineSupport = false
89
86
90
87
scene = new BABYLON . Scene ( engine )
91
88
camera = new BABYLON . FreeCamera ( 'camera' , new BABYLON . Vector3 ( 0 , 0 , 0 ) , scene )
92
89
93
- initXrScene ( { scene , camera } ) // Add objects to the scene and set starting camera position.
90
+ initXrScene ( ) // Add objects to the scene and set starting camera position.
94
91
95
92
// Connect the camera to the XR engine and show camera feed
96
93
camera . addBehavior ( XR8 . Babylonjs . xrCameraBehavior ( ) , true )
@@ -109,16 +106,21 @@ const startScene = () => {
109
106
}
110
107
111
108
const onxrloaded = ( ) => {
112
- XR8 . addCameraPipelineModules ( [ // Add camera pipeline modules.
113
- XRExtras . AlmostThere . pipelineModule ( ) , // Detects unsupported browsers and gives hints.
114
- XRExtras . FullWindowCanvas . pipelineModule ( ) , // Modifies the canvas to fill the window.
115
- XRExtras . Loading . pipelineModule ( ) , // Manages the loading screen on startup.
116
- XRExtras . RuntimeError . pipelineModule ( ) , // Shows an error image on runtime error.
109
+ XR8 . addCameraPipelineModules ( [ // Add camera pipeline modules.
110
+ XRExtras . AlmostThere . pipelineModule ( ) , // Detects unsupported browsers and gives hints.
111
+ XRExtras . Loading . pipelineModule ( ) , // Manages the loading screen on startup.
112
+ XRExtras . RuntimeError . pipelineModule ( ) , // Shows an error image on runtime error.
117
113
] )
118
114
119
115
startScene ( )
120
116
}
121
117
122
118
// Show loading screen before the full XR library has been loaded.
123
119
const load = ( ) => { XRExtras . Loading . showLoading ( { onxrloaded} ) }
124
- window . onload = ( ) => { window . XRExtras ? load ( ) : window . addEventListener ( 'xrextrasloaded' , load ) }
120
+ window . onload = ( ) => {
121
+ if ( window . XRExtras ) {
122
+ load ( )
123
+ } else {
124
+ window . addEventListener ( 'xrextrasloaded' , load )
125
+ }
126
+ }
0 commit comments