69 lines
1.9 KiB
Text
69 lines
1.9 KiB
Text
|
|
#version 460
|
||
|
|
#extension GL_NV_ray_tracing : require
|
||
|
|
#extension GL_EXT_nonuniform_qualifier : enable
|
||
|
|
|
||
|
|
layout(location = 0) rayPayloadInNV vec3 hitValue;
|
||
|
|
layout(location = 2) rayPayloadNV bool shadowed;
|
||
|
|
hitAttributeNV vec3 attribs;
|
||
|
|
|
||
|
|
layout(binding = 0, set = 0) uniform accelerationStructureNV topLevelAS;
|
||
|
|
layout(binding = 2, set = 0) uniform CameraProperties
|
||
|
|
{
|
||
|
|
mat4 viewInverse;
|
||
|
|
mat4 projInverse;
|
||
|
|
vec4 lightPos;
|
||
|
|
} cam;
|
||
|
|
layout(binding = 3, set = 0) buffer Vertices { vec4 v[]; } vertices;
|
||
|
|
layout(binding = 4, set = 0) buffer Indices { uint i[]; } indices;
|
||
|
|
|
||
|
|
struct Vertex
|
||
|
|
{
|
||
|
|
vec3 pos;
|
||
|
|
vec3 normal;
|
||
|
|
vec3 color;
|
||
|
|
vec2 uv;
|
||
|
|
float _pad0;
|
||
|
|
};
|
||
|
|
|
||
|
|
Vertex unpack(uint index)
|
||
|
|
{
|
||
|
|
vec4 d0 = vertices.v[3 * index + 0];
|
||
|
|
vec4 d1 = vertices.v[3 * index + 1];
|
||
|
|
vec4 d2 = vertices.v[3 * index + 2];
|
||
|
|
|
||
|
|
Vertex v;
|
||
|
|
v.pos = d0.xyz;
|
||
|
|
v.normal = vec3(d0.w, d1.x, d1.y);
|
||
|
|
v.color = vec3(d1.z, d1.w, d2.x);
|
||
|
|
return v;
|
||
|
|
}
|
||
|
|
|
||
|
|
void main()
|
||
|
|
{
|
||
|
|
ivec3 index = ivec3(indices.i[3 * gl_PrimitiveID], indices.i[3 * gl_PrimitiveID + 1], indices.i[3 * gl_PrimitiveID + 2]);
|
||
|
|
|
||
|
|
Vertex v0 = unpack(index.x);
|
||
|
|
Vertex v1 = unpack(index.y);
|
||
|
|
Vertex v2 = unpack(index.z);
|
||
|
|
|
||
|
|
// Interpolate normal
|
||
|
|
const vec3 barycentricCoords = vec3(1.0f - attribs.x - attribs.y, attribs.x, attribs.y);
|
||
|
|
vec3 normal = normalize(v0.normal * barycentricCoords.x + v1.normal * barycentricCoords.y + v2.normal * barycentricCoords.z);
|
||
|
|
|
||
|
|
// Basic lighting
|
||
|
|
vec3 lightVector = normalize(cam.lightPos.xyz);
|
||
|
|
float dot_product = max(dot(lightVector, normal), 0.2);
|
||
|
|
hitValue = v0.color * vec3(dot_product);
|
||
|
|
|
||
|
|
// Shadow casting
|
||
|
|
float tmin = 0.001;
|
||
|
|
float tmax = 100.0;
|
||
|
|
vec3 origin = gl_WorldRayOriginNV + gl_WorldRayDirectionNV * gl_HitTNV;
|
||
|
|
shadowed = true;
|
||
|
|
// Offset indices to match shadow hit/miss index
|
||
|
|
traceNV(topLevelAS, gl_RayFlagsTerminateOnFirstHitNV | gl_RayFlagsOpaqueNV|gl_RayFlagsSkipClosestHitShaderNV, 0xFF, 1, 0, 1, origin, tmin, lightVector, tmax, 2);
|
||
|
|
if (shadowed) {
|
||
|
|
hitValue *= 0.3;
|
||
|
|
}
|
||
|
|
}
|