Skip to content
Open
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
12 changes: 12 additions & 0 deletions src/webgl/material.js
Original file line number Diff line number Diff line change
Expand Up @@ -2590,6 +2590,11 @@ function material(p5, fn) {
* <a href="#/p5/loadModel">`loadModel()`</a> apply their own normal map from
* the `.mtl` file's `map_Bump`.
*
* Note: On a shape whose texture coordinates wrap all the way around, such as
* <a href="#/p5/sphere">`sphere()`</a>, the two edges of the image meet. The
* image has to tile for them to line up, otherwise a seam shows where they
* join.
*
* Note: `normalTexture()` can only be used in WebGL mode.
*
* @method normalTexture
Expand Down Expand Up @@ -2682,6 +2687,13 @@ function material(p5, fn) {
*
* A light source is needed to see the effect.
*
* Note: On a shape whose texture coordinates wrap all the way around, such as
* <a href="#/p5/sphere">`sphere()`</a>, the two edges of the image meet. The
* image has to tile for them to line up. Because a bump map is read by
* comparing neighbouring pixels, also call
* <a href="#/p5/textureWrap">`textureWrap(REPEAT)`</a> so those comparisons

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does this apply to other textures like bump maps? Also I think for shapes like sphere() it won't end up mattering because there aren't faces that go past 1 in texture coordinates, so it's probably sufficient to leave out the textureWrap bit

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

the tiling half applies to any texture wrapped around a sphere, plain texture() included, so that part isn't bump specific.

the textureWrap half is though, and i think it does still matter. you're right that no face goes past u=1, but the bump path deliberately samples one texel to each side of the current coordinate to work out the slope, so at u=1 that lookup does read past the edge even though the face doesn't.

i just tested it on this branch to be sure, same build and same map, only difference being the textureWrap(REPEAT) line: without it there's still a visible seam down the sphere, with it the surface is clean. the both-sides change in this pr made it much fainter than it was (one side still clamps instead of both), but it didn't remove it.

happy to drop the mention if you'd rather keep the docs lighter, or move it somewhere less prominent. can also post the two screenshots if it's useful to see the difference.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

if you have screenshots that'd be helpful for context! but ok if p5 does apply it to all textures including bump textures then that's good, I wasn't sure if it would actually get applied to it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yep it does get applied. bumpTexture() binds through the same texture path as any other map, so whatever textureWrap() is set to is what the sampler uses when the slope lookup reads past the edge.

here they are side by side, same sphere and same map, the only difference is the wrap mode:

bump map seam with and without textureWrap(REPEAT)

left is the default clamp, where the lookup runs off the edge and reads the same texel back, so the slope flattens along that column. right is REPEAT, where it wraps to the other side instead.

* carry across the join instead of stopping at the edge.
*
* Note: `bumpTexture()` can only be used in WebGL mode.
*
* @method bumpTexture
Expand Down
15 changes: 10 additions & 5 deletions src/webgl/shaders/phong.frag
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,17 @@ void main(void) {
vec3 mapN;
if (uNormalMapMode == 1) {
// bump map: brightness is height, so the tangent-space normal comes from
// how fast that height changes between neighbouring texels.
float h = TEXTURE(uNormalSampler, vTexCoord).r;
float hu = TEXTURE(uNormalSampler, vTexCoord + vec2(uNormalTexelSize.x, 0.0)).r;
float hv = TEXTURE(uNormalSampler, vTexCoord + vec2(0.0, uNormalTexelSize.y)).r;
// how fast that height changes between neighbouring texels. sampling both
// sides keeps the slope right at the edges of the map, where reaching past
// one side would otherwise clamp and read back the same texel.
vec2 du = vec2(uNormalTexelSize.x, 0.0);
vec2 dv = vec2(0.0, uNormalTexelSize.y);
float hl = TEXTURE(uNormalSampler, vTexCoord - du).r;
float hr = TEXTURE(uNormalSampler, vTexCoord + du).r;
float hd = TEXTURE(uNormalSampler, vTexCoord - dv).r;
float hu = TEXTURE(uNormalSampler, vTexCoord + dv).r;
// the surface leans away from the direction height increases in
mapN = normalize(vec3(h - hu, h - hv, 1.0));
mapN = normalize(vec3((hl - hr) * 0.5, (hd - hu) * 0.5, 1.0));
} else {
// normal map: rgb already holds the tangent-space normal
mapN = TEXTURE(uNormalSampler, vTexCoord).rgb * 2.0 - 1.0;
Expand Down
15 changes: 10 additions & 5 deletions src/webgpu/shaders/material.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,12 +401,17 @@ ${useTextureMaps ? ` if (material.uHasNormalMap == 1) {
var mapN: vec3<f32>;
if (material.uNormalMapMode == 1u) {
// bump map: brightness is height, so the tangent-space normal comes from
// how fast that height changes between neighbouring texels.
let h = textureSample(uNormalSampler, uNormalSampler_sampler, input.vTexCoord).r;
let hu = textureSample(uNormalSampler, uNormalSampler_sampler, input.vTexCoord + vec2<f32>(material.uNormalTexelSize.x, 0.0)).r;
let hv = textureSample(uNormalSampler, uNormalSampler_sampler, input.vTexCoord + vec2<f32>(0.0, material.uNormalTexelSize.y)).r;
// how fast that height changes between neighbouring texels. sampling both
// sides keeps the slope right at the edges of the map, where reaching past
// one side would otherwise clamp and read back the same texel.
let du = vec2<f32>(material.uNormalTexelSize.x, 0.0);
let dv = vec2<f32>(0.0, material.uNormalTexelSize.y);
let hl = textureSample(uNormalSampler, uNormalSampler_sampler, input.vTexCoord - du).r;
let hr = textureSample(uNormalSampler, uNormalSampler_sampler, input.vTexCoord + du).r;
let hd = textureSample(uNormalSampler, uNormalSampler_sampler, input.vTexCoord - dv).r;
let hu = textureSample(uNormalSampler, uNormalSampler_sampler, input.vTexCoord + dv).r;
// the surface leans away from the direction height increases in
mapN = normalize(vec3<f32>(h - hu, h - hv, 1.0));
mapN = normalize(vec3<f32>((hl - hr) * 0.5, (hd - hu) * 0.5, 1.0));
} else {
// normal map: rgb already holds the tangent-space normal
mapN = textureSample(uNormalSampler, uNormalSampler_sampler, input.vTexCoord).rgb * 2.0 - 1.0;
Expand Down
Loading