فهرست منبع

Sky pipeline near finish, tommorow i try to add it to the main render pass, or creating another one

Ghastrod 1 سال پیش
والد
کامیت
e6554afc06
4فایلهای تغییر یافته به همراه101 افزوده شده و 0 حذف شده
  1. 1 0
      src/main.rs
  2. 51 0
      src/shaders/sky.wgsl
  3. 1 0
      src/sky/mod.rs
  4. 48 0
      src/sky/sky_pipeline.rs

+ 1 - 0
src/main.rs

@@ -4,6 +4,7 @@ use crate::object::generate_plane;
 
 mod camera;
 mod compute;
+mod sky;
 mod object;
 fn main() {
     println!("Hello, world!");

+ 51 - 0
src/shaders/sky.wgsl

@@ -0,0 +1,51 @@
+struct Uniforms {
+  vec2 iResolution;  // Resolution of the screen
+  vec2 iMouse;       // Normalized mouse position (0.0 to 1.0)
+};
+
+fn getSky(uv: vec2<f32>) -> vec3<f32> {
+  let atmosphere = sqrt(1.0 - uv.y);
+  let skyColor = vec3<f32>(0.2, 0.4, 0.8);
+
+  let scatter = pow((uniforms.iMouse.y / uniforms.iResolution.y), 1.0 / 15.0);
+  let scatter = clamp(scatter, 0.8, 1.0);
+
+  let scatterColor = mix(vec3<f32>(1.0), vec3<f32>(1.0, 0.3, 0.0) * 1.5, scatter);
+  return mix(skyColor, scatterColor, atmosphere / 1.3);
+}
+
+fn getSun(uv: vec2<f32>) -> vec3<f32> {
+  let sun = 1.0 - distance(uv, uniforms.iMouse.xy / uniforms.iResolution.y);
+  let sun = clamp(sun, 0.0, 1.0);
+
+  let mut glow = sun;
+  glow = clamp(glow, 0.0, 1.0);
+
+  sun = pow(sun, 100.0);
+  sun *= 100.0;
+  sun = clamp(sun, 0.0, 1.0);
+
+  glow = pow(glow, 6.0) * 1.0;
+  glow = pow(glow, uv.y);
+  glow = clamp(glow, 0.0, 1.0);
+
+  sun *= pow(dot(uv.y, uv.y), 1.0 / 1.65);
+  glow *= pow(dot(uv.y, uv.y), 1.0 / 2.0);
+
+  sun += glow;
+
+  let sunColor = vec3<f32>(1.0, 0.6, 0.05) * sun;
+  return sunColor;
+}
+
+fn main_image(fragColor: out vec4<f32>, uv: in vec2<f32>) {
+  let sky = getSky(uv);
+  let sun = getSun(uv);
+
+  fragColor = vec4<f32>(sky + sun, 1.0);
+}
+
+// Entry point for the shader pipeline
+fn main() {
+  main_image(fragColor, gl_FragCoord.xy / uniforms.iResolution.y);
+}

+ 1 - 0
src/sky/mod.rs

@@ -0,0 +1 @@
+pub mod sky_pipeline;

+ 48 - 0
src/sky/sky_pipeline.rs

@@ -0,0 +1,48 @@
+use wgpu::{include_wgsl, RenderPipeline};
+
+pub fn new(device: &wgpu::Device, config: &wgpu::SurfaceConfiguration) -> RenderPipeline {
+    let sky_shader = device.create_shader_module(include_wgsl!("../shaders/sky.wgsl"));
+
+    let render_pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
+        label: Some("Sky Render Pipeline Layout"),
+        bind_group_layouts: &[],
+        push_constant_ranges: &[],
+    });
+
+    let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
+        label: Some("Sky Render Pipeline"),
+        layout: Some(&render_pipeline_layout),
+        vertex: wgpu::VertexState {
+            module: &sky_shader,
+            entry_point: "vs_main",
+            buffers: &[],
+        },
+        primitive: wgpu::PrimitiveState {
+            topology: wgpu::PrimitiveTopology::TriangleList,
+            strip_index_format: None,
+            front_face: wgpu::FrontFace::Ccw,
+            conservative: false,
+            cull_mode: None,
+            unclipped_depth: false,
+            polygon_mode: wgpu::PolygonMode::Fill,
+        },
+        depth_stencil: None,
+        multisample: wgpu::MultisampleState {
+            count: 1,
+            mask: !0,
+            alpha_to_coverage_enabled: false,
+        },
+        fragment: Some(wgpu::FragmentState {
+            module: &sky_shader,
+            entry_point: "fs_main",
+            targets: &[Some(wgpu::ColorTargetState {
+                format: config.format,
+                blend: Some(wgpu::BlendState::REPLACE),
+                write_mask: wgpu::ColorWrites::ALL,
+            })],
+        }),
+        multiview: None,
+    });
+
+    return render_pipeline;
+}