2023-08-30 01:20:58 +00:00
|
|
|
struct SkyOutput {
|
|
|
|
@builtin(position) position: vec4<f32>,
|
2023-08-30 22:40:16 +00:00
|
|
|
@location(0) sampledir: vec3<f32>,
|
2023-08-30 01:20:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Data {
|
|
|
|
// from camera to screen
|
|
|
|
proj: mat4x4<f32>,
|
|
|
|
// from screen to camera
|
|
|
|
proj_inv: mat4x4<f32>,
|
|
|
|
// from world to camera
|
|
|
|
view: mat4x4<f32>,
|
|
|
|
// camera position
|
|
|
|
cam_pos: vec4<f32>,
|
|
|
|
};
|
|
|
|
@group(0)
|
|
|
|
@binding(0)
|
|
|
|
var<uniform> r_data: Data;
|
|
|
|
|
|
|
|
@vertex
|
|
|
|
fn vs_sky(@builtin(vertex_index) vertex_index: u32) -> SkyOutput {
|
|
|
|
// hacky way to draw a large triangle
|
|
|
|
let tmp1 = i32(vertex_index) / 2;
|
|
|
|
let tmp2 = i32(vertex_index) & 1;
|
|
|
|
let pos = vec4<f32>(
|
|
|
|
f32(tmp1) * 4.0 - 1.0,
|
|
|
|
f32(tmp2) * 4.0 - 1.0,
|
|
|
|
1.0,
|
|
|
|
1.0
|
|
|
|
);
|
|
|
|
|
|
|
|
// transposition = inversion for this orthonormal matrix
|
|
|
|
let inv_model_view = transpose(mat3x3<f32>(r_data.view[0].xyz, r_data.view[1].xyz, r_data.view[2].xyz));
|
|
|
|
let unprojected = r_data.proj_inv * pos;
|
|
|
|
|
|
|
|
var result: SkyOutput;
|
2023-08-30 22:40:16 +00:00
|
|
|
result.sampledir = inv_model_view * unprojected.xyz;
|
2023-08-30 01:20:58 +00:00
|
|
|
result.position = pos;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-08-30 22:18:05 +00:00
|
|
|
struct GroundOutput {
|
|
|
|
@builtin(position) position: vec4<f32>,
|
|
|
|
@location(4) pos: vec3<f32>,
|
|
|
|
};
|
|
|
|
|
|
|
|
@vertex
|
|
|
|
fn vs_ground(@builtin(vertex_index) vertex_index: u32) -> GroundOutput {
|
|
|
|
// hacky way to draw two triangles that make a square
|
2023-08-31 09:21:39 +00:00
|
|
|
let tmp1 = i32(vertex_index)/2-i32(vertex_index)/3;
|
|
|
|
let tmp2 = i32(vertex_index)&1;
|
2023-08-30 22:18:05 +00:00
|
|
|
let pos = vec3<f32>(
|
|
|
|
f32(tmp1) * 2.0 - 1.0,
|
|
|
|
0.0,
|
|
|
|
f32(tmp2) * 2.0 - 1.0
|
2023-09-05 07:18:07 +00:00
|
|
|
) * 160.0;
|
2023-08-30 22:18:05 +00:00
|
|
|
|
|
|
|
var result: GroundOutput;
|
|
|
|
result.pos = pos;
|
|
|
|
result.position = r_data.proj * r_data.view * vec4<f32>(pos, 1.0);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-08-30 01:20:58 +00:00
|
|
|
struct EntityOutput {
|
|
|
|
@builtin(position) position: vec4<f32>,
|
2023-09-06 21:11:56 +00:00
|
|
|
@location(1) texture: vec2<f32>,
|
|
|
|
@location(2) normal: vec3<f32>,
|
2023-08-30 01:20:58 +00:00
|
|
|
@location(3) view: vec3<f32>,
|
|
|
|
};
|
|
|
|
|
2023-09-05 19:22:38 +00:00
|
|
|
@group(1)
|
|
|
|
@binding(0)
|
2023-09-05 20:05:40 +00:00
|
|
|
var<uniform> r_EntityTransform: mat4x4<f32>;
|
2023-09-01 21:59:42 +00:00
|
|
|
|
2023-08-30 01:20:58 +00:00
|
|
|
@vertex
|
|
|
|
fn vs_entity(
|
|
|
|
@location(0) pos: vec3<f32>,
|
2023-09-06 21:11:56 +00:00
|
|
|
@location(1) texture: vec2<f32>,
|
|
|
|
@location(2) normal: vec3<f32>,
|
2023-08-30 01:20:58 +00:00
|
|
|
) -> EntityOutput {
|
2023-09-05 20:05:40 +00:00
|
|
|
var position: vec4<f32> = r_EntityTransform * vec4<f32>(pos, 1.0);
|
2023-08-30 01:20:58 +00:00
|
|
|
var result: EntityOutput;
|
2023-09-05 20:05:40 +00:00
|
|
|
result.normal = (r_EntityTransform * vec4<f32>(normal, 0.0)).xyz;
|
2023-09-06 21:11:56 +00:00
|
|
|
result.texture=texture;
|
2023-09-05 20:05:40 +00:00
|
|
|
result.view = position.xyz - r_data.cam_pos.xyz;
|
|
|
|
result.position = r_data.proj * r_data.view * position;
|
2023-08-30 01:20:58 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@group(0)
|
|
|
|
@binding(1)
|
|
|
|
var r_texture: texture_cube<f32>;
|
|
|
|
@group(0)
|
|
|
|
@binding(2)
|
|
|
|
var r_sampler: sampler;
|
|
|
|
|
|
|
|
@fragment
|
|
|
|
fn fs_sky(vertex: SkyOutput) -> @location(0) vec4<f32> {
|
2023-08-30 22:40:16 +00:00
|
|
|
return textureSample(r_texture, r_sampler, vertex.sampledir);
|
2023-08-30 01:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@fragment
|
|
|
|
fn fs_entity(vertex: EntityOutput) -> @location(0) vec4<f32> {
|
|
|
|
let incident = normalize(vertex.view);
|
|
|
|
let normal = normalize(vertex.normal);
|
2023-09-06 21:11:56 +00:00
|
|
|
let d = dot(normal, incident);
|
|
|
|
let reflected = incident - 2.0 * d * normal;
|
2023-08-30 01:20:58 +00:00
|
|
|
|
2023-09-06 21:11:56 +00:00
|
|
|
let dir = vec3<f32>(-1.0)+2.0*vec3<f32>(vertex.texture.x,0.0,vertex.texture.y);
|
|
|
|
let texture_color = textureSample(r_texture, r_sampler, dir).rgb;
|
2023-08-30 01:20:58 +00:00
|
|
|
let reflected_color = textureSample(r_texture, r_sampler, reflected).rgb;
|
2023-09-06 21:11:56 +00:00
|
|
|
return vec4<f32>(mix(vec3<f32>(0.1) + 0.5 * reflected_color,texture_color,abs(d)), 1.0);
|
2023-08-30 01:20:58 +00:00
|
|
|
}
|
2023-08-30 22:18:05 +00:00
|
|
|
|
2023-09-01 07:02:50 +00:00
|
|
|
fn modulo_euclidean (a: f32, b: f32) -> f32 {
|
|
|
|
var m = a % b;
|
|
|
|
if (m < 0.0) {
|
|
|
|
if (b < 0.0) {
|
|
|
|
m -= b;
|
|
|
|
} else {
|
|
|
|
m += b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2023-08-30 22:18:05 +00:00
|
|
|
@fragment
|
|
|
|
fn fs_ground(vertex: GroundOutput) -> @location(0) vec4<f32> {
|
2023-09-01 07:02:50 +00:00
|
|
|
let dir = vec3<f32>(-1.0)+vec3<f32>(modulo_euclidean(vertex.pos.x/16.,1.0),0.0,modulo_euclidean(vertex.pos.z/16.,1.0))*2.0;
|
2023-08-30 22:18:05 +00:00
|
|
|
return vec4<f32>(textureSample(r_texture, r_sampler, dir).rgb, 1.0);
|
|
|
|
}
|