-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
119 lines (97 loc) · 3.42 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import {
WebGLRenderer,
PerspectiveCamera,
Scene,
Clock,
Box3,
Vector3,
Color,
} from './three.module.js'
import { VRButton } from './VRButton.js';
import { OrbitControls } from './OrbitControls.js'
import { XRControllerModelFactory } from './XRControllerModelFactory.js';
import { GLTFGoogleTiltBrushMaterialExtension } from './three-icosa.module.js';
import { GLTFLoader } from './GLTFLoader.js';
class Sketch {
constructor() {
this.renderer = new WebGLRenderer({
antialias: true
})
this.renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
this.renderer.setSize(window.innerWidth, window.innerHeight)
this.renderer.setClearColor(0x000000, 0)
this.renderer.xr.enabled = true;
this.camera = new PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
0.1,
1000
)
this.camera.position.set(0, 0.95, 2)
this.scene = new Scene()
this.scene.background = new Color(0, 0, 0);
this.canvas = null
this.controls = new OrbitControls(this.camera, this.renderer.domElement)
this.controls.enableDamping = true
this.clock = new Clock()
this.resize()
this.init()
}
init() {
this.addCanvas()
this.addVRButton()
this.addVRControllers()
this.addEvents()
this.addElements()
this.render()
}
addCanvas() {
this.canvas = this.renderer.domElement
document.body.appendChild(this.canvas)
}
addVRButton() {
document.body.appendChild( VRButton.createButton( this.renderer ) );
}
addVRControllers() {
// The XRControllerModelFactory will automatically fetch controller models
// that match what the user is holding as closely as possible. The models
// should be attached to the object returned from getControllerGrip in
// order to match the orientation of the held device.
const controllerModelFactory = new XRControllerModelFactory();
this.controllerGrip1 = this.renderer.xr.getControllerGrip( 0 );
this.controllerGrip1.add( controllerModelFactory.createControllerModel( this.controllerGrip1 ) );
this.scene.add( this.controllerGrip1 );
this.controllerGrip2 = this.renderer.xr.getControllerGrip( 1 );
this.controllerGrip2.add( controllerModelFactory.createControllerModel( this.controllerGrip2 ) );
this.scene.add( this.controllerGrip2 );
}
addEvents() {
window.addEventListener('resize', this.resize.bind(this))
}
addElements() {
const gltfLoader = new GLTFLoader()
gltfLoader.register(parser => new GLTFGoogleTiltBrushMaterialExtension(parser, './brushes/'));
gltfLoader.load( 'model.glb', ( tiltData ) => {
this.mesh = tiltData.scene
// Tilt Brush/Open Brush doesn't center model on export. Here's a quick snippet to find it's geometric center.
// You may need to adjust this depending on the bounds of your model, or use a different method entirely.
const box = new Box3().setFromObject(this.mesh)
this.controls.target.set(0, 0.95, 0)
this.scene.add(this.mesh)
});
}
resize() {
this.renderer.setSize(window.innerWidth, window.innerHeight)
this.camera.aspect = window.innerWidth / window.innerHeight
this.camera.updateProjectionMatrix()
}
render() {
this.controls.update()
if(this.mesh) {
this.mesh.rotation.y = this.clock.getElapsedTime() * 0.35
}
this.renderer.setAnimationLoop(this.render.bind(this))
this.renderer.render(this.scene, this.camera)
}
}
new Sketch()