Skip to content

Commit de46c36

Browse files
committed
add axis to shapes example
1 parent 7f6714a commit de46c36

File tree

1 file changed

+72
-5
lines changed
  • wasm/example-unavi-shapes/src

1 file changed

+72
-5
lines changed

wasm/example-unavi-shapes/src/lib.rs

+72-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1+
use std::f32::consts::FRAC_PI_2;
2+
13
use bindings::{
24
exports::wired::script::types::{Guest, GuestScript},
35
unavi::{
46
scene::api::{Root, Scene},
57
shapes::api::{Cuboid, Cylinder, Rectangle, Sphere},
68
},
7-
wired::math::types::{Transform, Vec2, Vec3},
9+
wired::{
10+
math::types::{Quat, Transform, Vec2, Vec3},
11+
scene::{
12+
material::{Color, Material},
13+
node::Node,
14+
},
15+
},
816
};
917

1018
#[allow(warnings)]
@@ -18,31 +26,46 @@ impl GuestScript for Script {
1826
let scene = Scene::new();
1927

2028
{
29+
let translation = Vec3::new(3.0, 0.0, 0.0);
30+
spawn_axis(translation);
31+
2132
let cuboid = Cuboid::new(Vec3::new(1.0, 0.5, 1.5)).to_physics_node();
22-
cuboid.set_transform(Transform::from_translation(Vec3::new(3.0, 0.0, 0.0)));
33+
cuboid.set_transform(Transform::from_translation(translation));
2334
scene.add_node(&cuboid);
2435
}
2536

2637
{
38+
let translation = Vec3::new(1.5, 0.0, 2.0);
39+
spawn_axis(translation);
40+
2741
let sphere = Sphere::new_ico(0.5).to_physics_node();
28-
sphere.set_transform(Transform::from_translation(Vec3::new(1.5, 0.0, 2.0)));
42+
sphere.set_transform(Transform::from_translation(translation));
2943
scene.add_node(&sphere);
3044
}
3145

3246
{
47+
let translation = Vec3::new(1.5, 0.0, 0.0);
48+
spawn_axis(translation);
49+
3350
let sphere = Sphere::new_uv(0.5).to_physics_node();
34-
sphere.set_transform(Transform::from_translation(Vec3::new(1.5, 0.0, 0.0)));
51+
sphere.set_transform(Transform::from_translation(translation));
3552
scene.add_node(&sphere);
3653
}
3754

3855
{
56+
let translation = Vec3::default();
57+
spawn_axis(translation);
58+
3959
let cylinder = Cylinder::new(0.5, 1.0).to_physics_node();
4060
scene.add_node(&cylinder);
4161
}
4262

4363
{
64+
let translation = Vec3::new(-1.5, 0.0, 0.0);
65+
spawn_axis(translation);
66+
4467
let rectangle = Rectangle::new(Vec2::splat(1.0)).to_physics_node();
45-
rectangle.set_transform(Transform::from_translation(Vec3::new(-1.5, 0.0, 0.0)));
68+
rectangle.set_transform(Transform::from_translation(translation));
4669
scene.add_node(&rectangle);
4770
}
4871

@@ -54,6 +77,50 @@ impl GuestScript for Script {
5477
fn update(&self, _delta: f32) {}
5578
}
5679

80+
fn spawn_axis(translation: Vec3) {
81+
let shape = Cylinder::new(0.01, 2.0);
82+
shape.set_resolution(8);
83+
84+
let root = Node::new();
85+
root.set_transform(Transform::from_translation(translation));
86+
87+
let red = Material::new();
88+
red.set_color(Color {
89+
r: 1.0,
90+
g: 0.1,
91+
b: 0.1,
92+
a: 0.9,
93+
});
94+
let green = Material::new();
95+
green.set_color(Color {
96+
r: 0.1,
97+
g: 1.0,
98+
b: 0.1,
99+
a: 0.9,
100+
});
101+
let blue = Material::new();
102+
blue.set_color(Color {
103+
r: 0.1,
104+
g: 0.1,
105+
b: 1.0,
106+
a: 0.9,
107+
});
108+
109+
let x = shape.to_node();
110+
root.add_child(&x);
111+
x.mesh().unwrap().list_primitives()[0].set_material(Some(&red));
112+
x.set_transform(Transform::from_rotation(Quat::from_rotation_z(FRAC_PI_2)));
113+
114+
let y = shape.to_node();
115+
root.add_child(&y);
116+
y.mesh().unwrap().list_primitives()[0].set_material(Some(&green));
117+
118+
let z = shape.to_node();
119+
root.add_child(&z);
120+
z.mesh().unwrap().list_primitives()[0].set_material(Some(&blue));
121+
z.set_transform(Transform::from_rotation(Quat::from_rotation_x(FRAC_PI_2)));
122+
}
123+
57124
struct Types;
58125

59126
impl Guest for Types {

0 commit comments

Comments
 (0)