This repository has been archived by the owner on Apr 18, 2022. It is now read-only.
/
main.rs
155 lines (124 loc) · 4.32 KB
/
main.rs
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//! Demonstrates loading custom assets using the Amethyst engine.
// TODO: Add asset loader directory store for the meshes.
extern crate amethyst;
extern crate rayon;
use amethyst::{
assets::{Loader, Result as AssetResult, SimpleFormat},
core::{
nalgebra::{Vector2, Vector3},
Transform, TransformBundle,
},
input::InputBundle,
prelude::*,
renderer::{
Camera, DrawShaded, Light, Material, MaterialDefaults, Mesh, MeshData, PointLight,
PosNormTex, Projection, Rgba,
},
utils::application_root_dir,
Error,
};
#[derive(Clone)]
struct Custom;
impl SimpleFormat<Mesh> for Custom {
const NAME: &'static str = "CUSTOM";
type Options = ();
/// Reads the given bytes and produces asset data.
fn import(&self, bytes: Vec<u8>, _: ()) -> AssetResult<MeshData> {
let data: String = String::from_utf8(bytes)?;
let trimmed: Vec<&str> = data.lines().filter(|line| line.len() >= 1).collect();
let mut result = Vec::new();
for line in trimmed {
let nums: Vec<&str> = line.split_whitespace().collect();
let position = Vector3::new(
nums[0].parse::<f32>().unwrap(),
nums[1].parse::<f32>().unwrap(),
nums[2].parse::<f32>().unwrap(),
);
let normal = Vector3::new(
nums[3].parse::<f32>().unwrap(),
nums[4].parse::<f32>().unwrap(),
nums[5].parse::<f32>().unwrap(),
);
result.push(PosNormTex {
position,
normal,
tex_coord: Vector2::new(0.0, 0.0),
});
}
Ok(result.into())
}
}
struct AssetsExample;
impl SimpleState for AssetsExample {
fn on_start(&mut self, data: StateData<GameData>) {
let StateData { world, .. } = data;
world.add_resource(0usize);
initialise_camera(world);
initialise_lights(world);
// Add custom cube object to scene
let (mesh, mtl) = {
let mat_defaults = world.read_resource::<MaterialDefaults>();
let loader = world.read_resource::<Loader>();
let meshes = &world.read_resource();
let textures = &world.read_resource();
let mesh = loader.load("mesh/cuboid.custom", Custom, (), (), meshes);
let albedo = loader.load_from_data([0.0, 0.0, 1.0, 0.0].into(), (), textures);
let mat = Material {
albedo,
..mat_defaults.0.clone()
};
(mesh, mat)
};
let mut trans = Transform::default();
trans.set_xyz(-5.0, 0.0, 0.0);
trans.set_scale(2.0, 2.0, 2.0);
world
.create_entity()
.with(mesh)
.with(mtl)
.with(trans)
.build();
}
}
fn main() -> Result<(), Error> {
amethyst::start_logger(Default::default());
let app_root = application_root_dir();
// Add our meshes directory to the asset loader.
let resources_directory = format!("{}/examples/assets", app_root);
let display_config_path = format!(
"{}/examples/asset_loading/resources/display_config.ron",
app_root
);
let game_data = GameDataBuilder::default()
.with_bundle(InputBundle::<String, String>::new())?
.with_bundle(TransformBundle::new())?
.with_basic_renderer(display_config_path, DrawShaded::<PosNormTex>::new(), false)?;
let mut game = Application::new(resources_directory, AssetsExample, game_data)?;
game.run();
Ok(())
}
fn initialise_camera(world: &mut World) {
let mut transform = Transform::default();
transform.set_xyz(0.0, -20.0, 10.0);
transform.rotate_local(Vector3::x_axis(), 1.3257521);
world
.create_entity()
.with(Camera::from(Projection::perspective(
1.0,
std::f32::consts::FRAC_PI_3,
))).with(transform)
.build();
}
/// Adds lights to the scene.
fn initialise_lights(world: &mut World) {
let light: Light = PointLight {
intensity: 100.0,
radius: 1.0,
color: Rgba::white(),
..Default::default()
}.into();
let mut transform = Transform::default();
transform.set_xyz(5.0, -20.0, 15.0);
// Add point light.
world.create_entity().with(light).with(transform).build();
}