Skip to content

Commit

Permalink
Merge pull request #255 from asny/crate-split
Browse files Browse the repository at this point in the history
  • Loading branch information
asny authored May 30, 2022
2 parents 42da2b5 + 2835e54 commit e9e2eb5
Show file tree
Hide file tree
Showing 89 changed files with 998 additions and 3,234 deletions.
17 changes: 7 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,24 @@ rustdoc-args = ["--cfg", "docsrs"]
targets = ["x86_64-apple-darwin", "x86_64-unknown-linux-gnu", "x86_64-pc-windows-msvc", "wasm32-unknown-unknown"]

[features]
default = ["glutin-window", "canvas", "egui-gui", "obj-io", "gltf-io", "image-io", "reqwest"]
glutin-window = ["glutin"] # Default window for desktop (only available when NOT building for the wasm32 architecture)
canvas = [] # Default window for web (only available when building for the wasm32 architecture)
default = ["window", "egui-gui", "image-io", "obj-io", "gltf-io", "three-d-asset/vol"]
window = ["glutin"] # Window module
egui-gui = ["egui"] # Additional GUI features
image-io = ["image"] # Additional image functionality, for example loading an image to a texture
obj-io = ["wavefront_obj", "image-io"]
gltf-io = ["gltf", "image-io"]
image-io = ["three-d-asset/http", "three-d-asset/hdr", "three-d-asset/tiff", "three-d-asset/tga", "three-d-asset/gif", "three-d-asset/bmp"] # Additional image functionality, for example loading an image to a texture
obj-io = ["three-d-asset/obj", "three-d-asset/http"]
gltf-io = ["three-d-asset/gltf", "three-d-asset/http"]

