Skip to content

Commit

Permalink
Animation example
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Jul 4, 2017
1 parent c1f3bf5 commit 01c31b1
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 30 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ name = "sprite"
[[example]]
name = "group"

[[example]]
name = "anim"

[[example]]
name = "aviator"
path = "examples/aviator/main.rs"
137 changes: 137 additions & 0 deletions examples/anim.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/aviator/plane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl AirPlane {

let cockpit = {
let mut geo = three::Geometry::new_box(80.0, 50.0, 50.0);
for v in geo.vertices.iter_mut() {
for v in geo.base_shape.vertices.iter_mut() {
if v.x < 0.0 {
v.z += if v.y > 0.0 {-20.0} else {20.0};
v.y += if v.y > 0.0 {-10.0} else {30.0};
Expand Down
2 changes: 1 addition & 1 deletion examples/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn create_cubes(factory: &mut three::Factory,
-> Vec<Cube>
{
let mut geometry = three::Geometry::new_box(2.0, 2.0, 2.0);
for v in geometry.vertices.iter_mut() {
for v in geometry.base_shape.vertices.iter_mut() {
v.z += 1.0;
}

Expand Down
69 changes: 43 additions & 26 deletions src/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,9 @@ impl Factory {
Group::new(self.hub.lock().unwrap().spawn_empty())
}

fn mesh_object(&mut self, geom: &Geometry, mat: Material) -> Object {
let shape = &geom.base_shape;
let vertices: Vec<_> = if shape.normals.is_empty() {
geom.base_shape.vertices.iter().map(|v| Vertex {
fn mesh_vertices(shape: &GeometryShape) -> Vec<Vertex> {
if shape.normals.is_empty() {
shape.vertices.iter().map(|v| Vertex {
pos: [v.x, v.y, v.z, 1.0],
uv: [0.0, 0.0], //TODO
normal: NORMAL_Z,
Expand All @@ -195,39 +194,60 @@ impl Factory {
uv: [0.0, 0.0], //TODO
normal: [f2i(n.x), f2i(n.y), f2i(n.z), I8Norm(0)],
}).collect()
};
}
}

/// Create new `Mesh` with desired `Geometry` and `Material`.
pub fn mesh(&mut self, geometry: Geometry, mat: Material) -> Mesh {
let vertices = Self::mesh_vertices(&geometry.base_shape);
let cbuf = self.backend.create_constant_buffer(1);
let (vbuf, slice) = if geom.faces.is_empty() {
let (vbuf, slice) = if geometry.faces.is_empty() {
self.backend.create_vertex_buffer_with_slice(&vertices, ())
} else {
let faces: &[u16] = gfx::memory::cast_slice(&geom.faces);
let faces: &[u16] = gfx::memory::cast_slice(&geometry.faces);
self.backend.create_vertex_buffer_with_slice(&vertices, faces)
};
self.hub.lock().unwrap().spawn_visual(mat, GpuData {
slice,
vertices: vbuf,
constants: cbuf,
pending: None,
})
}

/// Create new `Mesh` with desired `Geometry` and `Material`.
pub fn mesh(&mut self, geometry: Geometry, mat: Material) -> Mesh {
Mesh {
object: self.mesh_object(&geometry, mat),
object: self.hub.lock().unwrap().spawn_visual(mat, GpuData {
slice,
vertices: vbuf,
constants: cbuf,
pending: None,
}),
}
}

/// Create a new `DynamicMesh` with desired `Geometry` and `Material`.
pub fn mesh_dynamic(&mut self, geometry: Geometry, mat: Material) -> DynamicMesh {
let num_vertices = geometry.base_shape.vertices.len();
let buffer = self.backend.create_upload_buffer(num_vertices).unwrap();
let slice = {
let data: &[u16] = gfx::memory::cast_slice(&geometry.faces);
gfx::Slice {
start: 0,
end: data.len() as u32,
base_vertex: 0,
instances: None,
buffer: self.backend.create_index_buffer(data),
}
};
let (num_vertices, vertices) = {
let data = Self::mesh_vertices(&geometry.base_shape);
let buf = self.backend.create_buffer_immutable(&data,
gfx::buffer::Role::Vertex, gfx::memory::TRANSFER_DST).unwrap();
(data.len(), buf)
};
let constants = self.backend.create_constant_buffer(1);

DynamicMesh {
object: self.mesh_object(&geometry, mat),
object: self.hub.lock().unwrap().spawn_visual(mat, GpuData {
slice,
vertices,
constants,
pending: None,
}),
geometry,
dynamic: DynamicData {
num_vertices,
buffer,
buffer: self.backend.create_upload_buffer(num_vertices).unwrap(),
},
}
}
Expand Down Expand Up @@ -321,6 +341,7 @@ pub struct GeometryShape {
}

impl GeometryShape {
/// Create an empty shape.
pub fn empty() -> Self {
GeometryShape {
vertices: Vec::new(),
Expand All @@ -339,8 +360,6 @@ pub struct Geometry {
pub shapes: HashMap<String, GeometryShape>,
/// Faces.
pub faces: Vec<[u16; 3]>,
/// Whether geometry is dynamic or not.
pub is_dynamic: bool,
}

impl Geometry {
Expand All @@ -350,7 +369,6 @@ impl Geometry {
base_shape: GeometryShape::empty(),
shapes: HashMap::new(),
faces: Vec::new(),
is_dynamic: false,
}
}

Expand Down Expand Up @@ -385,7 +403,6 @@ impl Geometry {
.triangulate()
.map(|t| [t.x as u16, t.y as u16, t.z as u16])
.collect(),
is_dynamic: false,
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ mod scene;
mod window;

pub use camera::{OrbitControls, Orthographic, Perspective};
pub use factory::{Factory, Geometry, ShadowMap, Texture};
pub use factory::{Factory, Geometry, GeometryShape, ShadowMap, Texture};
pub use input::{Button, KeyAxis, Timer, Input,
KEY_ESCAPE, KEY_SPACE, MOUSE_LEFT, MOUSE_RIGHT,
AXIS_LEFT_RIGHT, AXIS_DOWN_UP};
Expand Down
2 changes: 1 addition & 1 deletion src/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ macro_rules! as_node {
}
}

as_node!(Object, Group, Mesh, Sprite,
as_node!(Object, Group, Mesh, DynamicMesh, Sprite,
AmbientLight, DirectionalLight, HemisphereLight, PointLight);

macro_rules! deref_objects {
Expand Down

0 comments on commit 01c31b1

Please sign in to comment.