Update brute ray shader

This commit is contained in:
luboslenco
2020-09-05 15:18:23 +02:00
parent a252d44226
commit 53bdcbee5d
13 changed files with 52 additions and 34 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,4 +1,3 @@
#include "std/rand.hlsl"
#include "std/attrib.hlsl"
#include "std/math.hlsl"
@@ -89,6 +88,13 @@ void raygeneration() {
TraceRay(scene, RAY_FLAG_FORCE_OPAQUE, ~0, 0, 1, 0, ray, payload);
#endif
#ifdef _EMISSION
if (payload.color.a == -2) {
accum += payload.color.rgb;
break;
}
#endif
// Miss
if (payload.color.a < 0) {
#ifdef _TRANSPARENCY
@@ -97,7 +103,6 @@ void raygeneration() {
transparentHits++;
i--;
}
// else {
#endif
if (i == 0 && constant_buffer.params.x < 0) { // No envmap
@@ -167,7 +172,7 @@ void closesthit(inout RayPayload payload, in BuiltInTriangleIntersectionAttribut
float4 texpaint1 = mytexture1.Load(uint3(tex_coord * size, 0));
float4 texpaint2 = mytexture2.Load(uint3(tex_coord * size, 0));
float3 color = payload.color.rgb * pow(texpaint0.rgb, float3(2.2, 2.2, 2.2));
float3 texcolor = pow(texpaint0.rgb, float3(2.2, 2.2, 2.2));
float3 tangent = float3(0, 0, 0);
float3 binormal = float3(0, 0, 0);
@@ -177,26 +182,27 @@ void closesthit(inout RayPayload payload, in BuiltInTriangleIntersectionAttribut
texpaint1.g = -texpaint1.g;
n = mul(texpaint1.rgb, float3x3(tangent, binormal, n));
float3 diffuseDir = cos_weighted_hemisphere_direction(n, payload.color.a, seed, constant_buffer.eye.w, mytexture_sobol, mytexture_scramble, mytexture_rank);
float f = rand(DispatchRaysIndex().x, DispatchRaysIndex().y, payload.color.a, seed, constant_buffer.eye.w, mytexture_sobol, mytexture_scramble, mytexture_rank);
if (f > 0.5) {
payload.ray_dir = lerp(reflect(WorldRayDirection(), n), cos_weighted_hemisphere_direction(n, payload.color.a, seed, constant_buffer.eye.w, mytexture_sobol, mytexture_scramble, mytexture_rank), pow(texpaint2.g, 1.2));
if (f < 0.5) {
float3 specularDir = reflect(WorldRayDirection(), n);
payload.ray_dir = lerp(specularDir, diffuseDir, texpaint2.g * texpaint2.g);
float3 v = normalize(constant_buffer.eye.xyz - hit_world_position());
float dotNV = max(dot(n, v), 0.0);
float3 specular = surfaceSpecular(texcolor, texpaint2.b);
payload.color.xyz *= envBRDFApprox(specular, texpaint2.g, dotNV);
}
else {
payload.ray_dir = cos_weighted_hemisphere_direction(n, payload.color.a, seed, constant_buffer.eye.w, mytexture_sobol, mytexture_scramble, mytexture_rank);
color = color * (1.0 - texpaint2.b);
payload.ray_dir = diffuseDir;
payload.color.xyz *= surfaceAlbedo(texcolor, texpaint2.b);
}
// float dotNL = dot(n, -WorldRayDirection());
// float3 wi = payload.ray_dir.x * tangent + payload.ray_dir.y * binormal + payload.ray_dir.z * n;
// float dotNV = dot(n, wi);
payload.ray_origin = hit_world_position() + payload.ray_dir * 0.0001f;
payload.color.xyz = color.xyz;
#ifdef _EMISSION
if (texpaint1.a == 1.0) { // matid
payload.color.xyz *= 100.0f;
payload.color.a = -1.0;
payload.color.a = -2.0;
}
#endif
@@ -211,7 +217,7 @@ void closesthit(inout RayPayload payload, in BuiltInTriangleIntersectionAttribut
void miss(inout RayPayload payload) {
#ifdef _EMISSION
if (payload.color.a == -1.0) {
if (payload.color.a == -2.0) {
return;
}
#endif
+28 -18
View File
@@ -15,12 +15,6 @@ void create_basis(float3 normal, out float3 tangent, out float3 binormal) {
binormal = cross(tangent, normal);
}
float schlick_weight(float cosTheta) {
float m = saturate(1. - cosTheta);
float m2 = m * m;
return m2 * m2 * m;
}
void generate_camera_ray(float2 screen_pos, out float3 ray_origin, out float3 ray_dir, float3 eye, float4x4 inv_vp) {
screen_pos.y = -screen_pos.y;
float4 world = mul(float4(screen_pos, 0, 1), inv_vp);
@@ -38,18 +32,34 @@ float2 equirect(float3 normal, float angle) {
}
float3 cos_weighted_hemisphere_direction(float3 n, uint sample, uint seed, int frame, Texture2D<float4> sobol, Texture2D<float4> scramble, Texture2D<float4> rank) {
float2 r = float2(
rand(DispatchRaysIndex().x, DispatchRaysIndex().y, sample, seed, frame, sobol, scramble, rank),
rand(DispatchRaysIndex().x, DispatchRaysIndex().y, sample, seed + 1, frame, sobol, scramble, rank)
);
float3 uu = normalize(cross(n, float3(0.0, 1.0, 1.0)));
float3 vv = cross(uu, n);
float ra = sqrt(r.y);
float rx = ra * cos(6.2831 * r.x);
float ry = ra * sin(6.2831 * r.x);
float rz = sqrt(1.0 - r.y);
float3 rr = float3(rx * uu + ry * vv + rz * n);
return normalize(rr);
const float PI = 3.1415926535;
const float PI2 = PI * 2.0;
float f0 = rand(DispatchRaysIndex().x, DispatchRaysIndex().y, sample, seed, frame, sobol, scramble, rank);
float f1 = rand(DispatchRaysIndex().x, DispatchRaysIndex().y, sample, seed + 1, frame, sobol, scramble, rank);
float z = f0 * 2.0f - 1.0f;
float a = f1 * PI2;
float r = sqrt(1.0f - z * z);
float x = r * cos(a);
float y = r * sin(a);
return normalize(n + float3(x, y, z));
}
float3 surfaceAlbedo(const float3 baseColor, const float metalness) {
return lerp(baseColor, float3(0.0, 0.0, 0.0), metalness);
}
float3 surfaceSpecular(const float3 baseColor, const float metalness) {
return lerp(float3(0.04, 0.04, 0.04), baseColor, metalness);
}
// https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile
float3 envBRDFApprox(float3 specular, float roughness, float dotNV) {
const float4 c0 = float4(-1, -0.0275, -0.572, 0.022);
const float4 c1 = float4(1, 0.0425, 1.04, -0.04);
float4 r = roughness * c0 + c1;
float a004 = min(r.x * r.x, exp2(-9.28 * dotNV)) * r.x + r.y;
float2 ab = float2(-1.04, 1.04) * a004 + r.zw;
return specular * ab.x + ab.y;
}
#endif
@@ -48,8 +48,9 @@ class RenderPathRaytrace {
lastLayer = null;
}
if (!Context.envmapLoaded) Context.loadEnvmap();
var probe = Scene.active.world.probe;
var savedEnvmap = Context.showEnvmapBlur ? probe.radianceMipmaps[0] : probe.radiance;
var savedEnvmap = Context.showEnvmapBlur ? probe.radianceMipmaps[0] : Context.savedEnvmap;
var isLive = Config.raw.brush_live && RenderPathPaint.liveLayerDrawn > 0;
var materialSpace = UIHeader.inst.worktab.position == SpaceMaterial;
var layer = (isLive || materialSpace) ? RenderPathPaint.liveLayer : Context.layer;
@@ -166,8 +167,9 @@ class RenderPathRaytrace {
return;
}
if (!Context.envmapLoaded) Context.loadEnvmap();
var probe = Scene.active.world.probe;
var savedEnvmap = Context.showEnvmapBlur ? probe.radianceMipmaps[0] : probe.radiance;
var savedEnvmap = Context.showEnvmapBlur ? probe.radianceMipmaps[0] : Context.savedEnvmap;
if (lastEnvmap != savedEnvmap || lastLayer != Context.layer.texpaint) {
lastEnvmap = savedEnvmap;
lastLayer = Context.layer.texpaint;