Clemsim před 2 roky
rodič
revize
f2af59a1c3

+ 45 - 0
assets/shaders/animate_shader.wgsl

@@ -0,0 +1,45 @@
+// The time since startup data is in the globals binding which is part of the mesh_view_bindings import
+#import bevy_pbr::{
+    mesh_view_bindings::globals,
+    forward_io::VertexOutput,
+}
+
+fn oklab_to_linear_srgb(c: vec3<f32>) -> vec3<f32> {
+    let L = c.x;
+    let a = c.y;
+    let b = c.z;
+
+    let l_ = L + 0.3963377774 * a + 0.2158037573 * b;
+    let m_ = L - 0.1055613458 * a - 0.0638541728 * b;
+    let s_ = L - 0.0894841775 * a - 1.2914855480 * b;
+
+    let l = l_ * l_ * l_;
+    let m = m_ * m_ * m_;
+    let s = s_ * s_ * s_;
+
+    return vec3<f32>(
+        4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
+        -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
+        -0.0041960863 * l - 0.7034186147 * m + 1.7076147010 * s,
+    );
+}
+
+@fragment
+fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
+    let speed = 2.0;
+    // The globals binding contains various global values like time
+    // which is the time since startup in seconds
+    let t_1 = sin(globals.time * speed) * 0.5 + 0.5;
+    let t_2 = cos(globals.time * speed);
+
+    let distance_to_center = distance(in.uv, vec2<f32>(0.5)) * 1.4;
+
+    // blending is done in a perceptual color space: https://bottosson.github.io/posts/oklab/
+    let red = vec3<f32>(0.627955, 0.224863, 0.125846);
+    let green = vec3<f32>(0.86644, -0.233887, 0.179498);
+    let blue = vec3<f32>(0.701674, 0.274566, -0.169156);
+    let white = vec3<f32>(1.0, 0.0, 0.0);
+    let mixed = mix(mix(red, blue, t_1), mix(green, white, t_2), distance_to_center);
+
+    return vec4<f32>(oklab_to_linear_srgb(mixed), 1.0);
+}

+ 37 - 0
assets/shaders/custom_vertex_attribute.wgsl

@@ -0,0 +1,37 @@
+#import bevy_pbr::mesh_functions::{get_model_matrix, mesh_position_local_to_clip}
+
+struct CustomMaterial {
+    color: vec4<f32>,
+};
+@group(1) @binding(0) var<uniform> material: CustomMaterial;
+
+struct Vertex {
+    @builtin(instance_index) instance_index: u32,
+    @location(0) position: vec3<f32>,
+    @location(1) blend_color: vec4<f32>,
+};
+
+struct VertexOutput {
+    @builtin(position) clip_position: vec4<f32>,
+    @location(0) blend_color: vec4<f32>,
+};
+
+@vertex
+fn vertex(vertex: Vertex) -> VertexOutput {
+    var out: VertexOutput;
+    out.clip_position = mesh_position_local_to_clip(
+        get_model_matrix(vertex.instance_index),
+        vec4<f32>(vertex.position, 1.0),
+    );
+    out.blend_color = vertex.blend_color;
+    return out;
+}
+
+struct FragmentInput {
+    @location(0) blend_color: vec4<f32>,
+};
+
+@fragment
+fn fragment(input: FragmentInput) -> @location(0) vec4<f32> {
+    return material.color * input.blend_color;
+}

+ 11 - 0
assets/shaders/fragment.wgsl

@@ -0,0 +1,11 @@
+use bevy::prelude::*;
+
+struct FragmentInput {
+    [[location(0)]] position: Vec2,
+    [[location(1)]] color: Vec3,
+}
+
+#[stage(fragment)]
+fn main([[location(0)]] input: FragmentInput) -> [[location(0)]] Vec4 {
+    return Vec4::new(input.color.x, input.color.y, input.color.z, 1.0);
+}

+ 14 - 0
assets/shaders/vertex.wgsl

@@ -0,0 +1,14 @@
+use bevy::prelude::*;
+
+struct VertexOutput {
+    [[builtin(position)]] position: Vec4,
+    [[location(0)]] color: Vec3,
+}
+
+#[stage(vertex)]
+fn main([[location(0)]] position: Vec2, [[location(1)]] color: Vec3) -> VertexOutput {
+    return VertexOutput {
+        position: Vec4::new(position.x, position.y, 1.0, 1.0),
+        color,
+    };
+}

+ 25 - 54
src/main.rs

@@ -1,71 +1,42 @@
-use bevy::prelude::*;
-
-mod config;
-mod player;
-
-#[derive(Component)]
-struct Plane;
+use bevy::{
+    prelude::*,
+    reflect::TypePath,
+    render::render_resource::{AsBindGroup, ShaderRef},
+};
 
 fn main() {
     App::new()
-        .add_plugins(DefaultPlugins)
-        .add_systems(Startup, setup1)
-        .add_systems(Update, player::movement::player_movement)
-        .add_systems(Update, planequery)
+        .add_plugins((DefaultPlugins, MaterialPlugin::<CustomMaterial>::default()))
+        .add_systems(Startup, setup)
         .run();
 }
 
-fn setup1(
+fn setup(
     mut commands: Commands,
     mut meshes: ResMut<Assets<Mesh>>,
-    mut materials: ResMut<Assets<StandardMaterial>>,
+    mut materials: ResMut<Assets<CustomMaterial>>,
 ) {
-    commands.spawn((
-        PbrBundle {
-            mesh: meshes.add(Mesh::from(shape::Cube { size: 2.0 })),
-            material: materials.add(Color::rgb(0.5, 0.5, 1.0).into()),
-            ..Default::default()
-        },
-        Plane,
-    ));
-
-    commands.spawn((
-        Camera3dBundle {
-            transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
-            ..default()
-        },
-        player::movement::Player,
-    ));
+    // cube
+    commands.spawn(MaterialMeshBundle {
+        mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
+        transform: Transform::from_xyz(0.0, 0.5, 0.0),
+        material: materials.add(CustomMaterial {}),
+        ..default()
+    });
 
-    commands.spawn(PointLightBundle {
-        point_light: PointLight {
-            intensity: 1500.0,
-            shadows_enabled: true,
-            ..default()
-        },
-        transform: Transform::from_xyz(4.0, 8.0, 4.0),
+    // camera
+    commands.spawn(Camera3dBundle {
+        transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
         ..default()
     });
 }
 
-#[system(dependencies = [Res<AssetServer>])]
-fn planequery(mut commands: Commands, mut query: Query<(&Plane, &Handle<Mesh>)>, asset_server: Res<AssetServer>) {
-    for (plane, mesh_handle) in query.iter_mut() {
-        // Load the mesh data from the asset server.
-        let mesh = asset_server.load(mesh_handle).await.unwrap();
+#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
+struct CustomMaterial {}
 
-        // Get the vertex attribute values for the mesh.
-        let vertex_attribute_values = mesh.vertex_attribute_values().unwrap();
-
-        // Get the vertices from the vertex attribute values.
-        let vertices = vertex_attribute_values.get("Position").unwrap().as_slice::<Vec3>();
-
-        // Move the vertices up and down.
-        for vertex in vertices.iter_mut() {
-            vertex.y += 0.1; // Move the vertex up by 0.1 units.
-        }
-
-        // Update the mesh with the new vertex positions.
-        mesh.set_vertex_attribute_values(vertex_attribute_values).unwrap();
+impl Material for CustomMaterial {
+    fn fragment_shader() -> ShaderRef {
+        "shaders/animate_shader.wgsl".into()
     }
 }
+