Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions src/webgl/p5.RendererGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ p5.RendererGL = class RendererGL extends p5.Renderer {
this.uMVMatrix = new p5.Matrix();
this.uPMatrix = new p5.Matrix();
this.uNMatrix = new p5.Matrix('mat3');
this.curMatrix = new p5.Matrix('mat3');

// Current vertex normal
this._currentNormal = new p5.Vector(0, 0, 1);
Expand Down
5 changes: 5 additions & 0 deletions src/webgl/p5.Shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,11 @@ p5.Shader = class {
this._renderer.uNMatrix.inverseTranspose(this._renderer.uMVMatrix);
this.setUniform('uNormalMatrix', this._renderer.uNMatrix.mat3);
}
if (this.uniforms.uCameraRotation) {
this._renderer.curMatrix.inverseTranspose(this._renderer.
_curCamera.cameraMatrix);
this.setUniform('uCameraRotation', this._renderer.curMatrix.mat3);
}
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/webgl/shaders/lighting.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ uniform bool uUseLighting;

uniform int uAmbientLightCount;
uniform vec3 uAmbientColor[5];

uniform mat3 uCameraRotation;
uniform int uDirectionalLightCount;
uniform vec3 uLightingDirection[5];
uniform vec3 uDirectionalDiffuseColors[5];
Expand Down Expand Up @@ -112,7 +112,7 @@ vec2 mapTextureToNormal( vec3 v ){
vec3 calculateImageDiffuse( vec3 vNormal, vec3 vViewPosition ){
// make 2 seperate builds
vec3 worldCameraPosition = vec3(0.0, 0.0, 0.0); // hardcoded world camera position
vec3 worldNormal = normalize(vNormal);
vec3 worldNormal = normalize(vNormal * uCameraRotation);
vec2 newTexCoor = mapTextureToNormal( worldNormal );
vec4 texture = TEXTURE( environmentMapDiffused, newTexCoor );
// this is to make the darker sections more dark
Expand All @@ -124,7 +124,7 @@ vec3 calculateImageSpecular( vec3 vNormal, vec3 vViewPosition ){
vec3 worldCameraPosition = vec3(0.0, 0.0, 0.0);
vec3 worldNormal = normalize(vNormal);
vec3 lightDirection = normalize( vViewPosition - worldCameraPosition );
vec3 R = reflect(lightDirection, worldNormal);
vec3 R = reflect(lightDirection, worldNormal) * uCameraRotation;
vec2 newTexCoor = mapTextureToNormal( R );
Copy link
Contributor

Choose a reason for hiding this comment

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

I noticed something kinda interesting. When you rotate around 360 degrees, you can always see the sun in the reflection, even though presumably at some point it should be behind the sphere and not in the reflection:

Screen.Recording.2024-01-14.at.10.58.29.AM.mov

I wonder if this is a bug in mapTextureToNormal that we didn't notice before because we couldn't rotate the spheremap texture like we can now. Is that something you're interested in looking into as part of this change? The idea is to convert a normal into a spherical coordinate (an azimuthal angle and a polar angle, and a fixed radius of 1), and then map those angles to 2D texture coordinates on the input image. Maybe we're doing that slightly incorrectly?

#ifdef WEBGL2
vec4 outColor = textureLod(environmentMapSpecular, newTexCoor, levelOfDetail);
Expand Down