Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image Light Feature - GSOC 2023 #6255

Merged
merged 56 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
50a0c14
Added functions for diffused image light
Jul 6, 2023
dfa600c
Added functions for diffused image light
Jul 6, 2023
0521694
Merge branch 'main' into imageLight
Jul 10, 2023
b155bac
Merge branch 'main' into imageLight
Jul 13, 2023
34e6438
Added shaders and code to read them
Jul 13, 2023
916f5be
Merge branch 'main' into imageLight
Jul 20, 2023
c59ae6c
refactoring the ImageLight fragment shader
Jul 20, 2023
8b1e19c
added definition of map function in lightingglsl
Jul 21, 2023
5287a4f
removed errors
Jul 21, 2023
b05bca6
error correction
Jul 25, 2023
f6899a8
Merge branch 'main' into imageLight
Jul 25, 2023
8cd5775
corrected errors
Jul 25, 2023
3c9bf12
added comments
Aug 4, 2023
701cbee
added comments
Aug 4, 2023
4c63de5
Merge branch 'main' into imageLight
AryanKoundal Aug 5, 2023
b9837dc
made the changes from the build file test
AryanKoundal Aug 5, 2023
5546093
create a new map for storing the images and modifying the files where…
AryanKoundal Aug 6, 2023
98af50f
modifications according to the reviews
AryanKoundal Aug 8, 2023
8dbbff5
Merge branch 'main' into imageLight
AryanKoundal Aug 8, 2023
711c5f5
applied all the changes after debugging
AryanKoundal Aug 8, 2023
52c4d42
Merge branch 'main' into imageLight
AryanKoundal Aug 11, 2023
0121439
made changes according to the reviews
AryanKoundal Aug 11, 2023
8e9cd8a
Merge branch 'main' into imageLight
AryanKoundal Sep 8, 2023
7cf062d
Convert existing shaders to support GLES 300
davepagurek Sep 24, 2023
93a2428
Merge branch 'main' into imageLight
AryanKoundal Oct 10, 2023
d1c19b3
Merge branch 'imageLight' into feat/mipmap
davepagurek Oct 10, 2023
b803601
getting on track with main
AryanKoundal Oct 13, 2023
dc0ef6a
Merge branch 'main' into imageLight
AryanKoundal Oct 13, 2023
f0f3a19
Merge pull request #1 from davepagurek/feat/mipmap
AryanKoundal Oct 13, 2023
02d6f00
added code for specular light
AryanKoundal Oct 14, 2023
9a0c3fc
Merge branch 'main' into imageLight
AryanKoundal Oct 14, 2023
0860bd7
Fix tests
davepagurek Oct 15, 2023
0ade514
modifications to rendererGl and lighting.glsl
AryanKoundal Oct 18, 2023
07371e8
Merge branch 'main' into imageLight
AryanKoundal Oct 18, 2023
25a711b
Merge pull request #2 from davepagurek/feat/mipmap
AryanKoundal Oct 18, 2023
c04d916
additions
AryanKoundal Oct 23, 2023
9dbe8a0
Merge branch 'main' into imageLight
AryanKoundal Oct 23, 2023
5cac577
Fall back to regular texture on WebGL1
davepagurek Oct 23, 2023
5547d7d
resolved errors
AryanKoundal Oct 26, 2023
ff3973d
Merge branch 'main' into imageLight
AryanKoundal Oct 26, 2023
b37afe5
Updated the outcolor
AryanKoundal Oct 26, 2023
69008c0
added comments
AryanKoundal Oct 26, 2023
d713b35
resolved pow error and added documentation
AryanKoundal Oct 26, 2023
6656e90
added documentation and example
AryanKoundal Oct 27, 2023
374051b
resolved imageLight documentation example error
AryanKoundal Oct 27, 2023
b964403
modified timeout to 25sec for imageLight examples to work
AryanKoundal Oct 27, 2023
0e7c3bc
modified timeout to 25sec for imageLight examples to work
AryanKoundal Oct 27, 2023
14aed59
changed settimeout back to original 4sec limit and changed the image …
AryanKoundal Oct 27, 2023
5d99712
Merge branch 'main' into imageLight
AryanKoundal Oct 27, 2023
4fe2890
slight modifications on the suggestions
AryanKoundal Oct 28, 2023
6fec78a
resolved suggestion
AryanKoundal Oct 30, 2023
94e639b
used WorldNormalInstead of R in lighting.glgl
AryanKoundal Oct 31, 2023
f2c0f1e
changed angles in lighting.glsl
AryanKoundal Oct 31, 2023
e68f9f0
added images in examples and removed unnecessary code
AryanKoundal Oct 31, 2023
6df91ec
Merge branch 'main' into imageLight
AryanKoundal Nov 3, 2023
fe5e0db
replaced asset with smaller size
AryanKoundal Nov 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/webgl/p5.RendererGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ const defaultShaders = {
lineFrag:
lineDefs + readFileSync(join(__dirname, '/shaders/line.frag'), 'utf-8'),
pointVert: readFileSync(join(__dirname, '/shaders/point.vert'), 'utf-8'),
pointFrag: readFileSync(join(__dirname, '/shaders/point.frag'), 'utf-8')
pointFrag: readFileSync(join(__dirname, '/shaders/point.frag'), 'utf-8'),
imageLightVert : readFileSync(join(__dirname, '/shaders/imageLight.vert'), 'utf-8'),
imageLightFrag : readFileSync(join(__dirname, '/shaders/imageLight.frag'), 'utf-8')
};

