yum/demomo

A graphics engine built on wgpu.

git clone https://git.yummers.dev/yum/demomo

yumAdd support for user-defined uniforms8debfd1

master
1.1 KiB50 linesraw
1import demomo;
2
3public ConstantBuffer<Camera> camera;
4public ConstantBuffer<Transform> transform;
5public ConstantBuffer<Light> light;
6
7public struct MyUniforms {
8    float specular_power;
9};
10public ConstantBuffer<MyUniforms> cfg;
11
12struct appdata {
13  float3 objPos;
14  float3 normal;
15  float2 uv0;
16};
17
18struct v2f {
19  float4 clipPos : SV_POSITION;
20  float3 worldPos;
21  float3 normal;
22  float2 uv0;
23};
24
25[shader("vertex")]
26v2f vert(appdata v) {
27  v2f o = (v2f) 0;
28  o.worldPos = mul(transform.model, float4(v.objPos, 1.0)).xyz;
29  o.clipPos = mul(camera.viewProj, float4(o.worldPos, 1.0));
30  o.normal = normalize(mul(transform.modelIT, float4(v.normal, 0.0)).xyz);
31  o.uv0 = v.uv0;
32  return o;
33}
34
35[shader("fragment")]
36float4 frag(v2f i) {
37    float3 l = normalize(light.worldPos - i.worldPos);
38    float3 v = normalize(camera.worldPos - i.worldPos);
39    float3 n = normalize(i.normal);
40    float3 h = normalize(l + v);
41
42    float NoL = dot(n, l);
43    float diffuse = saturate(NoL);
44
45    float NoH = dot(n, h);
46    float specular = pow(saturate(NoH), cfg.specular_power);
47
48    float3 c = float3(1, 1, 1) * (diffuse + specular);
49    return float4(c, 1);
50}