1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
#ifdef DESC
[program]
type: graphics
vertex: main
fragment: main
[binding]
name: mesh
rate: vertex
[attribute]
name: position
type: vec3
[attribute]
name: normal
type: vec3
[attribute]
name: tangent
type: vec3
[attribute]
name: uv
type: vec2
[interpolator]
name: uv
type: vec2
[interpolator]
name: position
type: vec4
[interpolator]
name: normal
type: vec3
[interpolator]
name: tbn
type: mat3
[struct]
name: MVP
[variable]
name: model
type: mat4
[variable]
name: view_projection
type: mat4
[struct]
name: Material
[variable]
name: albedo
type: vec3
[variable]
name: metalness
type: float
[variable]
name: roughness
type: float
[variable]
name: ao
type: float
[cbuffer]
name: c_mvp
type: MVP
stage: vertex
[cbuffer]
name: material
type: Material
stage: fragment
[texture]
name: albedo
stage: fragment
dimension: 2
[texture]
name: ao
stage: fragment
dimension: 2
[texture]
name: metal
stage: fragment
dimension: 2
[texture]
name: rough
stage: fragment
dimension: 2
[texture]
name: normal
stage: fragment
dimension: 2
[target]
name: colour
type: vec4
#endif
#ifdef VERTEX_SHADER
void main() {
vec4 pos = c_mvp.model * vec4(position, 1.0);
vec3 t = normalize((c_mvp.model * vec4(tangent, 0.0)).xyz);
vec3 n = normalize((c_mvp.model * vec4(normal, 0.0)).xyz);
vec3 b = cross(t, n);
interpolator.normal = n;
interpolator.tbn = mat3(t, b, n);
interpolator.uv = uv;
interpolator.position = pos;
interpolator.tbn = mat3(t, b, n);
gl_Position = c_mvp.view_projection * pos;
}
#endif
#ifdef FRAGMENT_SHADER
void main() {
vec2 uv = interpolator.uv;
vec3 nrmsample = texture(normal, uv).rgb;
vec2 nrmxy = nrmsample.xy * 2.0 - 1.0;
vec3 nrm = normalize(vec3(nrmxy, 1.0));
if (nrmsample.b == 1.0) /* default texture */
nrm = vec3(0, 0, 1);
nrm = normalize(interpolator.tbn * nrm);
float light = max(dot(nrm, vec3(0, 0, 1)), 0.0);
vec3 ambient = 0.05.xxx * texture(ao, uv).r;
vec3 col = ambient + light * material.albedo;
col *= texture(albedo, uv).rgb;
colour = vec4(col, 1.0);
}
#endif
|