/**
Expand Down
113 changes: 113 additions & 0 deletions src/webgl/shaders/imageLight.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
precision mediump float;
varying vec3 localPos;

// the HDR cubemap converted (can be from an equirectangular environment map.)
uniform sampler2D environmentMap;
varying vec2 vTexCoord;

const float PI = 3.141;

vec2 normalToEquirectangular( vec3 v){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we use this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, I actually re-wrote the function in line 22. The function N2E is the one being used for now. I think I should remove the function on line 10 and rename the function at line 22.

vec2 uv;
// taking the arctangent of the v.z and v.x components and dividing it by the circumference of a circle
// Adding 0.5 ensures that the resulting value is in the range [0, 1].
uv.x = atan(v.z, v.x) / 3.14159 + 0.5;
// taking the arcsine of the v.y component and dividing it by the half circumference of a circle
uv.y = asin(v.y) / 3.14159 + 0.5;


return uv;
}

vec2 nTOE( vec3 v ){
// x = r sin(phi) cos(theta)
// y = r cos(phi)
// z = r sin(phi) sin(theta)
float phi = acos( v.y );
// if phi is 0, then there are no x, z components
float theta = 0.0;
// else
theta = acos(v.x / sin(phi));
float sinTheta = v.z / sin(phi);
if (sinTheta < 0.0) {
// Turn it into -theta, but in the 0-2PI range
theta = 2.0 * PI - theta;
}
theta = theta / (2.0 * 3.14159);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe where we use 3.14159 right now we can use the const float PI we defined earlier, and add more decimals to that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll clean up this file. It has some extra code as well.

phi = phi / 3.14159 ;

vec2 angles = vec2( phi, theta );
return angles;
}

// vec2 func( vec3 localPos ){
// // x = r sin(phi) cos(theta)
// // y = r cos(phi)
// // z = r sin(phi) sin(theta)
// float phi = acos( localPos.y );
// // if phi is 0, then there are no x, z components
// float theta = 0.0;
// // else
// theta = acos(normal.x / sin(phi));
// float sinTheta = normal.z / sin(phi);
// if (sinTheta < 0.0) {
// // Turn it into -theta, but in the 0-2PI range
// theta = 2.0 * PI - theta;
// }

// vec2 angles = vec2( theta, phi );
// return angles;
// }

void main()
{
// the sample direction equals the hemisphere's orientation
// vec2 angles = func( localPos );
// float theta = angles.a;
// float phi = angles.b;
// float x = sin(theta) * cos(phi);
// float y = sin(theta) * sin(phi);
// float z = cos(theta);
// vec3 normal = vec3( x, y, z);
float phi = vTexCoord.x * 2.0 * PI;
float theta = vTexCoord.y * PI;
float x = sin(theta) * cos(phi);
float y = sin(theta) * sin(phi);
float z = cos(theta);
vec3 normal = vec3( x, y, z);
// vec3 normal = abs(vec3( x, y, z));

// Discretely sampling the hemisphere given the integral's spherical coordinates translates to the following fragment code:
vec3 irradiance = vec3(0.0);
vec3 up = vec3(0.0, 1.0, 0.0);
vec3 right = normalize(cross(up, normal));
up = normalize(cross(normal, right));

// We specify a fixed sampleDelta delta value to traverse the hemisphere; decreasing or increasing the sample delta will increase or decrease the accuracy respectively.
const float sampleDelta = 0.025;
float nrSamples = 0.0;

for(float phi = 0.0; phi < 2.0 * PI; phi += sampleDelta)
{
for(float theta = 0.0; theta < ( 0.5 ) * PI; theta += sampleDelta)
{
// spherical to cartesian (in tangent space) // tangent space to world // add each sample result to irradiance
float x = sin(theta) * cos(phi);
float y = sin(theta) * sin(phi);
float z = cos(theta);
vec3 tangentSample = vec3( x, y, z);

vec3 sampleVec = tangentSample.x * right + tangentSample.y * up + tangentSample.z * normal;
irradiance += (texture2D(environmentMap, nTOE(sampleVec)).xyz) * cos(theta) * sin(theta);
nrSamples++;
}
}
// divide by the total number of samples taken, giving us the average sampled irradiance.
irradiance = PI * irradiance * (1.0 / float(nrSamples )) ;

gl_FragColor = vec4(irradiance, 1.0);
// gl_FragColor = vec4(normal.x, normal.y, normal.z, 1.0);
// gl_FragColor = vec4(normal.x, 0.0, 0.0, 1.0);
// gl_FragColor = vec4(0.0, normal.y, 0.0, 1.0);

}
32 changes: 32 additions & 0 deletions src/webgl/shaders/imageLight.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
attribute vec3 aPosition;
attribute vec3 aNormal;
attribute vec2 aTexCoord;

varying vec3 localPos;
varying vec3 vWorldNormal;
varying vec3 vWorldPosition;
varying vec2 vTexCoord;

uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;
uniform mat3 uNormalMatrix;

void main() {
// Multiply the position by the matrix.
vec4 viewModelPosition = uModelViewMatrix * vec4(aPosition, 1.0);
gl_Position = uProjectionMatrix * viewModelPosition;

// orient the normals and pass to the fragment shader
vWorldNormal = uNormalMatrix * aNormal;

// send the view position to the fragment shader
vWorldPosition = (uModelViewMatrix * vec4(aPosition, 1.0)).xyz;

localPos = vWorldPosition;
vTexCoord = aTexCoord;
}


/*
in the vertex shader we'll compute the world position and world oriented normal of the vertices and pass those to the fragment shader as varyings.
*/