struct VertexOutput {
[[builtin(position)]] Position : vec4<f32>;
[[location(0)]] fragPosition: vec3<f32>; // position in world space
[[location(1)]] fragNormal: vec3<f32>; // normal in world space
[[location(2)]] fragUV: vec2<f32>;
};
[[stage(vertex)]]
fn main([[location(0)]] position : vec3<f32>,
[[location(1)]] normal : vec3<f32>,
[[location(2)]] uv : vec2<f32>) -> VertexOutput {
var output : VertexOutput;
output.fragPosition = (uniforms.modelMatrix * vec4<f32>(position, 1.0)).xyz;
output.Position = camera.viewProjectionMatrix * vec4<f32>(output.fragPosition, 1.0);
output.fragNormal = normalize((uniforms.normalModelMatrix * vec4<f32>(normal, 1.0)).xyz);
output.fragUV = uv;
return output;
}
struct GBufferOutput {
[[location(0)]] position : vec4<f32>;
[[location(1)]] normal : vec4<f32>;
// Textures: diffuse color, specular color, smoothness, emissive etc. could go here
[[location(2)]] albedo : vec4<f32>;
};
[[stage(fragment)]]
fn main([[location(0)]] fragPosition: vec3<f32>,
[[location(1)]] fragNormal: vec3<f32>,
[[location(2)]] fragUV : vec2<f32>) -> GBufferOutput {
var output : GBufferOutput;
output.position = vec4<f32>(fragPosition, 1.0);
output.normal = vec4<f32>(fragNormal, 1.0);
// faking some kind of checkerboard texture
let uv = floor(30.0 * fragUV);
let c = 0.2 + 0.5 * ((uv.x + uv.y) - 2.0 * floor((uv.x + uv.y) / 2.0));
output.albedo = vec4<f32>(c, c, c, 1.0);
return output;
}
webgpu的shader代码貌似差不多了
来看看语法最终是啥情况
main函数要指定外部输入和输出的数据结构
这看起来海星
然后结构体,竟然可以把[[builtin(position)]],[[location(0)]]这种。标识符一样的东西写进去,需要习惯习惯
然后变量可以用var let 冒号后面要指定具体类型,和之前也差不多
慢慢学吧 应该差不多了
web3d 交流群511163089