-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathgltf-pbr-shader.rs
More file actions
68 lines (57 loc) · 2.52 KB
/
Copy pathgltf-pbr-shader.rs
File metadata and controls
68 lines (57 loc) · 2.52 KB
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
extern crate three;
use three::{camera::Camera, Object};
fn main() {
let mut win = three::Window::new("Three-rs glTF example");
let light = win.factory.directional_light(0xFFFFFF, 7.0);
light.look_at([1.0, 1.0, 1.0], [0.0, 0.0, 0.0], None);
win.scene.add(&light);
win.scene.background = three::Background::Color(0xC6F0FF);
let default = concat!(
env!("CARGO_MANIFEST_DIR"),
"/test_data/Lantern/Lantern.gltf"
);
let path = std::env::args().nth(1).unwrap_or(default.into());
println!("Loading {:?} (this may take a while)", path);
// Load the contents of the glTF files. Scenes loaded from the file are returned as
// `Template` objects, which can be used to instantiate the actual objects for rendering.
let templates = win.factory.load_gltf(&path);
// Instantiate the contents of the template, and then add it to the scene.
let (instance, _) = win.factory.instantiate_template(&templates[0]);
win.scene.add(&instance);
// Attempt to find a camera in the instantiated template to use as the perspective for
// rendering.
let cam = win
.scene
.sync_guard()
.find_child_of_type::<Camera>(&instance);
// If we didn't find a camera in the glTF scene, create a default one to use.
let cam = cam.unwrap_or_else(|| {
let default = win.factory.perspective_camera(60.0, 0.001..100.0);
win.scene.add(&default);
default
});
// Create a skybox for the scene.
let skybox_path = three::CubeMapPath {
front: "test_data/skybox/posz.jpg",
back: "test_data/skybox/negz.jpg",
up: "test_data/skybox/posy.jpg",
down: "test_data/skybox/negy.jpg",
left: "test_data/skybox/negx.jpg",
right: "test_data/skybox/posx.jpg",
};
let skybox = win.factory.load_cubemap(&skybox_path);
win.scene.background = three::Background::Skybox(skybox);
// Determine the current position of the camera so that we can use it to initialize the
// camera controller.
let init = win.scene.sync_guard().resolve_world(&cam).transform;
// Create a first person camera controller, starting at the camera's current position.
let mut controls = three::controls::FirstPerson::builder(&cam)
.position(init.position)
.move_speed(4.0)
.build();
// Run the main loop, updating the camera controller and rendering the scene every frame.
while win.update() && !win.input.hit(three::KEY_ESCAPE) {
controls.update(&win.input);
win.render(&cam);
}
}