#import bevy_pbr::{
mesh_bindings::mesh,
mesh_functions,
forward_io::{Vertex, VertexOutput},
view_transformations::position_world_to_clip,
}
struct GridScalarUniforms {
origin_counts: vec4<f32>,
grid_params: vec4<f32>,
scene_origin: vec4<f32>,
value_params: vec4<f32>,
base_altitude: vec4<f32>,
};
@group(#{MATERIAL_BIND_GROUP}) @binding(0)
var<uniform> grid: GridScalarUniforms;
@group(#{MATERIAL_BIND_GROUP}) @binding(1)
var scalar_texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(2)
var ramp_texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(3)
var ramp_sampler: sampler;
fn normalized_value(value: f32) -> f32 {
let range = grid.value_params.y - grid.value_params.x;
if abs(range) < 1e-6 {
return 0.5;
}
return clamp((value - grid.value_params.x) / range, 0.0, 1.0);
}
@vertex
fn vertex(vertex: Vertex) -> VertexOutput {
var out: VertexOutput;
let world_from_local = mesh_functions::get_world_from_local(vertex.instance_index);
out.world_position = mesh_functions::mesh_position_local_to_world(
world_from_local,
vec4<f32>(vertex.position, 1.0),
);
out.position = position_world_to_clip(out.world_position.xyz);
out.world_normal = vec3<f32>(0.0, 0.0, 1.0);
out.uv = vertex.uv;
return out;
}
@fragment
fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
let dims = textureDimensions(scalar_texture);
let cols = max(i32(dims.x), 1);
let rows = max(i32(dims.y), 1);
let col = clamp(i32(floor(in.uv.x * f32(cols))), 0, cols - 1);
let row = clamp(i32(floor(in.uv.y * f32(rows))), 0, rows - 1);
let value = textureLoad(scalar_texture, vec2<i32>(col, row), 0).x;
if isNan(value) {
discard;
}
if grid.value_params.w > 0.5 && abs(value - grid.value_params.z) < 1e-6 {
discard;
}
let t = normalized_value(value);
let ramp = textureSample(ramp_texture, ramp_sampler, vec2<f32>(t, 0.5));
return vec4<f32>(ramp.rgb, ramp.a * grid.grid_params.w);
}