Skip to content

Commit

Permalink
shadows for raymarchObject
Browse files Browse the repository at this point in the history
see #1250
  • Loading branch information
t3kt committed Nov 17, 2024
1 parent 5fade12 commit 5b0a85d
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 6 deletions.
Binary file modified src/operators/output/raymarchObject.tox
Binary file not shown.
2 changes: 1 addition & 1 deletion src/operators/output/raymarchObject.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
!rop
meta: !meta
opType: raytk.operators.output.raymarchObject
opVersion: '3'
opVersion: '4'
opStatus: alpha
opDef: !def
enable: true
Expand Down
44 changes: 39 additions & 5 deletions src/operators/output/raymarchObject_fragmentShaderBody.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -234,21 +234,48 @@ Ray getViewRay(vec2 shift) {
return ray;
}

float calcShadedLevel(vec3 p, MaterialContext matCtx) {
float sdfLevel = 1.0;
float geoLevel = 1.0;
int priorStage = pushStage(RAYTK_STAGE_SHADOW);
#ifdef THIS_Enablesdfshadow
{
vec3 lightVec = normalize(matCtx.light.pos - p);
Ray shadowRay = Ray(p+matCtx.normal * RAYTK_SURF_DIST*2., lightVec);
Sdf shadowRes = castRay(shadowRay, RAYTK_MAX_DIST);
if (!isNonHitSdf(shadowRes) && shadowRes.x < length(matCtx.light.pos - p)) {
sdfLevel = 0.0;
}
}
#endif
#ifdef THIS_Enablegeoshadow
{
geoLevel = 1.0 - TDHardShadow(matCtx.lightIndex, p);
}
#endif
popStage(priorStage);
return min(sdfLevel, geoLevel);
}

void prepareLights(vec3 p, inout MaterialContext matCtx) {
LightContext lightCtx = createLightContext(matCtx.result, matCtx.normal);
#ifdef RAYTK_GLOBAL_POS_IN_CONTEXT
lightCtx.globalPos = matCtx.globalPos;
#endif

// TODO: shadow
bool useShadow = false;
#if defined(THIS_Enablesdfshadow) && defined(RAYTK_USE_SHADOW)
if (matCtx.result.useShadow) {
useShadow = true;
}
#endif

#if RAYTK_LIGHT_COUNT > 1
{
for (int i = 0; i < RAYTK_LIGHT_COUNT; i++) {
Light light = createLight(uTDLights[i].position.xyz, uTDLights[i].diffuse.xyz);
matCtx.allLights[i] = light;
// matCtx.allShadedLevels[i] = calcShadedLevel(p, matCtx);
// TODO: shadow
matCtx.allShadedLevels[i] = 1;
matCtx.allShadedLevels[i] = calcShadedLevel(p, matCtx);
}
matCtx.light = matCtx.allLights[0];
matCtx.shadedLevel = matCtx.allShadedLevels[0];
Expand All @@ -259,7 +286,11 @@ void prepareLights(vec3 p, inout MaterialContext matCtx) {
Light light = createLight(uTDLights[0].position.xyz, uTDLights[0].diffuse.xyz);
matCtx.light = light;
matCtx.lightIndex = 0;
// TODO: shadow
#ifdef RAYTK_USE_SHADOW
if (useShadow) {
matCtx.shadedLevel = calcShadedLevel(p, matCtx);
}
#endif
}
#endif
}
Expand All @@ -285,6 +316,9 @@ void main()
//-----------------------------------------------------

Ray ray = getViewRay(shift);
vec4 p4 = vec4(ray.pos, 1);
p4 *= uTDMats[iVert.cameraIndex].world;
ray.pos *= p4.xyz;
//-----------------------------------------------------
// render
//-----------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions src/operators/output/raymarchObject_macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ parent().par.Uselimitbox 'THIS_USE_LIMIT_BOX' ''
'RAYTK_MAX_STEPS' 'THIS_Maxsteps'
'RAYTK_SURF_DIST' 'THIS_Surfdist'
'RAYTK_LIGHT_COUNT' 'TD_NUM_LIGHTS'
parent().par.Enablesdfshadow or parent().par.Enablegeoshadow 'RAYTK_USE_SHADOW'
1 change: 1 addition & 0 deletions src/operators/output/raymarchObject_params.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
names source handling readOnlyHandling conversion enable
Limitboxmin[xyz] Limitboxmax[xyz] Distfactor Maxdist param runtime macro 1
Maxsteps Surfdist param constant 1
Enablesdfshadow Enablegeoshadow param macro 1
Binary file modified tests/testCases/operators/output/raymarchObject_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/testCases/operators/output/raymarchObject_test.tox
Binary file not shown.

0 comments on commit 5b0a85d

Please sign in to comment.