Files
OneCAD-Draft/src/shaders/image.wgsl
T
Hakan SevenandClaude Opus 4.8 a2cb833c83 fix(scene): render raster images at correct position and size (#98)
Match the image shader's uniform layout to the shared 112-byte Uniforms
struct (was 144) and project quad corners via view_proj so texture-quad
draws no longer fail uniform binding validation.

Offset image quad corners by world_offset, matching all other Model-space
geometry, so rasters land in the same local space instead of being shifted
away. Downscale oversized images to the GPU's max texture dimension (8192)
to keep texture creation from failing.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-14 15:06:06 +03:00

61 lines
2.3 KiB
WebGPU Shading Language

// Textured-quad shader for raster images (RasterImage entity).
// Renders a four-vertex quad (two triangles) with a sampled texture.
// ── Bind group 0: shared projection uniforms ─────────────────────────────────
// Must match the shared `Uniforms` struct (scene::pipeline::uniforms, 112 B).
struct Uniforms {
view_proj: mat4x4<f32>,
camera_pos: vec4<f32>,
viewport_size: vec2<f32>,
world_per_pixel: f32,
lwdisplay_enable: f32,
flat_shade: f32,
transparency_enable: f32,
_pad: vec2<f32>,
};
@group(0) @binding(0) var<uniform> u: Uniforms;
// ── Bind group 1: per-image texture + sampler ────────────────────────────────
@group(1) @binding(0) var img_texture: texture_2d<f32>;
@group(1) @binding(1) var img_sampler: sampler;
// Per-image params (fade, clip flag, etc.)
struct ImageParams {
opacity: f32,
draw_depth: f32, // signed (-1,1) draw-order bias; 0 = neutral
_pad1: f32,
_pad2: f32,
};
@group(1) @binding(2) var<uniform> img_params: ImageParams;
// Draw-order depth bias (see wire.wgsl).
const DRAW_ORDER_BIAS: f32 = 0.001;
// ── Vertex stage ──────────────────────────────────────────────────────────────
struct VertIn {
@location(0) pos: vec3<f32>,
@location(1) uv: vec2<f32>,
};
struct VertOut {
@builtin(position) clip_pos: vec4<f32>,
@location(0) uv: vec2<f32>,
};
@vertex
fn vs_main(in: VertIn) -> VertOut {
var out: VertOut;
out.clip_pos = u.view_proj * vec4<f32>(in.pos, 1.0);
out.clip_pos.z = out.clip_pos.z - img_params.draw_depth * DRAW_ORDER_BIAS * out.clip_pos.w;
out.uv = in.uv;
return out;
}
// ── Fragment stage ────────────────────────────────────────────────────────────
@fragment
fn fs_main(in: VertOut) -> @location(0) vec4<f32> {
let col = textureSample(img_texture, img_sampler, in.uv);
return vec4<f32>(col.rgb, col.a * img_params.opacity);
}