~sircmpwn/xrgears

ref: 1ac9460a3fe49c9a47bc6d8262e124fd3d369758 xrgears/shaders/xrgears/scene.frag -rw-r--r-- 4.1 KiB
1ac9460aDrew DeVault Add wayland-direct window mode 2 years ago
                                                                                
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#version 450

#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
#extension GL_EXT_multiview : enable


layout (location = 0) in vec3 inNormal;
layout (location = 1) in vec3 inWorldPos;

layout (location = 2) in vec3 inViewPos;
layout (location = 3) in mat4 inInvModelView;

layout (location = 10) in vec3 inViewNormal;
layout (location = 11) in flat int inViewPortIndex;

layout (location = 0) out vec4 outColor;

layout(push_constant) uniform PushConsts {
	layout(offset = 12) float roughness;
	layout(offset = 16) float metallic;
	layout(offset = 20) float r;
	layout(offset = 24) float g;
	layout(offset = 28) float b;
} material;

layout (binding = 1) uniform UBOLights {
	vec4 lights[4];
} uboLights;

layout (binding = 2) uniform UBOCamera {
	mat4 projection[2];
	mat4 view[2];
	mat4 skyView[2];
	vec3 position;
} uboCamera;

layout (binding = 3) uniform samplerCube samplerCubeMap;


const float PI = 3.14159265359;

//#define ROUGHNESS_PATTERN 1

vec3 materialcolor() {
	return vec3(material.r, material.g, material.b);
}

// Normal Distribution function --------------------------------------
float D_GGX(float dotNH, float roughness) {
	float alpha = roughness * roughness;
	float alpha2 = alpha * alpha;
	float denom = dotNH * dotNH * (alpha2 - 1.0) + 1.0;
	return (alpha2)/(PI * denom*denom); 
}

// Geometric Shadowing function --------------------------------------
float G_SchlicksmithGGX(float dotNL, float dotNV, float roughness) {
	float r = (roughness + 1.0);
	float k = (r*r) / 8.0;
	float GL = dotNL / (dotNL * (1.0 - k) + k);
	float GV = dotNV / (dotNV * (1.0 - k) + k);
	return GL * GV;
}

// Fresnel function ----------------------------------------------------
vec3 F_Schlick(float cosTheta, float metallic, vec3 reflectionColor) {
	vec3 F0 = mix(vec3(0.04) * reflectionColor, materialcolor(), metallic) ;
	vec3 F = F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0); 
	return F;    
}

// Specular BRDF composition --------------------------------------------

vec3 BRDF(vec3 L, vec3 V, vec3 N, float metallic, float roughness, vec3 reflectionColor) {
	// Precalculate vectors and dot products	
	vec3 H = normalize (V + L);
	float dotNV = clamp(dot(N, V), 0.0, 1.0);
	float dotNL = clamp(dot(N, L), 0.0, 1.0);
	float dotLH = clamp(dot(L, H), 0.0, 1.0);
	float dotNH = clamp(dot(N, H), 0.0, 1.0);

	// Light color fixed
	vec3 lightColor = vec3(1.0);
	//vec3 lightColor = reflectionColor;

	vec3 color = vec3(0.0);

	if (dotNL > 0.0)
	{
		float rroughness = max(0.05, roughness);
		// D = Normal distribution (Distribution of the microfacets)
		float D = D_GGX(dotNH, roughness); 
		// G = Geometric shadowing term (Microfacets shadowing)
		float G = G_SchlicksmithGGX(dotNL, dotNV, roughness);
		// F = Fresnel factor (Reflectance depending on angle of incidence)
		vec3 F = F_Schlick(dotNV, metallic, reflectionColor);

		vec3 spec = D * F * G / (4.0 * dotNL * dotNV);

		color += spec * dotNL * lightColor;
		
		//color = F;
	}

	return color;
}

// ----------------------------------------------------------------------------
void main() {		  
	vec3 N = normalize(inNormal);
	//vec3 N = normalize(inViewNormal);
	vec3 V = normalize(uboCamera.position - inWorldPos);

	float roughness = material.roughness;

	// Add striped pattern to roughness based on vertex position
#ifdef ROUGHNESS_PATTERN
	roughness = max(roughness, step(fract(inWorldPos.y * 2.02), 0.5));
#endif


  // reflection mapping
	vec3 cI = normalize (inViewPos);
	vec3 cR = reflect (cI, inViewNormal);

	cR = vec3(inInvModelView * vec4(cR, 0.0));
	cR.x *= -1.0;
	vec3 reflectionColor = texture(samplerCubeMap, cR, 1.0).rgb;

	// Specular contribution
	vec3 Lo = vec3(0.0);
	for (int i = 0; i < uboLights.lights.length(); i++) {
		vec3 L = normalize(uboLights.lights[i].xyz - inWorldPos);
		Lo += BRDF(L, V, N, material.metallic, roughness, reflectionColor);
	};
	
	// Combine with ambient
	vec3 color = materialcolor() * 0.02;
	//color += 0.1 * reflectionColor;
	color += Lo;

	// Gamma correct
	color = pow(color, vec3(0.4545));

  outColor = vec4(color, 1.0);

	//outColor = inViewPortIndex * vec4(color, 1.0); //+ 0.1* vec4(reflectionColor, 1);

  // debug
 // outColor = vec4(L, 1.0);

}