Skip to content

Commit

Permalink
WebGPURenderer: Introduce ShaderLib.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mugen87 committed Nov 10, 2020
1 parent 81225bc commit a01651a
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 97 deletions.
95 changes: 95 additions & 0 deletions examples/jsm/renderers/webgpu/ShaderLib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
const ShaderLib = {
meshBasic: {
vertexShader: `#version 450
layout(location = 0) in vec3 position;
layout(location = 1) in vec2 uv;
layout(location = 0) out vec2 vUv;
layout(set = 0, binding = 0) uniform ModelUniforms {
mat4 modelMatrix;
mat4 modelViewMatrix;
mat3 normalMatrix;
} modelUniforms;
layout(set = 0, binding = 1) uniform CameraUniforms {
mat4 projectionMatrix;
mat4 viewMatrix;
} cameraUniforms;
void main(){
vUv = uv;
gl_Position = cameraUniforms.projectionMatrix * modelUniforms.modelViewMatrix * vec4( position, 1.0 );
}`,
fragmentShader: `#version 450
layout(set = 0, binding = 2) uniform OpacityUniforms {
float opacity;
} opacityUniforms;
layout(set = 0, binding = 3) uniform sampler mySampler;
layout(set = 0, binding = 4) uniform texture2D myTexture;
layout(location = 0) in vec2 vUv;
layout(location = 0) out vec4 outColor;
void main() {
outColor = texture( sampler2D( myTexture, mySampler ), vUv );
outColor.a *= opacityUniforms.opacity;
}`
},
pointsBasic: {
vertexShader: `#version 450
layout(location = 0) in vec3 position;
layout(set = 0, binding = 0) uniform ModelUniforms {
mat4 modelMatrix;
mat4 modelViewMatrix;
} modelUniforms;
layout(set = 0, binding = 1) uniform CameraUniforms {
mat4 projectionMatrix;
mat4 viewMatrix;
} cameraUniforms;
void main(){
gl_Position = cameraUniforms.projectionMatrix * modelUniforms.modelViewMatrix * vec4( position, 1.0 );
}`,
fragmentShader: `#version 450
layout(location = 0) out vec4 outColor;
void main() {
outColor = vec4( 1.0, 0.0, 0.0, 1.0 );
}`
},
lineBasic: {
vertexShader: `#version 450
layout(location = 0) in vec3 position;
layout(set = 0, binding = 0) uniform ModelUniforms {
mat4 modelMatrix;
mat4 modelViewMatrix;
} modelUniforms;
layout(set = 0, binding = 1) uniform CameraUniforms {
mat4 projectionMatrix;
mat4 viewMatrix;
} cameraUniforms;
void main(){
gl_Position = cameraUniforms.projectionMatrix * modelUniforms.modelViewMatrix * vec4( position, 1.0 );
}`,
fragmentShader: `#version 450
layout(location = 0) out vec4 outColor;
void main() {
outColor = vec4( 1.0, 0.0, 0.0, 1.0 );
}`
}
};

export default ShaderLib;
102 changes: 5 additions & 97 deletions examples/jsm/renderers/webgpu/WebGPURenderPipelines.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
ZeroFactor, OneFactor, SrcColorFactor, OneMinusSrcColorFactor, SrcAlphaFactor, OneMinusSrcAlphaFactor, DstAlphaFactor, OneMinusDstAlphaFactor, DstColorFactor, OneMinusDstColorFactor, SrcAlphaSaturateFactor
} from '../../../../build/three.module.js';

import ShaderLib from './ShaderLib.js';

class WebGPURenderPipelines {

constructor( renderer, properties, device, glslang, sampleCount ) {
Expand Down Expand Up @@ -57,15 +59,15 @@ class WebGPURenderPipelines {

if ( material.isMeshBasicMaterial ) {

shader = ShaderLib.mesh_basic;
shader = ShaderLib.meshBasic;

} else if ( material.isPointsMaterial ) {

shader = ShaderLib.points_basic;
shader = ShaderLib.pointsBasic;

} else if ( material.isLineBasicMaterial ) {

shader = ShaderLib.line_basic;
shader = ShaderLib.lineBasic;

} else {

Expand Down Expand Up @@ -790,98 +792,4 @@ class WebGPURenderPipelines {

}

const ShaderLib = {
mesh_basic: {
vertexShader: `#version 450
layout(location = 0) in vec3 position;
layout(location = 1) in vec2 uv;
layout(location = 0) out vec2 vUv;
layout(set = 0, binding = 0) uniform ModelUniforms {
mat4 modelMatrix;
mat4 modelViewMatrix;
mat3 normalMatrix;
} modelUniforms;
layout(set = 0, binding = 1) uniform CameraUniforms {
mat4 projectionMatrix;
mat4 viewMatrix;
} cameraUniforms;
void main(){
vUv = uv;
gl_Position = cameraUniforms.projectionMatrix * modelUniforms.modelViewMatrix * vec4( position, 1.0 );
}`,
fragmentShader: `#version 450
layout(set = 0, binding = 2) uniform OpacityUniforms {
float opacity;
} opacityUniforms;
layout(set = 0, binding = 3) uniform sampler mySampler;
layout(set = 0, binding = 4) uniform texture2D myTexture;
layout(location = 0) in vec2 vUv;
layout(location = 0) out vec4 outColor;
void main() {
outColor = texture( sampler2D( myTexture, mySampler ), vUv );
outColor.a *= opacityUniforms.opacity;
}`
},
points_basic: {
vertexShader: `#version 450
layout(location = 0) in vec3 position;
layout(set = 0, binding = 0) uniform ModelUniforms {
mat4 modelMatrix;
mat4 modelViewMatrix;
} modelUniforms;
layout(set = 0, binding = 1) uniform CameraUniforms {
mat4 projectionMatrix;
mat4 viewMatrix;
} cameraUniforms;
void main(){
gl_Position = cameraUniforms.projectionMatrix * modelUniforms.modelViewMatrix * vec4( position, 1.0 );
}`,
fragmentShader: `#version 450
layout(location = 0) out vec4 outColor;
void main() {
outColor = vec4( 1.0, 0.0, 0.0, 1.0 );
}`
},
line_basic: {
vertexShader: `#version 450
layout(location = 0) in vec3 position;
layout(set = 0, binding = 0) uniform ModelUniforms {
mat4 modelMatrix;
mat4 modelViewMatrix;
} modelUniforms;
layout(set = 0, binding = 1) uniform CameraUniforms {
mat4 projectionMatrix;
mat4 viewMatrix;
} cameraUniforms;
void main(){
gl_Position = cameraUniforms.projectionMatrix * modelUniforms.modelViewMatrix * vec4( position, 1.0 );
}`,
fragmentShader: `#version 450
layout(location = 0) out vec4 outColor;
void main() {
outColor = vec4( 1.0, 0.0, 0.0, 1.0 );
}`
}
};

export default WebGPURenderPipelines;

0 comments on commit a01651a

Please sign in to comment.