[dependencies]
glow = "0.11"
cgmath = "0.18"
three-d-asset = "0.1"
half = {version="1.8", features=["std", "num-traits", "zerocopy", "serde"]}
thiserror = "1.0"
reqwest = { version = "0.11", optional = true }
gltf = { version = "1.0.0", features = ["utils"], optional = true }
wavefront_obj = { version = "10.0", optional = true }
image = { version = "0.23", optional = true, default-features = false, features = ["gif", "jpeg", "ico", "png", "pnm", "tga", "tiff", "webp", "bmp", "hdr", "dxt", "dds", "farbfeld"]}
egui = { version = "0.13", optional = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
glutin = { version = "0.28", optional = true }
image = { version = "0.23", optional = true, default-features = false, features = ["png"]} # Used for automatically generate screenshots

[target.'cfg(target_arch = "wasm32")'.dependencies]
js-sys = "0.3"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ The crate consist of three main modules for drawing, `context`, `core` and `rend
| [`context`](https://docs.rs/three-d/0/three_d/context/) | Low-level rendering module - requires a solid understanding of graphics concepts. Gives you complete control over both setup and rendering.
| [`core`](https://docs.rs/three-d/0/three_d/core/) | Mid-level rendering module - requires at least some knowledge about graphics concepts. Use this if you want to write your own shaders and but don't want to spend time on setup and error handling. Can be combined with low-level functionality in the `context` module.
| [`renderer`](https://docs.rs/three-d/0/three_d/renderer/) | High-level rendering module - requires no knowledge about graphics concepts. Use this if you just want to draw something on the screen. Features include methods for rendering different types of standard objects with different types of shading. Can be combined seamlessly with the mid-level features in the `core` module as well as functionality in the `context` module. |
| [`io`](https://docs.rs/three-d/0/three_d/io/) | Contains functionality to load any type of asset runtime on both desktop and web as well as parsers for different image and 3D model formats. Also includes functionality to save data which is limited to desktop.
| [`window`](https://docs.rs/three-d/0/three_d/window/) | Contains functionality for creating a window on both cross-platform desktop (requires the `"glutin-window"` feature) and web (requires the `"canvas"` feature). Also contain render loop, event handling and camera control functionality. Can be replaced by anything that provides an OpenGL or WebGL2 graphics context, for example [eframe](https://github.com/emilk/egui/tree/master/eframe) as shown in [this](https://github.com/emilk/egui/blob/master/eframe/examples/custom_3d_three-d.rs) example.
| [`io`](https://docs.rs/three-d/0/three_d/io/) | *[Deprecated: Use the [three-d-asset](https://github.com/asny/three-d-asset) crate instead.]* Contains functionality to load any type of asset runtime on both native and web as well as parsers for different image and 3D model formats. Also includes functionality to save data which is limited to desktop.
| [`window`](https://docs.rs/three-d/0/three_d/window/) (requires the `"window"` feature) | Contains functionality for creating a window on both cross-platform native and web. Also contain render loop, event handling and camera control functionality. Can be replaced by anything that provides an OpenGL or WebGL2 graphics context, for example [eframe](https://github.com/emilk/egui/tree/master/eframe) as shown in [this](https://github.com/emilk/egui/blob/master/eframe/examples/custom_3d_three-d.rs) example.

### [Examples](https://github.com/asny/three-d/tree/master/examples)

Expand Down
1 change: 1 addition & 0 deletions examples/environment/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ crate-type = ["cdylib"]

[dependencies]
three-d = { path = "../../" }
three-d-asset = "0.1"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio = { version = "1.16", features = ["macros", "rt-multi-thread"] }
Expand Down
20 changes: 10 additions & 10 deletions examples/environment/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,27 @@ pub async fn run() {
.unwrap();
let mut control = OrbitControl::new(*camera.target(), 1.0, 100.0);

let mut loaded = Loader::load_async(
let mut loaded = three_d_asset::io::load_async(
&["examples/assets/chinese_garden_4k.hdr"], // Source: https://polyhaven.com/
)
.await
.unwrap();
let skybox =
Skybox::new_from_equirectangular(&context, &loaded.hdr_image("chinese_garden_4k").unwrap())
.unwrap();
let skybox = Skybox::new_from_equirectangular(
&context,
&loaded.deserialize("chinese_garden_4k").unwrap(),
)
.unwrap();
let light =
AmbientLight::new_with_environment(&context, 1.0, Color::WHITE, skybox.texture()).unwrap();

let mut model = Model::new_with_material(
&context,
&CpuMesh::sphere(32),
let mut model = Gm::new(
Mesh::new(&context, &CpuMesh::sphere(32)).unwrap(),
PhysicalMaterial {
roughness: 0.2,
metallic: 0.8,
..Default::default()
},
)
.unwrap();
);
let mut gui = three_d::GUI::new(&context).unwrap();

// main loop
Expand Down Expand Up @@ -87,7 +87,7 @@ pub async fn run() {
.screen()
.clear(ClearState::color_and_depth(0.5, 0.5, 0.5, 1.0, 1.0))
.unwrap()
.render(&camera, &[&skybox as &dyn Object, &model], &[&light])
.render(&camera, &[&skybox, &model], &[&light])
.unwrap()
.write(|| gui.render())
.unwrap();
Expand Down
1 change: 1 addition & 0 deletions examples/fog/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ crate-type = ["cdylib"]

[dependencies]
three-d = { path = "../../" }
three-d-asset = "0.1"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio = { version = "1.16", features = ["macros", "rt-multi-thread"] }
Expand Down
28 changes: 15 additions & 13 deletions examples/fog/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@ pub async fn run() {
.unwrap();
let mut control = FlyControl::new(0.05);

let mut loaded =
Loader::load_async(&["examples/assets/suzanne.obj", "examples/assets/suzanne.mtl"])
.await
.unwrap();
let mut loaded = three_d_asset::io::load_async(&[
"examples/assets/suzanne.obj",
"examples/assets/suzanne.mtl",
])
.await
.unwrap();

let (meshes, materials) = loaded.obj("suzanne.obj").unwrap();
let mut monkey_material = PhysicalMaterial::new(&context, &materials[0]).unwrap();
monkey_material.render_states.cull = Cull::Back;
let monkey = Model::new_with_material(&context, &meshes[0], monkey_material);
let mut monkey =
Model::<PhysicalMaterial>::new(&context, &loaded.deserialize("suzanne.obj").unwrap())
.unwrap();
monkey
.iter_mut()
.for_each(|m| m.material.render_states.cull = Cull::Back);

let ambient = AmbientLight::new(&context, 0.4, Color::WHITE).unwrap();
let directional =
Expand Down Expand Up @@ -75,7 +79,7 @@ pub async fn run() {
if change && fog_enabled {
depth_texture = Some(
pipeline
.depth_pass_texture(&camera, &[monkey.as_ref().unwrap()])
.depth_pass_texture(&camera, &monkey.to_objects())
.unwrap(),
);
}
Expand All @@ -84,11 +88,9 @@ pub async fn run() {
.screen()
.clear(ClearState::default())
.unwrap()
.render(&camera, &monkey.to_objects(), &[&ambient, &directional])
.unwrap()
.write(|| {
monkey
.as_ref()
.unwrap()
.render(&camera, &[&ambient, &directional])?;
if fog_enabled {
if let Some(ref depth_texture) = depth_texture {
fog_effect.apply(
Expand Down
1 change: 1 addition & 0 deletions examples/forest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ crate-type = ["cdylib"]

[dependencies]
three-d = { path = "../../" }
three-d-asset = "0.1"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio = { version = "1.16", features = ["macros", "rt-multi-thread"] }
Expand Down
79 changes: 35 additions & 44 deletions examples/forest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub async fn run() {
.unwrap();
let mut control = FlyControl::new(0.1);

let mut loaded = Loader::load_async(&[
let mut loaded = three_d_asset::io::load_async(&[
"examples/assets/Gledista_Triacanthos.obj",
"examples/assets/Gledista_Triacanthos.mtl",
"examples/assets/maps/gleditsia_triacanthos_flowers_color.jpg",
Expand All @@ -42,26 +42,15 @@ pub async fn run() {
.await
.unwrap();
// Tree
let (mut meshes, materials) = loaded.obj(".obj").unwrap();
let mut models = Vec::new();
for mut mesh in meshes.drain(..) {
mesh.compute_normals();
let mut model = Model::new_with_material(
&context,
&mesh,
PhysicalMaterial::new(
&context,
&materials
.iter()
.find(|m| Some(&m.name) == mesh.material_name.as_ref())
.unwrap(),
)
.unwrap(),
)
.unwrap();
model.material.render_states.cull = Cull::Back;
models.push(model);
}
let mut cpu_model: CpuModel = loaded.deserialize(".obj").unwrap();
cpu_model
.geometries
.iter_mut()
.for_each(|g| g.compute_normals());
let mut model = Model::<PhysicalMaterial>::new(&context, &cpu_model).unwrap();
model
.iter_mut()
.for_each(|m| m.material.render_states.cull = Cull::Back);

// Lights
let ambient = AmbientLight::new(&context, 0.3, Color::WHITE).unwrap();
Expand All @@ -70,7 +59,7 @@ pub async fn run() {

// Imposters
let mut aabb = AxisAlignedBoundingBox::EMPTY;
models.iter().for_each(|m| {
model.iter().for_each(|m| {
aabb.expand_with_aabb(&m.aabb());
});
let size = aabb.size();
Expand All @@ -87,38 +76,39 @@ pub async fn run() {
let imposters = Imposters::new(
&context,
&positions,
&models.iter().map(|m| m as &dyn Object).collect::<Vec<_>>(),
&model.to_objects(),
&[&ambient, &directional],
256,
)
.unwrap();

// Plane
let mut plane = Model::new_with_material(
&context,
&CpuMesh {
positions: Positions::F32(vec![
vec3(-10000.0, 0.0, 10000.0),
vec3(10000.0, 0.0, 10000.0),
vec3(0.0, 0.0, -10000.0),
]),
normals: Some(vec![
vec3(0.0, 1.0, 0.0),
vec3(0.0, 1.0, 0.0),
vec3(0.0, 1.0, 0.0),
]),
..Default::default()
},
let mut plane = Gm::new(
Mesh::new(
&context,
&CpuMesh {
positions: Positions::F32(vec![
vec3(-10000.0, 0.0, 10000.0),
vec3(10000.0, 0.0, 10000.0),
vec3(0.0, 0.0, -10000.0),
]),
normals: Some(vec![
vec3(0.0, 1.0, 0.0),
vec3(0.0, 1.0, 0.0),
vec3(0.0, 1.0, 0.0),
]),
..Default::default()
},
)
.unwrap(),
PhysicalMaterial {
albedo: Color::new_opaque(128, 200, 70),
metallic: 0.0,
roughness: 1.0,
..Default::default()
},
)
.unwrap();
);
plane.material.render_states.cull = Cull::Back;
models.push(plane);

// main loop
window
Expand All @@ -131,13 +121,14 @@ pub async fn run() {
.unwrap();

if redraw {
let mut models = models.iter().map(|m| m as &dyn Object).collect::<Vec<_>>();
models.push(&imposters);
let mut objects = model.to_objects();
objects.push(&imposters);
objects.push(&plane);
frame_input
.screen()
.clear(ClearState::color_and_depth(0.8, 0.8, 0.8, 1.0, 1.0))
.unwrap()
.render(&camera, &models, &[&ambient, &directional])
.render(&camera, &objects, &[&ambient, &directional])
.unwrap();
}

Expand Down
2 changes: 1 addition & 1 deletion examples/headless/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
three-d = { path = "../../", default-features = false, features=["glutin-window"] }
three-d = { path = "../../", default-features = false, features=["window"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
log = "0.4"
Expand Down
53 changes: 31 additions & 22 deletions examples/headless/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,26 @@ fn main() {
.unwrap();

// Create the scene - a single colored triangle
let mut model = Model::new(
&context,
&CpuMesh {
positions: Positions::F32(vec![
vec3(0.5, -0.5, 0.0), // bottom right
vec3(-0.5, -0.5, 0.0), // bottom left
vec3(0.0, 0.5, 0.0), // top
]),
colors: Some(vec![
Color::new(255, 0, 0, 255), // bottom right
Color::new(0, 255, 0, 255), // bottom left
Color::new(0, 0, 255, 255), // top
]),
..Default::default()
},
)
.unwrap();
let mut model = Gm::new(
Mesh::new(
&context,
&CpuMesh {
positions: Positions::F32(vec![
vec3(0.5, -0.5, 0.0), // bottom right
vec3(-0.5, -0.5, 0.0), // bottom left
vec3(0.0, 0.5, 0.0), // top
]),
colors: Some(vec![
Color::new(255, 0, 0, 255), // bottom right
Color::new(0, 255, 0, 255), // bottom left
Color::new(0, 0, 255, 255), // top
]),
..Default::default()
},
)
.unwrap(),
ColorMaterial::default(),
);

// Create a color texture to render into
let mut texture = Texture2D::new_empty::<[u8; 4]>(
Expand Down Expand Up @@ -86,11 +89,17 @@ fn main() {
.unwrap();

// Save the rendered image
Saver::save_pixels(
format!("headless-{}.png", frame_index),
&pixels,
texture.width(),
texture.height(),
use three_d_asset::io::Serialize;

three_d_asset::io::save(
&CpuTexture {
data: TextureData::RgbaU8(pixels),
width: texture.width(),
height: texture.height(),
..Default::default()
}
.serialize(format!("headless-{}.png", frame_index))
.unwrap(),
)
.unwrap();
}
Expand Down
1 change: 1 addition & 0 deletions examples/image/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ crate-type = ["cdylib"]

[dependencies]
three-d = { path = "../../" }
three-d-asset = "0.1"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio = { version = "1.16", features = ["macros", "rt-multi-thread"] }
Expand Down
4 changes: 2 additions & 2 deletions examples/image/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ pub async fn run() {
let context = window.gl().unwrap();
let mut image_effect = ImageEffect::new(&context, include_str!("shader.frag")).unwrap();

let mut loaded = Loader::load_async(
let mut loaded = three_d_asset::io::load_async(
&["examples/assets/syferfontein_18d_clear_4k.hdr"], // Source: https://polyhaven.com/
)
.await
.unwrap();
let image = Texture2D::new(&context, &loaded.hdr_image("").unwrap()).unwrap();
let image = Texture2D::new(&context, &loaded.deserialize("").unwrap()).unwrap();

let mut gui = GUI::new(&context).unwrap();

Expand Down
Loading

0 comments on commit e9e2eb5

Please sign in to comment.