Skip to content

Commit

Permalink
Shader WGSL example - split single shader into vertex and fragment (#…
Browse files Browse the repository at this point in the history
…7310)

Co-authored-by: Martin Valigursky <[email protected]>
  • Loading branch information
mvaligursky and Martin Valigursky authored Jan 23, 2025
1 parent 3b12d5e commit a354950
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
4 changes: 2 additions & 2 deletions examples/src/examples/graphics/wgsl-shader.example.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ app.on('destroy', () => {

const material = new pc.ShaderMaterial({
uniqueName: 'MyWGSLShader',
vertexCode: files['shader.wgsl'],
fragmentCode: files['shader.wgsl'],
vertexCode: files['shader.vert.wgsl'],
fragmentCode: files['shader.frag.wgsl'],
shaderLanguage: pc.SHADERLANGUAGE_WGSL,

// For now WGSL shaders need to provide their own bind group formats as they aren't processed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ struct VertexOutput {
@group(2) @binding(0) var<uniform> uvMesh : ub_mesh;
@group(0) @binding(0) var<uniform> ubView : ub_view;

@vertex
fn vertexMain(@location(0) position : vec4f) -> VertexOutput {
var output : VertexOutput;
output.position = ubView.matrix_viewProjection * (uvMesh.matrix_model * position);
output.fragPosition = 0.5 * (position + vec4(1.0));
return output;
}

@fragment
fn fragmentMain(input : VertexOutput) -> @location(0) vec4f {
var color : vec3f = input.fragPosition.rgb;
Expand Down
28 changes: 28 additions & 0 deletions examples/src/examples/graphics/wgsl-shader.shader.vert.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
struct ub_mesh {
matrix_model : mat4x4f,
amount : f32,
}

struct ub_view {
matrix_viewProjection : mat4x4f,
}

struct VertexOutput {
@builtin(position) position : vec4f,
@location(0) fragPosition: vec4f,
}

@group(2) @binding(0) var<uniform> uvMesh : ub_mesh;
@group(0) @binding(0) var<uniform> ubView : ub_view;

struct VertexInput {
@location(0) position : vec4f,
}

@vertex
fn vertexMain(input : VertexInput) -> VertexOutput {
var output : VertexOutput;
output.position = ubView.matrix_viewProjection * (uvMesh.matrix_model * input.position);
output.fragPosition = 0.5 * (input.position + vec4(1.0));
return output;
}

0 comments on commit a354950

Please sign in to comment.