examples: apply real texture assets to multi-mesh examples
- meshes/cube: procedural checkerboard -> uv_texture.jpg (8x8 UV grid) - meshes/pbr: floor -> ground.jpeg, bump cube -> cave.jpg + caveNormal.jpg (normal map pre-encoded via sRGB OETF to cancel the GPU sRGB decode) - lights/shadow: ground -> ground.jpeg, cube -> uv_texture.jpg - effects/demo: ground -> ground.jpeg, cube -> uv_texture.jpg - effects/fog: ground -> ground.jpeg (tiled 80x80), cubes -> stonewall.jpg - effects/dof: ground -> ground.jpeg, cubes -> uv_texture.jpg - cameras/culling: shared cube mesh -> uv_texture.jpg - add lib/examples/assets/textures/ (19 assets, 6.5 MB) - document assets + usage in examples READMEs, docs/user/examples.md, docs/user/meshes/materials.md (CARGO_MANIFEST_DIR pattern, sRGB caveat)
This commit is contained in:
+18
-22
@@ -1,4 +1,5 @@
|
||||
//! A lit unit cube that rotates, **textured** with a procedural checkerboard via the diffuse path
|
||||
//! A lit unit cube that rotates, **textured** with the `uv_texture.jpg` asset (an 8×8 UV atlas
|
||||
//! visualization — each labelled cell shows exactly where a face's UVs land) via the diffuse path
|
||||
//! (bind group `@group(2)`).
|
||||
//!
|
||||
//! A 3D mesh with Phong lighting on screen — the library's 3D showcase.
|
||||
@@ -7,7 +8,9 @@
|
||||
//! `add_material_shader`/`add_material_texture` + `create_mesh` + `add_entity`. The mesh
|
||||
//! is declared from a **`Geometry`** (positions, normals, indices). A texture is
|
||||
//! registered by id (`add_texture`) and a textured material bound to it (`add_material_texture`);
|
||||
//! the texture is generated *procedurally* (RGBA 8×8 checkerboard) to stay self-contained, no on-disk asset.
|
||||
//! here the texture is a **file asset** (`assets/textures/uv_texture.jpg`, loaded via
|
||||
//! `Texture::from_file`) — the path is resolved against `CARGO_MANIFEST_DIR` so the example
|
||||
//! works from any working directory.
|
||||
//! The default active camera (`Scene::default`, position (0,0,3), fov 45°) frames the cube, and
|
||||
//! `AppHandler::update` rotates the entity via `set_entity_transform` each frame.
|
||||
use glam::{Quat, Vec3};
|
||||
@@ -17,27 +20,15 @@ use wsg_lib::mesh::cube;
|
||||
use wsg_lib::resources::Texture;
|
||||
use wsg_lib::utils::WsgError;
|
||||
|
||||
/// Texture asset directory, resolved against the crate root so the example works from any CWD.
|
||||
const TEXTURES: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/examples/assets/textures");
|
||||
|
||||
/// Demo handler: rotates the textured cube in `update`.
|
||||
struct Cube {
|
||||
/// Cumulative rotation angle (radians), incremented each frame.
|
||||
angle: f32,
|
||||
}
|
||||
|
||||
/// Generates a *procedural* RGBA 8×8 checkerboard (white/brick), no on-disk asset, to texture the
|
||||
/// cube. Returned as a raw RGBA8 `Vec<u8>`, loadable via `Texture::from_rgba8`.
|
||||
fn checkerboard_rgba() -> Vec<u8> {
|
||||
const SIZE: u32 = 8;
|
||||
let mut rgba = Vec::with_capacity((SIZE * SIZE * 4) as usize);
|
||||
for y in 0..SIZE {
|
||||
for x in 0..SIZE {
|
||||
let even = (x + y) % 2 == 0;
|
||||
let (r, g, b) = if even { (255, 255, 255) } else { (190, 40, 40) };
|
||||
rgba.extend_from_slice(&[r, g, b, 255]);
|
||||
}
|
||||
}
|
||||
rgba
|
||||
}
|
||||
|
||||
impl AppHandler for Cube {
|
||||
fn setup(&mut self, app: &mut wsg_lib::App) {
|
||||
// Phong shader `standard` (carries the frame + object + texture bind groups).
|
||||
@@ -45,17 +36,22 @@ impl AppHandler for Cube {
|
||||
.register_shader("standard", wsg_lib::utils::STANDARD_SHADER_PATH)
|
||||
.unwrap();
|
||||
|
||||
// Builds the checkerboard texture with the Context's device/queue (via `app.context()`), then
|
||||
// Loads the `uv_texture.jpg` asset with the Context's device/queue (via `app.context()`), then
|
||||
// registers it in the scene by id; a textured material is then bound to that id.
|
||||
let (device, queue) = {
|
||||
let ctx = app.context();
|
||||
(ctx.device.clone(), ctx.queue.clone())
|
||||
};
|
||||
let texture =
|
||||
Texture::from_rgba8(&device, &queue, 8, 8, &checkerboard_rgba(), "checker").unwrap();
|
||||
app.scene.add_texture("checker_texture", texture).unwrap();
|
||||
let texture = Texture::from_file(
|
||||
&device,
|
||||
&queue,
|
||||
"uv_atlas",
|
||||
&format!("{TEXTURES}/uv_texture.jpg"),
|
||||
)
|
||||
.unwrap();
|
||||
app.scene.add_texture("uv_texture", texture).unwrap();
|
||||
app.scene
|
||||
.add_material_texture("cube_material", "standard", "checker_texture")
|
||||
.add_material_texture("cube_material", "standard", "uv_texture")
|
||||
.unwrap();
|
||||
|
||||
app.scene
|
||||
|
||||
Reference in New Issue
Block a user