+ | Better material pathtraced system

This commit is contained in:
TheRedShip
2025-01-04 19:34:05 +01:00
parent 8f256e5c35
commit d02b9ffb5d
4 changed files with 49 additions and 66 deletions

10
.vscode/settings.json vendored
View File

@ -71,6 +71,14 @@
"iomanip": "cpp",
"numbers": "cpp",
"cinttypes": "cpp",
"fstream": "cpp"
"fstream": "cpp",
"list": "cpp",
"locale": "cpp",
"xhash": "cpp",
"xlocbuf": "cpp",
"xlocmes": "cpp",
"xlocmon": "cpp",
"xloctime": "cpp",
"xtree": "cpp"
}
}

View File

@ -1,21 +1,43 @@
MAT 255 255 255 5.0 0.0 0.0 // white light 0
MAT 255 255 255 7.5 0.0 0.0 // white light 0
MAT 255 50 50 0.0 0.0 0.0 // red 1
MAT 50 50 50 0.0 0.0 0.0 // gray 2
MAT 50 255 50 0.0 0.0 0.0 // green 3
MAT 255 10 10 0.0 0.0 0.0 // red 1
MAT 10 10 10 0.0 0.0 0.0 // gray 2
MAT 10 255 10 0.0 0.0 0.0 // green 3
MAT 255 255 255 0.0 0.0 0.0 // white 4
MAT 50 50 255 0.0 0.0 0.0 // white 5
// walls of a long width box
MAT 255 255 255 0.0 0.0 1.0 // diffuse.0 6
MAT 255 255 255 0.0 0.4 1.0 // reflective 7
MAT 255 255 255 0.0 0.6 1.0 // reflective 8
MAT 255 255 255 0.0 0.8 1.0 // reflective 9
MAT 255 255 255 0.0 1.0 1.0 // reflective 10
MAT 255 255 255 0.0 1.0 0.02 // tomato 11
MAT 255 255 255 0.0 1.0 0.15 // tomato 12
MAT 255 255 255 0.0 1.0 0.4 // tomato 13
MAT 255 255 255 0.0 1.0 1.0 // tomato 14
pl 0 0 -3 0 0 1 4 // back wall
pl 0 0 3 0 0 -1 4 // front wall
pl 0 0 3 0 0 -1 5 // front wall
pl 4 0 0 -1 0 0 3 // right wall
pl -4 0 0 1 0 0 1 // left wall
pl 0 3 0 0 -1 0 2 // ceiling
pl 0 -3 0 0 1 0 4 // floor
sp -2.4 0 -1 1.5 7
sp -0.8 0 -1 1.5 8
sp 0.8 0 -1 1.5 9
sp 2.4 0 -1 1.5 10
sp -2.4 1.5 -1 1.5 11
sp -0.8 1.5 -1 1.5 12
sp 0.8 1.5 -1 1.5 13
sp 2.4 1.5 -1 1.5 14
// light quad
qu -1 2.9 -1 2 0 0 0 0 2 0

View File

@ -73,22 +73,18 @@ hitInfo traceRay(Ray ray)
return (hit);
}
Ray newRay(hitInfo hit, Ray ray, inout uint rng_state)
Ray newRay(hitInfo hit, Ray ray, bool is_specular, inout uint rng_state)
{
GPUObject obj;
Ray new_ray;
// vec3 in_unit_sphere;
obj = objects[hit.obj_index];
// in_unit_sphere = normalize(randomVec3(uv, u_time));
// in_unit_sphere *= sign(dot(in_unit_sphere, hit.normal));
vec3 diffuse_dir = normalize(randomHemisphereDirection(hit.normal, rng_state));
vec3 diffuse_dir = normalize(hit.normal + randomDirection(rng_state));
vec3 specular_dir = reflect(ray.direction, hit.normal);
new_ray.origin = hit.position + hit.normal * 0.001;
new_ray.direction = mix(diffuse_dir, specular_dir, obj.roughness);
new_ray.direction = mix(diffuse_dir, specular_dir, obj.roughness * float(is_specular));
return (new_ray);
}
@ -110,14 +106,15 @@ vec3 pathtrace(Ray ray, inout uint rng_state)
}
GPUObject obj = objects[hit.obj_index];
color *= obj.color;
bool is_specular = (obj.metallic >= randomValue(rng_state));
color *= mix(obj.color, vec3(1.0, 1.0, 1.0), is_specular);
light += obj.emission * obj.color;
if (obj.emission > 0.0)
break;
ray = newRay(hit, ray, rng_state);
ray = newRay(hit, ray, is_specular, rng_state);
}
return (color * light);
}
@ -142,14 +139,15 @@ void main()
Ray ray = Ray(u_cameraPosition, ray_direction);
vec3 color = pathtrace(ray, rng_state);
// color = vec3(sqrt(color.x), sqrt(color.y), sqrt(color.z));
float blend = 1.0 / float(u_frameCount + 1);
vec4 accum = imageLoad(accumulation_image, pixel_coords);
accum.rgb = mix(accum.rgb, color, blend);
accum.a = 1.0;
accum.a = 1.0;
imageStore(accumulation_image, pixel_coords, accum);
imageStore(output_image, pixel_coords, accum);
vec4 final_color = vec4(sqrt(accum.r), sqrt(accum.g), sqrt(accum.b), accum.a);
imageStore(output_image, pixel_coords, final_color);
}

View File

@ -1,48 +1,3 @@
float rand_seed = 0;
uint hash( uint x ) {
x += ( x << 10u );
x ^= ( x >> 6u );
x += ( x << 3u );
x ^= ( x >> 11u );
x += ( x << 15u );
return x;
}
uint hash( uvec2 v ) { return hash( v.x ^ hash(v.y) ); }
uint hash( uvec3 v ) { return hash( v.x ^ hash(v.y) ^ hash(v.z) ); }
uint hash( uvec4 v ) { return hash( v.x ^ hash(v.y) ^ hash(v.z) ^ hash(v.w) ); }
float floatConstruct( uint m ) {
const uint ieeeMantissa = 0x007FFFFFu;
const uint ieeeOne = 0x3F800000u;
m &= ieeeMantissa;
m |= ieeeOne;
float f = uintBitsToFloat( m );
return f - 1.0;
}
float randombis( float x ) { return floatConstruct(hash(floatBitsToUint(x))); }
float randombis( vec2 v ) { return floatConstruct(hash(floatBitsToUint(v))); }
float randombis( vec3 v ) { return floatConstruct(hash(floatBitsToUint(v))); }
float randombis( vec4 v ) { return floatConstruct(hash(floatBitsToUint(v))); }
float random(vec2 uv, float time)
{
if (rand_seed == 0)
rand_seed = time;
rand_seed *= 2;
return randombis(vec3(uv.xy, time * rand_seed));
}
vec3 randomVec3(vec2 uv, float time)
{
return (vec3((random(uv, time) - 0.5) * 2, (random(uv, time) - 0.5) * 2, (random(uv, time) - 0.5) * 2));
}
float randomValue(inout uint rng_state)
{
rng_state = rng_state * 747796405 + 2891336453;