forked from StrafesNET/strafe-client
Compare commits
No commits in common. "depth-fudge" and "master" have entirely different histories.
depth-fudg
...
master
134
src/main.rs
134
src/main.rs
@ -20,16 +20,12 @@ struct Entity {
|
||||
//temp?
|
||||
struct ModelData {
|
||||
transform: glam::Mat4,
|
||||
transform_depth: glam::Mat4,
|
||||
use_depth: glam::Vec4,
|
||||
vertex_buf: wgpu::Buffer,
|
||||
entities: Vec<Entity>,
|
||||
}
|
||||
|
||||
struct Model {
|
||||
transform: glam::Mat4,
|
||||
transform_depth: glam::Mat4,
|
||||
use_depth: glam::Vec4,
|
||||
vertex_buf: wgpu::Buffer,
|
||||
entities: Vec<Entity>,
|
||||
bind_group: wgpu::BindGroup,
|
||||
@ -130,15 +126,11 @@ pub struct Skybox {
|
||||
sky_pipeline: wgpu::RenderPipeline,
|
||||
entity_pipeline: wgpu::RenderPipeline,
|
||||
ground_pipeline: wgpu::RenderPipeline,
|
||||
special_teapot: Model,
|
||||
checkered_pipeline: wgpu::RenderPipeline,
|
||||
depth_overwrite_pipeline: wgpu::RenderPipeline,
|
||||
main_bind_group: wgpu::BindGroup,
|
||||
camera_buf: wgpu::Buffer,
|
||||
models: Vec<Model>,
|
||||
depth_view: wgpu::TextureView,
|
||||
staging_belt: wgpu::util::StagingBelt,
|
||||
start_time: Instant,
|
||||
}
|
||||
|
||||
impl Skybox {
|
||||
@ -167,18 +159,9 @@ impl Skybox {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_model_uniform_data(model:&Model) -> [f32; 4*4*2+4] {
|
||||
let mut raw = [0f32; 4*4*2+4];
|
||||
raw[0..16].copy_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&model.transform)[..]);
|
||||
raw[16..32].copy_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&model.transform_depth)[..]);
|
||||
raw[32..36].copy_from_slice(AsRef::<[f32; 4]>::as_ref(&model.use_depth));
|
||||
raw
|
||||
}
|
||||
fn get_modeldata_uniform_data(model:&ModelData) -> [f32; 4*4*2+4] {
|
||||
let mut raw = [0f32; 4*4*2+4];
|
||||
raw[0..16].copy_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&model.transform)[..]);
|
||||
raw[16..32].copy_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&model.transform_depth)[..]);
|
||||
raw[32..36].copy_from_slice(AsRef::<[f32; 4]>::as_ref(&model.use_depth));
|
||||
fn get_transform_uniform_data(transform:&glam::Mat4) -> [f32; 4*4] {
|
||||
let mut raw = [0f32; 4*4];
|
||||
raw[0..16].copy_from_slice(&AsRef::<[f32; 4*4]>::as_ref(transform)[..]);
|
||||
raw
|
||||
}
|
||||
|
||||
@ -226,8 +209,6 @@ fn add_obj(device:&wgpu::Device,modeldatas:& mut Vec<ModelData>,source:&[u8]){
|
||||
});
|
||||
modeldatas.push(ModelData {
|
||||
transform: glam::Mat4::default(),
|
||||
transform_depth: glam::Mat4::default(),
|
||||
use_depth: glam::Vec4::ZERO,
|
||||
vertex_buf,
|
||||
entities,
|
||||
})
|
||||
@ -253,13 +234,7 @@ impl strafe_client::framework::Example for Skybox {
|
||||
add_obj(device,& mut modeldatas,include_bytes!("../models/teapot.obj"));
|
||||
println!("models.len = {:?}", modeldatas.len());
|
||||
modeldatas[1].transform=glam::Mat4::from_translation(glam::vec3(10.,5.,10.));
|
||||
|
||||
let proj1 = glam::Mat4::orthographic_rh(-20.0, 20.0, -20.0, 20.0, -20.0, 20.0);
|
||||
let model0 = glam::Mat4::from_translation(glam::vec3(-10.,5.,10.)) * glam::Mat4::from_scale(glam::vec3(10.0,10.0,10.0));
|
||||
|
||||
modeldatas[2].transform=model0;
|
||||
modeldatas[2].transform_depth=proj1;// * view1_inv
|
||||
modeldatas[2].use_depth=glam::Vec4::Z;
|
||||
modeldatas[2].transform=glam::Mat4::from_translation(glam::vec3(-10.,5.,10.));
|
||||
|
||||
let main_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: None,
|
||||
@ -429,60 +404,6 @@ impl strafe_client::framework::Example for Skybox {
|
||||
multisample: wgpu::MultisampleState::default(),
|
||||
multiview: None,
|
||||
});
|
||||
let checkered_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("Checkered"),
|
||||
layout: Some(&pipeline_layout),
|
||||
vertex: wgpu::VertexState {
|
||||
module: &shader,
|
||||
entry_point: "vs_square",
|
||||
buffers: &[],
|
||||
},
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
module: &shader,
|
||||
entry_point: "fs_checkered",
|
||||
targets: &[Some(config.view_formats[0].into())],
|
||||
}),
|
||||
primitive: wgpu::PrimitiveState {
|
||||
front_face: wgpu::FrontFace::Cw,
|
||||
..Default::default()
|
||||
},
|
||||
depth_stencil: Some(wgpu::DepthStencilState {
|
||||
format: Self::DEPTH_FORMAT,
|
||||
depth_write_enabled: false,
|
||||
depth_compare: wgpu::CompareFunction::Always,
|
||||
stencil: wgpu::StencilState::default(),
|
||||
bias: wgpu::DepthBiasState::default(),
|
||||
}),
|
||||
multisample: wgpu::MultisampleState::default(),
|
||||
multiview: None,
|
||||
});
|
||||
let depth_overwrite_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("Overwrite"),
|
||||
layout: Some(&pipeline_layout),
|
||||
vertex: wgpu::VertexState {
|
||||
module: &shader,
|
||||
entry_point: "vs_square",
|
||||
buffers: &[],
|
||||
},
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
module: &shader,
|
||||
entry_point: "fs_overwrite",
|
||||
targets: &[Some(config.view_formats[0].into())],
|
||||
}),
|
||||
primitive: wgpu::PrimitiveState {
|
||||
front_face: wgpu::FrontFace::Cw,
|
||||
..Default::default()
|
||||
},
|
||||
depth_stencil: Some(wgpu::DepthStencilState {
|
||||
format: Self::DEPTH_FORMAT,
|
||||
depth_write_enabled: true,
|
||||
depth_compare: wgpu::CompareFunction::Always,
|
||||
stencil: wgpu::StencilState::default(),
|
||||
bias: wgpu::DepthBiasState::default(),
|
||||
}),
|
||||
multisample: wgpu::MultisampleState::default(),
|
||||
multiview: None,
|
||||
});
|
||||
|
||||
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
|
||||
label: None,
|
||||
@ -589,7 +510,7 @@ impl strafe_client::framework::Example for Skybox {
|
||||
//drain the modeldata vec so entities can be /moved/ to models.entities
|
||||
let mut models = Vec::<Model>::with_capacity(modeldatas.len());
|
||||
for (i,modeldata) in modeldatas.drain(..).enumerate() {
|
||||
let model_uniforms = get_modeldata_uniform_data(&modeldata);
|
||||
let model_uniforms = get_transform_uniform_data(&modeldata.transform);
|
||||
let model_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some(format!("Model{}",i).as_str()),
|
||||
contents: bytemuck::cast_slice(&model_uniforms),
|
||||
@ -608,8 +529,6 @@ impl strafe_client::framework::Example for Skybox {
|
||||
//all of these are being moved here
|
||||
models.push(Model{
|
||||
transform: modeldata.transform,
|
||||
transform_depth: modeldata.transform_depth,
|
||||
use_depth: modeldata.use_depth,
|
||||
vertex_buf:modeldata.vertex_buf,
|
||||
entities: modeldata.entities,
|
||||
bind_group: model_bind_group,
|
||||
@ -617,8 +536,6 @@ impl strafe_client::framework::Example for Skybox {
|
||||
})
|
||||
}
|
||||
|
||||
let teapot = models.pop().unwrap();
|
||||
|
||||
let depth_view = Self::create_depth_texture(config, device);
|
||||
|
||||
Skybox {
|
||||
@ -626,15 +543,11 @@ impl strafe_client::framework::Example for Skybox {
|
||||
sky_pipeline,
|
||||
entity_pipeline,
|
||||
ground_pipeline,
|
||||
special_teapot: teapot,
|
||||
checkered_pipeline,
|
||||
depth_overwrite_pipeline,
|
||||
main_bind_group,
|
||||
camera_buf,
|
||||
models,
|
||||
depth_view,
|
||||
staging_belt: wgpu::util::StagingBelt::new(0x100),
|
||||
start_time: Instant::now(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -745,11 +658,6 @@ impl strafe_client::framework::Example for Skybox {
|
||||
}
|
||||
}
|
||||
|
||||
let proj1 = glam::Mat4::orthographic_rh(-20.0, 20.0, -20.0, 20.0, -20.0, 20.0);
|
||||
let model1 = glam::Mat4::from_euler(glam::EulerRot::YXZ, self.start_time.elapsed().as_secs_f32(),0f32,0f32);
|
||||
|
||||
self.special_teapot.transform_depth=proj1 * model1;
|
||||
|
||||
let mut encoder =
|
||||
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
|
||||
|
||||
@ -764,22 +672,9 @@ impl strafe_client::framework::Example for Skybox {
|
||||
device,
|
||||
)
|
||||
.copy_from_slice(bytemuck::cast_slice(&camera_uniforms));
|
||||
//special teapot
|
||||
{
|
||||
let model_uniforms = get_model_uniform_data(&self.special_teapot);
|
||||
self.staging_belt
|
||||
.write_buffer(
|
||||
&mut encoder,
|
||||
&self.special_teapot.model_buf,//description of where data will be written when command is executed
|
||||
0,//offset in staging belt?
|
||||
wgpu::BufferSize::new((model_uniforms.len() * 4) as wgpu::BufferAddress).unwrap(),
|
||||
device,
|
||||
)
|
||||
.copy_from_slice(bytemuck::cast_slice(&model_uniforms));
|
||||
}
|
||||
//This code only needs to run when the uniforms change
|
||||
for model in self.models.iter() {
|
||||
let model_uniforms = get_model_uniform_data(&model);
|
||||
let model_uniforms = get_transform_uniform_data(&model.transform);
|
||||
self.staging_belt
|
||||
.write_buffer(
|
||||
&mut encoder,
|
||||
@ -820,23 +715,6 @@ impl strafe_client::framework::Example for Skybox {
|
||||
|
||||
rpass.set_bind_group(0, &self.main_bind_group, &[]);
|
||||
|
||||
//draw special teapot
|
||||
rpass.set_bind_group(1, &self.special_teapot.bind_group, &[]);
|
||||
rpass.set_pipeline(&self.checkered_pipeline);
|
||||
rpass.draw(0..6, 0..1);
|
||||
|
||||
rpass.set_pipeline(&self.entity_pipeline);
|
||||
rpass.set_vertex_buffer(0, self.special_teapot.vertex_buf.slice(..));
|
||||
|
||||
for entity in self.special_teapot.entities.iter() {
|
||||
rpass.set_index_buffer(entity.index_buf.slice(..), wgpu::IndexFormat::Uint16);
|
||||
rpass.draw_indexed(0..entity.index_count, 0, 0..1);
|
||||
}
|
||||
|
||||
rpass.set_pipeline(&self.depth_overwrite_pipeline);
|
||||
rpass.draw(0..6, 0..1);
|
||||
|
||||
//draw models
|
||||
rpass.set_pipeline(&self.entity_pipeline);
|
||||
for model in self.models.iter() {
|
||||
rpass.set_bind_group(1, &model.bind_group, &[]);
|
||||
|
@ -68,15 +68,9 @@ struct EntityOutput {
|
||||
@location(3) view: vec3<f32>,
|
||||
};
|
||||
|
||||
struct TransformData {
|
||||
transform: mat4x4<f32>,
|
||||
depth: mat4x4<f32>,
|
||||
use_depth: vec4<f32>,
|
||||
};
|
||||
|
||||
@group(1)
|
||||
@binding(0)
|
||||
var<uniform> transform: TransformData;
|
||||
var<uniform> r_EntityTransform: mat4x4<f32>;
|
||||
|
||||
@vertex
|
||||
fn vs_entity(
|
||||
@ -84,42 +78,12 @@ fn vs_entity(
|
||||
@location(1) texture: vec2<f32>,
|
||||
@location(2) normal: vec3<f32>,
|
||||
) -> EntityOutput {
|
||||
var position_depth: vec4<f32> = transform.depth * vec4<f32>(pos, 1.0);
|
||||
var position_depth_0: vec4<f32> = position_depth;
|
||||
position_depth_0.z=0.0;
|
||||
var position: vec4<f32> = transform.transform * mix(position_depth,position_depth_0,transform.use_depth);
|
||||
|
||||
var position: vec4<f32> = r_EntityTransform * vec4<f32>(pos, 1.0);
|
||||
var result: EntityOutput;
|
||||
result.normal = (transform.transform * mix(vec4<f32>(normal,0.0),vec4<f32>(0.0,0.0,1.0,0.0),transform.use_depth.z)).xyz;
|
||||
result.normal = (r_EntityTransform * vec4<f32>(normal, 0.0)).xyz;
|
||||
result.texture=texture;
|
||||
result.view = position.xyz - r_data.cam_pos.xyz;
|
||||
var screen_position: vec4<f32> = r_data.proj * r_data.view * position;
|
||||
result.position = mix(screen_position,position_depth,transform.use_depth);
|
||||
return result;
|
||||
}
|
||||
|
||||
struct SquareOutput {
|
||||
@builtin(position) position: vec4<f32>,
|
||||
@location(2) normal: vec3<f32>,
|
||||
@location(3) view: vec3<f32>,
|
||||
@location(4) pos: vec3<f32>,
|
||||
};
|
||||
@vertex
|
||||
fn vs_square(@builtin(vertex_index) vertex_index: u32) -> SquareOutput {
|
||||
// hacky way to draw two triangles that make a square
|
||||
let tmp1 = i32(vertex_index)/2-i32(vertex_index)/3;
|
||||
let tmp2 = i32(vertex_index)&1;
|
||||
let pos = vec3<f32>(
|
||||
(f32(tmp1) - 0.5)*1.8,
|
||||
f32(tmp2) - 0.5,
|
||||
0.0
|
||||
);
|
||||
|
||||
var result: SquareOutput;
|
||||
result.normal = vec3<f32>(0.0,0.0,1.0);
|
||||
result.pos = (transform.transform * vec4<f32>(pos, 1.0)).xyz;
|
||||
result.view = result.pos - r_data.cam_pos.xyz;
|
||||
result.position = r_data.proj * r_data.view * transform.transform * vec4<f32>(pos, 1.0);
|
||||
result.position = r_data.proj * r_data.view * position;
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -165,24 +129,3 @@ fn fs_ground(vertex: GroundOutput) -> @location(0) vec4<f32> {
|
||||
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;
|
||||
return vec4<f32>(textureSample(r_texture, r_sampler, dir).rgb, 1.0);
|
||||
}
|
||||
|
||||
@fragment
|
||||
fn fs_checkered(vertex: SquareOutput) -> @location(0) vec4<f32> {
|
||||
let voxel_parity: f32 = f32(
|
||||
u32(modulo_euclidean(vertex.pos.x,2.0)<1.0)
|
||||
^ u32(modulo_euclidean(vertex.pos.y,2.0)<1.0)
|
||||
//^ u32(modulo_euclidean(vertex.pos.z,2.0)<1.0)
|
||||
);
|
||||
|
||||
let incident = normalize(vertex.view);
|
||||
let normal = normalize(vertex.normal);
|
||||
let d = dot(normal, incident);
|
||||
let reflected = incident - 2.0 * d * normal;
|
||||
|
||||
let texture_color = vec3<f32>(1.0)*voxel_parity;
|
||||
let reflected_color = textureSample(r_texture, r_sampler, reflected).rgb;
|
||||
return vec4<f32>(mix(vec3<f32>(0.1) + 0.5 * reflected_color,texture_color,1.0-pow(1.0-abs(d),2.0)), 1.0);
|
||||
}
|
||||
|
||||
@fragment
|
||||
fn fs_overwrite(vertex: SquareOutput) {}
|
Loading…
Reference in New Issue
Block a user