// This is a tool that merges the shader code with the shader code from the shader module. struct UniformCommonTools { model_matrix: mat4x4f, view_matrix: mat4x4f, proj_matrix: mat4x4f, camera_x: f32, camera_y: f32, camera_z: f32, camera_target_x: f32, camera_target_y: f32, camera_target_z: f32, camera_up_x: f32, camera_up_y: f32, camera_up_z: f32, // camera_position: vec3f, // camera_front: vec3f, // camera_up: vec3f, } @group(0) @binding(0) var common_tools: UniformCommonTools; const PI:f32 = 3.14159265358979323846264338327950288; const TWO_PI:f32 = 6.28318530717958647692528676655900576; const HALF_PI:f32 = 1.57079632679489661923132169163975144; const LOG2:f32 = 0.693147180559945309417232121458176568; const LOG10:f32 = 2.30258509299404568401799145468436421; @group(1) @binding(0) var color_map_texture: texture_1d; @group(1) @binding(1) var color_map_sampler: sampler; @group(1) @binding(2) var color_map_params: ColorMapParams; struct ColorMapParams { color_count: u32, value_min: f32, value_max: f32, invalid_value: f32 } fn find_idx(ratio: f32) -> f32 { var sum = 0.0; var i = 0.0; let count = f32(color_map_params.color_count - 1); while (ratio > sum) { sum += textureSample(color_map_texture, color_map_sampler, i / count).r; i += 1.0; } return i / count; } fn linear_colormap(value: f32) -> vec4f { let v = clamp((value - color_map_params.value_min) / (color_map_params.value_max - color_map_params.value_min), 0.0, 1.0); let idx = find_idx(v); let c0 = textureSample(color_map_texture, color_map_sampler, idx).rgb; return vec4f(c0, 1.0); } // Common Uniforms // common_tools // model_matrix: mat4, // view_matrix: mat4, // proj_matrix: mat4, // camera_position: vec3, // camera_front: vec3, // camera_up: vec3, // light_position: vec3, // light_color: vec3, // light_intensity: float, // Uniforms @group(2) @binding(0) var params: UniformParams; // Data Buffer @group(2) @binding(1) var data: array; struct UniformParams { origin: vec4f } struct VertexOutput { @builtin(position) position: vec4f, @location(0) r_range: vec4f, @location(1) idx: u32 } @vertex fn vertex( @location(0) position: vec4f, @location(1) r_range: vec4f, // @location(2) idx: u32 ) -> VertexOutput { var out: VertexOutput; // Transform position out.position = vec4(position.xyz, 1.0); out.r_range = r_range; let idx = u32(position.w); out.idx = idx; return out; } fn polar_forward(cartesian: vec3f) -> vec3f { let r = length(cartesian.xy - params.origin.xy); let theta = atan2(cartesian.y, cartesian.x); let z = cartesian.z; return vec3f(r, theta, z); } @fragment fn fragment(input: VertexOutput) -> @location(0) vec4f { // Sample data texture let value = data[input.idx]; let ear = polar_forward(input.position.xyz); // let color = linear_colormap(value); var color = clamp(value / 75.0, 0.0, 1.0); // let r = ear.x; // Valid range // let r_range = input.r_range; // let outside_lower_bound = step(r, r_range.x); // let outside_upper_bound = step(r_range.y, r); // let is_outside = outside_lower_bound + outside_upper_bound; // color.a *= 1.0 - is_outside; return vec4(color,color,color, 1.0); }