@@ -89,6 +89,68 @@ const createHatchMaterial = (device, textures) => {
8989 gl_Position = matrix_viewProjection * worldPos;
9090 }
9191 ` ,
92+ vertexWGSL : /* wgsl */ `
93+
94+ // include code transform shader functionality provided by the engine. It automatically
95+ // declares vertex_position attribute, and handles skinning and morphing if necessary.
96+ // It also adds uniforms: matrix_viewProjection, matrix_model, matrix_normal.
97+ // Functions added: getModelMatrix, getLocalPosition
98+ #include "transformCoreVS"
99+
100+ // include code for normal shader functionality provided by the engine. It automatically
101+ // declares vertex_normal attribute, and handles skinning and morphing if necessary.
102+ // Functions added: getNormalMatrix, getLocalNormal
103+ #include "normalCoreVS"
104+
105+ // add additional attributes we need
106+ attribute aUv0: vec2f;
107+
108+ // engine supplied uniforms
109+ uniform view_position: vec3f;
110+
111+ // out custom uniforms
112+ uniform uLightDir: vec3f;
113+ uniform uMetalness: f32;
114+
115+ // variables we pass to the fragment shader
116+ varying uv0: vec2f;
117+ varying brightness: f32;
118+
119+ @vertex
120+ fn vertexMain(input: VertexInput) -> VertexOutput
121+ {
122+ var output: VertexOutput;
123+
124+ // use functionality from transformCore to get a world position, which includes skinning and morphing as needed
125+ let modelMatrix: mat4x4f = getModelMatrix();
126+ let localPos: vec3f = getLocalPosition(vertex_position.xyz);
127+ let worldPos: vec4f = modelMatrix * vec4f(localPos, 1.0);
128+
129+ // use functionality from normalCore to get the world normal, which includes skinning and morphing as needed
130+ let normalMatrix: mat3x3f = getNormalMatrix(modelMatrix);
131+ let localNormal: vec3f = getLocalNormal(vertex_normal);
132+ let worldNormal: vec3f = normalize(normalMatrix * localNormal);
133+
134+ // simple wrap-around diffuse lighting using normal and light direction
135+ let diffuse: f32 = dot(worldNormal, uniform.uLightDir) * 0.5 + 0.5;
136+
137+ // a simple specular lighting
138+ let viewDir: vec3f = normalize(uniform.view_position - worldPos.xyz);
139+ let reflectDir: vec3f = reflect(-uniform.uLightDir, worldNormal);
140+ let specular: f32 = pow(max(dot(viewDir, reflectDir), 0.0), 9.0);
141+
142+ // combine the lighting
143+ output.brightness = diffuse * (1.0 - uniform.uMetalness) + specular * uniform.uMetalness;
144+
145+ // Pass the texture coordinates
146+ output.uv0 = aUv0;
147+
148+ // Transform the geometry
149+ output.position = uniform.matrix_viewProjection * worldPos;
150+
151+ return output;
152+ }
153+ ` ,
92154 fragmentGLSL : /* glsl */ `
93155 // this gives us gamma correction functions, such as gammaCorrectOutput
94156 #include "gammaPS"
@@ -133,6 +195,56 @@ const createHatchMaterial = (device, textures) => {
133195 gl_FragColor.a = 1.0;
134196 }
135197 ` ,
198+ fragmentWGSL : /* wgsl */ `
199+ // this gives us gamma correction functions, such as gammaCorrectOutput
200+ #include "gammaPS"
201+
202+ // this give us tonemapping functionality: toneMap
203+ #include "tonemappingPS"
204+
205+ // this gives us for functionality: addFog
206+ #include "fogPS"
207+
208+ varying brightness: f32;
209+ varying uv0: vec2f;
210+
211+ var uDiffuseMap: texture_2d_array<f32>;
212+ var uDiffuseMapSampler: sampler;
213+ uniform uDensity: f32;
214+ uniform uNumTextures: f32;
215+ uniform uColor: vec3f;
216+
217+ @fragment
218+ fn fragmentMain(input: FragmentInput) -> FragmentOutput
219+ {
220+ var output: FragmentOutput;
221+ var colorLinear: vec3f;
222+
223+ #ifdef TOON
224+
225+ // just a simple toon shader - no texture sampling
226+ let level: f32 = f32(i32(input.brightness * uniform.uNumTextures)) / uniform.uNumTextures;
227+ colorLinear = level * uniform.uColor;
228+
229+ #else
230+ // brightness dictates the hatch texture level
231+ let level: f32 = (1.0 - input.brightness) * uniform.uNumTextures;
232+
233+ // sample the two nearest levels and interpolate between them
234+ let hatchUnder: vec3f = textureSample(uDiffuseMap, uDiffuseMapSampler, input.uv0 * uniform.uDensity, i32(floor(level))).xyz;
235+ let hatchAbove: vec3f = textureSample(uDiffuseMap, uDiffuseMapSampler, input.uv0 * uniform.uDensity, i32(min(ceil(level), uniform.uNumTextures - 1.0))).xyz;
236+ colorLinear = mix(hatchUnder, hatchAbove, fract(level)) * uniform.uColor;
237+ #endif
238+
239+ // handle standard color processing - the called functions are automatically attached to the
240+ // shader based on the current fog / tone-mapping / gamma settings
241+ let fogged: vec3f = addFog(colorLinear);
242+ let toneMapped: vec3f = toneMap(fogged);
243+ output.color = vec4f(gammaCorrectOutput(toneMapped), 1.0);
244+
245+ return output;
246+ }
247+ ` ,
136248 attributes : {
137249 vertex_position : SEMANTIC_POSITION ,
138250 vertex_normal : SEMANTIC_NORMAL ,
0 commit comments