From f8b04292e5c6c3637366d4bba00e140abc2afa29 Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Tue, 13 Aug 2024 23:51:15 +0100 Subject: [PATCH 01/38] Refactored volume.glsl for clarity. --- lighting/raymarch/volume.glsl | 55 ++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index 78f4352a..925905cb 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -23,24 +23,32 @@ examples: #endif #endif +#ifndef LIGHT_INTENSITY +#define LIGHT_INTENSITY 100.0 +#endif + #ifndef RAYMARCH_BACKGROUND #define RAYMARCH_BACKGROUND vec3(0.0) #endif #ifndef RAYMARCH_SAMPLES -#define RAYMARCH_SAMPLES 64 +#define RAYMARCH_SAMPLES 512 #endif #ifndef RAYMARCH_MIN_DIST -#define RAYMARCH_MIN_DIST 1.0 +#define RAYMARCH_MIN_DIST 0.1 #endif #ifndef RAYMARCH_MAX_DIST -#define RAYMARCH_MAX_DIST 10.0 +#define RAYMARCH_MAX_DIST 20.0 #endif #ifndef RAYMARCH_MAP_FNC -#define RAYMARCH_MAP_FNC(POS) raymarchMap(POS) +#define RAYMARCH_MAP_FNC raymarchMap +#endif + +#ifndef RAYMARCH_MEDIUM_DENSITY +#define RAYMARCH_MEDIUM_DENSITY 100.0 #endif #ifndef FNC_RAYMARCH_VOLUMERENDER @@ -53,7 +61,7 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec3 cameraForward const float tmax = RAYMARCH_MAX_DIST; const float fSamples = float(RAYMARCH_SAMPLES); const float tstep = tmax/fSamples; - const float absorption = 100.; + const float mediumDensity = RAYMARCH_MEDIUM_DENSITY / fSamples; #ifdef LIGHT_POSITION const int nbSampleLight = 6; @@ -62,32 +70,31 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec3 cameraForward vec3 sun_direction = normalize( LIGHT_POSITION ); #endif - float T = 1.; + float transmittance = 1.; float t = tmin; - vec4 col = vec4(0.0); + vec4 col = vec4(0.0, 0.0, 0.0, 0.0); vec3 pos = rayOrigin; for(int i = 0; i < RAYMARCH_SAMPLES; i++) { - Material res = RAYMARCH_MAP_FNC(pos); - float density = (0.1 - res.sdf); - if (density > 0.0) { - float tmp = density / fSamples; - T *= 1.0 - tmp * absorption; - if( T <= 0.001) - break; - - col += res.albedo * fSamples * tmp * T; - + Material res = RAYMARCH_MAP_FNC(pos); + float dist = -res.sdf; + if (dist > 0.0) { + float density = saturate(dist * mediumDensity); + transmittance *= 1.0 - density; + + col += res.albedo * density * transmittance; + //Light scattering #ifdef LIGHT_POSITION - float Tl = 1.0; + float transmittanceLight = 1.0; for (int j = 0; j < nbSampleLight; j++) { - float densityLight = RAYMARCH_MAP_FNC( pos + sun_direction * float(j) * tstepl ).sdf; - if (densityLight>0.) - Tl *= 1. - densityLight * absorption/fSamples; - if (Tl <= 0.01) - break; + Material resLight = RAYMARCH_MAP_FNC( pos + sun_direction * float(j) * tstepl ); + float distLight = resLight.sdf; + if (distLight > 0.0) { + float densityLight = saturate(distLight * mediumDensity); + transmittanceLight *= 1.0 - densityLight; + } } - col += vec4(LIGHT_COLOR * 80. * tmp * T * Tl, 1.0); + col += vec4(LIGHT_COLOR, 1.0) * LIGHT_INTENSITY * dist / fSamples * transmittance * transmittanceLight; #endif } pos += rayDirection * tstep; From 4919b5e58610293fc013c234ea5ee0ba52ea5f3d Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Thu, 15 Aug 2024 21:42:06 +0100 Subject: [PATCH 02/38] Rewrote volume renderer (GLSL) --- lighting/raymarch/volume.glsl | 85 +++++++++++++++++------------------ 1 file changed, 42 insertions(+), 43 deletions(-) diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index 925905cb..e92cf9a4 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -1,15 +1,14 @@ #include "map.glsl" -#include "normal.glsl" /* -contributors: Inigo Quiles -description: Default raymarching renderer -use: raymarchVolume( in rayOriging, in rayDirection, in cameraForward, - out eyeDepth, out worldPosition, out worldNormal ) +contributors: Shadi El Hajj +description: Default raymarching renderer. Based on Sébastien Hillaire's paper "Physically Based Sky, Atmosphere & Cloud Rendering in Frostbite" +use: raymarchVolume( in rayOrigin, in rayDirection, in cameraForward, + out eyeDepth ) options: - - RAYMARCH_BACKGROUND vec3(0.0) - - RAYMARCH_AMBIENT vec3(1.0) + - RAYMARCH_MEDIUM_DENSITY 1.0 - LIGHT_COLOR vec3(0.5) + - LIGHT_INTENSITY 1.0 - LIGHT_POSITION vec3(0.0, 10.0, -50.0) examples: - /shaders/lighting_raymarching_volume.frag @@ -24,7 +23,7 @@ examples: #endif #ifndef LIGHT_INTENSITY -#define LIGHT_INTENSITY 100.0 +#define LIGHT_INTENSITY 1.0 #endif #ifndef RAYMARCH_BACKGROUND @@ -35,12 +34,16 @@ examples: #define RAYMARCH_SAMPLES 512 #endif +#ifndef RAYMARCH_SAMPLES +#define RAYMARCH_SAMPLES_LIGHT 8 +#endif + #ifndef RAYMARCH_MIN_DIST #define RAYMARCH_MIN_DIST 0.1 #endif #ifndef RAYMARCH_MAX_DIST -#define RAYMARCH_MAX_DIST 20.0 +#define RAYMARCH_MAX_DIST 10.0 #endif #ifndef RAYMARCH_MAP_FNC @@ -48,63 +51,59 @@ examples: #endif #ifndef RAYMARCH_MEDIUM_DENSITY -#define RAYMARCH_MEDIUM_DENSITY 100.0 +#define RAYMARCH_MEDIUM_DENSITY 1.0 #endif #ifndef FNC_RAYMARCH_VOLUMERENDER #define FNC_RAYMARCH_VOLUMERENDER -vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec3 cameraForward, - out float eyeDepth, out vec3 worldPos, out vec3 worldNormal) { +vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec3 cameraForward, out float eyeDepth) { + + const float tmin = RAYMARCH_MIN_DIST; + const float tmax = RAYMARCH_MAX_DIST; + const float tstep = tmax/float(RAYMARCH_SAMPLES); + const float tstepLight = tmax/float(RAYMARCH_SAMPLES_LIGHT); - const float tmin = RAYMARCH_MIN_DIST; - const float tmax = RAYMARCH_MAX_DIST; - const float fSamples = float(RAYMARCH_SAMPLES); - const float tstep = tmax/fSamples; - const float mediumDensity = RAYMARCH_MEDIUM_DENSITY / fSamples; + #if defined(LIGHT_DIRECTION) + vec3 lightDirection = LIGHT_DIRECTION; + #endif - #ifdef LIGHT_POSITION - const int nbSampleLight = 6; - const float fSampleLight = float(nbSampleLight); - const float tstepl = tmax/fSampleLight; - vec3 sun_direction = normalize( LIGHT_POSITION ); + #if defined(LIGHT_POSITION) + vec3 lightDirection = normalize( LIGHT_POSITION ); #endif - float transmittance = 1.; + float transmittance = 1.0; float t = tmin; - vec4 col = vec4(0.0, 0.0, 0.0, 0.0); - vec3 pos = rayOrigin; + vec4 color = vec4(0.0, 0.0, 0.0, 0.0); + vec3 position = rayOrigin; + for(int i = 0; i < RAYMARCH_SAMPLES; i++) { - Material res = RAYMARCH_MAP_FNC(pos); + Material res = RAYMARCH_MAP_FNC(position); float dist = -res.sdf; if (dist > 0.0) { - float density = saturate(dist * mediumDensity); - transmittance *= 1.0 - density; - - col += res.albedo * density * transmittance; + float sampleTransmittance = exp(-dist*RAYMARCH_MEDIUM_DENSITY*tstep); - //Light scattering - #ifdef LIGHT_POSITION float transmittanceLight = 1.0; - for (int j = 0; j < nbSampleLight; j++) { - Material resLight = RAYMARCH_MAP_FNC( pos + sun_direction * float(j) * tstepl ); - float distLight = resLight.sdf; + #if defined(LIGHT_DIRECTION) || defined(LIGHT_POSITION) + for (int j = 0; j < RAYMARCH_SAMPLES_LIGHT; j++) { + Material resLight = RAYMARCH_MAP_FNC(position + lightDirection * float(j) * tstepLight); + float distLight = -resLight.sdf; if (distLight > 0.0) { - float densityLight = saturate(distLight * mediumDensity); - transmittanceLight *= 1.0 - densityLight; + transmittanceLight *= exp(-distLight*RAYMARCH_MEDIUM_DENSITY*tstepLight); } } - col += vec4(LIGHT_COLOR, 1.0) * LIGHT_INTENSITY * dist / fSamples * transmittance * transmittanceLight; #endif + + vec4 luminance = vec4(LIGHT_COLOR, 1.0) * LIGHT_INTENSITY * transmittanceLight; + color += res.albedo * luminance * (RAYMARCH_MEDIUM_DENSITY*tstep) * transmittance; + transmittance *= sampleTransmittance; } - pos += rayDirection * tstep; + position += rayDirection * tstep; } - worldPos = rayOrigin + t * rayDirection; - worldNormal = raymarchNormal( worldPos ); eyeDepth = t * dot(rayDirection, cameraForward); - return col; + return color; } -#endif \ No newline at end of file +#endif From e0a4e2df21d0aa52b004868197c740b9299ed625 Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Thu, 15 Aug 2024 22:06:19 +0100 Subject: [PATCH 03/38] Minor refactor for clarity. --- lighting/raymarch/volume.glsl | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index e92cf9a4..85d5780e 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -80,22 +80,24 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec3 cameraForward for(int i = 0; i < RAYMARCH_SAMPLES; i++) { Material res = RAYMARCH_MAP_FNC(position); float dist = -res.sdf; + float density = RAYMARCH_MEDIUM_DENSITY*tstep; if (dist > 0.0) { - float sampleTransmittance = exp(-dist*RAYMARCH_MEDIUM_DENSITY*tstep); + float sampleTransmittance = exp(-dist*density); float transmittanceLight = 1.0; #if defined(LIGHT_DIRECTION) || defined(LIGHT_POSITION) for (int j = 0; j < RAYMARCH_SAMPLES_LIGHT; j++) { Material resLight = RAYMARCH_MAP_FNC(position + lightDirection * float(j) * tstepLight); float distLight = -resLight.sdf; + float densityLight = RAYMARCH_MEDIUM_DENSITY*tstepLight; if (distLight > 0.0) { - transmittanceLight *= exp(-distLight*RAYMARCH_MEDIUM_DENSITY*tstepLight); + transmittanceLight *= exp(-distLight*densityLight); } } #endif vec4 luminance = vec4(LIGHT_COLOR, 1.0) * LIGHT_INTENSITY * transmittanceLight; - color += res.albedo * luminance * (RAYMARCH_MEDIUM_DENSITY*tstep) * transmittance; + color += res.albedo * luminance * density * transmittance; transmittance *= sampleTransmittance; } position += rayDirection * tstep; From b34a91cb1894aa68e5299bd67cc90928e25ee299 Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Thu, 15 Aug 2024 22:37:06 +0100 Subject: [PATCH 04/38] Added energy-conserving scattering integration --- lighting/raymarch/volume.glsl | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index 85d5780e..c4ed354e 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -1,4 +1,5 @@ #include "map.glsl" +#include "../../math/const.glsl" /* contributors: Shadi El Hajj @@ -79,25 +80,32 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec3 cameraForward for(int i = 0; i < RAYMARCH_SAMPLES; i++) { Material res = RAYMARCH_MAP_FNC(position); - float dist = -res.sdf; + float extinction = -res.sdf; float density = RAYMARCH_MEDIUM_DENSITY*tstep; - if (dist > 0.0) { - float sampleTransmittance = exp(-dist*density); + if (extinction > 0.0) { + float sampleTransmittance = exp(-extinction*density); float transmittanceLight = 1.0; #if defined(LIGHT_DIRECTION) || defined(LIGHT_POSITION) for (int j = 0; j < RAYMARCH_SAMPLES_LIGHT; j++) { Material resLight = RAYMARCH_MAP_FNC(position + lightDirection * float(j) * tstepLight); - float distLight = -resLight.sdf; + float extinctionLight = -resLight.sdf; float densityLight = RAYMARCH_MEDIUM_DENSITY*tstepLight; - if (distLight > 0.0) { - transmittanceLight *= exp(-distLight*densityLight); + if (extinctionLight > 0.0) { + transmittanceLight *= exp(-extinctionLight*densityLight); } } #endif vec4 luminance = vec4(LIGHT_COLOR, 1.0) * LIGHT_INTENSITY * transmittanceLight; - color += res.albedo * luminance * density * transmittance; + + // usual scaterring integration + //color += res.albedo * luminance * density * transmittance; + + // energy-conserving scattering integration + vec4 integScatt = (luminance - luminance * sampleTransmittance) / max(extinction, EPSILON); + color += res.albedo * transmittance * integScatt; + transmittance *= sampleTransmittance; } position += rayDirection * tstep; From 9f0d87a8c8e36734de795d1a82fce942862f471b Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Thu, 15 Aug 2024 23:52:05 +0100 Subject: [PATCH 05/38] Added offset jittering. --- lighting/raymarch.glsl | 8 ++++---- lighting/raymarch/render.glsl | 2 +- lighting/raymarch/volume.glsl | 7 +++++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lighting/raymarch.glsl b/lighting/raymarch.glsl index 9b789d08..7f5e3838 100644 --- a/lighting/raymarch.glsl +++ b/lighting/raymarch.glsl @@ -80,13 +80,13 @@ vec4 raymarch( mat4 viewMatrix, vec2 st #endif #if RAYMARCH_RETURN == 0 - color += RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward); + color += RAYMARCH_RENDER_FNC(camera, rayDirection, st, cameraForward); #elif RAYMARCH_RETURN == 1 - color += RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward, sampleDepth); + color += RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward, st, sampleDepth); #elif RAYMARCH_RETURN == 2 - color += RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward, sampleDepth, mat); + color += RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward, st, sampleDepth, mat); #endif #if RAYMARCH_RETURN >= 1 @@ -199,7 +199,7 @@ vec4 raymarch( mat4 viewMatrix, vec2 st // Single sample vec3 rayDirection = viewMatrix3 * normalize(vec3(st*2.0-1.0, fov)); - return RAYMARCH_RENDER_FNC( camera, rayDirection, cameraForward + return RAYMARCH_RENDER_FNC( camera, rayDirection, cameraForward, st #if RAYMARCH_RETURN >= 1 ,eyeDepth #endif diff --git a/lighting/raymarch/render.glsl b/lighting/raymarch/render.glsl index 92a31172..88860174 100644 --- a/lighting/raymarch/render.glsl +++ b/lighting/raymarch/render.glsl @@ -30,7 +30,7 @@ examples: #ifndef FNC_RAYMARCH_DEFAULT #define FNC_RAYMARCH_DEFAULT -vec4 raymarchDefaultRender( in vec3 rayOrigin, in vec3 rayDirection, vec3 cameraForward +vec4 raymarchDefaultRender( in vec3 rayOrigin, in vec3 rayDirection, vec3 cameraForward, vec2 set #if RAYMARCH_RETURN >= 1 ,out float eyeDepth #endif diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index c4ed354e..0f230355 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -1,4 +1,5 @@ #include "map.glsl" +#include "../../generative/random.glsl" #include "../../math/const.glsl" /* @@ -58,7 +59,7 @@ examples: #ifndef FNC_RAYMARCH_VOLUMERENDER #define FNC_RAYMARCH_VOLUMERENDER -vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec3 cameraForward, out float eyeDepth) { +vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec3 cameraForward, vec2 st, out float eyeDepth) { const float tmin = RAYMARCH_MIN_DIST; const float tmax = RAYMARCH_MAX_DIST; @@ -108,7 +109,9 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec3 cameraForward transmittance *= sampleTransmittance; } - position += rayDirection * tstep; + + float offset = random(st)*0.001; + position += rayDirection * (tstep + offset); } eyeDepth = t * dot(rayDirection, cameraForward); From 6da43125e1a9a65378883aeb7dc56d06ba62d345 Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Fri, 16 Aug 2024 00:00:37 +0100 Subject: [PATCH 06/38] Clean-up --- lighting/raymarch/volume.glsl | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index 0f230355..aa2a5662 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -28,12 +28,8 @@ examples: #define LIGHT_INTENSITY 1.0 #endif -#ifndef RAYMARCH_BACKGROUND -#define RAYMARCH_BACKGROUND vec3(0.0) -#endif - #ifndef RAYMARCH_SAMPLES -#define RAYMARCH_SAMPLES 512 +#define RAYMARCH_SAMPLES 256 #endif #ifndef RAYMARCH_SAMPLES From 1d43f019ed5f5a978904c7ba3cef587bb924f315 Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Fri, 16 Aug 2024 13:09:29 +0100 Subject: [PATCH 07/38] Renamed volume options and added RAYMARCH_VOLUME_MAP_FNC --- lighting/raymarch/map.glsl | 5 +++++ lighting/raymarch/volume.glsl | 38 ++++++++++++++++------------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/lighting/raymarch/map.glsl b/lighting/raymarch/map.glsl index c9f12d41..d7d247c8 100644 --- a/lighting/raymarch/map.glsl +++ b/lighting/raymarch/map.glsl @@ -12,9 +12,14 @@ examples: #define RAYMARCH_MAP_FNC raymarchMap #endif +#ifndef RAYMARCH_VOLUME_MAP_FNC +#define RAYMARCH_VOLUME_MAP_FNC raymarchVolumeMap +#endif + #ifndef FNC_RAYMARCH_MAP #define FNC_RAYMARCH_MAP Material RAYMARCH_MAP_FNC( in vec3 pos ); +Material RAYMARCH_VOLUME_MAP_FNC( in vec3 pos ); #endif \ No newline at end of file diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index aa2a5662..4451468e 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -28,28 +28,24 @@ examples: #define LIGHT_INTENSITY 1.0 #endif -#ifndef RAYMARCH_SAMPLES -#define RAYMARCH_SAMPLES 256 +#ifndef RAYMARCH_VOLUME_SAMPLES +#define RAYMARCH_VOLUME_SAMPLES 256 #endif -#ifndef RAYMARCH_SAMPLES -#define RAYMARCH_SAMPLES_LIGHT 8 +#ifndef RAYMARCH_VOLUME_SAMPLES_LIGHT +#define RAYMARCH_VOLUME_SAMPLES_LIGHT 8 #endif -#ifndef RAYMARCH_MIN_DIST -#define RAYMARCH_MIN_DIST 0.1 +#ifndef RAYMARCH_VOLUME_MAP_FNC +#define RAYMARCH_VOLUME_MAP_FNC raymarchVolumeMap #endif -#ifndef RAYMARCH_MAX_DIST -#define RAYMARCH_MAX_DIST 10.0 +#ifndef RAYMARCH_VOLUME_DENSITY +#define RAYMARCH_VOLUME_DENSITY 1.0 #endif -#ifndef RAYMARCH_MAP_FNC -#define RAYMARCH_MAP_FNC raymarchMap -#endif - -#ifndef RAYMARCH_MEDIUM_DENSITY -#define RAYMARCH_MEDIUM_DENSITY 1.0 +#ifndef RAYMARCH_VOLUME_DITHER +#define RAYMARCH_VOLUME_DITHER 0.1 #endif #ifndef FNC_RAYMARCH_VOLUMERENDER @@ -59,8 +55,8 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec3 cameraForward const float tmin = RAYMARCH_MIN_DIST; const float tmax = RAYMARCH_MAX_DIST; - const float tstep = tmax/float(RAYMARCH_SAMPLES); - const float tstepLight = tmax/float(RAYMARCH_SAMPLES_LIGHT); + const float tstep = tmax/float(RAYMARCH_VOLUME_SAMPLES); + const float tstepLight = tmax/float(RAYMARCH_VOLUME_SAMPLES_LIGHT); #if defined(LIGHT_DIRECTION) vec3 lightDirection = LIGHT_DIRECTION; @@ -75,19 +71,19 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec3 cameraForward vec4 color = vec4(0.0, 0.0, 0.0, 0.0); vec3 position = rayOrigin; - for(int i = 0; i < RAYMARCH_SAMPLES; i++) { + for(int i = 0; i < RAYMARCH_VOLUME_SAMPLES; i++) { Material res = RAYMARCH_MAP_FNC(position); float extinction = -res.sdf; - float density = RAYMARCH_MEDIUM_DENSITY*tstep; + float density = RAYMARCH_VOLUME_DENSITY*tstep; if (extinction > 0.0) { float sampleTransmittance = exp(-extinction*density); float transmittanceLight = 1.0; #if defined(LIGHT_DIRECTION) || defined(LIGHT_POSITION) - for (int j = 0; j < RAYMARCH_SAMPLES_LIGHT; j++) { + for (int j = 0; j < RAYMARCH_VOLUME_SAMPLES_LIGHT; j++) { Material resLight = RAYMARCH_MAP_FNC(position + lightDirection * float(j) * tstepLight); float extinctionLight = -resLight.sdf; - float densityLight = RAYMARCH_MEDIUM_DENSITY*tstepLight; + float densityLight = RAYMARCH_VOLUME_DENSITY*tstepLight; if (extinctionLight > 0.0) { transmittanceLight *= exp(-extinctionLight*densityLight); } @@ -106,7 +102,7 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec3 cameraForward transmittance *= sampleTransmittance; } - float offset = random(st)*0.001; + float offset = random(st)*(tstep*RAYMARCH_VOLUME_DITHER); position += rayDirection * (tstep + offset); } From 834faa88653d4f3d56155b0e65aa28d6b76b2737 Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Fri, 16 Aug 2024 13:35:09 +0100 Subject: [PATCH 08/38] Added volume material. --- lighting/material/volumeNew.glsl | 43 ++++++++++++++++++++++++++++++++ lighting/raymarch/map.glsl | 3 ++- lighting/raymarch/volume.glsl | 25 ++++++++----------- lighting/volumeMaterial.glsl | 15 +++++++++++ 4 files changed, 71 insertions(+), 15 deletions(-) create mode 100644 lighting/material/volumeNew.glsl create mode 100644 lighting/volumeMaterial.glsl diff --git a/lighting/material/volumeNew.glsl b/lighting/material/volumeNew.glsl new file mode 100644 index 00000000..622f3fe2 --- /dev/null +++ b/lighting/material/volumeNew.glsl @@ -0,0 +1,43 @@ +#include "../volumeMaterial.glsl" + +/* +contributors: Shadi El Hajj +description: | + Volume Material Constructor. +use: + - void volumeMaterialNew(out _mat) + - volumeMaterialNew() +*/ + +#ifndef FNC_VOLUME_MATERIAL_NEW +#define FNC_VOLUME_MATERIAL_NEW + +void volumeMaterialNew(out VolumeMaterial _mat) { + _mat.color = vec3(1.0, 1.0, 1.0); + _mat.density = 1.0; + _mat.sdf = RAYMARCH_MAX_DIST; + +} + +VolumeMaterial volumeMaterialNew() { + VolumeMaterial mat; + volumeMaterialNew(mat); + return mat; +} + +VolumeMaterial volumeMaterialNew(vec3 color, float sdf) { + VolumeMaterial mat = volumeMaterialNew(); + mat.color.rgb = color; + mat.sdf = sdf; + return mat; +} + +VolumeMaterial volumeMaterialNew(vec3 color, float density, float sdf) { + VolumeMaterial mat = volumeMaterialNew(); + mat.color.rgb = color; + mat.density = density; + mat.sdf = sdf; + return mat; +} + +#endif diff --git a/lighting/raymarch/map.glsl b/lighting/raymarch/map.glsl index d7d247c8..764f7657 100644 --- a/lighting/raymarch/map.glsl +++ b/lighting/raymarch/map.glsl @@ -1,4 +1,5 @@ #include "../material.glsl" +#include "../volumeMaterial.glsl" /* contributors: Inigo Quiles @@ -20,6 +21,6 @@ examples: #define FNC_RAYMARCH_MAP Material RAYMARCH_MAP_FNC( in vec3 pos ); -Material RAYMARCH_VOLUME_MAP_FNC( in vec3 pos ); +VolumeMaterial RAYMARCH_VOLUME_MAP_FNC( in vec3 pos ); #endif \ No newline at end of file diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index 4451468e..b6f87e3e 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -1,6 +1,7 @@ #include "map.glsl" #include "../../generative/random.glsl" #include "../../math/const.glsl" +#include "../material/volumeNew.glsl" /* contributors: Shadi El Hajj @@ -40,10 +41,6 @@ examples: #define RAYMARCH_VOLUME_MAP_FNC raymarchVolumeMap #endif -#ifndef RAYMARCH_VOLUME_DENSITY -#define RAYMARCH_VOLUME_DENSITY 1.0 -#endif - #ifndef RAYMARCH_VOLUME_DITHER #define RAYMARCH_VOLUME_DITHER 0.1 #endif @@ -68,36 +65,36 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec3 cameraForward float transmittance = 1.0; float t = tmin; - vec4 color = vec4(0.0, 0.0, 0.0, 0.0); + vec3 color = vec3(0.0, 0.0, 0.0); vec3 position = rayOrigin; for(int i = 0; i < RAYMARCH_VOLUME_SAMPLES; i++) { - Material res = RAYMARCH_MAP_FNC(position); + VolumeMaterial res = RAYMARCH_VOLUME_MAP_FNC(position); float extinction = -res.sdf; - float density = RAYMARCH_VOLUME_DENSITY*tstep; + float density = res.density*tstep; if (extinction > 0.0) { float sampleTransmittance = exp(-extinction*density); float transmittanceLight = 1.0; #if defined(LIGHT_DIRECTION) || defined(LIGHT_POSITION) for (int j = 0; j < RAYMARCH_VOLUME_SAMPLES_LIGHT; j++) { - Material resLight = RAYMARCH_MAP_FNC(position + lightDirection * float(j) * tstepLight); + VolumeMaterial resLight = RAYMARCH_VOLUME_MAP_FNC(position + lightDirection * float(j) * tstepLight); float extinctionLight = -resLight.sdf; - float densityLight = RAYMARCH_VOLUME_DENSITY*tstepLight; + float densityLight = res.density*tstepLight; if (extinctionLight > 0.0) { transmittanceLight *= exp(-extinctionLight*densityLight); } } #endif - vec4 luminance = vec4(LIGHT_COLOR, 1.0) * LIGHT_INTENSITY * transmittanceLight; + vec3 luminance = LIGHT_COLOR * LIGHT_INTENSITY * transmittanceLight; // usual scaterring integration - //color += res.albedo * luminance * density * transmittance; + //color += res.color * luminance * density * transmittance; // energy-conserving scattering integration - vec4 integScatt = (luminance - luminance * sampleTransmittance) / max(extinction, EPSILON); - color += res.albedo * transmittance * integScatt; + vec3 integScatt = (luminance - luminance * sampleTransmittance) / max(extinction, EPSILON); + color += res.color * transmittance * integScatt; transmittance *= sampleTransmittance; } @@ -108,7 +105,7 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec3 cameraForward eyeDepth = t * dot(rayDirection, cameraForward); - return color; + return vec4(color, 1.0); } #endif diff --git a/lighting/volumeMaterial.glsl b/lighting/volumeMaterial.glsl new file mode 100644 index 00000000..35a5b4d3 --- /dev/null +++ b/lighting/volumeMaterial.glsl @@ -0,0 +1,15 @@ +/* +contributors: Shadi El Hajj +description: Volume Material Structure +*/ + +#ifndef STR_VOLUME_MATERIAL +#define STR_VOLUME_MATERIAL + +struct VolumeMaterial { + vec3 color; + float density; + float sdf; +}; + +#endif From 6ab889e2e19923b5a771e8300863d1b95f29320f Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Fri, 16 Aug 2024 13:37:17 +0100 Subject: [PATCH 09/38] Restored default renderer. --- lighting/raymarch.glsl | 8 ++++---- lighting/raymarch/render.glsl | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lighting/raymarch.glsl b/lighting/raymarch.glsl index 7f5e3838..9b789d08 100644 --- a/lighting/raymarch.glsl +++ b/lighting/raymarch.glsl @@ -80,13 +80,13 @@ vec4 raymarch( mat4 viewMatrix, vec2 st #endif #if RAYMARCH_RETURN == 0 - color += RAYMARCH_RENDER_FNC(camera, rayDirection, st, cameraForward); + color += RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward); #elif RAYMARCH_RETURN == 1 - color += RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward, st, sampleDepth); + color += RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward, sampleDepth); #elif RAYMARCH_RETURN == 2 - color += RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward, st, sampleDepth, mat); + color += RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward, sampleDepth, mat); #endif #if RAYMARCH_RETURN >= 1 @@ -199,7 +199,7 @@ vec4 raymarch( mat4 viewMatrix, vec2 st // Single sample vec3 rayDirection = viewMatrix3 * normalize(vec3(st*2.0-1.0, fov)); - return RAYMARCH_RENDER_FNC( camera, rayDirection, cameraForward, st + return RAYMARCH_RENDER_FNC( camera, rayDirection, cameraForward #if RAYMARCH_RETURN >= 1 ,eyeDepth #endif diff --git a/lighting/raymarch/render.glsl b/lighting/raymarch/render.glsl index 88860174..92a31172 100644 --- a/lighting/raymarch/render.glsl +++ b/lighting/raymarch/render.glsl @@ -30,7 +30,7 @@ examples: #ifndef FNC_RAYMARCH_DEFAULT #define FNC_RAYMARCH_DEFAULT -vec4 raymarchDefaultRender( in vec3 rayOrigin, in vec3 rayDirection, vec3 cameraForward, vec2 set +vec4 raymarchDefaultRender( in vec3 rayOrigin, in vec3 rayDirection, vec3 cameraForward #if RAYMARCH_RETURN >= 1 ,out float eyeDepth #endif From 243b5c0f807267f6865813679ee3d01ebadac700 Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Fri, 16 Aug 2024 16:24:23 +0100 Subject: [PATCH 10/38] Support for volume to co-exist with PBR. --- lighting/raymarch.glsl | 40 +++++++++++++++++++++-------------- lighting/raymarch/render.glsl | 17 +++++---------- lighting/raymarch/volume.glsl | 9 ++++---- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/lighting/raymarch.glsl b/lighting/raymarch.glsl index 9b789d08..cdeb6ca0 100644 --- a/lighting/raymarch.glsl +++ b/lighting/raymarch.glsl @@ -3,6 +3,7 @@ #include "../space/rotate.glsl" #include "../space/lookAtView.glsl" #include "raymarch/render.glsl" +#include "raymarch/volume.glsl" #include "material/zero.glsl" /* @@ -33,6 +34,10 @@ license: #define RAYMARCH_RENDER_FNC raymarchDefaultRender #endif +#ifndef RAYMARCH_VOLUME_RENDER_FNC +#define RAYMARCH_VOLUME_RENDER_FNC raymarchVolume +#endif + #ifndef RAYMARCH_CAMERA_FOV #define RAYMARCH_CAMERA_FOV 60.0 #endif @@ -75,18 +80,16 @@ vec4 raymarch( mat4 viewMatrix, vec2 st for (int i = 0; i < RAYMARCH_MULTISAMPLE; i++) { vec3 rayDirection = viewMatrix3 * normalize(vec3((st + offset * pixel)*2.0-1.0, fov)); - #if RAYMARCH_RETURN >= 1 float sampleDepth = 0.0; - #endif + float dist = 0.0; - #if RAYMARCH_RETURN == 0 - color += RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward); + #if RAYMARCH_RETURN != 2 + Material mat; + #endif - #elif RAYMARCH_RETURN == 1 - color += RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward, sampleDepth); - - #elif RAYMARCH_RETURN == 2 - color += RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward, sampleDepth, mat); + color += RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward, dist, sampleDepth, mat); + #ifdef RAYMARCH_VOLUME + color += RAYMARCH_VOLUME_RENDER_FNC(camera, rayDirection, st, dist); #endif #if RAYMARCH_RETURN >= 1 @@ -198,15 +201,20 @@ vec4 raymarch( mat4 viewMatrix, vec2 st // Single sample vec3 rayDirection = viewMatrix3 * normalize(vec3(st*2.0-1.0, fov)); - - return RAYMARCH_RENDER_FNC( camera, rayDirection, cameraForward - #if RAYMARCH_RETURN >= 1 - ,eyeDepth + float dist = 0.0; + #if RAYMARCH_RETURN == 0 + float eyeDepth = 0.0; #endif - #if RAYMARCH_RETURN == 2 - ,mat + #if RAYMARCH_RETURN != 2 + Material mat; #endif - ); + + vec4 color = RAYMARCH_RENDER_FNC( camera, rayDirection, cameraForward, dist, eyeDepth, mat); + #ifdef RAYMARCH_VOLUME + color += RAYMARCH_VOLUME_RENDER_FNC(camera, rayDirection, st, dist); + #endif + + return color; #endif } diff --git a/lighting/raymarch/render.glsl b/lighting/raymarch/render.glsl index 92a31172..4d0465ab 100644 --- a/lighting/raymarch/render.glsl +++ b/lighting/raymarch/render.glsl @@ -30,18 +30,8 @@ examples: #ifndef FNC_RAYMARCH_DEFAULT #define FNC_RAYMARCH_DEFAULT -vec4 raymarchDefaultRender( in vec3 rayOrigin, in vec3 rayDirection, vec3 cameraForward -#if RAYMARCH_RETURN >= 1 - ,out float eyeDepth -#endif -#if RAYMARCH_RETURN == 2 - ,out Material res -#endif - ) { - -#if RAYMARCH_RETURN != 2 - Material res; -#endif +vec4 raymarchDefaultRender( in vec3 rayOrigin, in vec3 rayDirection, vec3 cameraForward, + out float dist ,out float eyeDepth, out Material res) { res = raymarchCast(rayOrigin, rayDirection); float t = res.sdf; @@ -55,6 +45,9 @@ vec4 raymarchDefaultRender( in vec3 rayOrigin, in vec3 rayDirection, vec3 camera res.ambientOcclusion = raymarchAO(res.position, res.normal); res.V = -rayDirection; color = RAYMARCH_SHADING_FNC(res); + dist = t; + } else { + dist = RAYMARCH_MAX_DIST; } color.rgb = raymarchFog(color.rgb, t, rayOrigin, rayDirection); diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index b6f87e3e..e43fac5d 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -48,7 +48,7 @@ examples: #ifndef FNC_RAYMARCH_VOLUMERENDER #define FNC_RAYMARCH_VOLUMERENDER -vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec3 cameraForward, vec2 st, out float eyeDepth) { +vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec2 st, float minDist) { const float tmin = RAYMARCH_MIN_DIST; const float tmax = RAYMARCH_MAX_DIST; @@ -69,10 +69,11 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec3 cameraForward vec3 position = rayOrigin; for(int i = 0; i < RAYMARCH_VOLUME_SAMPLES; i++) { + vec3 position = rayOrigin + rayDirection * t; VolumeMaterial res = RAYMARCH_VOLUME_MAP_FNC(position); float extinction = -res.sdf; float density = res.density*tstep; - if (extinction > 0.0) { + if (t < minDist && extinction > 0.0) { float sampleTransmittance = exp(-extinction*density); float transmittanceLight = 1.0; @@ -100,11 +101,9 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec3 cameraForward } float offset = random(st)*(tstep*RAYMARCH_VOLUME_DITHER); - position += rayDirection * (tstep + offset); + t += tstep + offset; } - eyeDepth = t * dot(rayDirection, cameraForward); - return vec4(color, 1.0); } From c87fd14cad041fd3c1539b8e2d994101229e47af Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Mon, 19 Aug 2024 10:01:34 +0100 Subject: [PATCH 11/38] HLSL port. --- lighting/material/volumeNew.hlsl | 43 ++++++++++ lighting/raymarch.glsl | 96 ++++++++++----------- lighting/raymarch.hlsl | 140 +++++++++++++++++-------------- lighting/raymarch/map.hlsl | 6 ++ lighting/raymarch/render.hlsl | 20 ++--- lighting/raymarch/volume.hlsl | 138 ++++++++++++++++-------------- lighting/volumeMaterial.hlsl | 15 ++++ 7 files changed, 268 insertions(+), 190 deletions(-) create mode 100644 lighting/material/volumeNew.hlsl create mode 100644 lighting/volumeMaterial.hlsl diff --git a/lighting/material/volumeNew.hlsl b/lighting/material/volumeNew.hlsl new file mode 100644 index 00000000..d5b7cbc2 --- /dev/null +++ b/lighting/material/volumeNew.hlsl @@ -0,0 +1,43 @@ +#include "../volumeMaterial.hlsl" + +/* +contributors: Shadi El Hajj +description: | + Volume Material Constructor. +use: + - void volumeMaterialNew(out _mat) + - volumeMaterialNew() +*/ + +#ifndef FNC_VOLUME_MATERIAL_NEW +#define FNC_VOLUME_MATERIAL_NEW + +void volumeMaterialNew(out VolumeMaterial _mat) { + _mat.color = float3(1.0, 1.0, 1.0); + _mat.density = 1.0; + _mat.sdf = RAYMARCH_MAX_DIST; + +} + +VolumeMaterial volumeMaterialNew() { + VolumeMaterial mat; + volumeMaterialNew(mat); + return mat; +} + +VolumeMaterial volumeMaterialNew(float3 color, float sdf) { + VolumeMaterial mat = volumeMaterialNew(); + mat.color.rgb = color; + mat.sdf = sdf; + return mat; +} + +VolumeMaterial volumeMaterialNew(float3 color, float density, float sdf) { + VolumeMaterial mat = volumeMaterialNew(); + mat.color.rgb = color; + mat.density = density; + mat.sdf = sdf; + return mat; +} + +#endif diff --git a/lighting/raymarch.glsl b/lighting/raymarch.glsl index cdeb6ca0..c7438ba9 100644 --- a/lighting/raymarch.glsl +++ b/lighting/raymarch.glsl @@ -70,8 +70,8 @@ vec4 raymarch( mat4 viewMatrix, vec2 st eyeDepth = 0.0; #endif #if RAYMARCH_RETURN == 2 - Material tmp; - materialZero(tmp); + Material matAcc; + materialZero(matAcc); #endif vec2 pixel = 1.0/RESOLUTION; @@ -98,52 +98,52 @@ vec4 raymarch( mat4 viewMatrix, vec2 st #if RAYMARCH_RETURN == 2 // Accumulate material properties - tmp.albedo += mat.albedo; - tmp.emissive += mat.emissive; - tmp.position += mat.position; - tmp.normal += mat.normal; + matAcc.albedo += mat.albedo; + matAcc.emissive += mat.emissive; + matAcc.position += mat.position; + matAcc.normal += mat.normal; #if defined(SCENE_BACK_SURFACE) - tmp.normal_back += mat.normal_back; + matAcc.normal_back += mat.normal_back; #endif - tmp.ior += mat.ior; - tmp.f0 += mat.f0; - tmp.roughness += mat.roughness; - tmp.metallic += mat.metallic; - tmp.ambientOcclusion += mat.ambientOcclusion; + matAcc.ior += mat.ior; + matAcc.f0 += mat.f0; + matAcc.roughness += mat.roughness; + matAcc.metallic += mat.metallic; + matAcc.ambientOcclusion += mat.ambientOcclusion; #if defined(SHADING_MODEL_CLEAR_COAT) - tmp.clearCoat += mat.clearCoat; - tmp.clearCoatRoughness += mat.clearCoatRoughness; + matAcc.clearCoat += mat.clearCoat; + matAcc.clearCoatRoughness += mat.clearCoatRoughness; #if defined(MATERIAL_HAS_CLEAR_COAT_NORMAL) - tmp.clearCoatNormal += mat.clearCoatNormal; + matAcc.clearCoatNormal += mat.clearCoatNormal; #endif #endif #if defined(SHADING_MODEL_IRIDESCENCE) - tmp.thickness += mat.thickness; + matAcc.thickness += mat.thickness; #endif #if defined(SHADING_MODEL_SUBSURFACE) - tmp.subsurfaceColor += mat.subsurfaceColor; - tmp.subsurfacePower += mat.subsurfacePower; - tmp.subsurfaceThickness += mat.subsurfaceThickness; + matAcc.subsurfaceColor += mat.subsurfaceColor; + matAcc.subsurfacePower += mat.subsurfacePower; + matAcc.subsurfaceThickness += mat.subsurfaceThickness; #endif #if defined(SHADING_MODEL_CLOTH) - tmp.sheenColor += mat.sheenColor; + matAcc.sheenColor += mat.sheenColor; #endif #if defined(SHADING_MODEL_SPECULAR_GLOSSINESS) - tmp.specularColor += mat.specularColor; - tmp.glossiness += mat.glossiness; + matAcc.specularColor += mat.specularColor; + matAcc.glossiness += mat.glossiness; #endif // I don't thing nobody needs this - // tmp.V += mat.V; - // tmp.R += mat.R; - // tmp.NoV += mat.NoV; + // matAcc.V += mat.V; + // matAcc.R += mat.R; + // matAcc.NoV += mat.NoV; #endif offset = rotate(offset, HALF_PI); @@ -156,44 +156,44 @@ vec4 raymarch( mat4 viewMatrix, vec2 st #if RAYMARCH_RETURN == 2 // Average material - mat.albedo = tmp.albedo * RAYMARCH_MULTISAMPLE_FACTOR; - mat.emissive = tmp.emissive * RAYMARCH_MULTISAMPLE_FACTOR; - mat.position = tmp.position * RAYMARCH_MULTISAMPLE_FACTOR; - mat.normal = tmp.normal * RAYMARCH_MULTISAMPLE_FACTOR; + mat.albedo = matAcc.albedo * RAYMARCH_MULTISAMPLE_FACTOR; + mat.emissive = matAcc.emissive * RAYMARCH_MULTISAMPLE_FACTOR; + mat.position = matAcc.position * RAYMARCH_MULTISAMPLE_FACTOR; + mat.normal = matAcc.normal * RAYMARCH_MULTISAMPLE_FACTOR; #if defined(SCENE_BACK_SURFACE) - mat.normal_back = tmp.normal_back * RAYMARCH_MULTISAMPLE_FACTOR; + mat.normal_back = matAcc.normal_back * RAYMARCH_MULTISAMPLE_FACTOR; #endif - mat.ior = tmp.ior * RAYMARCH_MULTISAMPLE_FACTOR; - mat.f0 = tmp.f0 * RAYMARCH_MULTISAMPLE_FACTOR; - mat.roughness = tmp.roughness * RAYMARCH_MULTISAMPLE_FACTOR; - mat.metallic = tmp.metallic * RAYMARCH_MULTISAMPLE_FACTOR; - mat.ambientOcclusion = tmp.ambientOcclusion * RAYMARCH_MULTISAMPLE_FACTOR; + mat.ior = matAcc.ior * RAYMARCH_MULTISAMPLE_FACTOR; + mat.f0 = matAcc.f0 * RAYMARCH_MULTISAMPLE_FACTOR; + mat.roughness = matAcc.roughness * RAYMARCH_MULTISAMPLE_FACTOR; + mat.metallic = matAcc.metallic * RAYMARCH_MULTISAMPLE_FACTOR; + mat.ambientOcclusion = matAcc.ambientOcclusion * RAYMARCH_MULTISAMPLE_FACTOR; #if defined(SHADING_MODEL_CLEAR_COAT) - mat.clearCoat = tmp.clearCoat * RAYMARCH_MULTISAMPLE_FACTOR; - mat.clearCoatRoughness = tmp.clearCoatRoughness * RAYMARCH_MULTISAMPLE_FACTOR; + mat.clearCoat = matAcc.clearCoat * RAYMARCH_MULTISAMPLE_FACTOR; + mat.clearCoatRoughness = matAcc.clearCoatRoughness * RAYMARCH_MULTISAMPLE_FACTOR; #if defined(MATERIAL_HAS_CLEAR_COAT_NORMAL) - mat.clearCoatNormal = tmp.clearCoatNormal * RAYMARCH_MULTISAMPLE_FACTOR; + mat.clearCoatNormal = matAcc.clearCoatNormal * RAYMARCH_MULTISAMPLE_FACTOR; #endif #endif #if defined(SHADING_MODEL_IRIDESCENCE) - mat.thickness = tmp.thickness * RAYMARCH_MULTISAMPLE_FACTOR; + mat.thickness = matAcc.thickness * RAYMARCH_MULTISAMPLE_FACTOR; #endif #if defined(SHADING_MODEL_SUBSURFACE) - mat.subsurfaceColor = tmp.subsurfaceColor * RAYMARCH_MULTISAMPLE_FACTOR; - mat.subsurfacePower = tmp.subsurfacePower * RAYMARCH_MULTISAMPLE_FACTOR; - mat.subsurfaceThickness = tmp.subsurfaceThickness * RAYMARCH_MULTISAMPLE_FACTOR; + mat.subsurfaceColor = matAcc.subsurfaceColor * RAYMARCH_MULTISAMPLE_FACTOR; + mat.subsurfacePower = matAcc.subsurfacePower * RAYMARCH_MULTISAMPLE_FACTOR; + mat.subsurfaceThickness = matAcc.subsurfaceThickness * RAYMARCH_MULTISAMPLE_FACTOR; #endif #if defined(SHADING_MODEL_CLOTH) - mat.sheenColor = tmp.sheenColor * RAYMARCH_MULTISAMPLE_FACTOR; + mat.sheenColor = matAcc.sheenColor * RAYMARCH_MULTISAMPLE_FACTOR; #endif #if defined(SHADING_MODEL_SPECULAR_GLOSSINESS) - mat.specularColor = tmp.specularColor * RAYMARCH_MULTISAMPLE_FACTOR; - mat.glossiness = tmp.glossiness * RAYMARCH_MULTISAMPLE_FACTOR; + mat.specularColor = matAcc.specularColor * RAYMARCH_MULTISAMPLE_FACTOR; + mat.glossiness = matAcc.glossiness * RAYMARCH_MULTISAMPLE_FACTOR; #endif // I don't thing nobody needs this - // mat.V = tmp.V * RAYMARCH_MULTISAMPLE_FACTOR; - // mat.R = tmp.R * RAYMARCH_MULTISAMPLE_FACTOR; - // mat.NoV = tmp.NoV * RAYMARCH_MULTISAMPLE_FACTOR; + // mat.V = matAcc.V * RAYMARCH_MULTISAMPLE_FACTOR; + // mat.R = matAcc.R * RAYMARCH_MULTISAMPLE_FACTOR; + // mat.NoV = matAcc.NoV * RAYMARCH_MULTISAMPLE_FACTOR; #endif return color * RAYMARCH_MULTISAMPLE_FACTOR; diff --git a/lighting/raymarch.hlsl b/lighting/raymarch.hlsl index 33fbab77..fdea4f7a 100644 --- a/lighting/raymarch.hlsl +++ b/lighting/raymarch.hlsl @@ -2,6 +2,7 @@ #include "../space/rotate.hlsl" #include "../space/lookAtView.hlsl" #include "raymarch/render.hlsl" +#include "raymarch/volume.hlsl" #include "material/zero.hlsl" /* @@ -30,6 +31,10 @@ license: #define RAYMARCH_RENDER_FNC raymarchDefaultRender #endif +#ifndef RAYMARCH_VOLUME_RENDER_FNC +#define RAYMARCH_VOLUME_RENDER_FNC raymarchVolume +#endif + #ifndef RESOLUTION #define RESOLUTION _ScreenParams.xy #endif @@ -65,8 +70,8 @@ float4 raymarch(float4x4 viewMatrix, float2 st eyeDepth = 0.0; #endif #if RAYMARCH_RETURN == 2 - Material tmp; - materialZero(tmp); + Material matAcc; + materialZero(matAcc); #endif float2 pixel = 1.0/RESOLUTION; @@ -75,72 +80,70 @@ float4 raymarch(float4x4 viewMatrix, float2 st for (int i = 0; i < RAYMARCH_MULTISAMPLE; i++) { float3 rayDirection = mul((float3x3)viewMatrix, normalize(float3((st + offset * pixel)*2.0-1.0, fov))); - #if RAYMARCH_RETURN >= 1 float sampleDepth = 0.0; - #endif + float dist = 0.0; - #if RAYMARCH_RETURN == 0 - color += RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward); + #if RAYMARCH_RETURN != 2 + Material mat; + #endif - #elif RAYMARCH_RETURN == 1 - color += RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward, sampleDepth); - - #elif RAYMARCH_RETURN == 2 - color += RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward, sampleDepth, mat); + color += RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward, dist, sampleDepth, mat); + #ifdef RAYMARCH_VOLUME + color += RAYMARCH_VOLUME_RENDER_FNC(camera, rayDirection, st, dist); #endif - + #if RAYMARCH_RETURN >= 1 eyeDepth += sampleDepth; #endif #if RAYMARCH_RETURN == 2 // Accumulate material properties - tmp.albedo += mat.albedo; - tmp.emissive += mat.emissive; - tmp.position += mat.position; - tmp.normal += mat.normal; + matAcc.albedo += mat.albedo; + matAcc.emissive += mat.emissive; + matAcc.position += mat.position; + matAcc.normal += mat.normal; #if defined(SCENE_BACK_SURFACE) - tmp.normal_back += mat.normal_back; + matAcc.normal_back += mat.normal_back; #endif - tmp.ior += mat.ior; - tmp.f0 += mat.f0; - tmp.roughness += mat.roughness; - tmp.metallic += mat.metallic; - tmp.ambientOcclusion += mat.ambientOcclusion; + matAcc.ior += mat.ior; + matAcc.f0 += mat.f0; + matAcc.roughness += mat.roughness; + matAcc.metallic += mat.metallic; + matAcc.ambientOcclusion += mat.ambientOcclusion; #if defined(SHADING_MODEL_CLEAR_COAT) - tmp.clearCoat += mat.clearCoat; - tmp.clearCoatRoughness += mat.clearCoatRoughness; + matAcc.clearCoat += mat.clearCoat; + matAcc.clearCoatRoughness += mat.clearCoatRoughness; #if defined(MATERIAL_HAS_CLEAR_COAT_NORMAL) - tmp.clearCoatNormal += mat.clearCoatNormal; + matAcc.clearCoatNormal += mat.clearCoatNormal; #endif #endif #if defined(SHADING_MODEL_IRIDESCENCE) - tmp.thickness += mat.thickness; + matAcc.thickness += mat.thickness; #endif #if defined(SHADING_MODEL_SUBSURFACE) - tmp.subsurfaceColor += mat.subsurfaceColor; - tmp.subsurfacePower += mat.subsurfacePower; - tmp.subsurfaceThickness += mat.subsurfaceThickness; + matAcc.subsurfaceColor += mat.subsurfaceColor; + matAcc.subsurfacePower += mat.subsurfacePower; + matAcc.subsurfaceThickness += mat.subsurfaceThickness; #endif #if defined(SHADING_MODEL_CLOTH) - tmp.sheenColor += mat.sheenColor; + matAcc.sheenColor += mat.sheenColor; #endif #if defined(SHADING_MODEL_SPECULAR_GLOSSINESS) - tmp.specularColor += mat.specularColor; - tmp.glossiness += mat.glossiness; + matAcc.specularColor += mat.specularColor; + matAcc.glossiness += mat.glossiness; #endif // I don't thing nobody needs this - // tmp.V += mat.V; - // tmp.R += mat.R; - // tmp.NoV += mat.NoV; + // matAcc.V += mat.V; + // matAcc.R += mat.R; + // matAcc.NoV += mat.NoV; #endif offset = rotate(offset, HALF_PI); @@ -153,58 +156,65 @@ float4 raymarch(float4x4 viewMatrix, float2 st #if RAYMARCH_RETURN == 2 // Average material - mat.albedo = tmp.albedo * RAYMARCH_MULTISAMPLE_FACTOR; - mat.emissive = tmp.emissive * RAYMARCH_MULTISAMPLE_FACTOR; - mat.position = tmp.position * RAYMARCH_MULTISAMPLE_FACTOR; - mat.normal = tmp.normal * RAYMARCH_MULTISAMPLE_FACTOR; + mat.albedo = matAcc.albedo * RAYMARCH_MULTISAMPLE_FACTOR; + mat.emissive = matAcc.emissive * RAYMARCH_MULTISAMPLE_FACTOR; + mat.position = matAcc.position * RAYMARCH_MULTISAMPLE_FACTOR; + mat.normal = matAcc.normal * RAYMARCH_MULTISAMPLE_FACTOR; #if defined(SCENE_BACK_SURFACE) - mat.normal_back = tmp.normal_back * RAYMARCH_MULTISAMPLE_FACTOR; + mat.normal_back = matAcc.normal_back * RAYMARCH_MULTISAMPLE_FACTOR; #endif - mat.ior = tmp.ior * RAYMARCH_MULTISAMPLE_FACTOR; - mat.f0 = tmp.f0 * RAYMARCH_MULTISAMPLE_FACTOR; - mat.roughness = tmp.roughness * RAYMARCH_MULTISAMPLE_FACTOR; - mat.metallic = tmp.metallic * RAYMARCH_MULTISAMPLE_FACTOR; - mat.ambientOcclusion = tmp.ambientOcclusion * RAYMARCH_MULTISAMPLE_FACTOR; + mat.ior = matAcc.ior * RAYMARCH_MULTISAMPLE_FACTOR; + mat.f0 = matAcc.f0 * RAYMARCH_MULTISAMPLE_FACTOR; + mat.roughness = matAcc.roughness * RAYMARCH_MULTISAMPLE_FACTOR; + mat.metallic = matAcc.metallic * RAYMARCH_MULTISAMPLE_FACTOR; + mat.ambientOcclusion = matAcc.ambientOcclusion * RAYMARCH_MULTISAMPLE_FACTOR; #if defined(SHADING_MODEL_CLEAR_COAT) - mat.clearCoat = tmp.clearCoat * RAYMARCH_MULTISAMPLE_FACTOR; - mat.clearCoatRoughness = tmp.clearCoatRoughness * RAYMARCH_MULTISAMPLE_FACTOR; + mat.clearCoat = matAcc.clearCoat * RAYMARCH_MULTISAMPLE_FACTOR; + mat.clearCoatRoughness = matAcc.clearCoatRoughness * RAYMARCH_MULTISAMPLE_FACTOR; #if defined(MATERIAL_HAS_CLEAR_COAT_NORMAL) - mat.clearCoatNormal = tmp.clearCoatNormal * RAYMARCH_MULTISAMPLE_FACTOR; + mat.clearCoatNormal = matAcc.clearCoatNormal * RAYMARCH_MULTISAMPLE_FACTOR; #endif #endif #if defined(SHADING_MODEL_IRIDESCENCE) - mat.thickness = tmp.thickness * RAYMARCH_MULTISAMPLE_FACTOR; + mat.thickness = matAcc.thickness * RAYMARCH_MULTISAMPLE_FACTOR; #endif #if defined(SHADING_MODEL_SUBSURFACE) - mat.subsurfaceColor = tmp.subsurfaceColor * RAYMARCH_MULTISAMPLE_FACTOR; - mat.subsurfacePower = tmp.subsurfacePower * RAYMARCH_MULTISAMPLE_FACTOR; - mat.subsurfaceThickness = tmp.subsurfaceThickness * RAYMARCH_MULTISAMPLE_FACTOR; + mat.subsurfaceColor = matAcc.subsurfaceColor * RAYMARCH_MULTISAMPLE_FACTOR; + mat.subsurfacePower = matAcc.subsurfacePower * RAYMARCH_MULTISAMPLE_FACTOR; + mat.subsurfaceThickness = matAcc.subsurfaceThickness * RAYMARCH_MULTISAMPLE_FACTOR; #endif #if defined(SHADING_MODEL_CLOTH) - mat.sheenColor = tmp.sheenColor * RAYMARCH_MULTISAMPLE_FACTOR; + mat.sheenColor = matAcc.sheenColor * RAYMARCH_MULTISAMPLE_FACTOR; #endif #if defined(SHADING_MODEL_SPECULAR_GLOSSINESS) - mat.specularColor = tmp.specularColor * RAYMARCH_MULTISAMPLE_FACTOR; - mat.glossiness = tmp.glossiness * RAYMARCH_MULTISAMPLE_FACTOR; + mat.specularColor = matAcc.specularColor * RAYMARCH_MULTISAMPLE_FACTOR; + mat.glossiness = matAcc.glossiness * RAYMARCH_MULTISAMPLE_FACTOR; #endif // I don't thing nobody needs this - // mat.V = tmp.V * RAYMARCH_MULTISAMPLE_FACTOR; - // mat.R = tmp.R * RAYMARCH_MULTISAMPLE_FACTOR; - // mat.NoV = tmp.NoV * RAYMARCH_MULTISAMPLE_FACTOR; + // mat.V = matAcc.V * RAYMARCH_MULTISAMPLE_FACTOR; + // mat.R = matAcc.R * RAYMARCH_MULTISAMPLE_FACTOR; + // mat.NoV = matAcc.NoV * RAYMARCH_MULTISAMPLE_FACTOR; #endif return color * RAYMARCH_MULTISAMPLE_FACTOR; #else - float3 rayDirection = mul((float3x3)viewMatrix, normalize(float3(st * 2.0 - 1.0, fov))); - return RAYMARCH_RENDER_FNC( camera, rayDirection, cameraForward - #if RAYMARCH_RETURN >= 1 - ,eyeDepth + // Single sample + float3 rayDirection = mul((float3x3)viewMatrix, normalize(float3(st * 2.0 - 1.0, fov))); + float dist = 0.0; + #if RAYMARCH_RETURN == 0 + float eyeDepth = 0.0; #endif - #if RAYMARCH_RETURN == 2 - ,mat + #if RAYMARCH_RETURN != 2 + Material mat; #endif - ); + + float4 color = RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward, dist, eyeDepth, mat); + #ifdef RAYMARCH_VOLUME + color += RAYMARCH_VOLUME_RENDER_FNC(camera, rayDirection, st, dist); + #endif + + return color; #endif } diff --git a/lighting/raymarch/map.hlsl b/lighting/raymarch/map.hlsl index 4b8c4689..cd388ffa 100644 --- a/lighting/raymarch/map.hlsl +++ b/lighting/raymarch/map.hlsl @@ -1,4 +1,5 @@ #include "../material.hlsl" +#include "../volumeMaterial.hlsl" /* contributors: Inigo Quiles @@ -10,9 +11,14 @@ use: raymarchMap( in pos ) #define RAYMARCH_MAP_FNC raymarchMap #endif +#ifndef RAYMARCH_VOLUME_MAP_FNC +#define RAYMARCH_VOLUME_MAP_FNC raymarchVolumeMap +#endif + #ifndef FNC_RAYMARCH_MAP #define FNC_RAYMARCH_MAP Material RAYMARCH_MAP_FNC( in float3 pos ); +VolumeMaterial RAYMARCH_VOLUME_MAP_FNC(in float3 pos); #endif \ No newline at end of file diff --git a/lighting/raymarch/render.hlsl b/lighting/raymarch/render.hlsl index 1a757bd6..be2ac2c2 100644 --- a/lighting/raymarch/render.hlsl +++ b/lighting/raymarch/render.hlsl @@ -28,18 +28,8 @@ options: #ifndef FNC_RAYMARCH_DEFAULT #define FNC_RAYMARCH_DEFAULT -float4 raymarchDefaultRender(in float3 rayOrigin, in float3 rayDirection, float3 cameraForward -#if RAYMARCH_RETURN != 0 - ,out float eyeDepth -#endif -#if RAYMARCH_RETURN == 2 - ,out Material res -#endif - ) { - -#if RAYMARCH_RETURN != 2 - Material res; -#endif +float4 raymarchDefaultRender(in float3 rayOrigin, in float3 rayDirection, float3 cameraForward, + out float dist, out float eyeDepth, out Material res) { res = raymarchCast(rayOrigin, rayDirection); float t = res.sdf; @@ -53,10 +43,14 @@ float4 raymarchDefaultRender(in float3 rayOrigin, in float3 rayDirection, float3 res.ambientOcclusion = raymarchAO(res.position, res.normal); res.V = -rayDirection; color = RAYMARCH_SHADING_FNC(res); + dist = t; + } + else { + dist = RAYMARCH_MAX_DIST; } color.rgb = raymarchFog(color.rgb, t, rayOrigin, rayDirection); - #if RAYMARCH_RETURN != 0 + #if RAYMARCH_RETURN >= 1 // Eye-space depth. See https://www.shadertoy.com/view/4tByz3 eyeDepth = t * dot(rayDirection, cameraForward); #endif diff --git a/lighting/raymarch/volume.hlsl b/lighting/raymarch/volume.hlsl index 826eeab5..5441d2de 100644 --- a/lighting/raymarch/volume.hlsl +++ b/lighting/raymarch/volume.hlsl @@ -1,101 +1,111 @@ #include "map.hlsl" -#include "normal.hlsl" +#include "../../generative/random.hlsl" +#include "../../math/const.hlsl" +#include "../material/volumeNew.hlsl" /* -contributors: Inigo Quiles -description: Default raymarching renderer -use: raymarchVolume( in rayOriging, in rayDirection, in cameraForward, - out eyeDepth, out worldPosition, out worldNormal ) +contributors: Shadi El Hajj +description: Default raymarching renderer. Based on Sébastien Hillaire's paper "Physically Based Sky, Atmosphere & Cloud Rendering in Frostbite" +use: raymarchVolume( in rayOrigin, in rayDirection, in cameraForward, st, float minDist ) options: - - RAYMARCH_BACKGROUND float3(0.0) - - LIGHT_COLOR float3(0.5) - - LIGHT_POSITION float3(0.0, 10.0, -50.0) + - RAYMARCH_VOLUME_SAMPLES 256 + - RAYMARCH_VOLUME_SAMPLES_LIGHT 8 + - RAYMARCH_VOLUME_MAP_FNC raymarchVolumeMap + - RAYMARCH_VOLUME_DITHER 0.1 + - LIGHT_COLOR float3(0.5) + - LIGHT_INTENSITY 1.0 + - LIGHT_POSITION float3(0.0, 10.0, -50.0) +examples: + - /shaders/lighting_raymarching_volume.frag */ #ifndef LIGHT_COLOR #if defined(GLSLVIEWER) #define LIGHT_COLOR u_lightColor #else -#define LIGHT_COLOR float3(0.5, 0.5, 0.5) +#define LIGHT_COLOR float3(0.5) #endif #endif -#ifndef RAYMARCH_BACKGROUND -#define RAYMARCH_BACKGROUND float3(0.0, 0.0, 0.0) +#ifndef LIGHT_INTENSITY +#define LIGHT_INTENSITY 1.0 #endif -#ifndef RAYMARCH_SAMPLES -#define RAYMARCH_SAMPLES 64 +#ifndef RAYMARCH_VOLUME_SAMPLES +#define RAYMARCH_VOLUME_SAMPLES 256 #endif -#ifndef RAYMARCH_MIN_DIST -#define RAYMARCH_MIN_DIST 1.0 +#ifndef RAYMARCH_VOLUME_SAMPLES_LIGHT +#define RAYMARCH_VOLUME_SAMPLES_LIGHT 8 #endif -#ifndef RAYMARCH_MAX_DIST -#define RAYMARCH_MAX_DIST 10.0 +#ifndef RAYMARCH_VOLUME_MAP_FNC +#define RAYMARCH_VOLUME_MAP_FNC raymarchVolumeMap #endif -#ifndef RAYMARCH_MAP_FNC -#define RAYMARCH_MAP_FNC(POS) raymarchMap(POS) +#ifndef RAYMARCH_VOLUME_DITHER +#define RAYMARCH_VOLUME_DITHER 0.1 #endif #ifndef FNC_RAYMARCH_VOLUMERENDER #define FNC_RAYMARCH_VOLUMERENDER -float4 raymarchVolume(in float3 rayOrigin, in float3 rayDirection, float3 cameraForward, - out float eyeDepth, out float3 worldPos, out float3 worldNormal) +float4 raymarchVolume(in float3 rayOrigin, in float3 rayDirection, float2 st, float minDist) { - const float tmin = RAYMARCH_MIN_DIST; - const float tmax = RAYMARCH_MAX_DIST; - const float fSamples = float(RAYMARCH_SAMPLES); - const float tstep = tmax/fSamples; - const float absorption = 100.; + const float tmin = RAYMARCH_MIN_DIST; + const float tmax = RAYMARCH_MAX_DIST; + const float tstep = tmax / float(RAYMARCH_VOLUME_SAMPLES); + const float tstepLight = tmax / float(RAYMARCH_VOLUME_SAMPLES_LIGHT); - #ifdef LIGHT_POSITION - const int nbSampleLight = 6; - const float fSampleLight = float(nbSampleLight); - const float tstepl = tmax/fSampleLight; - float3 sun_direction = normalize( LIGHT_POSITION ); - #endif +#if defined(LIGHT_DIRECTION) + float3 lightDirection = LIGHT_DIRECTION; +#endif - float T = 1.; + float transmittance = 1.0; float t = tmin; - float4 col = float4(0.0, 0.0, 0.0, 0.0); - float3 pos = rayOrigin; - for(int i = 0; i < RAYMARCH_SAMPLES; i++) { - Material res = RAYMARCH_MAP_FNC(pos); - float density = (0.1 - res.sdf); - if (density > 0.0) { - float tmp = density / fSamples; - T *= 1.0 - tmp * absorption; - if( T <= 0.001) - break; - - col += res.albedo * fSamples * tmp * T; - - //Light scattering - #ifdef LIGHT_POSITION - float Tl = 1.0; - for (int j = 0; j < nbSampleLight; j++) { - float densityLight = RAYMARCH_MAP_FNC( pos + sun_direction * float(j) * tstepl ).sdf; - if (densityLight>0.) - Tl *= 1. - densityLight * absorption/fSamples; - if (Tl <= 0.01) - break; + float3 color = float3(0.0, 0.0, 0.0); + float3 position = rayOrigin; + + for (int i = 0; i < RAYMARCH_VOLUME_SAMPLES; i++) + { + float3 position = rayOrigin + rayDirection * t; + VolumeMaterial res = RAYMARCH_VOLUME_MAP_FNC(position); + float extinction = -res.sdf; + float density = res.density * tstep; + if (t < minDist && extinction > 0.0) + { + float sampleTransmittance = exp(-extinction * density); + + float transmittanceLight = 1.0; +#if defined(LIGHT_DIRECTION) + for (int j = 0; j < RAYMARCH_VOLUME_SAMPLES_LIGHT; j++) { + VolumeMaterial resLight = RAYMARCH_VOLUME_MAP_FNC(position + lightDirection * float(j) * tstepLight); + float extinctionLight = -resLight.sdf; + float densityLight = res.density*tstepLight; + if (extinctionLight > 0.0) { + transmittanceLight *= exp(-extinctionLight*densityLight); + } } - col += float4(LIGHT_COLOR * 80. * tmp * T * Tl, 1.0); - #endif +#endif + + float3 luminance = LIGHT_COLOR * LIGHT_INTENSITY * transmittanceLight; + + // usual scaterring integration + //color += res.color * luminance * density * transmittance; + + // energy-conserving scattering integration + float3 integScatt = (luminance - luminance * sampleTransmittance) / max(extinction, EPSILON); + color += res.color * transmittance * integScatt; + + transmittance *= sampleTransmittance; } - pos += rayDirection * tstep; - } - worldPos = rayOrigin + t * rayDirection; - worldNormal = raymarchNormal( worldPos ); - eyeDepth = t * dot(rayDirection, cameraForward); + float offset = random(st) * (tstep * RAYMARCH_VOLUME_DITHER); + t += tstep + offset; + } - return col; + return float4(color, 1.0); } -#endif \ No newline at end of file +#endif diff --git a/lighting/volumeMaterial.hlsl b/lighting/volumeMaterial.hlsl new file mode 100644 index 00000000..5431603d --- /dev/null +++ b/lighting/volumeMaterial.hlsl @@ -0,0 +1,15 @@ +/* +contributors: Shadi El Hajj +description: Volume Material Structure +*/ + +#ifndef STR_VOLUME_MATERIAL +#define STR_VOLUME_MATERIAL + +struct VolumeMaterial { + float3 color; + float density; + float sdf; +}; + +#endif From 7a70ef009b2e0e4b25c29efd2e7e9ed88ac0210c Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Mon, 19 Aug 2024 11:40:33 +0100 Subject: [PATCH 12/38] Added license, updated doc. --- lighting/material/volumeNew.glsl | 1 + lighting/material/volumeNew.hlsl | 1 + lighting/raymarch/volume.glsl | 15 +++++++++------ lighting/raymarch/volume.hlsl | 3 ++- lighting/volumeMaterial.glsl | 1 + lighting/volumeMaterial.hlsl | 1 + 6 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lighting/material/volumeNew.glsl b/lighting/material/volumeNew.glsl index 622f3fe2..f71c037a 100644 --- a/lighting/material/volumeNew.glsl +++ b/lighting/material/volumeNew.glsl @@ -7,6 +7,7 @@ description: | use: - void volumeMaterialNew(out _mat) - volumeMaterialNew() +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ #ifndef FNC_VOLUME_MATERIAL_NEW diff --git a/lighting/material/volumeNew.hlsl b/lighting/material/volumeNew.hlsl index d5b7cbc2..3c83226f 100644 --- a/lighting/material/volumeNew.hlsl +++ b/lighting/material/volumeNew.hlsl @@ -7,6 +7,7 @@ description: | use: - void volumeMaterialNew(out _mat) - volumeMaterialNew() +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ #ifndef FNC_VOLUME_MATERIAL_NEW diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index e43fac5d..7217ef71 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -6,15 +6,18 @@ /* contributors: Shadi El Hajj description: Default raymarching renderer. Based on Sébastien Hillaire's paper "Physically Based Sky, Atmosphere & Cloud Rendering in Frostbite" -use: raymarchVolume( in rayOrigin, in rayDirection, in cameraForward, - out eyeDepth ) +use: raymarchVolume( in rayOrigin, in rayDirection, in cameraForward, st, float minDist ) options: - - RAYMARCH_MEDIUM_DENSITY 1.0 - - LIGHT_COLOR vec3(0.5) - - LIGHT_INTENSITY 1.0 - - LIGHT_POSITION vec3(0.0, 10.0, -50.0) + - RAYMARCH_VOLUME_SAMPLES 256 + - RAYMARCH_VOLUME_SAMPLES_LIGHT 8 + - RAYMARCH_VOLUME_MAP_FNC raymarchVolumeMap + - RAYMARCH_VOLUME_DITHER 0.1 + - LIGHT_COLOR vec3(0.5) + - LIGHT_INTENSITY 1.0 + - LIGHT_POSITION vec3(0.0, 10.0, -50.0) examples: - /shaders/lighting_raymarching_volume.frag +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ #ifndef LIGHT_COLOR diff --git a/lighting/raymarch/volume.hlsl b/lighting/raymarch/volume.hlsl index 5441d2de..addf177e 100644 --- a/lighting/raymarch/volume.hlsl +++ b/lighting/raymarch/volume.hlsl @@ -5,7 +5,7 @@ /* contributors: Shadi El Hajj -description: Default raymarching renderer. Based on Sébastien Hillaire's paper "Physically Based Sky, Atmosphere & Cloud Rendering in Frostbite" +description: Default raymarching renderer. Based on S�bastien Hillaire's paper "Physically Based Sky, Atmosphere & Cloud Rendering in Frostbite" use: raymarchVolume( in rayOrigin, in rayDirection, in cameraForward, st, float minDist ) options: - RAYMARCH_VOLUME_SAMPLES 256 @@ -17,6 +17,7 @@ options: - LIGHT_POSITION float3(0.0, 10.0, -50.0) examples: - /shaders/lighting_raymarching_volume.frag +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ #ifndef LIGHT_COLOR diff --git a/lighting/volumeMaterial.glsl b/lighting/volumeMaterial.glsl index 35a5b4d3..1508568a 100644 --- a/lighting/volumeMaterial.glsl +++ b/lighting/volumeMaterial.glsl @@ -1,6 +1,7 @@ /* contributors: Shadi El Hajj description: Volume Material Structure +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ #ifndef STR_VOLUME_MATERIAL diff --git a/lighting/volumeMaterial.hlsl b/lighting/volumeMaterial.hlsl index 5431603d..71f0b1e2 100644 --- a/lighting/volumeMaterial.hlsl +++ b/lighting/volumeMaterial.hlsl @@ -1,6 +1,7 @@ /* contributors: Shadi El Hajj description: Volume Material Structure +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ #ifndef STR_VOLUME_MATERIAL From 42597a9748c60621bce8b4136cadd6b48c517afd Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Mon, 19 Aug 2024 11:43:12 +0100 Subject: [PATCH 13/38] Added license. --- math/aamirror.glsl | 1 + math/aamirror.hlsl | 1 + math/dist.glsl | 1 + math/dist.hlsl | 1 + math/dist.wgsl | 1 + math/nyquist.glsl | 1 + math/nyquist.hlsl | 1 + sample/normalFromHeightMap.glsl | 1 + sample/normalFromHeightMap.hlsl | 1 + space/eulerView.glsl | 1 + space/eulerView.hlsl | 1 + space/lookAtView.glsl | 1 + space/lookAtView.hlsl | 2 ++ space/translate.glsl | 1 + space/translate.hlsl | 1 + 15 files changed, 16 insertions(+) diff --git a/math/aamirror.glsl b/math/aamirror.glsl index 75616236..11bebb8c 100644 --- a/math/aamirror.glsl +++ b/math/aamirror.glsl @@ -4,6 +4,7 @@ contributors: Shadi El Hajj description: An anti-aliased triangle wave function. use: mirror( x) +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ #ifndef FNC_AAMIRROR diff --git a/math/aamirror.hlsl b/math/aamirror.hlsl index ceebb48a..48937b75 100644 --- a/math/aamirror.hlsl +++ b/math/aamirror.hlsl @@ -4,6 +4,7 @@ contributors: Shadi El Hajj description: An anti-aliased triangle wave function. use: mirror( x) +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ #ifndef FNC_AAMIRROR diff --git a/math/dist.glsl b/math/dist.glsl index 2c13a420..5f565e7f 100644 --- a/math/dist.glsl +++ b/math/dist.glsl @@ -4,6 +4,7 @@ description: Commonly used distance functions. options: - DIST_FNC: change the distance function, currently implemented are distEuclidean, distManhattan, distChebychev and distMinkowski - DIST_MINKOWSKI_P: the power of the Minkowski distance function (1.0 Manhattan, 2.0 Euclidean, Infinity Chebychev) +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ #ifndef DIST_FNC diff --git a/math/dist.hlsl b/math/dist.hlsl index d248b270..09bea996 100644 --- a/math/dist.hlsl +++ b/math/dist.hlsl @@ -4,6 +4,7 @@ description: Commonly used distance functions. options: - DIST_FNC: change the distance function, currently implemented are distEuclidean, distManhattan, distChebychev and distMinkowski - DIST_MINKOWSKI_P: the power of the Minkowski distance function (1.0 Manhattan, 2.0 Euclidean, Infinity Chebychev) +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ #ifndef DIST_FNC diff --git a/math/dist.wgsl b/math/dist.wgsl index 2aae1cf8..789eb735 100644 --- a/math/dist.wgsl +++ b/math/dist.wgsl @@ -5,6 +5,7 @@ notes: - While the GLSL and HLSL versions of this file support defining the default distance function (DIST_FNC), WGSL does not have a standard way to do this. As such, the current implementation uses distEuclidean. options: - DIST_MINKOWSKI_P: the power of the Minkowski distance function (1.0 Manhattan, 2.0 Euclidean, Infinity Chebychev) +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ const DIST_MINKOWSKI_P: f32 = 2.0; // 1: Manhattan, 2: Euclidean, Infinity: Chebychev diff --git a/math/nyquist.glsl b/math/nyquist.glsl index e0287e67..4db64c43 100644 --- a/math/nyquist.glsl +++ b/math/nyquist.glsl @@ -3,6 +3,7 @@ contributors: Shadi El Hajj description: A simple low-pass filter which attenuates high-frequencies. use: nyquist( value, fwidth) use: nyquist( value, width, strength) +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ #ifndef FNC_NYQUIST diff --git a/math/nyquist.hlsl b/math/nyquist.hlsl index 88f4db56..adadb79f 100644 --- a/math/nyquist.hlsl +++ b/math/nyquist.hlsl @@ -3,6 +3,7 @@ contributors: Shadi El Hajj description: A simple low-pass filter which attenuates high-frequencies. use: nyquist( value, fwidth) use: nyquist( value, width, strength) +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ #ifndef FNC_NYQUIST diff --git a/sample/normalFromHeightMap.glsl b/sample/normalFromHeightMap.glsl index 8f5e34d9..d426a635 100644 --- a/sample/normalFromHeightMap.glsl +++ b/sample/normalFromHeightMap.glsl @@ -7,6 +7,7 @@ description: Given a height map texture, calculate normal at point (s, t) use: normalFromHeightMap( heightMap, st, strength, offset) options: - SAMPLE_CHANNEL: texture channel to sample from. Defaults to 0 (red) +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ #ifndef SAMPLE_CHANNEL diff --git a/sample/normalFromHeightMap.hlsl b/sample/normalFromHeightMap.hlsl index a2bb468f..cee878cd 100644 --- a/sample/normalFromHeightMap.hlsl +++ b/sample/normalFromHeightMap.hlsl @@ -7,6 +7,7 @@ description: Given a height map texture, calculate normal at point (s, t) use: normalFromHeightMap( heightMap, st, strength, offset) options: - SAMPLE_CHANNEL: texture channel to sample from. Defaults to 0 (red) +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ #ifndef SAMPLE_CHANNEL diff --git a/space/eulerView.glsl b/space/eulerView.glsl index 3f3ccc5f..07361e84 100644 --- a/space/eulerView.glsl +++ b/space/eulerView.glsl @@ -2,6 +2,7 @@ contributors: Shadi El Hajj description: Create a view matrix from camera position and camera rotation (euler angles) use: eulerView(in position, in euler) +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ #include "../math/rotate3dX.glsl" diff --git a/space/eulerView.hlsl b/space/eulerView.hlsl index b1958237..dcb0c569 100644 --- a/space/eulerView.hlsl +++ b/space/eulerView.hlsl @@ -2,6 +2,7 @@ contributors: Shadi El Hajj description: Create a view matrix from camera position and camera rotation (euler angles) use: eulerView(in position, in euler) +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ #include "../math/rotate3dX.hlsl" diff --git a/space/lookAtView.glsl b/space/lookAtView.glsl index 864dd410..5108722b 100644 --- a/space/lookAtView.glsl +++ b/space/lookAtView.glsl @@ -2,6 +2,7 @@ contributors: Shadi El Hajj description: Create a look-at view matrix use: lookAtView(in position, in target, in up) +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ #include "lookAt.glsl" diff --git a/space/lookAtView.hlsl b/space/lookAtView.hlsl index fa0bf221..eb3aae92 100644 --- a/space/lookAtView.hlsl +++ b/space/lookAtView.hlsl @@ -2,6 +2,8 @@ contributors: Shadi El Hajj description: Create a look-at view matrix use: lookAtView(in position, in target, in up) +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ #include "lookAt.hlsl" diff --git a/space/translate.glsl b/space/translate.glsl index 6e7dc74a..a54d165d 100644 --- a/space/translate.glsl +++ b/space/translate.glsl @@ -2,6 +2,7 @@ contributors: Shadi El Hajj description: Add a translation component to a transform matrix use: translate(in matrix, in tranaslation) +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ #ifndef FNC_TRANSLATE diff --git a/space/translate.hlsl b/space/translate.hlsl index c50c579d..b46ed014 100644 --- a/space/translate.hlsl +++ b/space/translate.hlsl @@ -2,6 +2,7 @@ contributors: Shadi El Hajj description: Add a translation component to a transform matrix use: translate(in matrix, in tranaslation) +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ #ifndef FNC_TRANSLATE From 235deee228e70e265bd5be76b4331547acd22476 Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Mon, 19 Aug 2024 13:44:57 +0100 Subject: [PATCH 14/38] Factored out beerLambert. --- lighting/common/beerLambert.glsl | 14 ++++++++++++++ lighting/common/beerLambert.hlsl | 14 ++++++++++++++ lighting/raymarch/volume.glsl | 5 +++-- lighting/raymarch/volume.hlsl | 4 ++-- 4 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 lighting/common/beerLambert.glsl create mode 100644 lighting/common/beerLambert.hlsl diff --git a/lighting/common/beerLambert.glsl b/lighting/common/beerLambert.glsl new file mode 100644 index 00000000..397c8a18 --- /dev/null +++ b/lighting/common/beerLambert.glsl @@ -0,0 +1,14 @@ +/* +contributors: Shadi El Hajj +description: The Beer-Lambert law +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj +*/ + +#ifndef FNC_BEER_LAMBERT +#define FNC_BEER_LAMBERT + +float beerLambert(float absorption, float dist) { + return exp(-absorption * dist); +} + +#endif diff --git a/lighting/common/beerLambert.hlsl b/lighting/common/beerLambert.hlsl new file mode 100644 index 00000000..397c8a18 --- /dev/null +++ b/lighting/common/beerLambert.hlsl @@ -0,0 +1,14 @@ +/* +contributors: Shadi El Hajj +description: The Beer-Lambert law +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj +*/ + +#ifndef FNC_BEER_LAMBERT +#define FNC_BEER_LAMBERT + +float beerLambert(float absorption, float dist) { + return exp(-absorption * dist); +} + +#endif diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index 7217ef71..15539e2a 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -1,4 +1,5 @@ #include "map.glsl" +#include "../common/beerLambert.glsl" #include "../../generative/random.glsl" #include "../../math/const.glsl" #include "../material/volumeNew.glsl" @@ -77,7 +78,7 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec2 st, float min float extinction = -res.sdf; float density = res.density*tstep; if (t < minDist && extinction > 0.0) { - float sampleTransmittance = exp(-extinction*density); + float sampleTransmittance = beerLambert(density, extinction); float transmittanceLight = 1.0; #if defined(LIGHT_DIRECTION) || defined(LIGHT_POSITION) @@ -86,7 +87,7 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec2 st, float min float extinctionLight = -resLight.sdf; float densityLight = res.density*tstepLight; if (extinctionLight > 0.0) { - transmittanceLight *= exp(-extinctionLight*densityLight); + transmittanceLight *= beerLambert(densityLight, extinctionLight); } } #endif diff --git a/lighting/raymarch/volume.hlsl b/lighting/raymarch/volume.hlsl index addf177e..90940332 100644 --- a/lighting/raymarch/volume.hlsl +++ b/lighting/raymarch/volume.hlsl @@ -76,7 +76,7 @@ float4 raymarchVolume(in float3 rayOrigin, in float3 rayDirection, float2 st, fl float density = res.density * tstep; if (t < minDist && extinction > 0.0) { - float sampleTransmittance = exp(-extinction * density); + float sampleTransmittance = beerLambert(density, extinction); float transmittanceLight = 1.0; #if defined(LIGHT_DIRECTION) @@ -85,7 +85,7 @@ float4 raymarchVolume(in float3 rayOrigin, in float3 rayDirection, float2 st, fl float extinctionLight = -resLight.sdf; float densityLight = res.density*tstepLight; if (extinctionLight > 0.0) { - transmittanceLight *= exp(-extinctionLight*densityLight); + transmittanceLight *= beerLambert(densityLight, extinctionLight); } } #endif From c74c62f275cff90533998d94524909321233aa97 Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Mon, 19 Aug 2024 13:48:16 +0100 Subject: [PATCH 15/38] Missing include. --- lighting/raymarch/volume.hlsl | 1 + 1 file changed, 1 insertion(+) diff --git a/lighting/raymarch/volume.hlsl b/lighting/raymarch/volume.hlsl index 90940332..5ef554d9 100644 --- a/lighting/raymarch/volume.hlsl +++ b/lighting/raymarch/volume.hlsl @@ -1,4 +1,5 @@ #include "map.hlsl" +#include "../common/beerLambert.hlsl" #include "../../generative/random.hlsl" #include "../../math/const.hlsl" #include "../material/volumeNew.hlsl" From 9a809bf22bf1f1ffd05c4de9b48ac62c92e9f07d Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Mon, 19 Aug 2024 16:09:55 +0100 Subject: [PATCH 16/38] Added attenuation. --- lighting/common/attenuation.glsl | 34 ++++++++++++++++++++++++++++++++ lighting/common/attenuation.hlsl | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 lighting/common/attenuation.glsl create mode 100644 lighting/common/attenuation.hlsl diff --git a/lighting/common/attenuation.glsl b/lighting/common/attenuation.glsl new file mode 100644 index 00000000..8b337a10 --- /dev/null +++ b/lighting/common/attenuation.glsl @@ -0,0 +1,34 @@ +/* +contributors: Shadi El Hajj +description: Light attenuation equation +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj +*/ + +#ifndef LIGHT_ATTENUATION_CONSTANT +#define LIGHT_ATTENUATION_CONSTANT 0 +#endif + +#ifndef LIGHT_ATTENUATION_LINEAR +#define LIGHT_ATTENUATION_LINEAR 0 +#endif + +#ifndef LIGHT_ATTENUATION_EXPONENTIAL +#define LIGHT_ATTENUATION_EXPONENTIAL 1 +#endif + +#ifndef LIGHT_ATTENUATION_EXPONENT +#define LIGHT_ATTENUATION_EXPONENT 2 +#endif + +#ifndef FNC_LIGHT_ATTENUATION +#define FNC_LIGHT_ATTENUATION + +float attenuation(float dist) { + return 1.0 / ( + LIGHT_ATTENUATION_CONSTANT + + LIGHT_ATTENUATION_LINEAR * dist + + LIGHT_ATTENUATION_EXPONENTIAL * pow(dist, LIGHT_ATTENUATION_EXPONENT) + ); +} + +#endif diff --git a/lighting/common/attenuation.hlsl b/lighting/common/attenuation.hlsl new file mode 100644 index 00000000..8b337a10 --- /dev/null +++ b/lighting/common/attenuation.hlsl @@ -0,0 +1,34 @@ +/* +contributors: Shadi El Hajj +description: Light attenuation equation +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj +*/ + +#ifndef LIGHT_ATTENUATION_CONSTANT +#define LIGHT_ATTENUATION_CONSTANT 0 +#endif + +#ifndef LIGHT_ATTENUATION_LINEAR +#define LIGHT_ATTENUATION_LINEAR 0 +#endif + +#ifndef LIGHT_ATTENUATION_EXPONENTIAL +#define LIGHT_ATTENUATION_EXPONENTIAL 1 +#endif + +#ifndef LIGHT_ATTENUATION_EXPONENT +#define LIGHT_ATTENUATION_EXPONENT 2 +#endif + +#ifndef FNC_LIGHT_ATTENUATION +#define FNC_LIGHT_ATTENUATION + +float attenuation(float dist) { + return 1.0 / ( + LIGHT_ATTENUATION_CONSTANT + + LIGHT_ATTENUATION_LINEAR * dist + + LIGHT_ATTENUATION_EXPONENTIAL * pow(dist, LIGHT_ATTENUATION_EXPONENT) + ); +} + +#endif From e01ed48993b7a2754d6e4ca244ff31bd2a2d098f Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Mon, 19 Aug 2024 17:31:34 +0100 Subject: [PATCH 17/38] Fixed light direction. --- lighting/raymarch/volume.glsl | 9 +++------ lighting/raymarch/volume.hlsl | 7 ++++--- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index 15539e2a..c6801699 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -63,10 +63,6 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec2 st, float min vec3 lightDirection = LIGHT_DIRECTION; #endif - #if defined(LIGHT_POSITION) - vec3 lightDirection = normalize( LIGHT_POSITION ); - #endif - float transmittance = 1.0; float t = tmin; vec3 color = vec3(0.0, 0.0, 0.0); @@ -81,9 +77,10 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec2 st, float min float sampleTransmittance = beerLambert(density, extinction); float transmittanceLight = 1.0; - #if defined(LIGHT_DIRECTION) || defined(LIGHT_POSITION) + #if defined(LIGHT_DIRECTION) for (int j = 0; j < RAYMARCH_VOLUME_SAMPLES_LIGHT; j++) { - VolumeMaterial resLight = RAYMARCH_VOLUME_MAP_FNC(position + lightDirection * float(j) * tstepLight); + vec3 positionLight = position - lightDirection * float(j) * tstepLight; + VolumeMaterial resLight = RAYMARCH_VOLUME_MAP_FNC(positionLight); float extinctionLight = -resLight.sdf; float densityLight = res.density*tstepLight; if (extinctionLight > 0.0) { diff --git a/lighting/raymarch/volume.hlsl b/lighting/raymarch/volume.hlsl index 5ef554d9..c6bd82b7 100644 --- a/lighting/raymarch/volume.hlsl +++ b/lighting/raymarch/volume.hlsl @@ -80,16 +80,17 @@ float4 raymarchVolume(in float3 rayOrigin, in float3 rayDirection, float2 st, fl float sampleTransmittance = beerLambert(density, extinction); float transmittanceLight = 1.0; -#if defined(LIGHT_DIRECTION) + #if defined(LIGHT_DIRECTION) for (int j = 0; j < RAYMARCH_VOLUME_SAMPLES_LIGHT; j++) { - VolumeMaterial resLight = RAYMARCH_VOLUME_MAP_FNC(position + lightDirection * float(j) * tstepLight); + float3 positionLight = position - lightDirection * float(j) * tstepLight; + VolumeMaterial resLight = RAYMARCH_VOLUME_MAP_FNC(positionLight); float extinctionLight = -resLight.sdf; float densityLight = res.density*tstepLight; if (extinctionLight > 0.0) { transmittanceLight *= beerLambert(densityLight, extinctionLight); } } -#endif + #endif float3 luminance = LIGHT_COLOR * LIGHT_INTENSITY * transmittanceLight; From dd0cd6146149ce57e655168de6496c3158ad471c Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Mon, 19 Aug 2024 19:41:15 +0100 Subject: [PATCH 18/38] Added support for point light. --- lighting/raymarch/volume.glsl | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index c6801699..2d765de8 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -25,7 +25,7 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj #if defined(GLSLVIEWER) #define LIGHT_COLOR u_lightColor #else -#define LIGHT_COLOR vec3(0.5) +#define LIGHT_COLOR vec3(0.5, 0.5, 0.5) #endif #endif @@ -56,12 +56,6 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec2 st, float min const float tmin = RAYMARCH_MIN_DIST; const float tmax = RAYMARCH_MAX_DIST; - const float tstep = tmax/float(RAYMARCH_VOLUME_SAMPLES); - const float tstepLight = tmax/float(RAYMARCH_VOLUME_SAMPLES_LIGHT); - - #if defined(LIGHT_DIRECTION) - vec3 lightDirection = LIGHT_DIRECTION; - #endif float transmittance = 1.0; float t = tmin; @@ -72,14 +66,24 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec2 st, float min vec3 position = rayOrigin + rayDirection * t; VolumeMaterial res = RAYMARCH_VOLUME_MAP_FNC(position); float extinction = -res.sdf; + float tstep = tmax/float(RAYMARCH_VOLUME_SAMPLES); float density = res.density*tstep; if (t < minDist && extinction > 0.0) { float sampleTransmittance = beerLambert(density, extinction); float transmittanceLight = 1.0; - #if defined(LIGHT_DIRECTION) + #if defined(LIGHT_DIRECTION) || defined(LIGHT_POSITION) for (int j = 0; j < RAYMARCH_VOLUME_SAMPLES_LIGHT; j++) { - vec3 positionLight = position - lightDirection * float(j) * tstepLight; + + #if defined(LIGHT_POSITION) // point light + float tstepLight = distance(LIGHT_POSITION, position)/float(RAYMARCH_VOLUME_SAMPLES_LIGHT); + vec3 rayDirectionLight = normalize(LIGHT_POSITION - position); + #else // directional light + float tstepLight = tmax/float(RAYMARCH_VOLUME_SAMPLES_LIGHT); + vec3 rayDirectionLight = -LIGHT_DIRECTION; + #endif + + vec3 positionLight = position + rayDirectionLight * j * tstepLight; VolumeMaterial resLight = RAYMARCH_VOLUME_MAP_FNC(positionLight); float extinctionLight = -resLight.sdf; float densityLight = res.density*tstepLight; From 9a0d17889a4a23a01297aae88cecb12faf7d084e Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Mon, 19 Aug 2024 20:45:21 +0100 Subject: [PATCH 19/38] Added point light attenuation (GLSL) --- lighting/raymarch/volume.glsl | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index 2d765de8..26f39473 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -1,4 +1,5 @@ #include "map.glsl" +#include "../common/attenuation.glsl" #include "../common/beerLambert.glsl" #include "../../generative/random.glsl" #include "../../math/const.glsl" @@ -71,18 +72,20 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec2 st, float min if (t < minDist && extinction > 0.0) { float sampleTransmittance = beerLambert(density, extinction); - float transmittanceLight = 1.0; #if defined(LIGHT_DIRECTION) || defined(LIGHT_POSITION) - for (int j = 0; j < RAYMARCH_VOLUME_SAMPLES_LIGHT; j++) { - - #if defined(LIGHT_POSITION) // point light - float tstepLight = distance(LIGHT_POSITION, position)/float(RAYMARCH_VOLUME_SAMPLES_LIGHT); - vec3 rayDirectionLight = normalize(LIGHT_POSITION - position); - #else // directional light - float tstepLight = tmax/float(RAYMARCH_VOLUME_SAMPLES_LIGHT); - vec3 rayDirectionLight = -LIGHT_DIRECTION; - #endif - + #if defined(LIGHT_DIRECTION) // directional light + float tstepLight = tmax/float(RAYMARCH_VOLUME_SAMPLES_LIGHT); + vec3 rayDirectionLight = -LIGHT_DIRECTION; + const float attenuationLight = 1.0; + #else // point light + float distToLight = distance(LIGHT_POSITION, position); + float tstepLight = distToLight/float(RAYMARCH_VOLUME_SAMPLES_LIGHT); + vec3 rayDirectionLight = normalize(LIGHT_POSITION - position); + float attenuationLight = attenuation(distToLight); + #endif + + float transmittanceLight = 1.0; + for (int j = 0; j < RAYMARCH_VOLUME_SAMPLES_LIGHT; j++) { vec3 positionLight = position + rayDirectionLight * j * tstepLight; VolumeMaterial resLight = RAYMARCH_VOLUME_MAP_FNC(positionLight); float extinctionLight = -resLight.sdf; @@ -91,10 +94,11 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec2 st, float min transmittanceLight *= beerLambert(densityLight, extinctionLight); } } + vec3 luminance = LIGHT_COLOR * LIGHT_INTENSITY * attenuationLight * transmittanceLight; + #else // no lighting + vec3 luminance = 1.0; #endif - vec3 luminance = LIGHT_COLOR * LIGHT_INTENSITY * transmittanceLight; - // usual scaterring integration //color += res.color * luminance * density * transmittance; From 86addc3871496595a038bc2395501ea4ecf43ed2 Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Mon, 19 Aug 2024 21:30:47 +0100 Subject: [PATCH 20/38] Support for point lights (HLSL) --- lighting/pbr.hlsl | 41 ++++++++++--------------------- lighting/raymarch/volume.glsl | 9 +++---- lighting/raymarch/volume.hlsl | 45 +++++++++++++++++++++-------------- 3 files changed, 44 insertions(+), 51 deletions(-) diff --git a/lighting/pbr.hlsl b/lighting/pbr.hlsl index ea8e46c1..d0819aab 100644 --- a/lighting/pbr.hlsl +++ b/lighting/pbr.hlsl @@ -1,32 +1,3 @@ -#ifndef CAMERA_POSITION -#if defined(UNITY_COMPILER_HLSL) -#define CAMERA_POSITION _WorldSpaceCameraPos -#else -#define CAMERA_POSITION float3(0.0, 0.0, -10.0) -#endif -#endif - -#ifndef LIGHT_DIRECTION -#if defined(UNITY_COMPILER_HLSL) -#define LIGHT_DIRECTION _WorldSpaceLightPos0.xyz -#else -#define LIGHT_DIRECTION float3(0.0, 10.0, -50.0) -#endif -#endif - -#ifndef LIGHT_COLOR -#if defined(UNITY_COMPILER_HLSL) -#include -#define LIGHT_COLOR _LightColor0.rgb -#else -#define LIGHT_COLOR float3(0.5, 0.5, 0.5) -#endif -#endif - -#ifndef IBL_LUMINANCE -#define IBL_LUMINANCE 1.0 -#endif - #include "../color/tonemap.hlsl" #include "material.hlsl" @@ -56,6 +27,18 @@ license: - Copyright (c) 2021 Patricio Gonzalez Vivo under Patron License - https://lygia.xyz/license */ +#ifndef CAMERA_POSITION +#if defined(UNITY_COMPILER_HLSL) +#define CAMERA_POSITION _WorldSpaceCameraPos +#else +#define CAMERA_POSITION float3(0.0, 0.0, -10.0) +#endif +#endif + +#ifndef IBL_LUMINANCE +#define IBL_LUMINANCE 1.0 +#endif + #ifndef FNC_PBR #define FNC_PBR diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index 26f39473..581cd109 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -14,9 +14,10 @@ options: - RAYMARCH_VOLUME_SAMPLES_LIGHT 8 - RAYMARCH_VOLUME_MAP_FNC raymarchVolumeMap - RAYMARCH_VOLUME_DITHER 0.1 - - LIGHT_COLOR vec3(0.5) + - LIGHT_COLOR vec3(0.5, 0.5, 0.5) - LIGHT_INTENSITY 1.0 - - LIGHT_POSITION vec3(0.0, 10.0, -50.0) + - LIGHT_POSITION + - LIGHT_DIRECTION examples: - /shaders/lighting_raymarching_volume.frag license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj @@ -99,7 +100,7 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec2 st, float min vec3 luminance = 1.0; #endif - // usual scaterring integration + // usual scattering integration //color += res.color * luminance * density * transmittance; // energy-conserving scattering integration @@ -113,7 +114,7 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec2 st, float min t += tstep + offset; } - return vec4(color, 1.0); + return vec4(color, 0.0); } #endif diff --git a/lighting/raymarch/volume.hlsl b/lighting/raymarch/volume.hlsl index c6bd82b7..e03b5ca4 100644 --- a/lighting/raymarch/volume.hlsl +++ b/lighting/raymarch/volume.hlsl @@ -1,4 +1,5 @@ #include "map.hlsl" +#include "../common/attenuation.hlsl" #include "../common/beerLambert.hlsl" #include "../../generative/random.hlsl" #include "../../math/const.hlsl" @@ -6,26 +7,27 @@ /* contributors: Shadi El Hajj -description: Default raymarching renderer. Based on S�bastien Hillaire's paper "Physically Based Sky, Atmosphere & Cloud Rendering in Frostbite" +description: Default raymarching renderer. Based on Sébastien Hillaire's paper "Physically Based Sky, Atmosphere & Cloud Rendering in Frostbite" use: raymarchVolume( in rayOrigin, in rayDirection, in cameraForward, st, float minDist ) options: - RAYMARCH_VOLUME_SAMPLES 256 - RAYMARCH_VOLUME_SAMPLES_LIGHT 8 - RAYMARCH_VOLUME_MAP_FNC raymarchVolumeMap - RAYMARCH_VOLUME_DITHER 0.1 - - LIGHT_COLOR float3(0.5) + - LIGHT_COLOR float3(0.5, 0.5, 0.5) - LIGHT_INTENSITY 1.0 - - LIGHT_POSITION float3(0.0, 10.0, -50.0) + - LIGHT_POSITION + - LIGHT_DIRECTION examples: - /shaders/lighting_raymarching_volume.frag license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ #ifndef LIGHT_COLOR -#if defined(GLSLVIEWER) +#if defined(hlslVIEWER) #define LIGHT_COLOR u_lightColor #else -#define LIGHT_COLOR float3(0.5) +#define LIGHT_COLOR float3(0.5, 0.5, 0.5) #endif #endif @@ -57,12 +59,6 @@ float4 raymarchVolume(in float3 rayOrigin, in float3 rayDirection, float2 st, fl const float tmin = RAYMARCH_MIN_DIST; const float tmax = RAYMARCH_MAX_DIST; - const float tstep = tmax / float(RAYMARCH_VOLUME_SAMPLES); - const float tstepLight = tmax / float(RAYMARCH_VOLUME_SAMPLES_LIGHT); - -#if defined(LIGHT_DIRECTION) - float3 lightDirection = LIGHT_DIRECTION; -#endif float transmittance = 1.0; float t = tmin; @@ -74,15 +70,27 @@ float4 raymarchVolume(in float3 rayOrigin, in float3 rayDirection, float2 st, fl float3 position = rayOrigin + rayDirection * t; VolumeMaterial res = RAYMARCH_VOLUME_MAP_FNC(position); float extinction = -res.sdf; + float tstep = tmax / float(RAYMARCH_VOLUME_SAMPLES); float density = res.density * tstep; if (t < minDist && extinction > 0.0) { float sampleTransmittance = beerLambert(density, extinction); + #if defined(LIGHT_DIRECTION) || defined(LIGHT_POSITION) + #if defined(LIGHT_DIRECTION) // directional light + float tstepLight = tmax/float(RAYMARCH_VOLUME_SAMPLES_LIGHT); + float3 rayDirectionLight = -LIGHT_DIRECTION; + const float attenuationLight = 1.0; + #else // point light + float distToLight = distance(LIGHT_POSITION, position); + float tstepLight = distToLight/float(RAYMARCH_VOLUME_SAMPLES_LIGHT); + float3 rayDirectionLight = normalize(LIGHT_POSITION - position); + float attenuationLight = attenuation(distToLight); + #endif + float transmittanceLight = 1.0; - #if defined(LIGHT_DIRECTION) - for (int j = 0; j < RAYMARCH_VOLUME_SAMPLES_LIGHT; j++) { - float3 positionLight = position - lightDirection * float(j) * tstepLight; + for (int j = 0; j < RAYMARCH_VOLUME_SAMPLES_LIGHT; j++) { + float3 positionLight = position + rayDirectionLight * j * tstepLight; VolumeMaterial resLight = RAYMARCH_VOLUME_MAP_FNC(positionLight); float extinctionLight = -resLight.sdf; float densityLight = res.density*tstepLight; @@ -90,11 +98,12 @@ float4 raymarchVolume(in float3 rayOrigin, in float3 rayDirection, float2 st, fl transmittanceLight *= beerLambert(densityLight, extinctionLight); } } + float3 luminance = LIGHT_COLOR * LIGHT_INTENSITY * attenuationLight * transmittanceLight; + #else // no lighting + float3 luminance = 1.0; #endif - float3 luminance = LIGHT_COLOR * LIGHT_INTENSITY * transmittanceLight; - - // usual scaterring integration + // usual scattering integration //color += res.color * luminance * density * transmittance; // energy-conserving scattering integration @@ -108,7 +117,7 @@ float4 raymarchVolume(in float3 rayOrigin, in float3 rayDirection, float2 st, fl t += tstep + offset; } - return float4(color, 1.0); + return float4(color, 0.0); } #endif From 13cbdb53916741b848f52f09b328f312e3a63e15 Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Mon, 19 Aug 2024 22:47:54 +0100 Subject: [PATCH 21/38] Fixed light direction --- lighting/raymarch/volume.glsl | 4 ++-- lighting/raymarch/volume.hlsl | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index 581cd109..146bb66d 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -76,7 +76,7 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec2 st, float min #if defined(LIGHT_DIRECTION) || defined(LIGHT_POSITION) #if defined(LIGHT_DIRECTION) // directional light float tstepLight = tmax/float(RAYMARCH_VOLUME_SAMPLES_LIGHT); - vec3 rayDirectionLight = -LIGHT_DIRECTION; + vec3 rayDirectionLight = LIGHT_DIRECTION; const float attenuationLight = 1.0; #else // point light float distToLight = distance(LIGHT_POSITION, position); @@ -90,7 +90,7 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec2 st, float min vec3 positionLight = position + rayDirectionLight * j * tstepLight; VolumeMaterial resLight = RAYMARCH_VOLUME_MAP_FNC(positionLight); float extinctionLight = -resLight.sdf; - float densityLight = res.density*tstepLight; + float densityLight = resLight.density*tstepLight; if (extinctionLight > 0.0) { transmittanceLight *= beerLambert(densityLight, extinctionLight); } diff --git a/lighting/raymarch/volume.hlsl b/lighting/raymarch/volume.hlsl index e03b5ca4..6cf66390 100644 --- a/lighting/raymarch/volume.hlsl +++ b/lighting/raymarch/volume.hlsl @@ -79,7 +79,7 @@ float4 raymarchVolume(in float3 rayOrigin, in float3 rayDirection, float2 st, fl #if defined(LIGHT_DIRECTION) || defined(LIGHT_POSITION) #if defined(LIGHT_DIRECTION) // directional light float tstepLight = tmax/float(RAYMARCH_VOLUME_SAMPLES_LIGHT); - float3 rayDirectionLight = -LIGHT_DIRECTION; + float3 rayDirectionLight = LIGHT_DIRECTION; const float attenuationLight = 1.0; #else // point light float distToLight = distance(LIGHT_POSITION, position); @@ -93,7 +93,7 @@ float4 raymarchVolume(in float3 rayOrigin, in float3 rayDirection, float2 st, fl float3 positionLight = position + rayDirectionLight * j * tstepLight; VolumeMaterial resLight = RAYMARCH_VOLUME_MAP_FNC(positionLight); float extinctionLight = -resLight.sdf; - float densityLight = res.density*tstepLight; + float densityLight = resLight.density*tstepLight; if (extinctionLight > 0.0) { transmittanceLight *= beerLambert(densityLight, extinctionLight); } From fd32b4a98a1a39d2438661482a6d2e47ab9014a9 Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Mon, 19 Aug 2024 23:08:39 +0100 Subject: [PATCH 22/38] Fixed self-shadowing error. --- lighting/raymarch/volume.glsl | 4 +--- lighting/raymarch/volume.hlsl | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index 146bb66d..ab2ec6dc 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -91,9 +91,7 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec2 st, float min VolumeMaterial resLight = RAYMARCH_VOLUME_MAP_FNC(positionLight); float extinctionLight = -resLight.sdf; float densityLight = resLight.density*tstepLight; - if (extinctionLight > 0.0) { - transmittanceLight *= beerLambert(densityLight, extinctionLight); - } + transmittanceLight *= beerLambert(densityLight, extinctionLight); } vec3 luminance = LIGHT_COLOR * LIGHT_INTENSITY * attenuationLight * transmittanceLight; #else // no lighting diff --git a/lighting/raymarch/volume.hlsl b/lighting/raymarch/volume.hlsl index 6cf66390..f52054e1 100644 --- a/lighting/raymarch/volume.hlsl +++ b/lighting/raymarch/volume.hlsl @@ -94,9 +94,7 @@ float4 raymarchVolume(in float3 rayOrigin, in float3 rayDirection, float2 st, fl VolumeMaterial resLight = RAYMARCH_VOLUME_MAP_FNC(positionLight); float extinctionLight = -resLight.sdf; float densityLight = resLight.density*tstepLight; - if (extinctionLight > 0.0) { - transmittanceLight *= beerLambert(densityLight, extinctionLight); - } + transmittanceLight *= beerLambert(densityLight, extinctionLight); } float3 luminance = LIGHT_COLOR * LIGHT_INTENSITY * attenuationLight * transmittanceLight; #else // no lighting From b94ee2859c59d8ee01e1595c6ea1a8bfc61f1dbe Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Mon, 19 Aug 2024 23:30:47 +0100 Subject: [PATCH 23/38] Renamed color to albedo --- lighting/material/volumeNew.glsl | 10 +++++----- lighting/material/volumeNew.hlsl | 10 +++++----- lighting/raymarch/volume.glsl | 4 ++-- lighting/raymarch/volume.hlsl | 4 ++-- lighting/volumeMaterial.glsl | 2 +- lighting/volumeMaterial.hlsl | 2 +- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/lighting/material/volumeNew.glsl b/lighting/material/volumeNew.glsl index f71c037a..53b0dc57 100644 --- a/lighting/material/volumeNew.glsl +++ b/lighting/material/volumeNew.glsl @@ -14,7 +14,7 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj #define FNC_VOLUME_MATERIAL_NEW void volumeMaterialNew(out VolumeMaterial _mat) { - _mat.color = vec3(1.0, 1.0, 1.0); + _mat.albedo = vec3(1.0, 1.0, 1.0); _mat.density = 1.0; _mat.sdf = RAYMARCH_MAX_DIST; @@ -26,16 +26,16 @@ VolumeMaterial volumeMaterialNew() { return mat; } -VolumeMaterial volumeMaterialNew(vec3 color, float sdf) { +VolumeMaterial volumeMaterialNew(vec3 albedo, float sdf) { VolumeMaterial mat = volumeMaterialNew(); - mat.color.rgb = color; + mat.albedo.rgb = albedo; mat.sdf = sdf; return mat; } -VolumeMaterial volumeMaterialNew(vec3 color, float density, float sdf) { +VolumeMaterial volumeMaterialNew(vec3 albedo, float density, float sdf) { VolumeMaterial mat = volumeMaterialNew(); - mat.color.rgb = color; + mat.albedo.rgb = albedo; mat.density = density; mat.sdf = sdf; return mat; diff --git a/lighting/material/volumeNew.hlsl b/lighting/material/volumeNew.hlsl index 3c83226f..67bb74e4 100644 --- a/lighting/material/volumeNew.hlsl +++ b/lighting/material/volumeNew.hlsl @@ -14,7 +14,7 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj #define FNC_VOLUME_MATERIAL_NEW void volumeMaterialNew(out VolumeMaterial _mat) { - _mat.color = float3(1.0, 1.0, 1.0); + _mat.albedo = float3(1.0, 1.0, 1.0); _mat.density = 1.0; _mat.sdf = RAYMARCH_MAX_DIST; @@ -26,16 +26,16 @@ VolumeMaterial volumeMaterialNew() { return mat; } -VolumeMaterial volumeMaterialNew(float3 color, float sdf) { +VolumeMaterial volumeMaterialNew(float3 albedo, float sdf) { VolumeMaterial mat = volumeMaterialNew(); - mat.color.rgb = color; + mat.albedo.rgb = albedo; mat.sdf = sdf; return mat; } -VolumeMaterial volumeMaterialNew(float3 color, float density, float sdf) { +VolumeMaterial volumeMaterialNew(float3 albedo, float density, float sdf) { VolumeMaterial mat = volumeMaterialNew(); - mat.color.rgb = color; + mat.albedo.rgb = albedo; mat.density = density; mat.sdf = sdf; return mat; diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index ab2ec6dc..59a410a0 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -99,11 +99,11 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec2 st, float min #endif // usual scattering integration - //color += res.color * luminance * density * transmittance; + //color += res.albedo * luminance * density * transmittance; // energy-conserving scattering integration vec3 integScatt = (luminance - luminance * sampleTransmittance) / max(extinction, EPSILON); - color += res.color * transmittance * integScatt; + color += res.albedo * transmittance * integScatt; transmittance *= sampleTransmittance; } diff --git a/lighting/raymarch/volume.hlsl b/lighting/raymarch/volume.hlsl index f52054e1..593ae9eb 100644 --- a/lighting/raymarch/volume.hlsl +++ b/lighting/raymarch/volume.hlsl @@ -102,11 +102,11 @@ float4 raymarchVolume(in float3 rayOrigin, in float3 rayDirection, float2 st, fl #endif // usual scattering integration - //color += res.color * luminance * density * transmittance; + //color += res.albedo * luminance * density * transmittance; // energy-conserving scattering integration float3 integScatt = (luminance - luminance * sampleTransmittance) / max(extinction, EPSILON); - color += res.color * transmittance * integScatt; + color += res.albedo * transmittance * integScatt; transmittance *= sampleTransmittance; } diff --git a/lighting/volumeMaterial.glsl b/lighting/volumeMaterial.glsl index 1508568a..5e594d6c 100644 --- a/lighting/volumeMaterial.glsl +++ b/lighting/volumeMaterial.glsl @@ -8,7 +8,7 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj #define STR_VOLUME_MATERIAL struct VolumeMaterial { - vec3 color; + vec3 albedo; float density; float sdf; }; diff --git a/lighting/volumeMaterial.hlsl b/lighting/volumeMaterial.hlsl index 71f0b1e2..c0edd8e4 100644 --- a/lighting/volumeMaterial.hlsl +++ b/lighting/volumeMaterial.hlsl @@ -8,7 +8,7 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj #define STR_VOLUME_MATERIAL struct VolumeMaterial { - float3 color; + float3 albedo; float density; float sdf; }; From 843fb6e007a43426c5478b66ad41cf89bde72787 Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Tue, 20 Aug 2024 13:44:18 +0100 Subject: [PATCH 24/38] Refactor volume (GLSL) --- lighting/raymarch/volume.glsl | 60 +++++++++++++++++------------------ 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index 59a410a0..0a066b29 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -54,13 +54,34 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj #ifndef FNC_RAYMARCH_VOLUMERENDER #define FNC_RAYMARCH_VOLUMERENDER -vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec2 st, float minDist) { +vec3 shadowTransmittance(vec3 position) { + #if defined(LIGHT_DIRECTION) || defined(LIGHT_POSITION) + #if defined(LIGHT_DIRECTION) // directional light + float tstepLight = RAYMARCH_MAX_DIST/float(RAYMARCH_VOLUME_SAMPLES_LIGHT); + vec3 rayDirectionLight = LIGHT_DIRECTION; + const float attenuationLight = 1.0; + #else // point light + float distToLight = distance(LIGHT_POSITION, position); + float tstepLight = distToLight/float(RAYMARCH_VOLUME_SAMPLES_LIGHT); + vec3 rayDirectionLight = normalize(LIGHT_POSITION - position); + float attenuationLight = attenuation(distToLight); + #endif + + float transmittanceLight = 1.0; + for (int j = 0; j < RAYMARCH_VOLUME_SAMPLES_LIGHT; j++) { + vec3 positionLight = position + rayDirectionLight * j * tstepLight; + VolumeMaterial resLight = RAYMARCH_VOLUME_MAP_FNC(positionLight); + float extinctionLight = -resLight.sdf; + float densityLight = resLight.density*tstepLight; + transmittanceLight *= beerLambert(densityLight, extinctionLight); + } - const float tmin = RAYMARCH_MIN_DIST; - const float tmax = RAYMARCH_MAX_DIST; + return LIGHT_COLOR * LIGHT_INTENSITY * attenuationLight * transmittanceLight; +} +vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec2 st, float minDist) { float transmittance = 1.0; - float t = tmin; + float t = RAYMARCH_MIN_DIST; vec3 color = vec3(0.0, 0.0, 0.0); vec3 position = rayOrigin; @@ -68,42 +89,21 @@ vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec2 st, float min vec3 position = rayOrigin + rayDirection * t; VolumeMaterial res = RAYMARCH_VOLUME_MAP_FNC(position); float extinction = -res.sdf; - float tstep = tmax/float(RAYMARCH_VOLUME_SAMPLES); + float tstep = RAYMARCH_MAX_DIST/float(RAYMARCH_VOLUME_SAMPLES); float density = res.density*tstep; if (t < minDist && extinction > 0.0) { float sampleTransmittance = beerLambert(density, extinction); - - #if defined(LIGHT_DIRECTION) || defined(LIGHT_POSITION) - #if defined(LIGHT_DIRECTION) // directional light - float tstepLight = tmax/float(RAYMARCH_VOLUME_SAMPLES_LIGHT); - vec3 rayDirectionLight = LIGHT_DIRECTION; - const float attenuationLight = 1.0; - #else // point light - float distToLight = distance(LIGHT_POSITION, position); - float tstepLight = distToLight/float(RAYMARCH_VOLUME_SAMPLES_LIGHT); - vec3 rayDirectionLight = normalize(LIGHT_POSITION - position); - float attenuationLight = attenuation(distToLight); - #endif - - float transmittanceLight = 1.0; - for (int j = 0; j < RAYMARCH_VOLUME_SAMPLES_LIGHT; j++) { - vec3 positionLight = position + rayDirectionLight * j * tstepLight; - VolumeMaterial resLight = RAYMARCH_VOLUME_MAP_FNC(positionLight); - float extinctionLight = -resLight.sdf; - float densityLight = resLight.density*tstepLight; - transmittanceLight *= beerLambert(densityLight, extinctionLight); - } - vec3 luminance = LIGHT_COLOR * LIGHT_INTENSITY * attenuationLight * transmittanceLight; + vec3 luminance = shadowTransmittance(position); #else // no lighting vec3 luminance = 1.0; #endif // usual scattering integration - //color += res.albedo * luminance * density * transmittance; + color += res.albedo * luminance * density * transmittance; // energy-conserving scattering integration - vec3 integScatt = (luminance - luminance * sampleTransmittance) / max(extinction, EPSILON); - color += res.albedo * transmittance * integScatt; + //vec3 integScatt = (luminance - luminance * sampleTransmittance) / max(extinction, EPSILON); + //color += res.albedo * transmittance * integScatt; transmittance *= sampleTransmittance; } From c40e3e6e1279a6d81edde3e23e05f9d1d1262b50 Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Tue, 20 Aug 2024 15:23:22 +0100 Subject: [PATCH 25/38] Physically accurate volume single-scattering equation. --- lighting/common/beerLambert.glsl | 14 ----- lighting/common/beerLambert.hlsl | 14 ----- lighting/material/volumeNew.glsl | 17 ++--- lighting/material/volumeNew.hlsl | 19 ++---- lighting/raymarch.glsl | 8 ++- lighting/raymarch.hlsl | 10 +-- lighting/raymarch/volume.glsl | 97 +++++++++++++++------------- lighting/raymarch/volume.hlsl | 104 ++++++++++++++++--------------- lighting/volumeMaterial.glsl | 4 +- lighting/volumeMaterial.hlsl | 4 +- 10 files changed, 134 insertions(+), 157 deletions(-) delete mode 100644 lighting/common/beerLambert.glsl delete mode 100644 lighting/common/beerLambert.hlsl diff --git a/lighting/common/beerLambert.glsl b/lighting/common/beerLambert.glsl deleted file mode 100644 index 397c8a18..00000000 --- a/lighting/common/beerLambert.glsl +++ /dev/null @@ -1,14 +0,0 @@ -/* -contributors: Shadi El Hajj -description: The Beer-Lambert law -license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj -*/ - -#ifndef FNC_BEER_LAMBERT -#define FNC_BEER_LAMBERT - -float beerLambert(float absorption, float dist) { - return exp(-absorption * dist); -} - -#endif diff --git a/lighting/common/beerLambert.hlsl b/lighting/common/beerLambert.hlsl deleted file mode 100644 index 397c8a18..00000000 --- a/lighting/common/beerLambert.hlsl +++ /dev/null @@ -1,14 +0,0 @@ -/* -contributors: Shadi El Hajj -description: The Beer-Lambert law -license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj -*/ - -#ifndef FNC_BEER_LAMBERT -#define FNC_BEER_LAMBERT - -float beerLambert(float absorption, float dist) { - return exp(-absorption * dist); -} - -#endif diff --git a/lighting/material/volumeNew.glsl b/lighting/material/volumeNew.glsl index 53b0dc57..fe787db1 100644 --- a/lighting/material/volumeNew.glsl +++ b/lighting/material/volumeNew.glsl @@ -14,8 +14,8 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj #define FNC_VOLUME_MATERIAL_NEW void volumeMaterialNew(out VolumeMaterial _mat) { - _mat.albedo = vec3(1.0, 1.0, 1.0); - _mat.density = 1.0; + _mat.absorption = vec3(1.0, 1.0, 1.0); + _mat.scattering = vec3(1.0, 1.0, 1.0); _mat.sdf = RAYMARCH_MAX_DIST; } @@ -26,17 +26,10 @@ VolumeMaterial volumeMaterialNew() { return mat; } -VolumeMaterial volumeMaterialNew(vec3 albedo, float sdf) { +VolumeMaterial volumeMaterialNew(vec3 absorption, vec3 scattering, float sdf) { VolumeMaterial mat = volumeMaterialNew(); - mat.albedo.rgb = albedo; - mat.sdf = sdf; - return mat; -} - -VolumeMaterial volumeMaterialNew(vec3 albedo, float density, float sdf) { - VolumeMaterial mat = volumeMaterialNew(); - mat.albedo.rgb = albedo; - mat.density = density; + mat.absorption = absorption; + mat.scattering = scattering; mat.sdf = sdf; return mat; } diff --git a/lighting/material/volumeNew.hlsl b/lighting/material/volumeNew.hlsl index 67bb74e4..e66c1dfa 100644 --- a/lighting/material/volumeNew.hlsl +++ b/lighting/material/volumeNew.hlsl @@ -14,9 +14,9 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj #define FNC_VOLUME_MATERIAL_NEW void volumeMaterialNew(out VolumeMaterial _mat) { - _mat.albedo = float3(1.0, 1.0, 1.0); - _mat.density = 1.0; - _mat.sdf = RAYMARCH_MAX_DIST; + _mat.absorption = float3(1.0, 1.0, 1.0); + _mat.scattering = float3(1.0, 1.0, 1.0); + _mat.sdf = RAYMARCH_MAX_DIST; } @@ -26,17 +26,10 @@ VolumeMaterial volumeMaterialNew() { return mat; } -VolumeMaterial volumeMaterialNew(float3 albedo, float sdf) { +VolumeMaterial volumeMaterialNew(float3 absorption, float3 scattering, float sdf) { VolumeMaterial mat = volumeMaterialNew(); - mat.albedo.rgb = albedo; - mat.sdf = sdf; - return mat; -} - -VolumeMaterial volumeMaterialNew(float3 albedo, float density, float sdf) { - VolumeMaterial mat = volumeMaterialNew(); - mat.albedo.rgb = albedo; - mat.density = density; + mat.absorption = absorption; + mat.scattering = scattering; mat.sdf = sdf; return mat; } diff --git a/lighting/raymarch.glsl b/lighting/raymarch.glsl index c7438ba9..34f47e2a 100644 --- a/lighting/raymarch.glsl +++ b/lighting/raymarch.glsl @@ -87,9 +87,11 @@ vec4 raymarch( mat4 viewMatrix, vec2 st Material mat; #endif - color += RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward, dist, sampleDepth, mat); + vec4 opaque = RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward, dist, sampleDepth, mat); #ifdef RAYMARCH_VOLUME - color += RAYMARCH_VOLUME_RENDER_FNC(camera, rayDirection, st, dist); + color += vec4(RAYMARCH_VOLUME_RENDER_FNC(camera, rayDirection, st, dist, opaque.rgb), opaque.a); + #else + color += opaque; #endif #if RAYMARCH_RETURN >= 1 @@ -211,7 +213,7 @@ vec4 raymarch( mat4 viewMatrix, vec2 st vec4 color = RAYMARCH_RENDER_FNC( camera, rayDirection, cameraForward, dist, eyeDepth, mat); #ifdef RAYMARCH_VOLUME - color += RAYMARCH_VOLUME_RENDER_FNC(camera, rayDirection, st, dist); + color.rgb = RAYMARCH_VOLUME_RENDER_FNC(camera, rayDirection, st, dist, color.rgb); #endif return color; diff --git a/lighting/raymarch.hlsl b/lighting/raymarch.hlsl index fdea4f7a..33d59c18 100644 --- a/lighting/raymarch.hlsl +++ b/lighting/raymarch.hlsl @@ -87,9 +87,11 @@ float4 raymarch(float4x4 viewMatrix, float2 st Material mat; #endif - color += RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward, dist, sampleDepth, mat); + float4 opaque = RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward, dist, sampleDepth, mat); #ifdef RAYMARCH_VOLUME - color += RAYMARCH_VOLUME_RENDER_FNC(camera, rayDirection, st, dist); + color += float4(RAYMARCH_VOLUME_RENDER_FNC(camera, rayDirection, st, dist, opaque.rgb), opaque.a); + #else + color += opaque; #endif #if RAYMARCH_RETURN >= 1 @@ -209,9 +211,9 @@ float4 raymarch(float4x4 viewMatrix, float2 st Material mat; #endif - float4 color = RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward, dist, eyeDepth, mat); + float4 color = RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward, dist, eyeDepth, mat); #ifdef RAYMARCH_VOLUME - color += RAYMARCH_VOLUME_RENDER_FNC(camera, rayDirection, st, dist); + color.rgb = RAYMARCH_VOLUME_RENDER_FNC(camera, rayDirection, st, dist, color.rgb); #endif return color; diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index 0a066b29..279c0f27 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -1,6 +1,5 @@ #include "map.glsl" #include "../common/attenuation.glsl" -#include "../common/beerLambert.glsl" #include "../../generative/random.glsl" #include "../../math/const.glsl" #include "../material/volumeNew.glsl" @@ -14,6 +13,7 @@ options: - RAYMARCH_VOLUME_SAMPLES_LIGHT 8 - RAYMARCH_VOLUME_MAP_FNC raymarchVolumeMap - RAYMARCH_VOLUME_DITHER 0.1 + - RAYMARCH_ENERGY_CONSERVING - LIGHT_COLOR vec3(0.5, 0.5, 0.5) - LIGHT_INTENSITY 1.0 - LIGHT_POSITION @@ -54,65 +54,74 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj #ifndef FNC_RAYMARCH_VOLUMERENDER #define FNC_RAYMARCH_VOLUMERENDER -vec3 shadowTransmittance(vec3 position) { - #if defined(LIGHT_DIRECTION) || defined(LIGHT_POSITION) - #if defined(LIGHT_DIRECTION) // directional light - float tstepLight = RAYMARCH_MAX_DIST/float(RAYMARCH_VOLUME_SAMPLES_LIGHT); - vec3 rayDirectionLight = LIGHT_DIRECTION; - const float attenuationLight = 1.0; - #else // point light - float distToLight = distance(LIGHT_POSITION, position); - float tstepLight = distToLight/float(RAYMARCH_VOLUME_SAMPLES_LIGHT); - vec3 rayDirectionLight = normalize(LIGHT_POSITION - position); - float attenuationLight = attenuation(distToLight); - #endif - - float transmittanceLight = 1.0; +vec3 shadowTransmittance(vec3 position, vec3 rayDirectionL, float stepSizeL) { + vec3 transmittanceL = vec3(1.0, 1.0, 1.0); + for (int j = 0; j < RAYMARCH_VOLUME_SAMPLES_LIGHT; j++) { - vec3 positionLight = position + rayDirectionLight * j * tstepLight; - VolumeMaterial resLight = RAYMARCH_VOLUME_MAP_FNC(positionLight); - float extinctionLight = -resLight.sdf; - float densityLight = resLight.density*tstepLight; - transmittanceLight *= beerLambert(densityLight, extinctionLight); + vec3 positionL = position + rayDirectionL * j * stepSizeL; + VolumeMaterial resL = RAYMARCH_VOLUME_MAP_FNC(positionL); + float densityL = -resL.sdf; + vec3 extinctionL = resL.absorption + resL.scattering; + transmittanceL *= exp(-densityL * extinctionL * stepSizeL); } - return LIGHT_COLOR * LIGHT_INTENSITY * attenuationLight * transmittanceLight; + return transmittanceL; } -vec4 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec2 st, float minDist) { - float transmittance = 1.0; +vec3 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec2 st, float minDist, vec3 background) { + vec3 scatteredLuminance = vec3(0.0, 0.0, 0.0); + vec3 transmittance = vec3(1.0, 1.0, 1.0); + float stepSize = RAYMARCH_MAX_DIST/float(RAYMARCH_VOLUME_SAMPLES); + float t = RAYMARCH_MIN_DIST; - vec3 color = vec3(0.0, 0.0, 0.0); vec3 position = rayOrigin; - - for(int i = 0; i < RAYMARCH_VOLUME_SAMPLES; i++) { + + for(int i = 0; i < RAYMARCH_VOLUME_SAMPLES; i++) { vec3 position = rayOrigin + rayDirection * t; VolumeMaterial res = RAYMARCH_VOLUME_MAP_FNC(position); - float extinction = -res.sdf; - float tstep = RAYMARCH_MAX_DIST/float(RAYMARCH_VOLUME_SAMPLES); - float density = res.density*tstep; - if (t < minDist && extinction > 0.0) { - float sampleTransmittance = beerLambert(density, extinction); - vec3 luminance = shadowTransmittance(position); + float density = -res.sdf; + vec3 extinction = res.absorption + res.scattering; + + if (t < minDist && density > 0.0) { + + #if defined(LIGHT_DIRECTION) || defined(LIGHT_POSITION) + #if defined(LIGHT_DIRECTION) // directional light + float stepSizeL = RAYMARCH_MAX_DIST/float(RAYMARCH_VOLUME_SAMPLES_LIGHT); + vec3 rayDirectionL = LIGHT_DIRECTION; + const float attenuationL = 1.0; + #else // point light + float distL = distance(LIGHT_POSITION, position); + float stepSizeL = distL/float(RAYMARCH_VOLUME_SAMPLES_LIGHT); + vec3 rayDirectionL = normalize(LIGHT_POSITION - position); + float attenuationL = attenuation(distL); + #endif + vec3 shadow = shadowTransmittance(position, rayDirectionL, stepSizeL); + vec3 L = LIGHT_COLOR * LIGHT_INTENSITY; #else // no lighting - vec3 luminance = 1.0; + const float attenuationL = 1.0; + const vec3 shadow = 1.0; + const vec3 L = vec3(1.0, 1.0, 1.0); #endif - // usual scattering integration - color += res.albedo * luminance * density * transmittance; - - // energy-conserving scattering integration - //vec3 integScatt = (luminance - luminance * sampleTransmittance) / max(extinction, EPSILON); - //color += res.albedo * transmittance * integScatt; - - transmittance *= sampleTransmittance; + #if defined RAYMARCH_ENERGY_CONSERVING + // energy-conserving scattering integration + vec3 S = L * attenuationL * shadow * density * res.scattering; + vec3 sampleExtinction = max(vec3(EPSILON, EPSILON, EPSILON), density * extinction); + vec3 Sint = (S - S * exp(-sampleExtinction * stepSize)) / sampleExtinction; + scatteredLuminance += transmittance * Sint; + transmittance *= exp(-sampleExtinction * stepSize); + #else + // usual scattering integration. Not energy-conserving. + scatteredLuminance += attenuationL * shadow * transmittance * density * res.scattering * stepSize * L; + transmittance *= exp(-density * extinction * stepSize); + #endif } - float offset = random(st)*(tstep*RAYMARCH_VOLUME_DITHER); - t += tstep + offset; + float offset = random(st)*(stepSize*RAYMARCH_VOLUME_DITHER); + t += stepSize + offset; } - return vec4(color, 0.0); + return background * transmittance + scatteredLuminance; } #endif diff --git a/lighting/raymarch/volume.hlsl b/lighting/raymarch/volume.hlsl index 593ae9eb..62102cd3 100644 --- a/lighting/raymarch/volume.hlsl +++ b/lighting/raymarch/volume.hlsl @@ -1,6 +1,5 @@ #include "map.hlsl" #include "../common/attenuation.hlsl" -#include "../common/beerLambert.hlsl" #include "../../generative/random.hlsl" #include "../../math/const.hlsl" #include "../material/volumeNew.hlsl" @@ -14,6 +13,7 @@ options: - RAYMARCH_VOLUME_SAMPLES_LIGHT 8 - RAYMARCH_VOLUME_MAP_FNC raymarchVolumeMap - RAYMARCH_VOLUME_DITHER 0.1 + - RAYMARCH_ENERGY_CONSERVING - LIGHT_COLOR float3(0.5, 0.5, 0.5) - LIGHT_INTENSITY 1.0 - LIGHT_POSITION @@ -54,68 +54,74 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj #ifndef FNC_RAYMARCH_VOLUMERENDER #define FNC_RAYMARCH_VOLUMERENDER -float4 raymarchVolume(in float3 rayOrigin, in float3 rayDirection, float2 st, float minDist) -{ +float3 shadowTransmittance(float3 position, float3 rayDirectionL, float stepSizeL) { + float3 transmittanceL = float3(1.0, 1.0, 1.0); + + for (int j = 0; j < RAYMARCH_VOLUME_SAMPLES_LIGHT; j++) { + float3 positionL = position + rayDirectionL * j * stepSizeL; + VolumeMaterial resL = RAYMARCH_VOLUME_MAP_FNC(positionL); + float densityL = -resL.sdf; + float3 extinctionL = resL.absorption + resL.scattering; + transmittanceL *= exp(-densityL * extinctionL * stepSizeL); + } + + return transmittanceL; +} - const float tmin = RAYMARCH_MIN_DIST; - const float tmax = RAYMARCH_MAX_DIST; +float3 raymarchVolume( in float3 rayOrigin, in float3 rayDirection, float2 st, float minDist, float3 background) { + float3 scatteredLuminance = float3(0.0, 0.0, 0.0); + float3 transmittance = float3(1.0, 1.0, 1.0); + float stepSize = RAYMARCH_MAX_DIST/float(RAYMARCH_VOLUME_SAMPLES); - float transmittance = 1.0; - float t = tmin; - float3 color = float3(0.0, 0.0, 0.0); + float t = RAYMARCH_MIN_DIST; float3 position = rayOrigin; - - for (int i = 0; i < RAYMARCH_VOLUME_SAMPLES; i++) - { + + for(int i = 0; i < RAYMARCH_VOLUME_SAMPLES; i++) { float3 position = rayOrigin + rayDirection * t; VolumeMaterial res = RAYMARCH_VOLUME_MAP_FNC(position); - float extinction = -res.sdf; - float tstep = tmax / float(RAYMARCH_VOLUME_SAMPLES); - float density = res.density * tstep; - if (t < minDist && extinction > 0.0) - { - float sampleTransmittance = beerLambert(density, extinction); + float density = -res.sdf; + float3 extinction = res.absorption + res.scattering; - #if defined(LIGHT_DIRECTION) || defined(LIGHT_POSITION) - #if defined(LIGHT_DIRECTION) // directional light - float tstepLight = tmax/float(RAYMARCH_VOLUME_SAMPLES_LIGHT); - float3 rayDirectionLight = LIGHT_DIRECTION; - const float attenuationLight = 1.0; - #else // point light - float distToLight = distance(LIGHT_POSITION, position); - float tstepLight = distToLight/float(RAYMARCH_VOLUME_SAMPLES_LIGHT); - float3 rayDirectionLight = normalize(LIGHT_POSITION - position); - float attenuationLight = attenuation(distToLight); - #endif + if (t < minDist && density > 0.0) { - float transmittanceLight = 1.0; - for (int j = 0; j < RAYMARCH_VOLUME_SAMPLES_LIGHT; j++) { - float3 positionLight = position + rayDirectionLight * j * tstepLight; - VolumeMaterial resLight = RAYMARCH_VOLUME_MAP_FNC(positionLight); - float extinctionLight = -resLight.sdf; - float densityLight = resLight.density*tstepLight; - transmittanceLight *= beerLambert(densityLight, extinctionLight); - } - float3 luminance = LIGHT_COLOR * LIGHT_INTENSITY * attenuationLight * transmittanceLight; + #if defined(LIGHT_DIRECTION) || defined(LIGHT_POSITION) + #if defined(LIGHT_DIRECTION) // directional light + float stepSizeL = RAYMARCH_MAX_DIST/float(RAYMARCH_VOLUME_SAMPLES_LIGHT); + float3 rayDirectionL = LIGHT_DIRECTION; + const float attenuationL = 1.0; + #else // point light + float distL = distance(LIGHT_POSITION, position); + float stepSizeL = distL/float(RAYMARCH_VOLUME_SAMPLES_LIGHT); + float3 rayDirectionL = normalize(LIGHT_POSITION - position); + float attenuationL = attenuation(distL); + #endif + float3 shadow = shadowTransmittance(position, rayDirectionL, stepSizeL); + float3 L = LIGHT_COLOR * LIGHT_INTENSITY; #else // no lighting - float3 luminance = 1.0; + const float attenuationL = 1.0; + const float3 shadow = 1.0; + const float3 L = float3(1.0, 1.0, 1.0); #endif - // usual scattering integration - //color += res.albedo * luminance * density * transmittance; - - // energy-conserving scattering integration - float3 integScatt = (luminance - luminance * sampleTransmittance) / max(extinction, EPSILON); - color += res.albedo * transmittance * integScatt; - - transmittance *= sampleTransmittance; + #if defined RAYMARCH_ENERGY_CONSERVING + // energy-conserving scattering integration + float3 S = L * attenuationL * shadow * density * res.scattering; + float3 sampleExtinction = max(float3(EPSILON, EPSILON, EPSILON), density * extinction); + float3 Sint = (S - S * exp(-sampleExtinction * stepSize)) / sampleExtinction; + scatteredLuminance += transmittance * Sint; + transmittance *= exp(-sampleExtinction * stepSize); + #else + // usual scattering integration. Not energy-conserving. + scatteredLuminance += attenuationL * shadow * transmittance * density * res.scattering * stepSize * L; + transmittance *= exp(-density * extinction * stepSize); + #endif } - float offset = random(st) * (tstep * RAYMARCH_VOLUME_DITHER); - t += tstep + offset; + float offset = random(st)*(stepSize*RAYMARCH_VOLUME_DITHER); + t += stepSize + offset; } - return float4(color, 0.0); + return background * transmittance + scatteredLuminance; } #endif diff --git a/lighting/volumeMaterial.glsl b/lighting/volumeMaterial.glsl index 5e594d6c..136e7e85 100644 --- a/lighting/volumeMaterial.glsl +++ b/lighting/volumeMaterial.glsl @@ -8,8 +8,8 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj #define STR_VOLUME_MATERIAL struct VolumeMaterial { - vec3 albedo; - float density; + vec3 absorption; + vec3 scattering; float sdf; }; diff --git a/lighting/volumeMaterial.hlsl b/lighting/volumeMaterial.hlsl index c0edd8e4..5d8c7011 100644 --- a/lighting/volumeMaterial.hlsl +++ b/lighting/volumeMaterial.hlsl @@ -8,8 +8,8 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj #define STR_VOLUME_MATERIAL struct VolumeMaterial { - float3 albedo; - float density; + float3 absorption; + float3 scattering; float sdf; }; From 1331a2932c126dd2a0de3152d4cd9e454c3bb5be Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Tue, 20 Aug 2024 15:55:14 +0100 Subject: [PATCH 26/38] Volume: Swapped scattering an absorption --- lighting/material/volumeNew.glsl | 10 +++++++--- lighting/material/volumeNew.hlsl | 10 +++++++--- lighting/volumeMaterial.glsl | 2 +- lighting/volumeMaterial.hlsl | 2 +- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/lighting/material/volumeNew.glsl b/lighting/material/volumeNew.glsl index fe787db1..1cbdafcb 100644 --- a/lighting/material/volumeNew.glsl +++ b/lighting/material/volumeNew.glsl @@ -14,8 +14,8 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj #define FNC_VOLUME_MATERIAL_NEW void volumeMaterialNew(out VolumeMaterial _mat) { - _mat.absorption = vec3(1.0, 1.0, 1.0); _mat.scattering = vec3(1.0, 1.0, 1.0); + _mat.absorption = vec3(1.0, 1.0, 1.0); _mat.sdf = RAYMARCH_MAX_DIST; } @@ -26,12 +26,16 @@ VolumeMaterial volumeMaterialNew() { return mat; } -VolumeMaterial volumeMaterialNew(vec3 absorption, vec3 scattering, float sdf) { +VolumeMaterial volumeMaterialNew(vec3 scattering, vec3 absorption, float sdf) { VolumeMaterial mat = volumeMaterialNew(); - mat.absorption = absorption; mat.scattering = scattering; + mat.absorption = absorption; mat.sdf = sdf; return mat; } +VolumeMaterial volumeMaterialNew(vec3 scattering, float sdf) { + return volumeMaterialNew(scattering, vec3(0.0, 0.0, 0.0), sdf); +} + #endif diff --git a/lighting/material/volumeNew.hlsl b/lighting/material/volumeNew.hlsl index e66c1dfa..2acef85b 100644 --- a/lighting/material/volumeNew.hlsl +++ b/lighting/material/volumeNew.hlsl @@ -14,8 +14,8 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj #define FNC_VOLUME_MATERIAL_NEW void volumeMaterialNew(out VolumeMaterial _mat) { - _mat.absorption = float3(1.0, 1.0, 1.0); _mat.scattering = float3(1.0, 1.0, 1.0); + _mat.absorption = float3(1.0, 1.0, 1.0); _mat.sdf = RAYMARCH_MAX_DIST; } @@ -26,12 +26,16 @@ VolumeMaterial volumeMaterialNew() { return mat; } -VolumeMaterial volumeMaterialNew(float3 absorption, float3 scattering, float sdf) { +VolumeMaterial volumeMaterialNew(float3 scattering, float3 absorption, float sdf) { VolumeMaterial mat = volumeMaterialNew(); - mat.absorption = absorption; mat.scattering = scattering; + mat.absorption = absorption; mat.sdf = sdf; return mat; } +VolumeMaterial volumeMaterialNew(float3 scattering, float sdf) { + return volumeMaterialNew(scattering, float3(0.0, 0.0, 0.0), sdf); +} + #endif diff --git a/lighting/volumeMaterial.glsl b/lighting/volumeMaterial.glsl index 136e7e85..bb4872bf 100644 --- a/lighting/volumeMaterial.glsl +++ b/lighting/volumeMaterial.glsl @@ -8,8 +8,8 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj #define STR_VOLUME_MATERIAL struct VolumeMaterial { - vec3 absorption; vec3 scattering; + vec3 absorption; float sdf; }; diff --git a/lighting/volumeMaterial.hlsl b/lighting/volumeMaterial.hlsl index 5d8c7011..ccba72de 100644 --- a/lighting/volumeMaterial.hlsl +++ b/lighting/volumeMaterial.hlsl @@ -8,8 +8,8 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj #define STR_VOLUME_MATERIAL struct VolumeMaterial { - float3 absorption; float3 scattering; + float3 absorption; float sdf; }; From 4b656121f18b40d82f72a51d13a18b24225ce799 Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Tue, 20 Aug 2024 16:54:49 +0100 Subject: [PATCH 27/38] Volume: Added opaque shadowing --- lighting/raymarch/volume.glsl | 21 +++++++++++++++------ lighting/raymarch/volume.hlsl | 19 ++++++++++++++----- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index 279c0f27..3bf80369 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -36,11 +36,11 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj #endif #ifndef RAYMARCH_VOLUME_SAMPLES -#define RAYMARCH_VOLUME_SAMPLES 256 +#define RAYMARCH_VOLUME_SAMPLES 64 #endif #ifndef RAYMARCH_VOLUME_SAMPLES_LIGHT -#define RAYMARCH_VOLUME_SAMPLES_LIGHT 8 +#define RAYMARCH_VOLUME_SAMPLES_LIGHT 32 #endif #ifndef RAYMARCH_VOLUME_MAP_FNC @@ -56,13 +56,23 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj vec3 shadowTransmittance(vec3 position, vec3 rayDirectionL, float stepSizeL) { vec3 transmittanceL = vec3(1.0, 1.0, 1.0); - + float tL = 0.0; + for (int j = 0; j < RAYMARCH_VOLUME_SAMPLES_LIGHT; j++) { - vec3 positionL = position + rayDirectionL * j * stepSizeL; + vec3 positionL = position + rayDirectionL * tL; + #if defined(RAYMARCH_VOLUME_OPAQUE_SHADOWING) + Material mat = RAYMARCH_MAP_FNC(positionL); + if (mat.sdf <= 0.0) { + return vec3(0.0, 0.0, 0.0); + } + #endif VolumeMaterial resL = RAYMARCH_VOLUME_MAP_FNC(positionL); float densityL = -resL.sdf; vec3 extinctionL = resL.absorption + resL.scattering; transmittanceL *= exp(-densityL * extinctionL * stepSizeL); + + float offset = random(position)*(stepSizeL*RAYMARCH_VOLUME_DITHER); + tL += stepSizeL + offset; } return transmittanceL; @@ -74,7 +84,6 @@ vec3 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec2 st, float min float stepSize = RAYMARCH_MAX_DIST/float(RAYMARCH_VOLUME_SAMPLES); float t = RAYMARCH_MIN_DIST; - vec3 position = rayOrigin; for(int i = 0; i < RAYMARCH_VOLUME_SAMPLES; i++) { vec3 position = rayOrigin + rayDirection * t; @@ -87,7 +96,7 @@ vec3 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec2 st, float min #if defined(LIGHT_DIRECTION) || defined(LIGHT_POSITION) #if defined(LIGHT_DIRECTION) // directional light float stepSizeL = RAYMARCH_MAX_DIST/float(RAYMARCH_VOLUME_SAMPLES_LIGHT); - vec3 rayDirectionL = LIGHT_DIRECTION; + vec3 rayDirectionL = normalize(LIGHT_DIRECTION); const float attenuationL = 1.0; #else // point light float distL = distance(LIGHT_POSITION, position); diff --git a/lighting/raymarch/volume.hlsl b/lighting/raymarch/volume.hlsl index 62102cd3..6612f670 100644 --- a/lighting/raymarch/volume.hlsl +++ b/lighting/raymarch/volume.hlsl @@ -36,11 +36,11 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj #endif #ifndef RAYMARCH_VOLUME_SAMPLES -#define RAYMARCH_VOLUME_SAMPLES 256 +#define RAYMARCH_VOLUME_SAMPLES 64 #endif #ifndef RAYMARCH_VOLUME_SAMPLES_LIGHT -#define RAYMARCH_VOLUME_SAMPLES_LIGHT 8 +#define RAYMARCH_VOLUME_SAMPLES_LIGHT 32 #endif #ifndef RAYMARCH_VOLUME_MAP_FNC @@ -56,13 +56,23 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj float3 shadowTransmittance(float3 position, float3 rayDirectionL, float stepSizeL) { float3 transmittanceL = float3(1.0, 1.0, 1.0); - + float tL = 0.0; + for (int j = 0; j < RAYMARCH_VOLUME_SAMPLES_LIGHT; j++) { - float3 positionL = position + rayDirectionL * j * stepSizeL; + float3 positionL = position + rayDirectionL * tL; + #if defined(RAYMARCH_VOLUME_OPAQUE_SHADOWING) + Material mat = RAYMARCH_MAP_FNC(positionL); + if (mat.sdf <= 0.0) { + return float3(0.0, 0.0, 0.0); + } + #endif VolumeMaterial resL = RAYMARCH_VOLUME_MAP_FNC(positionL); float densityL = -resL.sdf; float3 extinctionL = resL.absorption + resL.scattering; transmittanceL *= exp(-densityL * extinctionL * stepSizeL); + + float offset = random(position)*(stepSizeL*RAYMARCH_VOLUME_DITHER); + tL += stepSizeL + offset; } return transmittanceL; @@ -74,7 +84,6 @@ float3 raymarchVolume( in float3 rayOrigin, in float3 rayDirection, float2 st, f float stepSize = RAYMARCH_MAX_DIST/float(RAYMARCH_VOLUME_SAMPLES); float t = RAYMARCH_MIN_DIST; - float3 position = rayOrigin; for(int i = 0; i < RAYMARCH_VOLUME_SAMPLES; i++) { float3 position = rayOrigin + rayDirection * t; From d180c3d62d1382ffd57c88712228f1fd3b04ff2a Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Tue, 20 Aug 2024 17:56:11 +0100 Subject: [PATCH 28/38] Renamed VolumeMaterial to Volume. --- lighting/material/volumeNew.glsl | 41 ------------------- lighting/material/volumeNew.hlsl | 41 ------------------- lighting/raymarch/map.glsl | 4 +- lighting/raymarch/map.hlsl | 4 +- lighting/raymarch/volume.glsl | 6 +-- lighting/raymarch/volume.hlsl | 6 +-- lighting/{volumeMaterial.glsl => volume.glsl} | 8 ++-- lighting/{volumeMaterial.hlsl => volume.hlsl} | 8 ++-- lighting/volume/new.glsl | 41 +++++++++++++++++++ lighting/volume/new.hlsl | 41 +++++++++++++++++++ 10 files changed, 100 insertions(+), 100 deletions(-) delete mode 100644 lighting/material/volumeNew.glsl delete mode 100644 lighting/material/volumeNew.hlsl rename lighting/{volumeMaterial.glsl => volume.glsl} (59%) rename lighting/{volumeMaterial.hlsl => volume.hlsl} (59%) create mode 100644 lighting/volume/new.glsl create mode 100644 lighting/volume/new.hlsl diff --git a/lighting/material/volumeNew.glsl b/lighting/material/volumeNew.glsl deleted file mode 100644 index 1cbdafcb..00000000 --- a/lighting/material/volumeNew.glsl +++ /dev/null @@ -1,41 +0,0 @@ -#include "../volumeMaterial.glsl" - -/* -contributors: Shadi El Hajj -description: | - Volume Material Constructor. -use: - - void volumeMaterialNew(out _mat) - - volumeMaterialNew() -license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj -*/ - -#ifndef FNC_VOLUME_MATERIAL_NEW -#define FNC_VOLUME_MATERIAL_NEW - -void volumeMaterialNew(out VolumeMaterial _mat) { - _mat.scattering = vec3(1.0, 1.0, 1.0); - _mat.absorption = vec3(1.0, 1.0, 1.0); - _mat.sdf = RAYMARCH_MAX_DIST; - -} - -VolumeMaterial volumeMaterialNew() { - VolumeMaterial mat; - volumeMaterialNew(mat); - return mat; -} - -VolumeMaterial volumeMaterialNew(vec3 scattering, vec3 absorption, float sdf) { - VolumeMaterial mat = volumeMaterialNew(); - mat.scattering = scattering; - mat.absorption = absorption; - mat.sdf = sdf; - return mat; -} - -VolumeMaterial volumeMaterialNew(vec3 scattering, float sdf) { - return volumeMaterialNew(scattering, vec3(0.0, 0.0, 0.0), sdf); -} - -#endif diff --git a/lighting/material/volumeNew.hlsl b/lighting/material/volumeNew.hlsl deleted file mode 100644 index 2acef85b..00000000 --- a/lighting/material/volumeNew.hlsl +++ /dev/null @@ -1,41 +0,0 @@ -#include "../volumeMaterial.hlsl" - -/* -contributors: Shadi El Hajj -description: | - Volume Material Constructor. -use: - - void volumeMaterialNew(out _mat) - - volumeMaterialNew() -license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj -*/ - -#ifndef FNC_VOLUME_MATERIAL_NEW -#define FNC_VOLUME_MATERIAL_NEW - -void volumeMaterialNew(out VolumeMaterial _mat) { - _mat.scattering = float3(1.0, 1.0, 1.0); - _mat.absorption = float3(1.0, 1.0, 1.0); - _mat.sdf = RAYMARCH_MAX_DIST; - -} - -VolumeMaterial volumeMaterialNew() { - VolumeMaterial mat; - volumeMaterialNew(mat); - return mat; -} - -VolumeMaterial volumeMaterialNew(float3 scattering, float3 absorption, float sdf) { - VolumeMaterial mat = volumeMaterialNew(); - mat.scattering = scattering; - mat.absorption = absorption; - mat.sdf = sdf; - return mat; -} - -VolumeMaterial volumeMaterialNew(float3 scattering, float sdf) { - return volumeMaterialNew(scattering, float3(0.0, 0.0, 0.0), sdf); -} - -#endif diff --git a/lighting/raymarch/map.glsl b/lighting/raymarch/map.glsl index 764f7657..18212b10 100644 --- a/lighting/raymarch/map.glsl +++ b/lighting/raymarch/map.glsl @@ -1,5 +1,5 @@ #include "../material.glsl" -#include "../volumeMaterial.glsl" +#include "../volume.glsl" /* contributors: Inigo Quiles @@ -21,6 +21,6 @@ examples: #define FNC_RAYMARCH_MAP Material RAYMARCH_MAP_FNC( in vec3 pos ); -VolumeMaterial RAYMARCH_VOLUME_MAP_FNC( in vec3 pos ); +Volume RAYMARCH_VOLUME_MAP_FNC( in vec3 pos ); #endif \ No newline at end of file diff --git a/lighting/raymarch/map.hlsl b/lighting/raymarch/map.hlsl index cd388ffa..407e6f8a 100644 --- a/lighting/raymarch/map.hlsl +++ b/lighting/raymarch/map.hlsl @@ -1,5 +1,5 @@ #include "../material.hlsl" -#include "../volumeMaterial.hlsl" +#include "../volume.hlsl" /* contributors: Inigo Quiles @@ -19,6 +19,6 @@ use: raymarchMap( in pos ) #define FNC_RAYMARCH_MAP Material RAYMARCH_MAP_FNC( in float3 pos ); -VolumeMaterial RAYMARCH_VOLUME_MAP_FNC(in float3 pos); +Volume RAYMARCH_VOLUME_MAP_FNC(in float3 pos); #endif \ No newline at end of file diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index 3bf80369..0ecefefc 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -2,7 +2,7 @@ #include "../common/attenuation.glsl" #include "../../generative/random.glsl" #include "../../math/const.glsl" -#include "../material/volumeNew.glsl" +#include "../volume/new.glsl" /* contributors: Shadi El Hajj @@ -66,7 +66,7 @@ vec3 shadowTransmittance(vec3 position, vec3 rayDirectionL, float stepSizeL) { return vec3(0.0, 0.0, 0.0); } #endif - VolumeMaterial resL = RAYMARCH_VOLUME_MAP_FNC(positionL); + Volume resL = RAYMARCH_VOLUME_MAP_FNC(positionL); float densityL = -resL.sdf; vec3 extinctionL = resL.absorption + resL.scattering; transmittanceL *= exp(-densityL * extinctionL * stepSizeL); @@ -87,7 +87,7 @@ vec3 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec2 st, float min for(int i = 0; i < RAYMARCH_VOLUME_SAMPLES; i++) { vec3 position = rayOrigin + rayDirection * t; - VolumeMaterial res = RAYMARCH_VOLUME_MAP_FNC(position); + Volume res = RAYMARCH_VOLUME_MAP_FNC(position); float density = -res.sdf; vec3 extinction = res.absorption + res.scattering; diff --git a/lighting/raymarch/volume.hlsl b/lighting/raymarch/volume.hlsl index 6612f670..380aba45 100644 --- a/lighting/raymarch/volume.hlsl +++ b/lighting/raymarch/volume.hlsl @@ -2,7 +2,7 @@ #include "../common/attenuation.hlsl" #include "../../generative/random.hlsl" #include "../../math/const.hlsl" -#include "../material/volumeNew.hlsl" +#include "../volume/new.hlsl" /* contributors: Shadi El Hajj @@ -66,7 +66,7 @@ float3 shadowTransmittance(float3 position, float3 rayDirectionL, float stepSize return float3(0.0, 0.0, 0.0); } #endif - VolumeMaterial resL = RAYMARCH_VOLUME_MAP_FNC(positionL); + Volume resL = RAYMARCH_VOLUME_MAP_FNC(positionL); float densityL = -resL.sdf; float3 extinctionL = resL.absorption + resL.scattering; transmittanceL *= exp(-densityL * extinctionL * stepSizeL); @@ -87,7 +87,7 @@ float3 raymarchVolume( in float3 rayOrigin, in float3 rayDirection, float2 st, f for(int i = 0; i < RAYMARCH_VOLUME_SAMPLES; i++) { float3 position = rayOrigin + rayDirection * t; - VolumeMaterial res = RAYMARCH_VOLUME_MAP_FNC(position); + Volume res = RAYMARCH_VOLUME_MAP_FNC(position); float density = -res.sdf; float3 extinction = res.absorption + res.scattering; diff --git a/lighting/volumeMaterial.glsl b/lighting/volume.glsl similarity index 59% rename from lighting/volumeMaterial.glsl rename to lighting/volume.glsl index bb4872bf..990670e4 100644 --- a/lighting/volumeMaterial.glsl +++ b/lighting/volume.glsl @@ -1,13 +1,13 @@ /* contributors: Shadi El Hajj -description: Volume Material Structure +description: Volume Structure license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ -#ifndef STR_VOLUME_MATERIAL -#define STR_VOLUME_MATERIAL +#ifndef STR_VOLUME +#define STR_VOLUME -struct VolumeMaterial { +struct Volume { vec3 scattering; vec3 absorption; float sdf; diff --git a/lighting/volumeMaterial.hlsl b/lighting/volume.hlsl similarity index 59% rename from lighting/volumeMaterial.hlsl rename to lighting/volume.hlsl index ccba72de..709650fd 100644 --- a/lighting/volumeMaterial.hlsl +++ b/lighting/volume.hlsl @@ -1,13 +1,13 @@ /* contributors: Shadi El Hajj -description: Volume Material Structure +description: Volume Structure license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ -#ifndef STR_VOLUME_MATERIAL -#define STR_VOLUME_MATERIAL +#ifndef STR_VOLUME +#define STR_VOLUME -struct VolumeMaterial { +struct Volume { float3 scattering; float3 absorption; float sdf; diff --git a/lighting/volume/new.glsl b/lighting/volume/new.glsl new file mode 100644 index 00000000..9390a0b5 --- /dev/null +++ b/lighting/volume/new.glsl @@ -0,0 +1,41 @@ +#include "../volume.glsl" + +/* +contributors: Shadi El Hajj +description: | + Volume Constructor. +use: + - void volumeNew(out _mat) + - volumeNew() +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj +*/ + +#ifndef FNC_VOLUME_NEW +#define FNC_VOLUME_NEW + +void volumeNew(out Volume _mat) { + _mat.scattering = vec3(1.0, 1.0, 1.0); + _mat.absorption = vec3(1.0, 1.0, 1.0); + _mat.sdf = RAYMARCH_MAX_DIST; + +} + +Volume volumeNew() { + Volume mat; + volumeNew(mat); + return mat; +} + +Volume volumeNew(vec3 scattering, vec3 absorption, float sdf) { + Volume mat = volumeNew(); + mat.scattering = scattering; + mat.absorption = absorption; + mat.sdf = sdf; + return mat; +} + +Volume volumeNew(vec3 scattering, float sdf) { + return volumeNew(scattering, vec3(0.0, 0.0, 0.0), sdf); +} + +#endif diff --git a/lighting/volume/new.hlsl b/lighting/volume/new.hlsl new file mode 100644 index 00000000..91ed5f82 --- /dev/null +++ b/lighting/volume/new.hlsl @@ -0,0 +1,41 @@ +#include "../volume.hlsl" + +/* +contributors: Shadi El Hajj +description: | + Volume Constructor. +use: + - void volumeNew(out _mat) + - volumeNew() +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj +*/ + +#ifndef FNC_VOLUME_NEW +#define FNC_VOLUME_NEW + +void volumeNew(out Volume _mat) { + _mat.scattering = float3(1.0, 1.0, 1.0); + _mat.absorption = float3(1.0, 1.0, 1.0); + _mat.sdf = RAYMARCH_MAX_DIST; + +} + +Volume volumeNew() { + Volume mat; + volumeNew(mat); + return mat; +} + +Volume volumeNew(float3 scattering, float3 absorption, float sdf) { + Volume mat = volumeNew(); + mat.scattering = scattering; + mat.absorption = absorption; + mat.sdf = sdf; + return mat; +} + +Volume volumeNew(float3 scattering, float sdf) { + return volumeNew(scattering, float3(0.0, 0.0, 0.0), sdf); +} + +#endif From 833fd4b24254721485010c6569e80b3ac8ace978 Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Tue, 20 Aug 2024 18:13:16 +0100 Subject: [PATCH 29/38] Refactored raymarch.* --- lighting/material/add.glsl | 61 ++++++++++++++++++++++ lighting/material/add.hlsl | 61 ++++++++++++++++++++++ lighting/material/multiply.glsl | 53 +++++++++++++++++++ lighting/material/multiply.hlsl | 53 +++++++++++++++++++ lighting/raymarch.glsl | 92 ++------------------------------- lighting/raymarch.hlsl | 91 ++------------------------------ 6 files changed, 235 insertions(+), 176 deletions(-) create mode 100644 lighting/material/add.glsl create mode 100644 lighting/material/add.hlsl create mode 100644 lighting/material/multiply.glsl create mode 100644 lighting/material/multiply.hlsl diff --git a/lighting/material/add.glsl b/lighting/material/add.glsl new file mode 100644 index 00000000..42732ab5 --- /dev/null +++ b/lighting/material/add.glsl @@ -0,0 +1,61 @@ +#include "../material.glsl" + +/* +contributors: Shadi El Hajj +description: Add materials a and b, property by property, store result in r +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj +*/ + +#ifndef MATERIAL_ADD +#define MATERIAL_ADD + +void add(Material a, Material b, Material r) { + r.albedo = a.albedo + b.albedo; + r.emissive = a.emissive + b.emissive; + r.position = a.position + b.position; + r.normal = a.normal + b.normal; + + #if defined(SCENE_BACK_SURFACE) + r.normal_back = a.normal_back + b.normal_back; + #endif + + r.ior = a.ior + b.ior; + r.f0 = a.f0 + b.f0; + r.roughness = a.roughness + b.roughness; + r.metallic = a.metallic + b.metallic; + r.ambientOcclusion = a.ambientOcclusion + b.ambientOcclusion; + + #if defined(SHADING_MODEL_CLEAR_COAT) + r.clearCoat = a.clearCoat + b.clearCoat; + r.clearCoatRoughness = a.clearCoatRoughness + b.clearCoatRoughness; + #if defined(MATERIAL_HAS_CLEAR_COAT_NORMAL) + r.clearCoatNormal = a.clearCoatNormal + b.clearCoatNormal; + #endif + #endif + + #if defined(SHADING_MODEL_IRIDESCENCE) + r.thickness = a.thickness + b.thickness; + #endif + + #if defined(SHADING_MODEL_SUBSURFACE) + r.subsurfaceColor = a.subsurfaceColor + b.subsurfaceColor; + r.subsurfacePower = a.subsurfacePower + b.subsurfacePower; + r.subsurfaceThickness = a.subsurfaceThickness + b.subsurfaceThickness; + #endif + + #if defined(SHADING_MODEL_CLOTH) + r.sheenColor = a.sheenColor + b.sheenColor; + #endif + + #if defined(SHADING_MODEL_SPECULAR_GLOSSINESS) + r.specularColor = a.specularColor + b.specularColor; + r.glossiness = a.glossiness + b.glossiness; + #endif + + // I don't think anybody needs this + // r.V = a.V + b.V; + // r.R = a.R + b.R; + // r.NoV = a.NoV + b.NoV; +} + +#endif diff --git a/lighting/material/add.hlsl b/lighting/material/add.hlsl new file mode 100644 index 00000000..4450d664 --- /dev/null +++ b/lighting/material/add.hlsl @@ -0,0 +1,61 @@ +#include "../material.hlsl" + +/* +contributors: Shadi El Hajj +description: Add materials a and b, property by property, store result in r +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj +*/ + +#ifndef MATERIAL_ADD +#define MATERIAL_ADD + +void add(Material a, Material b, Material r) { + r.albedo = a.albedo + b.albedo; + r.emissive = a.emissive + b.emissive; + r.position = a.position + b.position; + r.normal = a.normal + b.normal; + + #if defined(SCENE_BACK_SURFACE) + r.normal_back = a.normal_back + b.normal_back; + #endif + + r.ior = a.ior + b.ior; + r.f0 = a.f0 + b.f0; + r.roughness = a.roughness + b.roughness; + r.metallic = a.metallic + b.metallic; + r.ambientOcclusion = a.ambientOcclusion + b.ambientOcclusion; + + #if defined(SHADING_MODEL_CLEAR_COAT) + r.clearCoat = a.clearCoat + b.clearCoat; + r.clearCoatRoughness = a.clearCoatRoughness + b.clearCoatRoughness; + #if defined(MATERIAL_HAS_CLEAR_COAT_NORMAL) + r.clearCoatNormal = a.clearCoatNormal + b.clearCoatNormal; + #endif + #endif + + #if defined(SHADING_MODEL_IRIDESCENCE) + r.thickness = a.thickness + b.thickness; + #endif + + #if defined(SHADING_MODEL_SUBSURFACE) + r.subsurfaceColor = a.subsurfaceColor + b.subsurfaceColor; + r.subsurfacePower = a.subsurfacePower + b.subsurfacePower; + r.subsurfaceThickness = a.subsurfaceThickness + b.subsurfaceThickness; + #endif + + #if defined(SHADING_MODEL_CLOTH) + r.sheenColor = a.sheenColor + b.sheenColor; + #endif + + #if defined(SHADING_MODEL_SPECULAR_GLOSSINESS) + r.specularColor = a.specularColor + b.specularColor; + r.glossiness = a.glossiness + b.glossiness; + #endif + + // I don't think anybody needs this + // r.V = a.V + b.V; + // r.R = a.R + b.R; + // r.NoV = a.NoV + b.NoV; +} + +#endif diff --git a/lighting/material/multiply.glsl b/lighting/material/multiply.glsl new file mode 100644 index 00000000..2b97384b --- /dev/null +++ b/lighting/material/multiply.glsl @@ -0,0 +1,53 @@ +#include "../material.glsl" + +/* +contributors: Shadi El Hajj +description: Mutiply material properties by a constant, store result in r +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj +*/ + +#ifndef MATERIAL_MULTIPLY +#define MATERIAL_MULTIPLY + +void multiply(Material mat, float f, Material r) { + r.albedo = mat.albedo * f; + r.emissive = mat.emissive * f; + r.position = mat.position * f; + r.normal = mat.normal * f; + #if defined(SCENE_BACK_SURFACE) + r.normal_back = mat.normal_back * f; + #endif + r.ior = mat.ior * f; + r.f0 = mat.f0 * f; + r.roughness = mat.roughness * f; + r.metallic = mat.metallic * f; + r.ambientOcclusion = mat.ambientOcclusion * f; + #if defined(SHADING_MODEL_CLEAR_COAT) + r.clearCoat = mat.clearCoat * f; + r.clearCoatRoughness = mat.clearCoatRoughness * f; + #if defined(MATERIAL_HAS_CLEAR_COAT_NORMAL) + r.clearCoatNormal = mat.clearCoatNormal * f; + #endif + #endif + #if defined(SHADING_MODEL_IRIDESCENCE) + r.thickness = mat.thickness * f; + #endif + #if defined(SHADING_MODEL_SUBSURFACE) + r.subsurfaceColor = mat.subsurfaceColor * f; + r.subsurfacePower = mat.subsurfacePower * f; + r.subsurfaceThickness = mat.subsurfaceThickness * f; + #endif + #if defined(SHADING_MODEL_CLOTH) + r.sheenColor = mat.sheenColor * f; + #endif + #if defined(SHADING_MODEL_SPECULAR_GLOSSINESS) + r.specularColor = mat.specularColor * f; + r.glossiness = mat.glossiness * f; + #endif + // I don't think anybody needs this + // r.V = mat.V * f; + // r.R = mat.R * f; + // r.NoV = mat.NoV * f; +} + +#endif diff --git a/lighting/material/multiply.hlsl b/lighting/material/multiply.hlsl new file mode 100644 index 00000000..9535590c --- /dev/null +++ b/lighting/material/multiply.hlsl @@ -0,0 +1,53 @@ +#include "../material.hlsl" + +/* +contributors: Shadi El Hajj +description: Mutiply material properties by a constant, store result in r +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj +*/ + +#ifndef MATERIAL_MULTIPLY +#define MATERIAL_MULTIPLY + +void multiply(Material mat, float f, Material r) { + r.albedo = mat.albedo * f; + r.emissive = mat.emissive * f; + r.position = mat.position * f; + r.normal = mat.normal * f; + #if defined(SCENE_BACK_SURFACE) + r.normal_back = mat.normal_back * f; + #endif + r.ior = mat.ior * f; + r.f0 = mat.f0 * f; + r.roughness = mat.roughness * f; + r.metallic = mat.metallic * f; + r.ambientOcclusion = mat.ambientOcclusion * f; + #if defined(SHADING_MODEL_CLEAR_COAT) + r.clearCoat = mat.clearCoat * f; + r.clearCoatRoughness = mat.clearCoatRoughness * f; + #if defined(MATERIAL_HAS_CLEAR_COAT_NORMAL) + r.clearCoatNormal = mat.clearCoatNormal * f; + #endif + #endif + #if defined(SHADING_MODEL_IRIDESCENCE) + r.thickness = mat.thickness * f; + #endif + #if defined(SHADING_MODEL_SUBSURFACE) + r.subsurfaceColor = mat.subsurfaceColor * f; + r.subsurfacePower = mat.subsurfacePower * f; + r.subsurfaceThickness = mat.subsurfaceThickness * f; + #endif + #if defined(SHADING_MODEL_CLOTH) + r.sheenColor = mat.sheenColor * f; + #endif + #if defined(SHADING_MODEL_SPECULAR_GLOSSINESS) + r.specularColor = mat.specularColor * f; + r.glossiness = mat.glossiness * f; + #endif + // I don't think anybody needs this + // r.V = mat.V * f; + // r.R = mat.R * f; + // r.NoV = mat.NoV * f; +} + +#endif diff --git a/lighting/raymarch.glsl b/lighting/raymarch.glsl index 34f47e2a..edf97d46 100644 --- a/lighting/raymarch.glsl +++ b/lighting/raymarch.glsl @@ -5,6 +5,7 @@ #include "raymarch/render.glsl" #include "raymarch/volume.glsl" #include "material/zero.glsl" +#include "material/add.glsl" /* contributors: Patricio Gonzalez Vivo @@ -13,13 +14,11 @@ use: - raymarch( viewMatrix, st) - raymarch( viewMatrix, st, out eyeDepth) - raymarch( viewMatrix, st, out eyeDepth, out mat) - - raymarch( viewMatrix, st, out eyeDepth, out worldPosition, out worldNormal) - raymarch( cameraPosition, lookAt, st) - raymarch( cameraPosition, lookAt, st, out eyeDepth) - raymarch( cameraPosition, lookAt, st, out eyeDepth, out mat) - - raymarch( cameraPosition, lookAt, st, out eyeDepth, out worldPosition, out worldNormal) options: - - RAYMARCH_RENDER_FNC(RO, RD): default raymarchDefaultRender(RO, RD, TA) + - RAYMARCH_RENDER_FNC: default raymarchDefaultRender - RAYMARCH_CAMERA_FOV: Filed of view express in degrees. Default 60.0 degrees - RAYMARCH_MULTISAMPLE: default 1. If it is greater than 1 it will render multisample - RAYMARCH_RETURN: 0. nothing (default), 1. depth; 2. depth and material @@ -100,52 +99,7 @@ vec4 raymarch( mat4 viewMatrix, vec2 st #if RAYMARCH_RETURN == 2 // Accumulate material properties - matAcc.albedo += mat.albedo; - matAcc.emissive += mat.emissive; - matAcc.position += mat.position; - matAcc.normal += mat.normal; - - #if defined(SCENE_BACK_SURFACE) - matAcc.normal_back += mat.normal_back; - #endif - - matAcc.ior += mat.ior; - matAcc.f0 += mat.f0; - matAcc.roughness += mat.roughness; - matAcc.metallic += mat.metallic; - matAcc.ambientOcclusion += mat.ambientOcclusion; - - #if defined(SHADING_MODEL_CLEAR_COAT) - matAcc.clearCoat += mat.clearCoat; - matAcc.clearCoatRoughness += mat.clearCoatRoughness; - #if defined(MATERIAL_HAS_CLEAR_COAT_NORMAL) - matAcc.clearCoatNormal += mat.clearCoatNormal; - #endif - #endif - - #if defined(SHADING_MODEL_IRIDESCENCE) - matAcc.thickness += mat.thickness; - #endif - - #if defined(SHADING_MODEL_SUBSURFACE) - matAcc.subsurfaceColor += mat.subsurfaceColor; - matAcc.subsurfacePower += mat.subsurfacePower; - matAcc.subsurfaceThickness += mat.subsurfaceThickness; - #endif - - #if defined(SHADING_MODEL_CLOTH) - matAcc.sheenColor += mat.sheenColor; - #endif - - #if defined(SHADING_MODEL_SPECULAR_GLOSSINESS) - matAcc.specularColor += mat.specularColor; - matAcc.glossiness += mat.glossiness; - #endif - - // I don't thing nobody needs this - // matAcc.V += mat.V; - // matAcc.R += mat.R; - // matAcc.NoV += mat.NoV; + add(matAcc, mat, matAcc); #endif offset = rotate(offset, HALF_PI); @@ -156,46 +110,8 @@ vec4 raymarch( mat4 viewMatrix, vec2 st #endif #if RAYMARCH_RETURN == 2 - // Average material - mat.albedo = matAcc.albedo * RAYMARCH_MULTISAMPLE_FACTOR; - mat.emissive = matAcc.emissive * RAYMARCH_MULTISAMPLE_FACTOR; - mat.position = matAcc.position * RAYMARCH_MULTISAMPLE_FACTOR; - mat.normal = matAcc.normal * RAYMARCH_MULTISAMPLE_FACTOR; - #if defined(SCENE_BACK_SURFACE) - mat.normal_back = matAcc.normal_back * RAYMARCH_MULTISAMPLE_FACTOR; - #endif - mat.ior = matAcc.ior * RAYMARCH_MULTISAMPLE_FACTOR; - mat.f0 = matAcc.f0 * RAYMARCH_MULTISAMPLE_FACTOR; - mat.roughness = matAcc.roughness * RAYMARCH_MULTISAMPLE_FACTOR; - mat.metallic = matAcc.metallic * RAYMARCH_MULTISAMPLE_FACTOR; - mat.ambientOcclusion = matAcc.ambientOcclusion * RAYMARCH_MULTISAMPLE_FACTOR; - #if defined(SHADING_MODEL_CLEAR_COAT) - mat.clearCoat = matAcc.clearCoat * RAYMARCH_MULTISAMPLE_FACTOR; - mat.clearCoatRoughness = matAcc.clearCoatRoughness * RAYMARCH_MULTISAMPLE_FACTOR; - #if defined(MATERIAL_HAS_CLEAR_COAT_NORMAL) - mat.clearCoatNormal = matAcc.clearCoatNormal * RAYMARCH_MULTISAMPLE_FACTOR; - #endif - #endif - #if defined(SHADING_MODEL_IRIDESCENCE) - mat.thickness = matAcc.thickness * RAYMARCH_MULTISAMPLE_FACTOR; - #endif - #if defined(SHADING_MODEL_SUBSURFACE) - mat.subsurfaceColor = matAcc.subsurfaceColor * RAYMARCH_MULTISAMPLE_FACTOR; - mat.subsurfacePower = matAcc.subsurfacePower * RAYMARCH_MULTISAMPLE_FACTOR; - mat.subsurfaceThickness = matAcc.subsurfaceThickness * RAYMARCH_MULTISAMPLE_FACTOR; - #endif - #if defined(SHADING_MODEL_CLOTH) - mat.sheenColor = matAcc.sheenColor * RAYMARCH_MULTISAMPLE_FACTOR; - #endif - #if defined(SHADING_MODEL_SPECULAR_GLOSSINESS) - mat.specularColor = matAcc.specularColor * RAYMARCH_MULTISAMPLE_FACTOR; - mat.glossiness = matAcc.glossiness * RAYMARCH_MULTISAMPLE_FACTOR; - #endif - // I don't thing nobody needs this - // mat.V = matAcc.V * RAYMARCH_MULTISAMPLE_FACTOR; - // mat.R = matAcc.R * RAYMARCH_MULTISAMPLE_FACTOR; - // mat.NoV = matAcc.NoV * RAYMARCH_MULTISAMPLE_FACTOR; + multiply(mat, RAYMARCH_MULTISAMPLE_FACTOR, mat); #endif return color * RAYMARCH_MULTISAMPLE_FACTOR; diff --git a/lighting/raymarch.hlsl b/lighting/raymarch.hlsl index 33d59c18..f6fd0bb8 100644 --- a/lighting/raymarch.hlsl +++ b/lighting/raymarch.hlsl @@ -12,13 +12,11 @@ use: - raymarch( viewMatrix, st) - raymarch( viewMatrix, st, out eyeDepth) - raymarch( viewMatrix, st, out eyeDepth, out mat) - - raymarch( viewMatrix, st, out eyeDepth, out worldPosition, out worldNormal) - raymarch( cameraPosition, lookAt, st) - raymarch( cameraPosition, lookAt, st, out eyeDepth) - raymarch( cameraPosition, lookAt, st, out eyeDepth, out mat) - - raymarch( cameraPosition, lookAt, st, out eyeDepth, out worldPosition, out worldNormal) options: - - RAYMARCH_RENDER_FNC(RO, RD): default raymarchDefaultRender(RO, RD, TA) + - RAYMARCH_RENDER_FNC: default raymarchDefaultRender - RAYMARCH_CAMERA_FOV: Filed of view express in degrees. Default 60.0 degrees - RAYMARCH_MULTISAMPLE: default 1. If it is greater than 1 it will render multisample - RAYMARCH_RETURN: 0. nothing (default), 1. depth; 2. depth and material @@ -100,52 +98,7 @@ float4 raymarch(float4x4 viewMatrix, float2 st #if RAYMARCH_RETURN == 2 // Accumulate material properties - matAcc.albedo += mat.albedo; - matAcc.emissive += mat.emissive; - matAcc.position += mat.position; - matAcc.normal += mat.normal; - - #if defined(SCENE_BACK_SURFACE) - matAcc.normal_back += mat.normal_back; - #endif - - matAcc.ior += mat.ior; - matAcc.f0 += mat.f0; - matAcc.roughness += mat.roughness; - matAcc.metallic += mat.metallic; - matAcc.ambientOcclusion += mat.ambientOcclusion; - - #if defined(SHADING_MODEL_CLEAR_COAT) - matAcc.clearCoat += mat.clearCoat; - matAcc.clearCoatRoughness += mat.clearCoatRoughness; - #if defined(MATERIAL_HAS_CLEAR_COAT_NORMAL) - matAcc.clearCoatNormal += mat.clearCoatNormal; - #endif - #endif - - #if defined(SHADING_MODEL_IRIDESCENCE) - matAcc.thickness += mat.thickness; - #endif - - #if defined(SHADING_MODEL_SUBSURFACE) - matAcc.subsurfaceColor += mat.subsurfaceColor; - matAcc.subsurfacePower += mat.subsurfacePower; - matAcc.subsurfaceThickness += mat.subsurfaceThickness; - #endif - - #if defined(SHADING_MODEL_CLOTH) - matAcc.sheenColor += mat.sheenColor; - #endif - - #if defined(SHADING_MODEL_SPECULAR_GLOSSINESS) - matAcc.specularColor += mat.specularColor; - matAcc.glossiness += mat.glossiness; - #endif - - // I don't thing nobody needs this - // matAcc.V += mat.V; - // matAcc.R += mat.R; - // matAcc.NoV += mat.NoV; + add(matAcc, mat, matAcc); #endif offset = rotate(offset, HALF_PI); @@ -156,46 +109,8 @@ float4 raymarch(float4x4 viewMatrix, float2 st #endif #if RAYMARCH_RETURN == 2 - // Average material - mat.albedo = matAcc.albedo * RAYMARCH_MULTISAMPLE_FACTOR; - mat.emissive = matAcc.emissive * RAYMARCH_MULTISAMPLE_FACTOR; - mat.position = matAcc.position * RAYMARCH_MULTISAMPLE_FACTOR; - mat.normal = matAcc.normal * RAYMARCH_MULTISAMPLE_FACTOR; - #if defined(SCENE_BACK_SURFACE) - mat.normal_back = matAcc.normal_back * RAYMARCH_MULTISAMPLE_FACTOR; - #endif - mat.ior = matAcc.ior * RAYMARCH_MULTISAMPLE_FACTOR; - mat.f0 = matAcc.f0 * RAYMARCH_MULTISAMPLE_FACTOR; - mat.roughness = matAcc.roughness * RAYMARCH_MULTISAMPLE_FACTOR; - mat.metallic = matAcc.metallic * RAYMARCH_MULTISAMPLE_FACTOR; - mat.ambientOcclusion = matAcc.ambientOcclusion * RAYMARCH_MULTISAMPLE_FACTOR; - #if defined(SHADING_MODEL_CLEAR_COAT) - mat.clearCoat = matAcc.clearCoat * RAYMARCH_MULTISAMPLE_FACTOR; - mat.clearCoatRoughness = matAcc.clearCoatRoughness * RAYMARCH_MULTISAMPLE_FACTOR; - #if defined(MATERIAL_HAS_CLEAR_COAT_NORMAL) - mat.clearCoatNormal = matAcc.clearCoatNormal * RAYMARCH_MULTISAMPLE_FACTOR; - #endif - #endif - #if defined(SHADING_MODEL_IRIDESCENCE) - mat.thickness = matAcc.thickness * RAYMARCH_MULTISAMPLE_FACTOR; - #endif - #if defined(SHADING_MODEL_SUBSURFACE) - mat.subsurfaceColor = matAcc.subsurfaceColor * RAYMARCH_MULTISAMPLE_FACTOR; - mat.subsurfacePower = matAcc.subsurfacePower * RAYMARCH_MULTISAMPLE_FACTOR; - mat.subsurfaceThickness = matAcc.subsurfaceThickness * RAYMARCH_MULTISAMPLE_FACTOR; - #endif - #if defined(SHADING_MODEL_CLOTH) - mat.sheenColor = matAcc.sheenColor * RAYMARCH_MULTISAMPLE_FACTOR; - #endif - #if defined(SHADING_MODEL_SPECULAR_GLOSSINESS) - mat.specularColor = matAcc.specularColor * RAYMARCH_MULTISAMPLE_FACTOR; - mat.glossiness = matAcc.glossiness * RAYMARCH_MULTISAMPLE_FACTOR; - #endif - // I don't thing nobody needs this - // mat.V = matAcc.V * RAYMARCH_MULTISAMPLE_FACTOR; - // mat.R = matAcc.R * RAYMARCH_MULTISAMPLE_FACTOR; - // mat.NoV = matAcc.NoV * RAYMARCH_MULTISAMPLE_FACTOR; + multiply(mat, RAYMARCH_MULTISAMPLE_FACTOR, mat); #endif return color * RAYMARCH_MULTISAMPLE_FACTOR; From c9de7113b8a05f5b42839f492d286a1814adac82 Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Tue, 20 Aug 2024 18:36:23 +0100 Subject: [PATCH 30/38] Refactored RAYMARCH_AOV interface. --- lighting/raymarch.glsl | 99 +++++++++++++++++------------------------- lighting/raymarch.hlsl | 93 ++++++++++++++++----------------------- 2 files changed, 78 insertions(+), 114 deletions(-) diff --git a/lighting/raymarch.glsl b/lighting/raymarch.glsl index edf97d46..a465654c 100644 --- a/lighting/raymarch.glsl +++ b/lighting/raymarch.glsl @@ -21,7 +21,7 @@ options: - RAYMARCH_RENDER_FNC: default raymarchDefaultRender - RAYMARCH_CAMERA_FOV: Filed of view express in degrees. Default 60.0 degrees - RAYMARCH_MULTISAMPLE: default 1. If it is greater than 1 it will render multisample - - RAYMARCH_RETURN: 0. nothing (default), 1. depth; 2. depth and material + - RAYMARCH_AOV: return AOVs in a Material structure examples: - /shaders/lighting_raymarching.frag license: @@ -41,37 +41,24 @@ license: #define RAYMARCH_CAMERA_FOV 60.0 #endif -#ifndef ENG_RAYMARCHING -#define ENG_RAYMARCHING +#ifndef ENG_RAYMARCH +#define ENG_RAYMARCH #if defined(RAYMARCH_MULTISAMPLE) const float RAYMARCH_MULTISAMPLE_FACTOR = 1.0/float(RAYMARCH_MULTISAMPLE); #endif -vec4 raymarch( mat4 viewMatrix, vec2 st -#if RAYMARCH_RETURN >= 1 - ,out float eyeDepth -#endif -#if RAYMARCH_RETURN == 2 - ,out Material mat -#endif - ) { - +vec4 raymarch(mat4 viewMatrix, vec2 st, out float eyeDepth, out Material mat) { + float fov = 1.0 / tan(RAYMARCH_CAMERA_FOV * DEG2RAD * 0.5); vec3 camera = viewMatrix[3].xyz; vec3 cameraForward = viewMatrix[2].xyz; mat3 viewMatrix3 = toMat3(viewMatrix); #if defined(RAYMARCH_MULTISAMPLE) - vec4 color = vec4(0.0); - #if RAYMARCH_RETURN >= 1 + vec4 color = vec4(0.0, 0.0, 0.0, 0.0); eyeDepth = 0.0; - #endif - #if RAYMARCH_RETURN == 2 - Material matAcc; - materialZero(matAcc); - #endif vec2 pixel = 1.0/RESOLUTION; vec2 offset = rotate( vec2(0.5, 0.0), EIGHTH_PI); @@ -82,10 +69,6 @@ vec4 raymarch( mat4 viewMatrix, vec2 st float sampleDepth = 0.0; float dist = 0.0; - #if RAYMARCH_RETURN != 2 - Material mat; - #endif - vec4 opaque = RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward, dist, sampleDepth, mat); #ifdef RAYMARCH_VOLUME color += vec4(RAYMARCH_VOLUME_RENDER_FNC(camera, rayDirection, st, dist, opaque.rgb), opaque.a); @@ -93,67 +76,65 @@ vec4 raymarch( mat4 viewMatrix, vec2 st color += opaque; #endif - #if RAYMARCH_RETURN >= 1 - eyeDepth += sampleDepth; - #endif + eyeDepth += sampleDepth; - #if RAYMARCH_RETURN == 2 - // Accumulate material properties + #if RAYMARCH_AOV add(matAcc, mat, matAcc); #endif offset = rotate(offset, HALF_PI); } - #if RAYMARCH_RETURN >= 1 - eyeDepth *= RAYMARCH_MULTISAMPLE_FACTOR; - #endif + eyeDepth *= RAYMARCH_MULTISAMPLE_FACTOR; - #if RAYMARCH_RETURN == 2 - // Average material + #if RAYMARCH_AOV multiply(mat, RAYMARCH_MULTISAMPLE_FACTOR, mat); #endif return color * RAYMARCH_MULTISAMPLE_FACTOR; -#else + +#else // Single sample - // Single sample vec3 rayDirection = viewMatrix3 * normalize(vec3(st*2.0-1.0, fov)); float dist = 0.0; - #if RAYMARCH_RETURN == 0 - float eyeDepth = 0.0; - #endif - #if RAYMARCH_RETURN != 2 - Material mat; - #endif + float eyeDepth = 0.0; - vec4 color = RAYMARCH_RENDER_FNC( camera, rayDirection, cameraForward, dist, eyeDepth, mat); + vec4 opaque = RAYMARCH_RENDER_FNC( camera, rayDirection, cameraForward, dist, eyeDepth, mat); #ifdef RAYMARCH_VOLUME - color.rgb = RAYMARCH_VOLUME_RENDER_FNC(camera, rayDirection, st, dist, color.rgb); + color = vec4(RAYMARCH_VOLUME_RENDER_FNC(camera, rayDirection, st, dist, opaque.rgb), opaque.a); + #else + color = opaque; #endif return color; + #endif } -vec4 raymarch( vec3 cameraPosition, vec3 cameraLookAt, vec2 st -#if RAYMARCH_RETURN >= 1 - ,out float depth -#endif -#if RAYMARCH_RETURN == 2 - ,out Material mat -#endif - ) { +vec4 raymarch(mat4 viewMatrix, vec2 st, out float eyeDepth) { + Material mat; + materialZero(mat); + return raymarch(viewMatrix, st, eyeDepth, mat); +} + +vec4 raymarch(mat4 viewMatrix, vec2 st) { + float eyeDepth = 0.0; + return raymarch(viewMatrix, st, eyeDepth); +} + +vec4 raymarch(vec3 cameraPosition, vec3 cameraLookAt, vec2 st, out float eyeDepth, out Material mat) { mat4 viewMatrix = lookAtView(cameraPosition, cameraLookAt); + return raymarch(viewMatrix, st, eyeDepth, mat); +} - return raymarch(viewMatrix, st - #if RAYMARCH_RETURN >= 1 - ,depth - #endif - #if RAYMARCH_RETURN == 2 - ,mat - #endif - ); +vec4 raymarch(vec3 cameraPosition, vec3 cameraLookAt, vec2 st, out float eyeDepth) { + mat4 viewMatrix = lookAtView(cameraPosition, cameraLookAt); + return raymarch(viewMatrix, st, eyeDepth); +} + +vec4 raymarch(vec3 cameraPosition, vec3 cameraLookAt, vec2 st) { + mat4 viewMatrix = lookAtView(cameraPosition, cameraLookAt); + return raymarch(viewMatrix, st); } #endif \ No newline at end of file diff --git a/lighting/raymarch.hlsl b/lighting/raymarch.hlsl index f6fd0bb8..f53d9945 100644 --- a/lighting/raymarch.hlsl +++ b/lighting/raymarch.hlsl @@ -19,7 +19,7 @@ options: - RAYMARCH_RENDER_FNC: default raymarchDefaultRender - RAYMARCH_CAMERA_FOV: Filed of view express in degrees. Default 60.0 degrees - RAYMARCH_MULTISAMPLE: default 1. If it is greater than 1 it will render multisample - - RAYMARCH_RETURN: 0. nothing (default), 1. depth; 2. depth and material + - RAYMARCH_AOV: return AOVs in a Material structure license: - Copyright (c) 2021 Patricio Gonzalez Vivo under Prosperity License - https://prosperitylicense.com/versions/3.0.0 - Copyright (c) 2021 Patricio Gonzalez Vivo under Patron License - https://lygia.xyz/license @@ -48,29 +48,16 @@ license: static const float RAYMARCH_MULTISAMPLE_FACTOR = 1.0/float(RAYMARCH_MULTISAMPLE); #endif -float4 raymarch(float4x4 viewMatrix, float2 st -#if RAYMARCH_RETURN >= 1 - ,out float eyeDepth -#endif -#if RAYMARCH_RETURN == 2 - ,out Material mat -#endif - ) { +float4 raymarch(float4x4 viewMatrix, float2 st, out float eyeDepth, out Material mat) { float fov = 1.0 / tan(RAYMARCH_CAMERA_FOV * DEG2RAD * 0.5); float3 camera = float3(viewMatrix._m03, viewMatrix._m13, viewMatrix._m23); float3 cameraForward = float3(viewMatrix._m02, viewMatrix._m12, viewMatrix._m22); #if defined(RAYMARCH_MULTISAMPLE) - float4 color = float4(0.0, 0.0, 0.0, 0.0); - #if RAYMARCH_RETURN >= 1 + float4 color = float4(0.0, 0.0, 0.0, 0.0); eyeDepth = 0.0; - #endif - #if RAYMARCH_RETURN == 2 - Material matAcc; - materialZero(matAcc); - #endif float2 pixel = 1.0/RESOLUTION; float2 offset = rotate( float2(0.5, 0.0), EIGHTH_PI); @@ -81,10 +68,6 @@ float4 raymarch(float4x4 viewMatrix, float2 st float sampleDepth = 0.0; float dist = 0.0; - #if RAYMARCH_RETURN != 2 - Material mat; - #endif - float4 opaque = RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward, dist, sampleDepth, mat); #ifdef RAYMARCH_VOLUME color += float4(RAYMARCH_VOLUME_RENDER_FNC(camera, rayDirection, st, dist, opaque.rgb), opaque.a); @@ -92,11 +75,9 @@ float4 raymarch(float4x4 viewMatrix, float2 st color += opaque; #endif - #if RAYMARCH_RETURN >= 1 - eyeDepth += sampleDepth; - #endif + eyeDepth += sampleDepth; - #if RAYMARCH_RETURN == 2 + #if RAYMARCH_AOV // Accumulate material properties add(matAcc, mat, matAcc); #endif @@ -104,55 +85,57 @@ float4 raymarch(float4x4 viewMatrix, float2 st offset = rotate(offset, HALF_PI); } - #if RAYMARCH_RETURN >= 1 - eyeDepth *= RAYMARCH_MULTISAMPLE_FACTOR; - #endif + eyeDepth *= RAYMARCH_MULTISAMPLE_FACTOR; - #if RAYMARCH_RETURN == 2 + #if RAYMARCH_AOV // Average material multiply(mat, RAYMARCH_MULTISAMPLE_FACTOR, mat); #endif return color * RAYMARCH_MULTISAMPLE_FACTOR; -#else - - // Single sample + +#else // Single sample + float3 rayDirection = mul((float3x3)viewMatrix, normalize(float3(st * 2.0 - 1.0, fov))); float dist = 0.0; - #if RAYMARCH_RETURN == 0 - float eyeDepth = 0.0; - #endif - #if RAYMARCH_RETURN != 2 - Material mat; - #endif + float eyeDepth = 0.0; - float4 color = RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward, dist, eyeDepth, mat); + float4 opaque = RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward, dist, eyeDepth, mat); #ifdef RAYMARCH_VOLUME - color.rgb = RAYMARCH_VOLUME_RENDER_FNC(camera, rayDirection, st, dist, color.rgb); + color = float4(RAYMARCH_VOLUME_RENDER_FNC(camera, rayDirection, st, dist, opaque.rgb), opaque.a); + #else + color = opaque; #endif return color; + #endif } -float4 raymarch(float3 cameraPosition, float3 cameraLookAt, float2 st -#if RAYMARCH_RETURN >= 1 - ,out float depth -#endif -#if RAYMARCH_RETURN == 2 - ,out Material mat -#endif - ){ +float4 raymarch(float4x4 viewMatrix, float2 st, out float eyeDepth) { + Material mat; + materialZero(mat); + return raymarch(viewMatrix, st, eyeDepth, mat); +} + +float4 raymarch(float4x4 viewMatrix, float2 st) { + float eyeDepth = 0.0; + return raymarch(viewMatrix, st, eyeDepth); +} + +float4 raymarch(float3 cameraPosition, float3 cameraLookAt, float2 st, out float eyeDepth, out Material mat) { float4x4 viewMatrix = lookAtView(cameraPosition, cameraLookAt); + return raymarch(viewMatrix, st, eyeDepth, mat); +} - return raymarch(viewMatrix, st - #if RAYMARCH_RETURN >= 1 - ,depth - #endif - #if RAYMARCH_RETURN == 2 - ,mat - #endif - ); +float4 raymarch(float3 cameraPosition, float3 cameraLookAt, float2 st, out float eyeDepth) { + float4x4 viewMatrix = lookAtView(cameraPosition, cameraLookAt); + return raymarch(viewMatrix, st, eyeDepth); +} + +float4 raymarch(float3 cameraPosition, float3 cameraLookAt, float2 st) { + float4x4 viewMatrix = lookAtView(cameraPosition, cameraLookAt); + return raymarch(viewMatrix, st); } #endif From b94685fcd9a57e766b9a427e01b39c5b88a05153 Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Tue, 20 Aug 2024 19:05:41 +0100 Subject: [PATCH 31/38] Renamed RAYMARCH_VOLUMETRIC_SHADOWS --- lighting/raymarch/volume.glsl | 2 +- lighting/raymarch/volume.hlsl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index 0ecefefc..789dad27 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -60,7 +60,7 @@ vec3 shadowTransmittance(vec3 position, vec3 rayDirectionL, float stepSizeL) { for (int j = 0; j < RAYMARCH_VOLUME_SAMPLES_LIGHT; j++) { vec3 positionL = position + rayDirectionL * tL; - #if defined(RAYMARCH_VOLUME_OPAQUE_SHADOWING) + #if defined(RAYMARCH_VOLUMETRIC_SHADOWS) Material mat = RAYMARCH_MAP_FNC(positionL); if (mat.sdf <= 0.0) { return vec3(0.0, 0.0, 0.0); diff --git a/lighting/raymarch/volume.hlsl b/lighting/raymarch/volume.hlsl index 380aba45..f70b81f8 100644 --- a/lighting/raymarch/volume.hlsl +++ b/lighting/raymarch/volume.hlsl @@ -60,7 +60,7 @@ float3 shadowTransmittance(float3 position, float3 rayDirectionL, float stepSize for (int j = 0; j < RAYMARCH_VOLUME_SAMPLES_LIGHT; j++) { float3 positionL = position + rayDirectionL * tL; - #if defined(RAYMARCH_VOLUME_OPAQUE_SHADOWING) + #if defined(RAYMARCH_VOLUMETRIC_SHADOWS) Material mat = RAYMARCH_MAP_FNC(positionL); if (mat.sdf <= 0.0) { return float3(0.0, 0.0, 0.0); From 1a2a6f28659ab9ea91729a85d683b5a8d96b4def Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Tue, 20 Aug 2024 21:06:47 +0100 Subject: [PATCH 32/38] Renamed volume to medium --- lighting/{volume.glsl => medium.glsl} | 8 +++--- lighting/{volume.hlsl => medium.hlsl} | 8 +++--- lighting/medium/new.glsl | 41 +++++++++++++++++++++++++++ lighting/medium/new.hlsl | 41 +++++++++++++++++++++++++++ lighting/raymarch/map.glsl | 4 +-- lighting/raymarch/map.hlsl | 4 +-- lighting/raymarch/volume.glsl | 6 ++-- lighting/raymarch/volume.hlsl | 6 ++-- lighting/volume/new.glsl | 41 --------------------------- lighting/volume/new.hlsl | 41 --------------------------- 10 files changed, 100 insertions(+), 100 deletions(-) rename lighting/{volume.glsl => medium.glsl} (67%) rename lighting/{volume.hlsl => medium.hlsl} (67%) create mode 100644 lighting/medium/new.glsl create mode 100644 lighting/medium/new.hlsl delete mode 100644 lighting/volume/new.glsl delete mode 100644 lighting/volume/new.hlsl diff --git a/lighting/volume.glsl b/lighting/medium.glsl similarity index 67% rename from lighting/volume.glsl rename to lighting/medium.glsl index 990670e4..6317f83c 100644 --- a/lighting/volume.glsl +++ b/lighting/medium.glsl @@ -1,13 +1,13 @@ /* contributors: Shadi El Hajj -description: Volume Structure +description: Medium Structure license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ -#ifndef STR_VOLUME -#define STR_VOLUME +#ifndef STR_MEDIUM +#define STR_MEDIUM -struct Volume { +struct Medium { vec3 scattering; vec3 absorption; float sdf; diff --git a/lighting/volume.hlsl b/lighting/medium.hlsl similarity index 67% rename from lighting/volume.hlsl rename to lighting/medium.hlsl index 709650fd..0f7be987 100644 --- a/lighting/volume.hlsl +++ b/lighting/medium.hlsl @@ -1,13 +1,13 @@ /* contributors: Shadi El Hajj -description: Volume Structure +description: Medium Structure license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ -#ifndef STR_VOLUME -#define STR_VOLUME +#ifndef STR_MEDIUM +#define STR_MEDIUM -struct Volume { +struct Medium { float3 scattering; float3 absorption; float sdf; diff --git a/lighting/medium/new.glsl b/lighting/medium/new.glsl new file mode 100644 index 00000000..7690b9c9 --- /dev/null +++ b/lighting/medium/new.glsl @@ -0,0 +1,41 @@ +#include "../medium.glsl" + +/* +contributors: Shadi El Hajj +description: | + Medium Constructor. +use: + - void mediumNew(out _mat) + - mediumNew() +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj +*/ + +#ifndef FNC_MEDIUM_NEW +#define FNC_MEDIUM_NEW + +void mediumNew(out Medium _mat) { + _mat.scattering = vec3(1.0, 1.0, 1.0); + _mat.absorption = vec3(1.0, 1.0, 1.0); + _mat.sdf = RAYMARCH_MAX_DIST; + +} + +Medium mediumNew() { + Medium mat; + mediumNew(mat); + return mat; +} + +Medium mediumNew(vec3 scattering, vec3 absorption, float sdf) { + Medium mat = mediumNew(); + mat.scattering = scattering; + mat.absorption = absorption; + mat.sdf = sdf; + return mat; +} + +Medium mediumNew(vec3 scattering, float sdf) { + return mediumNew(scattering, vec3(0.0, 0.0, 0.0), sdf); +} + +#endif diff --git a/lighting/medium/new.hlsl b/lighting/medium/new.hlsl new file mode 100644 index 00000000..2a0137e9 --- /dev/null +++ b/lighting/medium/new.hlsl @@ -0,0 +1,41 @@ +#include "../medium.hlsl" + +/* +contributors: Shadi El Hajj +description: | + Medium Constructor. +use: + - void mediumNew(out _mat) + - mediumNew() +license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj +*/ + +#ifndef FNC_MEDIUM_NEW +#define FNC_MEDIUM_NEW + +void mediumNew(out Medium _mat) { + _mat.scattering = float3(1.0, 1.0, 1.0); + _mat.absorption = float3(1.0, 1.0, 1.0); + _mat.sdf = RAYMARCH_MAX_DIST; + +} + +Medium mediumNew() { + Medium mat; + mediumNew(mat); + return mat; +} + +Medium mediumNew(float3 scattering, float3 absorption, float sdf) { + Medium mat = mediumNew(); + mat.scattering = scattering; + mat.absorption = absorption; + mat.sdf = sdf; + return mat; +} + +Medium mediumNew(float3 scattering, float sdf) { + return mediumNew(scattering, float3(0.0, 0.0, 0.0), sdf); +} + +#endif diff --git a/lighting/raymarch/map.glsl b/lighting/raymarch/map.glsl index 18212b10..0f4d214c 100644 --- a/lighting/raymarch/map.glsl +++ b/lighting/raymarch/map.glsl @@ -1,5 +1,5 @@ #include "../material.glsl" -#include "../volume.glsl" +#include "../medium.glsl" /* contributors: Inigo Quiles @@ -21,6 +21,6 @@ examples: #define FNC_RAYMARCH_MAP Material RAYMARCH_MAP_FNC( in vec3 pos ); -Volume RAYMARCH_VOLUME_MAP_FNC( in vec3 pos ); +Medium RAYMARCH_VOLUME_MAP_FNC( in vec3 pos ); #endif \ No newline at end of file diff --git a/lighting/raymarch/map.hlsl b/lighting/raymarch/map.hlsl index 407e6f8a..25ebbec4 100644 --- a/lighting/raymarch/map.hlsl +++ b/lighting/raymarch/map.hlsl @@ -1,5 +1,5 @@ #include "../material.hlsl" -#include "../volume.hlsl" +#include "../medium.hlsl" /* contributors: Inigo Quiles @@ -19,6 +19,6 @@ use: raymarchMap( in pos ) #define FNC_RAYMARCH_MAP Material RAYMARCH_MAP_FNC( in float3 pos ); -Volume RAYMARCH_VOLUME_MAP_FNC(in float3 pos); +Medium RAYMARCH_VOLUME_MAP_FNC(in float3 pos); #endif \ No newline at end of file diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index 789dad27..99e94336 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -2,7 +2,7 @@ #include "../common/attenuation.glsl" #include "../../generative/random.glsl" #include "../../math/const.glsl" -#include "../volume/new.glsl" +#include "../medium/new.glsl" /* contributors: Shadi El Hajj @@ -66,7 +66,7 @@ vec3 shadowTransmittance(vec3 position, vec3 rayDirectionL, float stepSizeL) { return vec3(0.0, 0.0, 0.0); } #endif - Volume resL = RAYMARCH_VOLUME_MAP_FNC(positionL); + Medium resL = RAYMARCH_VOLUME_MAP_FNC(positionL); float densityL = -resL.sdf; vec3 extinctionL = resL.absorption + resL.scattering; transmittanceL *= exp(-densityL * extinctionL * stepSizeL); @@ -87,7 +87,7 @@ vec3 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec2 st, float min for(int i = 0; i < RAYMARCH_VOLUME_SAMPLES; i++) { vec3 position = rayOrigin + rayDirection * t; - Volume res = RAYMARCH_VOLUME_MAP_FNC(position); + Medium res = RAYMARCH_VOLUME_MAP_FNC(position); float density = -res.sdf; vec3 extinction = res.absorption + res.scattering; diff --git a/lighting/raymarch/volume.hlsl b/lighting/raymarch/volume.hlsl index f70b81f8..31236501 100644 --- a/lighting/raymarch/volume.hlsl +++ b/lighting/raymarch/volume.hlsl @@ -2,7 +2,7 @@ #include "../common/attenuation.hlsl" #include "../../generative/random.hlsl" #include "../../math/const.hlsl" -#include "../volume/new.hlsl" +#include "../medium/new.hlsl" /* contributors: Shadi El Hajj @@ -66,7 +66,7 @@ float3 shadowTransmittance(float3 position, float3 rayDirectionL, float stepSize return float3(0.0, 0.0, 0.0); } #endif - Volume resL = RAYMARCH_VOLUME_MAP_FNC(positionL); + Medium resL = RAYMARCH_VOLUME_MAP_FNC(positionL); float densityL = -resL.sdf; float3 extinctionL = resL.absorption + resL.scattering; transmittanceL *= exp(-densityL * extinctionL * stepSizeL); @@ -87,7 +87,7 @@ float3 raymarchVolume( in float3 rayOrigin, in float3 rayDirection, float2 st, f for(int i = 0; i < RAYMARCH_VOLUME_SAMPLES; i++) { float3 position = rayOrigin + rayDirection * t; - Volume res = RAYMARCH_VOLUME_MAP_FNC(position); + Medium res = RAYMARCH_VOLUME_MAP_FNC(position); float density = -res.sdf; float3 extinction = res.absorption + res.scattering; diff --git a/lighting/volume/new.glsl b/lighting/volume/new.glsl deleted file mode 100644 index 9390a0b5..00000000 --- a/lighting/volume/new.glsl +++ /dev/null @@ -1,41 +0,0 @@ -#include "../volume.glsl" - -/* -contributors: Shadi El Hajj -description: | - Volume Constructor. -use: - - void volumeNew(out _mat) - - volumeNew() -license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj -*/ - -#ifndef FNC_VOLUME_NEW -#define FNC_VOLUME_NEW - -void volumeNew(out Volume _mat) { - _mat.scattering = vec3(1.0, 1.0, 1.0); - _mat.absorption = vec3(1.0, 1.0, 1.0); - _mat.sdf = RAYMARCH_MAX_DIST; - -} - -Volume volumeNew() { - Volume mat; - volumeNew(mat); - return mat; -} - -Volume volumeNew(vec3 scattering, vec3 absorption, float sdf) { - Volume mat = volumeNew(); - mat.scattering = scattering; - mat.absorption = absorption; - mat.sdf = sdf; - return mat; -} - -Volume volumeNew(vec3 scattering, float sdf) { - return volumeNew(scattering, vec3(0.0, 0.0, 0.0), sdf); -} - -#endif diff --git a/lighting/volume/new.hlsl b/lighting/volume/new.hlsl deleted file mode 100644 index 91ed5f82..00000000 --- a/lighting/volume/new.hlsl +++ /dev/null @@ -1,41 +0,0 @@ -#include "../volume.hlsl" - -/* -contributors: Shadi El Hajj -description: | - Volume Constructor. -use: - - void volumeNew(out _mat) - - volumeNew() -license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj -*/ - -#ifndef FNC_VOLUME_NEW -#define FNC_VOLUME_NEW - -void volumeNew(out Volume _mat) { - _mat.scattering = float3(1.0, 1.0, 1.0); - _mat.absorption = float3(1.0, 1.0, 1.0); - _mat.sdf = RAYMARCH_MAX_DIST; - -} - -Volume volumeNew() { - Volume mat; - volumeNew(mat); - return mat; -} - -Volume volumeNew(float3 scattering, float3 absorption, float sdf) { - Volume mat = volumeNew(); - mat.scattering = scattering; - mat.absorption = absorption; - mat.sdf = sdf; - return mat; -} - -Volume volumeNew(float3 scattering, float sdf) { - return volumeNew(scattering, float3(0.0, 0.0, 0.0), sdf); -} - -#endif From 67b3369e2ad316afc3a871219bb454747efb6dd0 Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Tue, 20 Aug 2024 22:01:23 +0100 Subject: [PATCH 33/38] Updated doc. --- lighting/raymarch/volume.glsl | 9 +++++---- lighting/raymarch/volume.hlsl | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index 99e94336..006f09c0 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -7,11 +7,12 @@ /* contributors: Shadi El Hajj description: Default raymarching renderer. Based on Sébastien Hillaire's paper "Physically Based Sky, Atmosphere & Cloud Rendering in Frostbite" -use: raymarchVolume( in rayOrigin, in rayDirection, in cameraForward, st, float minDist ) +use: raymarchVolume( rayOrigin, rayDirection, st, float minDist, background) options: - - RAYMARCH_VOLUME_SAMPLES 256 - - RAYMARCH_VOLUME_SAMPLES_LIGHT 8 + - RAYMARCH_VOLUME_SAMPLES 64 + - RAYMARCH_VOLUME_SAMPLES_LIGHT 32 - RAYMARCH_VOLUME_MAP_FNC raymarchVolumeMap + - RAYMARCH_VOLUMETRIC_SHADOWS - RAYMARCH_VOLUME_DITHER 0.1 - RAYMARCH_ENERGY_CONSERVING - LIGHT_COLOR vec3(0.5, 0.5, 0.5) @@ -78,7 +79,7 @@ vec3 shadowTransmittance(vec3 position, vec3 rayDirectionL, float stepSizeL) { return transmittanceL; } -vec3 raymarchVolume( in vec3 rayOrigin, in vec3 rayDirection, vec2 st, float minDist, vec3 background) { +vec3 raymarchVolume(vec3 rayOrigin, vec3 rayDirection, vec2 st, float minDist, vec3 background) { vec3 scatteredLuminance = vec3(0.0, 0.0, 0.0); vec3 transmittance = vec3(1.0, 1.0, 1.0); float stepSize = RAYMARCH_MAX_DIST/float(RAYMARCH_VOLUME_SAMPLES); diff --git a/lighting/raymarch/volume.hlsl b/lighting/raymarch/volume.hlsl index 31236501..e54acf56 100644 --- a/lighting/raymarch/volume.hlsl +++ b/lighting/raymarch/volume.hlsl @@ -7,11 +7,12 @@ /* contributors: Shadi El Hajj description: Default raymarching renderer. Based on Sébastien Hillaire's paper "Physically Based Sky, Atmosphere & Cloud Rendering in Frostbite" -use: raymarchVolume( in rayOrigin, in rayDirection, in cameraForward, st, float minDist ) +use: raymarchVolume( rayOrigin, rayDirection, st, minDist, background) options: - - RAYMARCH_VOLUME_SAMPLES 256 - - RAYMARCH_VOLUME_SAMPLES_LIGHT 8 + - RAYMARCH_VOLUME_SAMPLES 64 + - RAYMARCH_VOLUME_SAMPLES_LIGHT 32 - RAYMARCH_VOLUME_MAP_FNC raymarchVolumeMap + - RAYMARCH_VOLUMETRIC_SHADOWS - RAYMARCH_VOLUME_DITHER 0.1 - RAYMARCH_ENERGY_CONSERVING - LIGHT_COLOR float3(0.5, 0.5, 0.5) @@ -78,7 +79,7 @@ float3 shadowTransmittance(float3 position, float3 rayDirectionL, float stepSize return transmittanceL; } -float3 raymarchVolume( in float3 rayOrigin, in float3 rayDirection, float2 st, float minDist, float3 background) { +float3 raymarchVolume(in float3 rayOrigin, in float3 rayDirection, float2 st, float minDist, float3 background) { float3 scatteredLuminance = float3(0.0, 0.0, 0.0); float3 transmittance = float3(1.0, 1.0, 1.0); float stepSize = RAYMARCH_MAX_DIST/float(RAYMARCH_VOLUME_SAMPLES); From 5aa064f4faac1780b3268f7e3c7e1a0faca76653 Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Thu, 22 Aug 2024 14:44:30 +0100 Subject: [PATCH 34/38] Fixed single-sample raymarching. --- lighting/raymarch.glsl | 5 ++--- lighting/raymarch.hlsl | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/lighting/raymarch.glsl b/lighting/raymarch.glsl index a465654c..43ae19f0 100644 --- a/lighting/raymarch.glsl +++ b/lighting/raymarch.glsl @@ -97,13 +97,12 @@ vec4 raymarch(mat4 viewMatrix, vec2 st, out float eyeDepth, out Material mat) { vec3 rayDirection = viewMatrix3 * normalize(vec3(st*2.0-1.0, fov)); float dist = 0.0; - float eyeDepth = 0.0; vec4 opaque = RAYMARCH_RENDER_FNC( camera, rayDirection, cameraForward, dist, eyeDepth, mat); #ifdef RAYMARCH_VOLUME - color = vec4(RAYMARCH_VOLUME_RENDER_FNC(camera, rayDirection, st, dist, opaque.rgb), opaque.a); + vec4 color = vec4(RAYMARCH_VOLUME_RENDER_FNC(camera, rayDirection, st, dist, opaque.rgb), opaque.a); #else - color = opaque; + vec4 color = opaque; #endif return color; diff --git a/lighting/raymarch.hlsl b/lighting/raymarch.hlsl index f53d9945..d348d4d8 100644 --- a/lighting/raymarch.hlsl +++ b/lighting/raymarch.hlsl @@ -98,13 +98,12 @@ float4 raymarch(float4x4 viewMatrix, float2 st, out float eyeDepth, out Material float3 rayDirection = mul((float3x3)viewMatrix, normalize(float3(st * 2.0 - 1.0, fov))); float dist = 0.0; - float eyeDepth = 0.0; float4 opaque = RAYMARCH_RENDER_FNC(camera, rayDirection, cameraForward, dist, eyeDepth, mat); #ifdef RAYMARCH_VOLUME - color = float4(RAYMARCH_VOLUME_RENDER_FNC(camera, rayDirection, st, dist, opaque.rgb), opaque.a); + float4 color = float4(RAYMARCH_VOLUME_RENDER_FNC(camera, rayDirection, st, dist, opaque.rgb), opaque.a); #else - color = opaque; + float4 color = opaque; #endif return color; From e473c07ba6f7e379324cf0d954698ba1595acf3a Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Thu, 22 Aug 2024 16:34:01 +0100 Subject: [PATCH 35/38] Moved attenuation to /light --- lighting/{common => light}/attenuation.glsl | 0 lighting/{common => light}/attenuation.hlsl | 0 lighting/raymarch/volume.glsl | 2 +- lighting/raymarch/volume.hlsl | 2 +- space/lookAtView.hlsl | 1 - 5 files changed, 2 insertions(+), 3 deletions(-) rename lighting/{common => light}/attenuation.glsl (100%) rename lighting/{common => light}/attenuation.hlsl (100%) diff --git a/lighting/common/attenuation.glsl b/lighting/light/attenuation.glsl similarity index 100% rename from lighting/common/attenuation.glsl rename to lighting/light/attenuation.glsl diff --git a/lighting/common/attenuation.hlsl b/lighting/light/attenuation.hlsl similarity index 100% rename from lighting/common/attenuation.hlsl rename to lighting/light/attenuation.hlsl diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index 006f09c0..b9519ad9 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -1,5 +1,5 @@ #include "map.glsl" -#include "../common/attenuation.glsl" +#include "../light/attenuation.glsl" #include "../../generative/random.glsl" #include "../../math/const.glsl" #include "../medium/new.glsl" diff --git a/lighting/raymarch/volume.hlsl b/lighting/raymarch/volume.hlsl index e54acf56..ce9caa33 100644 --- a/lighting/raymarch/volume.hlsl +++ b/lighting/raymarch/volume.hlsl @@ -1,5 +1,5 @@ #include "map.hlsl" -#include "../common/attenuation.hlsl" +#include "../light/attenuation.hlsl" #include "../../generative/random.hlsl" #include "../../math/const.hlsl" #include "../medium/new.hlsl" diff --git a/space/lookAtView.hlsl b/space/lookAtView.hlsl index eb3aae92..eb8fcf74 100644 --- a/space/lookAtView.hlsl +++ b/space/lookAtView.hlsl @@ -3,7 +3,6 @@ contributors: Shadi El Hajj description: Create a look-at view matrix use: lookAtView(in position, in target, in up) license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj -license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ #include "lookAt.hlsl" From 6fa92e631f3cb9bac0fe7100ad28c43b64912ae3 Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Fri, 23 Aug 2024 15:19:58 +0100 Subject: [PATCH 36/38] Missing decimals for WebGL. --- lighting/light/attenuation.glsl | 8 ++++---- lighting/light/attenuation.hlsl | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lighting/light/attenuation.glsl b/lighting/light/attenuation.glsl index 8b337a10..03d56f31 100644 --- a/lighting/light/attenuation.glsl +++ b/lighting/light/attenuation.glsl @@ -5,19 +5,19 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ #ifndef LIGHT_ATTENUATION_CONSTANT -#define LIGHT_ATTENUATION_CONSTANT 0 +#define LIGHT_ATTENUATION_CONSTANT 0.0 #endif #ifndef LIGHT_ATTENUATION_LINEAR -#define LIGHT_ATTENUATION_LINEAR 0 +#define LIGHT_ATTENUATION_LINEAR 0.0 #endif #ifndef LIGHT_ATTENUATION_EXPONENTIAL -#define LIGHT_ATTENUATION_EXPONENTIAL 1 +#define LIGHT_ATTENUATION_EXPONENTIAL 1.0 #endif #ifndef LIGHT_ATTENUATION_EXPONENT -#define LIGHT_ATTENUATION_EXPONENT 2 +#define LIGHT_ATTENUATION_EXPONENT 2.0 #endif #ifndef FNC_LIGHT_ATTENUATION diff --git a/lighting/light/attenuation.hlsl b/lighting/light/attenuation.hlsl index 8b337a10..03d56f31 100644 --- a/lighting/light/attenuation.hlsl +++ b/lighting/light/attenuation.hlsl @@ -5,19 +5,19 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj */ #ifndef LIGHT_ATTENUATION_CONSTANT -#define LIGHT_ATTENUATION_CONSTANT 0 +#define LIGHT_ATTENUATION_CONSTANT 0.0 #endif #ifndef LIGHT_ATTENUATION_LINEAR -#define LIGHT_ATTENUATION_LINEAR 0 +#define LIGHT_ATTENUATION_LINEAR 0.0 #endif #ifndef LIGHT_ATTENUATION_EXPONENTIAL -#define LIGHT_ATTENUATION_EXPONENTIAL 1 +#define LIGHT_ATTENUATION_EXPONENTIAL 1.0 #endif #ifndef LIGHT_ATTENUATION_EXPONENT -#define LIGHT_ATTENUATION_EXPONENT 2 +#define LIGHT_ATTENUATION_EXPONENT 2.0 #endif #ifndef FNC_LIGHT_ATTENUATION From 60c64b88eb1c6a751cedef4e43a6c4d787210112 Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Fri, 23 Aug 2024 16:37:43 +0100 Subject: [PATCH 37/38] Added RAYMARCH_VOLUME guard. --- lighting/raymarch/volume.glsl | 6 +++--- lighting/raymarch/volume.hlsl | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index b9519ad9..ff7fb37d 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -52,8 +52,8 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj #define RAYMARCH_VOLUME_DITHER 0.1 #endif -#ifndef FNC_RAYMARCH_VOLUMERENDER -#define FNC_RAYMARCH_VOLUMERENDER +#if !defined(FNC_RAYMARCH_VOLUME) && defined(RAYMARCH_VOLUME) +#define FNC_RAYMARCH_VOLUME vec3 shadowTransmittance(vec3 position, vec3 rayDirectionL, float stepSizeL) { vec3 transmittanceL = vec3(1.0, 1.0, 1.0); @@ -113,7 +113,7 @@ vec3 raymarchVolume(vec3 rayOrigin, vec3 rayDirection, vec2 st, float minDist, v const vec3 L = vec3(1.0, 1.0, 1.0); #endif - #if defined RAYMARCH_ENERGY_CONSERVING + #if defined(RAYMARCH_ENERGY_CONSERVING) // energy-conserving scattering integration vec3 S = L * attenuationL * shadow * density * res.scattering; vec3 sampleExtinction = max(vec3(EPSILON, EPSILON, EPSILON), density * extinction); diff --git a/lighting/raymarch/volume.hlsl b/lighting/raymarch/volume.hlsl index ce9caa33..8c57949f 100644 --- a/lighting/raymarch/volume.hlsl +++ b/lighting/raymarch/volume.hlsl @@ -52,8 +52,8 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj #define RAYMARCH_VOLUME_DITHER 0.1 #endif -#ifndef FNC_RAYMARCH_VOLUMERENDER -#define FNC_RAYMARCH_VOLUMERENDER +#if !defined(FNC_RAYMARCH_VOLUME) && defined(RAYMARCH_VOLUME) +#define FNC_RAYMARCH_VOLUME float3 shadowTransmittance(float3 position, float3 rayDirectionL, float stepSizeL) { float3 transmittanceL = float3(1.0, 1.0, 1.0); @@ -113,7 +113,7 @@ float3 raymarchVolume(in float3 rayOrigin, in float3 rayDirection, float2 st, fl const float3 L = float3(1.0, 1.0, 1.0); #endif - #if defined RAYMARCH_ENERGY_CONSERVING + #if defined(RAYMARCH_ENERGY_CONSERVING) // energy-conserving scattering integration float3 S = L * attenuationL * shadow * density * res.scattering; float3 sampleExtinction = max(float3(EPSILON, EPSILON, EPSILON), density * extinction); From 0992d6299641a36da3f53f7628851c298790aae7 Mon Sep 17 00:00:00 2001 From: shadielhajj Date: Fri, 23 Aug 2024 16:53:39 +0100 Subject: [PATCH 38/38] Renamed private functions. --- lighting/raymarch/volume.glsl | 4 ++-- lighting/raymarch/volume.hlsl | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lighting/raymarch/volume.glsl b/lighting/raymarch/volume.glsl index ff7fb37d..a9953955 100644 --- a/lighting/raymarch/volume.glsl +++ b/lighting/raymarch/volume.glsl @@ -55,7 +55,7 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj #if !defined(FNC_RAYMARCH_VOLUME) && defined(RAYMARCH_VOLUME) #define FNC_RAYMARCH_VOLUME -vec3 shadowTransmittance(vec3 position, vec3 rayDirectionL, float stepSizeL) { +vec3 raymarchVolumeShadowTransmittance(vec3 position, vec3 rayDirectionL, float stepSizeL) { vec3 transmittanceL = vec3(1.0, 1.0, 1.0); float tL = 0.0; @@ -105,7 +105,7 @@ vec3 raymarchVolume(vec3 rayOrigin, vec3 rayDirection, vec2 st, float minDist, v vec3 rayDirectionL = normalize(LIGHT_POSITION - position); float attenuationL = attenuation(distL); #endif - vec3 shadow = shadowTransmittance(position, rayDirectionL, stepSizeL); + vec3 shadow = raymarchVolumeShadowTransmittance(position, rayDirectionL, stepSizeL); vec3 L = LIGHT_COLOR * LIGHT_INTENSITY; #else // no lighting const float attenuationL = 1.0; diff --git a/lighting/raymarch/volume.hlsl b/lighting/raymarch/volume.hlsl index 8c57949f..e49b361c 100644 --- a/lighting/raymarch/volume.hlsl +++ b/lighting/raymarch/volume.hlsl @@ -55,7 +55,7 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj #if !defined(FNC_RAYMARCH_VOLUME) && defined(RAYMARCH_VOLUME) #define FNC_RAYMARCH_VOLUME -float3 shadowTransmittance(float3 position, float3 rayDirectionL, float stepSizeL) { +float3 raymarchVolumeShadowTransmittance(float3 position, float3 rayDirectionL, float stepSizeL) { float3 transmittanceL = float3(1.0, 1.0, 1.0); float tL = 0.0; @@ -105,7 +105,7 @@ float3 raymarchVolume(in float3 rayOrigin, in float3 rayDirection, float2 st, fl float3 rayDirectionL = normalize(LIGHT_POSITION - position); float attenuationL = attenuation(distL); #endif - float3 shadow = shadowTransmittance(position, rayDirectionL, stepSizeL); + float3 shadow = raymarchVolumeShadowTransmittance(position, rayDirectionL, stepSizeL); float3 L = LIGHT_COLOR * LIGHT_INTENSITY; #else // no lighting const float attenuationL = 1.0;