-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shader): allow uploading and using a custom shader for sprites
- Loading branch information
Showing
21 changed files
with
852 additions
and
455 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
struct VertexOutput { | ||
// These two fields must be here | ||
@builtin(position) clip_position: vec4f, | ||
@location(0) tex_coords: vec2f, | ||
} | ||
|
||
@vertex | ||
fn vs_main( | ||
model: VertexInput, | ||
instance: InstanceInput, | ||
) -> VertexOutput { | ||
// This function needs this name and input types | ||
|
||
// Use the complicated vertex shader setup from the engine | ||
return vs_main_impl(model, instance); | ||
} | ||
|
||
@fragment | ||
fn fs_main(in: VertexOutput) -> @location(0) vec4f { | ||
// This function needs this name and output types | ||
|
||
// Return the pixel in a nearest-neighbor fashion | ||
return textureSample(t_diffuse, s_diffuse, in.tex_coords) | ||
// Add a red hue to the pixel | ||
+ vec4f(1.0, 0.0, 0.0, 0.0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//! Show how to load a custom shader. | ||
//! TODO. | ||
use chuot::{Config, Context, Game}; | ||
|
||
/// Define a game state for our example. | ||
struct GameState; | ||
|
||
impl Game for GameState { | ||
/// Render the game. | ||
fn render(&mut self, ctx: Context) { | ||
// Draw a sprite with a custom shader | ||
ctx.sprite("threeforms") | ||
// Use the custom shader | ||
.shader("shader") | ||
.translate_x(50.0) | ||
.draw(); | ||
|
||
// Draw a sprite with the default shader | ||
ctx.sprite("threeforms").translate_x(-50.0).draw(); | ||
} | ||
|
||
/// Do nothing during the update loop. | ||
fn update(&mut self, _ctx: Context) {} | ||
} | ||
|
||
/// Open an empty window. | ||
fn main() { | ||
// Game configuration | ||
let config = Config { | ||
buffer_width: 240.0, | ||
buffer_height: 192.0, | ||
// Apply a minimum of 3 times scaling for the buffer | ||
// Will result in a minimum, and on web exact, window size of 720x576 | ||
scaling: 3.0, | ||
..Default::default() | ||
}; | ||
|
||
// Spawn the window and run the 'game' | ||
GameState.run(chuot::load_assets!(), config); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Size of both width and height of the atlas texture | ||
const ATLAS_TEXTURE_SIZE: f32 = 4096.0; | ||
|
||
@group(0) @binding(0) | ||
var t_diffuse: texture_2d<f32>; | ||
@group(0) @binding(1) | ||
var s_diffuse: sampler; | ||
|
||
struct TextureInfo { | ||
@location(0) offset: vec2f, | ||
@location(1) _ignore: vec2f, | ||
} | ||
|
||
struct ScreenInfo { | ||
@location(0) size: vec2f, | ||
@location(1) half_size: vec2f, | ||
} | ||
|
||
@group(1) @binding(0) | ||
var<uniform> tex_info: array<TextureInfo, 1024>; | ||
|
||
@group(2) @binding(0) | ||
var<uniform> screen_info: ScreenInfo; | ||
|
||
struct VertexInput { | ||
@location(0) position: vec3f, | ||
@location(1) tex_coords: vec2f, | ||
} | ||
|
||
struct InstanceInput { | ||
// Matrix type is not supported in vertex input, construct it from 3 vec3's | ||
// The last row of the matrix is always 0 0 1 so we can save some bytes by constructing that ourselves | ||
@location(2) matrix: vec4f, | ||
// X and Y position used in the transformation matrix | ||
@location(3) translation: vec2f, | ||
// Sub rectangle of the texture to render, offset of the texture will be determined by the texture info uniform | ||
@location(4) sub_rectangle: vec4f, | ||
// Which texture to render, dimensions are stored in the uniform buffer | ||
@location(5) tex_index: u32, | ||
} | ||
|
||
fn vs_main_impl( | ||
model: VertexInput, | ||
instance: InstanceInput, | ||
) -> VertexOutput { | ||
// Create the 2D affine transformation matrix for each instance | ||
let instance_matrix = mat3x3<f32>( | ||
vec3f(instance.matrix.xy, 0.0), | ||
vec3f(instance.matrix.zw, 0.0), | ||
vec3f(instance.translation, 1.0), | ||
); | ||
|
||
// Get the texture rectangle from the atlas | ||
let offset = tex_info[instance.tex_index].offset; | ||
|
||
// Resize the quad to the size of the texture | ||
let model_position = model.position.xy * instance.sub_rectangle.zw; | ||
|
||
// Translate, rotate and skew with the instance matrix | ||
let projected_position = instance_matrix * vec3f(model_position, 1.0); | ||
|
||
// Move from 0..width to -1..1 | ||
let screen_offset = projected_position.xy / screen_info.half_size - 1.0; | ||
// Move the 0..1 texture coordinates to relative coordinates within the 4096x4096 atlas texture for the specified texture | ||
// Also apply the sub rectangle offset from the instance | ||
let tex_coords = (offset + instance.sub_rectangle.xy + instance.sub_rectangle.zw * model.tex_coords) / ATLAS_TEXTURE_SIZE; | ||
|
||
var out: VertexOutput; | ||
out.tex_coords = tex_coords; | ||
out.clip_position = vec4f(screen_offset.x, screen_offset.y, model.position.z, 1.0); | ||
|
||
return out; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.