-
Notifications
You must be signed in to change notification settings - Fork 97
/
project-options-and-multiple-scenes.html
100 lines (88 loc) · 3.44 KB
/
project-options-and-multiple-scenes.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover"
/>
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Project Options and Multiple Scenes</title>
<link rel="stylesheet" href="/css/examples.css?ver=1.0.0" />
<script src="/js/examples.js?ver=1.1.1"></script>
<script src="/lib/enable3d/enable3d.framework.0.25.4.min.js"></script>
</head>
<body>
<div id="info-text">Project Options and Multiple Scenes</div>
<div id="myCustomDiv"></div>
<script>
const { Project, Scene3D, PhysicsLoader, ExtendedObject3D, THREE } = ENABLE3D
class MenuScene extends Scene3D {
async preload() {
await this.load.preload('grass', '/assets/img/grass.jpg')
await this.load.preload('robot', '/assets/glb/robot.glb')
}
async create() {
// create a nice menu and then start level 1
this.start('MainScene', { level: 1 })
}
}
class MainScene extends Scene3D {
constructor() {
// define the key and if you want it to be an WebXR scene or not
super({ key: 'MainScene', enableXR: false })
}
async init(data) {
const { level } = data
this.currentLevel = level
console.log(`Playing level ${this.currentLevel}`)
}
async create() {
// we do not need a ground in this scene
const { orbitControls } = await this.warpSpeed('-ground')
// dispose the orbitControls on reload
this.deconstructor.add(() => orbitControls?.dispose())
// set a per scene physics
this.physics.setGravity(0, -9.81, 0)
// add grass
const grass = await this.load.texture('grass')
grass.wrapS = grass.wrapT = 1000 // RepeatWrapping
grass.offset.set(0, 0)
grass.repeat.set(2, 2)
this.physics.add.ground({ width: 20, height: 20, y: 0 }, { phong: { map: grass } })
// add robot
const gltf = await this.load.gltf('robot')
const robot = new ExtendedObject3D()
robot.name = 'robot'
robot.add(gltf.scene.children[0])
robot.traverse(child => {
if (child.isMesh) child.castShadow = child.receiveShadow = true
})
robot.position.set(0, 5, 0)
robot.scale.set(0.5, 0.5, 0.5)
this.add.existing(robot)
this.physics.add.existing(robot, { shape: 'box', offset: { y: -0.5 } })
// load the next level after 3 seconds
setTimeout(() => {
this.restart({ level: this.currentLevel + 1 })
}, 3000)
}
}
// if you do not use physics, just do not use the PhysicLoader
PhysicsLoader(
'/lib/ammo/kripken',
() =>
new Project({
anisotropy: 1, // https://threejs.org/docs/#api/en/textures/Texture.anisotropy
antialias: false, // false by default
fixedTimeStep: 1 / 60,
gravity: { x: 0, y: -9.81, z: 0 }, // the default gravity for all scenes
maxSubSteps: 1,
parent: 'myCustomDiv',
renderer: new THREE.WebGLRenderer({ antialias: false }), // add a custom renderer if you want
scenes: [MenuScene, MainScene]
})
)
</script>
</body>
</html>