Skip to content

Commit ea8a40e

Browse files
authored
Merge branch 'main' into dependabot/npm_and_yarn/brace-expansion-2.1.4
2 parents 70297e9 + 8c44dc5 commit ea8a40e

5 files changed

Lines changed: 64 additions & 12 deletions

File tree

src/strands/strands_transpiler.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,13 @@ const ASTCallbacks = {
564564
// Only inject the variable name if the first argument isn't already a string
565565
if (
566566
node.init.arguments.length === 0 ||
567-
node.init.arguments[0].type !== 'Literal' ||
568-
typeof node.init.arguments[0].value !== 'string'
567+
!(
568+
(
569+
node.init.arguments[0].type === 'Literal' &&
570+
typeof node.init.arguments[0].value === 'string'
571+
) ||
572+
node.init.arguments[0].type === 'TemplateLiteral'
573+
)
569574
) {
570575
const uniformName = getOrCreateInternalShaderName(
571576
state.shaderNameMap,

src/webgl/material.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2590,6 +2590,11 @@ function material(p5, fn) {
25902590
* <a href="#/p5/loadModel">`loadModel()`</a> apply their own normal map from
25912591
* the `.mtl` file's `map_Bump`.
25922592
*
2593+
* Note: On a shape whose texture coordinates wrap all the way around, such as
2594+
* <a href="#/p5/sphere">`sphere()`</a>, the two edges of the image meet. The
2595+
* image has to tile for them to line up, otherwise a seam shows where they
2596+
* join.
2597+
*
25932598
* Note: `normalTexture()` can only be used in WebGL mode.
25942599
*
25952600
* @method normalTexture
@@ -2682,6 +2687,13 @@ function material(p5, fn) {
26822687
*
26832688
* A light source is needed to see the effect.
26842689
*
2690+
* Note: On a shape whose texture coordinates wrap all the way around, such as
2691+
* <a href="#/p5/sphere">`sphere()`</a>, the two edges of the image meet. The
2692+
* image has to tile for them to line up. Because a bump map is read by
2693+
* comparing neighbouring pixels, also call
2694+
* <a href="#/p5/textureWrap">`textureWrap(REPEAT)`</a> so those comparisons
2695+
* carry across the join instead of stopping at the edge.
2696+
*
26852697
* Note: `bumpTexture()` can only be used in WebGL mode.
26862698
*
26872699
* @method bumpTexture

src/webgl/shaders/phong.frag

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,17 @@ void main(void) {
7676
vec3 mapN;
7777
if (uNormalMapMode == 1) {
7878
// bump map: brightness is height, so the tangent-space normal comes from
79-
// how fast that height changes between neighbouring texels.
80-
float h = TEXTURE(uNormalSampler, vTexCoord).r;
81-
float hu = TEXTURE(uNormalSampler, vTexCoord + vec2(uNormalTexelSize.x, 0.0)).r;
82-
float hv = TEXTURE(uNormalSampler, vTexCoord + vec2(0.0, uNormalTexelSize.y)).r;
79+
// how fast that height changes between neighbouring texels. sampling both
80+
// sides keeps the slope right at the edges of the map, where reaching past
81+
// one side would otherwise clamp and read back the same texel.
82+
vec2 du = vec2(uNormalTexelSize.x, 0.0);
83+
vec2 dv = vec2(0.0, uNormalTexelSize.y);
84+
float hl = TEXTURE(uNormalSampler, vTexCoord - du).r;
85+
float hr = TEXTURE(uNormalSampler, vTexCoord + du).r;
86+
float hd = TEXTURE(uNormalSampler, vTexCoord - dv).r;
87+
float hu = TEXTURE(uNormalSampler, vTexCoord + dv).r;
8388
// the surface leans away from the direction height increases in
84-
mapN = normalize(vec3(h - hu, h - hv, 1.0));
89+
mapN = normalize(vec3((hl - hr) * 0.5, (hd - hu) * 0.5, 1.0));
8590
} else {
8691
// normal map: rgb already holds the tangent-space normal
8792
mapN = TEXTURE(uNormalSampler, vTexCoord).rgb * 2.0 - 1.0;

src/webgpu/shaders/material.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,12 +401,17 @@ ${useTextureMaps ? ` if (material.uHasNormalMap == 1) {
401401
var mapN: vec3<f32>;
402402
if (material.uNormalMapMode == 1u) {
403403
// bump map: brightness is height, so the tangent-space normal comes from
404-
// how fast that height changes between neighbouring texels.
405-
let h = textureSample(uNormalSampler, uNormalSampler_sampler, input.vTexCoord).r;
406-
let hu = textureSample(uNormalSampler, uNormalSampler_sampler, input.vTexCoord + vec2<f32>(material.uNormalTexelSize.x, 0.0)).r;
407-
let hv = textureSample(uNormalSampler, uNormalSampler_sampler, input.vTexCoord + vec2<f32>(0.0, material.uNormalTexelSize.y)).r;
404+
// how fast that height changes between neighbouring texels. sampling both
405+
// sides keeps the slope right at the edges of the map, where reaching past
406+
// one side would otherwise clamp and read back the same texel.
407+
let du = vec2<f32>(material.uNormalTexelSize.x, 0.0);
408+
let dv = vec2<f32>(0.0, material.uNormalTexelSize.y);
409+
let hl = textureSample(uNormalSampler, uNormalSampler_sampler, input.vTexCoord - du).r;
410+
let hr = textureSample(uNormalSampler, uNormalSampler_sampler, input.vTexCoord + du).r;
411+
let hd = textureSample(uNormalSampler, uNormalSampler_sampler, input.vTexCoord - dv).r;
412+
let hu = textureSample(uNormalSampler, uNormalSampler_sampler, input.vTexCoord + dv).r;
408413
// the surface leans away from the direction height increases in
409-
mapN = normalize(vec3<f32>(h - hu, h - hv, 1.0));
414+
mapN = normalize(vec3<f32>((hl - hr) * 0.5, (hd - hu) * 0.5, 1.0));
410415
} else {
411416
// normal map: rgb already holds the tangent-space normal
412417
mapN = textureSample(uNormalSampler, uNormalSampler_sampler, input.vTexCoord).rgb * 2.0 - 1.0;

test/unit/webgl/p5.Shader.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,31 @@ suite('p5.Shader', function () {
894894
assert.approximately(pixelColor[2], 204, 5);
895895
});
896896

897+
test('handle custom uniform names with template strings', () => {
898+
myp5.createCanvas(50, 50, myp5.WEBGL);
899+
const testShader = myp5.baseMaterialShader().modify(
900+
() => {
901+
// Variable name is 'brightness' but uniform name is 'customBrightness'
902+
const brightness = myp5.uniformFloat(`customBrightness`, () => 0.8);
903+
myp5.getPixelInputs(inputs => {
904+
inputs.color = [brightness, brightness, brightness, 1.0];
905+
return inputs;
906+
});
907+
},
908+
{ myp5 }
909+
);
910+
911+
myp5.noStroke();
912+
myp5.shader(testShader);
913+
myp5.plane(myp5.width, myp5.height);
914+
915+
// Check that the shader uses the automatic value (0.8)
916+
const pixelColor = myp5.get(25, 25);
917+
assert.approximately(pixelColor[0], 204, 5); // 0.8 * 255 = 204
918+
assert.approximately(pixelColor[1], 204, 5);
919+
assert.approximately(pixelColor[2], 204, 5);
920+
});
921+
897922
test('handle custom uniform names with manual setUniform', () => {
898923
myp5.createCanvas(50, 50, myp5.WEBGL);
899924
const testShader = myp5.baseMaterialShader().modify(

0 commit comments

Comments
 (0)