Skip to content

Commit

Permalink
Merge pull request #231 from patriciogonzalezvivo/patriciogv/fix4threeJs
Browse files Browse the repository at this point in the history
  • Loading branch information
patriciogonzalezvivo authored Nov 18, 2024
2 parents 098a861 + e16b4e7 commit 14992e1
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 50 deletions.
4 changes: 3 additions & 1 deletion color/luminance.glsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/*
contributor: nan
description: Computes the luminance of the specified linear RGB color using the luminance coefficients from Rec. 709.
description: |
Computes the luminance of the specified linear RGB color using the luminance coefficients from Rec. 709.
Note, ThreeJS seems to inject this in all their shaders. Which could lead to issues
use: luminance(<vec3|vec4> color)
license:
- Copyright (c) 2021 Patricio Gonzalez Vivo under Prosperity License - https://prosperitylicense.com/versions/3.0.0
Expand Down
5 changes: 2 additions & 3 deletions color/tonemap/debug.glsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include "../luminance.glsl"

/*
contributors: nan
description: |
Expand Down Expand Up @@ -58,7 +56,8 @@ vec3 tonemapDebug(const vec3 x) {

// The 5th color in the array (cyan) represents middle gray (18%)
// Every stop above or below middle gray causes a color shift
float v = log2(luminance(x) / 0.18);
float l = dot(x, vec3(0.21250175, 0.71537574, 0.07212251));
float v = log2(l / 0.18);
v = clamp(v + 5.0, 0.0, 15.0);
int index = int(v);
return mix(debugColors[index], debugColors[index + 1], v - float(index));
Expand Down
4 changes: 0 additions & 4 deletions color/tonemap/filmic.glsl
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@


#include "../luminance.glsl"

/*
contributors: [Jim Hejl, Richard Burgess-Dawson ]
description: Haarm-Peter Duikers curve from John Hables presentation "Uncharted 2 HDR Lighting", Page 140, http://www.gdcvault.com/play/1012459/Uncharted_2__HDR_Lighting
Expand Down
4 changes: 1 addition & 3 deletions color/tonemap/reinhard.glsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include "../luminance.glsl"

/*
contributors: [Erik Reinhard, Michael Stark, Peter Shirley, James Ferwerda]
description: Photographic Tone Reproduction for Digital Images. http://www.cmap.polytechnique.fr/~peyre/cours/x2005signal/hdr_photographic.pdf
Expand All @@ -8,6 +6,6 @@ use: <vec3|vec4> tonemapReinhard(<vec3|vec4> x)

#ifndef FNC_TONEMAPREINHARD
#define FNC_TONEMAPREINHARD
vec3 tonemapReinhard(const vec3 v) { return v / (1.0 + luminance(v)); }
vec3 tonemapReinhard(const vec3 v) { return v / (1.0 + dot(v, vec3(0.21250175, 0.71537574, 0.07212251))); }
vec4 tonemapReinhard(const vec4 v) { return vec4( tonemapReinhard(v.rgb), v.a ); }
#endif
5 changes: 1 addition & 4 deletions color/tonemap/reinhardJodie.glsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@

#include "../luminance.glsl"

/*
contributors: [Erik Reinhard, Michael Stark, Peter Shirley, James Ferwerda]
description: Photographic Tone Reproduction for Digital Images. http://www.cmap.polytechnique.fr/~peyre/cours/x2005signal/hdr_photographic.pdf
Expand All @@ -10,7 +7,7 @@ use: <vec3|vec4> tonemapReinhardJodie(<vec3|vec4> x)
#ifndef FNC_TONEMAPREINHARDJODIE
#define FNC_TONEMAPREINHARDJODIE
vec3 tonemapReinhardJodie(const vec3 x) {
float l = luminance(x);
float l = dot(x, vec3(0.21250175, 0.71537574, 0.07212251));
vec3 tc = x / (x + 1.0);
return mix(x / (l + 1.0), tc, tc);
}
Expand Down
2 changes: 1 addition & 1 deletion lighting/envMap.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ license:
#endif
#endif

#if __VERSION__ < 430
#if !defined(ENVMAP_MAX_MIP_LEVEL) && __VERSION__ < 430
#define ENVMAP_MAX_MIP_LEVEL 3.0
#endif

Expand Down
18 changes: 11 additions & 7 deletions lighting/light/iblEvaluate.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void lightIBLEvaluate(Material mat, inout ShadingData shadingData) {
vec3 specularColorE = shadingData.specularColor * E.x + E.y;
#endif

vec3 energyCompensation = vec3(1.0, 1.0, 1.0);
vec3 energyCompensation = vec3(1.0, 1.0, 1.0);

#if defined(IBL_IMPORTANCE_SAMPLING) && __VERSION__ >= 130
vec3 Fr = specularImportanceSampling(shadingData.linearRoughness, shadingData.specularColor,
Expand All @@ -44,17 +44,21 @@ vec3 energyCompensation = vec3(1.0, 1.0, 1.0);
Fr += fresnelReflection(mat, shadingData);
#endif

vec3 Fd = shadingData.diffuseColor;
#if defined(SCENE_SH_ARRAY)
vec3 Fd = shadingData.diffuseColor * (1.0-specularColorE);
Fd *= tonemap( sphericalHarmonics(shadingData.N) );
#elif defined(IBL_IMPORTANCE_SAMPLING)
vec3 Fd = shadingData.diffuseColor;
Fd *= envMap(shadingData.N, 1.0);
#ifdef GLSLVIEWER
Fd *= tonemap(sphericalHarmonics(shadingData.N));
#else
Fd *= (sphericalHarmonics(shadingData.N));
#endif
#else
vec3 Fd = shadingData.diffuseColor * (1.0-specularColorE);
Fd *= envMap(shadingData.N, 1.0);
#endif

#if !defined(IBL_IMPORTANCE_SAMPLING)
Fd *= (1.0-specularColorE);
#endif

// AO
float diffuseAO = mat.ambientOcclusion;
Fd *= diffuseAO;
Expand Down
6 changes: 3 additions & 3 deletions lighting/pbrLittle.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ vec4 pbrLittle(Material mat, ShadingData shadingData) {
vec3 ambientSpecular = tonemap( envMap(mat, shadingData) ) * specIntensity;
ambientSpecular += fresnelReflection(mat, shadingData) * (1.0-mat.roughness);

albedo = albedo.rgb * notMetal + ( ambientSpecular
+ LIGHT_COLOR * 2.0 * spec
) * (notMetal * smoothness + albedo * mat.metallic);
albedo = albedo.rgb * notMetal + ( ambientSpecular
+ LIGHT_COLOR * 2.0 * spec
) * (notMetal * smoothness + albedo * mat.metallic);

return vec4(albedo, mat.albedo.a);
}
Expand Down
5 changes: 2 additions & 3 deletions lighting/specular/importanceSampling.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@ license: MIT License (MIT) Copyright (c) 2024 Shadi EL Hajj
#define IBL_IMPORTANCE_SAMPLING_SAMPLES 16
#endif

#if !defined(FNC_SPECULAR_IMPORTANCE_SAMPLING) && __VERSION__ >= 130
#if !defined(FNC_SPECULAR_IMPORTANCE_SAMPLING) && defined(SCENE_CUBEMAP) && __VERSION__ >= 130
#define FNC_SPECULAR_IMPORTANCE_SAMPLING


vec3 specularImportanceSampling(float roughness, vec3 f0, vec3 p, vec3 n, vec3 v, vec3 r, float NoV, out vec3 energyCompensation) {
const int numSamples = IBL_IMPORTANCE_SAMPLING_SAMPLES;
const float invNumSamples = 1.0 / float(IBL_IMPORTANCE_SAMPLING_SAMPLES);
const vec3 up = vec3(0.0, 0.0, 1.0);
mat3 T = tbn(n, up);
// T *= rotate3dZ(TWO_PI * random(p));

int width = textureSize(SCENE_CUBEMAP, 0).x;
float width = float(textureSize(SCENE_CUBEMAP, 0).x);
float omegaP = (4.0 * PI) / (6.0 * width * width);

vec3 indirectSpecular = vec3(0.0, 0.0, 0.0);
Expand Down
2 changes: 1 addition & 1 deletion math/hammersley.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ vec2 hammersley(uint index, int numSamples) {
bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
return vec2(float(index) / numSamples, float(bits) * tof);
return vec2(float(index) / float(numSamples), float(bits) * tof);
}

vec3 hemisphereCosSample(vec2 u) {
Expand Down
14 changes: 8 additions & 6 deletions space/lookAt.glsl
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/*
contributors: Patricio Gonzalez Vivo
description: create a look at matrix
description: create a look at matrix. Right handed by default.
use:
- <mat3> lookAt(<vec3> forward, <vec3> up)
- <mat3> lookAt(<vec3> eye, <vec3> target, <vec3> up)
- <mat3> lookAt(<vec3> eye, <vec3> target, <float> roll)
- <mat3> lookAt(<vec3> forward)
options:
- LOOK_AT_RIGHT_HANDED: assume right-handed coordinate system. Default is Left-handed.
- LOOK_AT_LEFT_HANDED: assume a left-handed coordinate system
- LOOK_AT_RIGHT_HANDED: assume a right-handed coordinate system
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
Expand All @@ -16,13 +18,13 @@ license:
#define FNC_LOOKAT

mat3 lookAt(vec3 forward, vec3 up) {
vec3 zaxis = forward;
#if defined (LOOK_AT_RIGHT_HANDED)
vec3 zaxis = normalize(forward);
#if defined(LOOK_AT_RIGHT_HANDED)
vec3 xaxis = normalize(cross(zaxis, up));
vec3 yaxis = normalize(cross(xaxis, zaxis));
vec3 yaxis = cross(xaxis, zaxis);
#else
vec3 xaxis = normalize(cross(up, zaxis));
vec3 yaxis = normalize(cross(zaxis, xaxis));
vec3 yaxis = cross(zaxis, xaxis);
#endif
return mat3(xaxis, yaxis, zaxis);
}
Expand Down
22 changes: 11 additions & 11 deletions space/lookAt.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ use:
- <float3x3> lookAt(<float3> forward, <float3> up)
- <float3x3> lookAt(<float3> target, <float3> eye, <float3> up)
- <float3x3> lookAt(<float3> target, <float3> eye, <float> roll)
- <float3x3> lookAt(<float3> forward)
options:
- LOOK_AT_LEFT_HANDED: assume a left-handed coordinate system
- LOOK_AT_RIGHT_HANDED: assume a right-handed coordinate system
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
Expand All @@ -13,15 +17,14 @@ license:
#ifndef FNC_LOOKAT
#define FNC_LOOKAT

float3x3 lookAt(float3 forward, float3 up)
{
float3 zaxis = forward;
float3x3 lookAt(float3 forward, float3 up) {
float3 zaxis = normalize(forward);
#if defined (LOOK_AT_RIGHT_HANDED)
float3 xaxis = normalize(cross(zaxis, up));
float3 yaxis = normalize(cross(xaxis, zaxis));
float3 yaxis = cross(xaxis, zaxis);
#else
float3 xaxis = normalize(cross(up, zaxis));
float3 yaxis = normalize(cross(zaxis, xaxis));
float3 yaxis = cross(zaxis, xaxis);
#endif
float3x3 m;
m._m00_m10_m20 = xaxis;
Expand All @@ -30,20 +33,17 @@ float3x3 lookAt(float3 forward, float3 up)
return m;
}

float3x3 lookAt(float3 eye, float3 target, float3 up)
{
float3x3 lookAt(float3 eye, float3 target, float3 up) {
float3 forward = normalize(target - eye);
return lookAt(forward, up);
}

float3x3 lookAt(float3 eye, float3 target, float roll)
{
float3x3 lookAt(float3 eye, float3 target, float roll) {
float3 up = float3(sin(roll), cos(roll), 0.0);
return lookAt(eye, target, up);
}

float3x3 lookAt(float3 forward)
{
float3x3 lookAt(float3 forward) {
return lookAt(forward, float3(0.0, 1.0, 0.0));
}

Expand Down
15 changes: 12 additions & 3 deletions space/lookAt.msl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ use:
- <mat3> lookAt(<float3> forward, <float3> up)
- <mat3> lookAt(<float3> eye, <float3> target, <float3> up)
- <mat3> lookAt(<float3> eye, <float3> target, <float> rolle)
- <mat3> lookAt(<float3> forward)
options:
- LOOK_AT_LEFT_HANDED: assume a left-handed coordinate system
- LOOK_AT_RIGHT_HANDED: assume a right-handed coordinate system
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
Expand All @@ -14,9 +18,14 @@ license:
#define FNC_LOOKAT

mat3 lookAt(float3 forward, float3 up) {
float3 xaxis = normalize(cross(forward, up));
float3 yaxis = up;
float3 zaxis = forward;
float3 zaxis = normalize(forward);
#if defined(LOOK_AT_RIGHT_HANDED)
float3 xaxis = normalize(cross(zaxis, up));
float3 yaxis = cross(xaxis, zaxis);
#else
float3 xaxis = normalize(cross(up, zaxis));
float3 yaxis = cross(zaxis, xaxis);
#endif
return mat3(xaxis, yaxis, zaxis);
}

Expand Down

0 comments on commit 14992e1

Please sign in to comment.