diff options
Diffstat (limited to 'intermediate/surface.glsl')
| -rw-r--r-- | intermediate/surface.glsl | 68 | 
1 files changed, 67 insertions, 1 deletions
| diff --git a/intermediate/surface.glsl b/intermediate/surface.glsl index 9fc1c48..10944ae 100644 --- a/intermediate/surface.glsl +++ b/intermediate/surface.glsl @@ -74,6 +74,15 @@ type: float  [variable]  name: colour  type: vec3 +[variable] +name: caster_id +type: int + +[struct] +name: Caster +[variable] +name: projection +type: mat4  [cbuffer]  name: c_vp @@ -95,6 +104,11 @@ name: lights  type: Light  stage: fragment +[sbuffer] +name: casters +type: Caster +stage: fragment +  [texture]  name: albedo  stage: fragment @@ -119,6 +133,10 @@ dimension: 2  name: env_cube  stage: fragment  dimension: cube +[texture] +name: shadowmaps +stage: fragment +dimension: shadowArray  [target]  name: colour @@ -174,6 +192,51 @@ float specular_brdf(vec2 uv, vec3 ref, vec3 l, vec3 v, vec3 n) {  	return (D * F * G) / (4.0 * ndl * ndv + 0.001);  } +vec2 poissonDisk[16] = vec2[](  +	vec2(-0.94201624,  -0.39906216), +	vec2(0.94558609,   -0.76890725), +	vec2(-0.094184101, -0.92938870), +	vec2(0.34495938,    0.29387760), +	vec2(-0.91588581,   0.45771432), +	vec2(-0.81544232,  -0.87912464), +	vec2(-0.38277543,   0.27676845), +	vec2(0.97484398,    0.75648379), +	vec2(0.44323325,   -0.97511554), +	vec2(0.53742981,   -0.47373420), +	vec2(-0.26496911,  -0.41893023), +	vec2(0.79197514,    0.19090188), +	vec2(-0.24188840,   0.99706507), +	vec2(-0.81409955,   0.91437590), +	vec2(0.19984126,    0.78641367), +	vec2(0.14383161,   -0.14100790) +); + +float random(vec3 seed, int i){ +	vec4 seed4 = vec4(seed,i); +	float dot_product = dot(seed4, vec4(12.9898,78.233,45.164,94.673)); +	return fract(sin(dot_product) * 43758.5453); +} + +float get_shadow(Light l, vec3 wpos) { +	const int taps = 4; +	const float w = 1.0 / float(taps); +	const float m = 1.0 / 1000.0; +	int i; +	float d = 0.0f; +	Caster caster = casters[l.caster_id]; +	vec4 surf = caster.projection * vec4(wpos, 1.0); +	surf /= surf.w; +	surf.xy = surf.xy * 0.5 + 0.5; +	surf.z -= 0.005; +	for (i = 0; i < taps; i++) { +		int index = int(16.0 * random(floor(wpos.xyz * 1000.0), i)) % 16; +		vec2 coord = surf.xy + poissonDisk[index] * m; +		vec4 vec = vec4(coord, float(l.caster_id), surf.z); +		d += texture(shadowmaps, vec).r * w; +	} +	return d; +} +  void main() {  	int i;  	vec2 uv = interpolator.uv; @@ -210,7 +273,10 @@ void main() {  			spec_col *  			specular_brdf(uv, ref, light_dir, view_dir, nrm) *  			cos_theta_i; -		light += (diffuse + spec) * l.brightness * l.colour; +		float shadow = 1.0f; +		if (l.caster_id >= 0) +			shadow = get_shadow(l, p); +		light += (diffuse + spec) * l.brightness * l.colour * shadow;  	}  	colour = vec4(ambient + light, 1.0); |