Skip to content

Commit 2adc450

Browse files
authored
Merge pull request #8424 from processing/fix/getTexture
Make sure getTexture works in vertex shaders
2 parents caf8f37 + e031b61 commit 2adc450

8 files changed

Lines changed: 71 additions & 29 deletions

File tree

src/webgl/p5.RendererGL.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -746,9 +746,9 @@ class RendererGL extends Renderer3D {
746746
if (!this._defaultNormalShader) {
747747
this._defaultNormalShader = new Shader(
748748
this,
749-
this._webGL2CompatibilityPrefix("vert", "mediump") +
749+
this._webGL2CompatibilityPrefix("vert", "highp") +
750750
defaultShaders.normalVert,
751-
this._webGL2CompatibilityPrefix("frag", "mediump") +
751+
this._webGL2CompatibilityPrefix("frag", "highp") +
752752
defaultShaders.normalFrag,
753753
{
754754
vertex: {
@@ -774,9 +774,9 @@ class RendererGL extends Renderer3D {
774774
if (!this._defaultColorShader) {
775775
this._defaultColorShader = new Shader(
776776
this,
777-
this._webGL2CompatibilityPrefix("vert", "mediump") +
777+
this._webGL2CompatibilityPrefix("vert", "highp") +
778778
defaultShaders.normalVert,
779-
this._webGL2CompatibilityPrefix("frag", "mediump") +
779+
this._webGL2CompatibilityPrefix("frag", "highp") +
780780
defaultShaders.basicFrag,
781781
{
782782
vertex: {
@@ -802,9 +802,9 @@ class RendererGL extends Renderer3D {
802802
if (!this._defaultLineShader) {
803803
this._defaultLineShader = new Shader(
804804
this,
805-
this._webGL2CompatibilityPrefix("vert", "mediump") +
805+
this._webGL2CompatibilityPrefix("vert", "highp") +
806806
defaultShaders.lineVert,
807-
this._webGL2CompatibilityPrefix("frag", "mediump") +
807+
this._webGL2CompatibilityPrefix("frag", "highp") +
808808
defaultShaders.lineFrag,
809809
{
810810
vertex: {
@@ -889,14 +889,20 @@ class RendererGL extends Renderer3D {
889889
*/
890890
_createImageLightShader(type) {
891891
if (type === 'diffused') {
892-
return this._pInst.createShader(
893-
defaultShaders.imageLightVert,
894-
defaultShaders.imageLightDiffusedFrag
892+
return new Shader(
893+
this,
894+
this._webGL2CompatibilityPrefix("vert", "highp") +
895+
defaultShaders.imageLightVert,
896+
this._webGL2CompatibilityPrefix("frag", "highp") +
897+
defaultShaders.imageLightDiffusedFrag
895898
);
896899
} else if (type === 'specular') {
897-
return this._pInst.createShader(
898-
defaultShaders.imageLightVert,
899-
defaultShaders.imageLightSpecularFrag
900+
return new Shader(
901+
this,
902+
this._webGL2CompatibilityPrefix("vert", "highp") +
903+
defaultShaders.imageLightVert,
904+
this._webGL2CompatibilityPrefix("frag", "highp") +
905+
defaultShaders.imageLightSpecularFrag
900906
);
901907
}
902908
throw new Error(`Unknown imageLight shader type: ${type}`);

src/webgl/shaders/imageLight.vert

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
precision highp float;
2-
attribute vec3 aPosition;
3-
attribute vec3 aNormal;
4-
attribute vec2 aTexCoord;
2+
IN vec3 aPosition;
3+
IN vec3 aNormal;
4+
IN vec2 aTexCoord;
55

6-
varying vec3 localPos;
7-
varying vec3 vWorldNormal;
8-
varying vec3 vWorldPosition;
9-
varying vec2 vTexCoord;
6+
OUT vec3 localPos;
7+
OUT vec3 vWorldNormal;
8+
OUT vec3 vWorldPosition;
9+
OUT vec2 vTexCoord;
1010

1111
uniform mat4 uModelViewMatrix;
1212
uniform mat4 uProjectionMatrix;

src/webgl/shaders/imageLightDiffused.frag

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
precision highp float;
2-
varying vec3 localPos;
2+
IN vec3 localPos;
33

44
// the HDR cubemap converted (can be from an equirectangular environment map.)
55
uniform sampler2D environmentMap;
6-
varying vec2 vTexCoord;
6+
IN vec2 vTexCoord;
77

88
const float PI = 3.14159265359;
99

@@ -70,13 +70,13 @@ void main()
7070
vec3 tangentSample = vec3( x, y, z);
7171

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

8080

81-
gl_FragColor = vec4(irradiance, 1.0);
81+
OUT_COLOR = vec4(irradiance, 1.0);
8282
}

src/webgl/shaders/imageLightSpecular.frag

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
precision highp float;
2-
varying vec3 localPos;
3-
varying vec2 vTexCoord;
2+
IN vec3 localPos;
3+
IN vec2 vTexCoord;
44

55
// our texture
66
uniform sampler2D environmentMap;
@@ -61,13 +61,13 @@ void main(){
6161
float NdotL = max(dot(N, L), 0.0);
6262
if (NdotL > 0.0)
6363
{
64-
prefilteredColor += texture2D(environmentMap, nTOE(L)).xyz * NdotL;
64+
prefilteredColor += TEXTURE(environmentMap, nTOE(L)).xyz * NdotL;
6565
totalWeight += NdotL;
6666
}
6767
}
6868
prefilteredColor = prefilteredColor / totalWeight;
6969

70-
gl_FragColor = vec4(prefilteredColor, 1.0);
70+
OUT_COLOR = vec4(prefilteredColor, 1.0);
7171
}
7272

7373
vec3 ImportanceSampleGGX(vec2 Xi, vec3 N, float roughness){

src/webgl/shaders/webgl2Compatibility.glsl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ out vec4 outColor;
2525

2626
#endif
2727

28-
#ifdef FRAGMENT_SHADER
2928
vec4 getTexture(in sampler2D content, vec2 coord) {
3029
vec4 color = TEXTURE(content, coord);
3130
if (color.a > 0.) color.rgb /= color.a;
3231
return color;
3332
}
34-
#endif

test/unit/visual/cases/webgl.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,41 @@ visualSuite('WebGL', function() {
852852
p5.rect(-20, -20, 40, 40);
853853
screenshot();
854854
});
855+
856+
visualTest('getTexture in vertex shaders', (p5, screenshot) => {
857+
p5.createCanvas(50, 50, p5.WEBGL);
858+
const positionData = p5.createFramebuffer({
859+
width: 3,
860+
height: 1,
861+
density: 1,
862+
antialias: false,
863+
format: p5.FLOAT,
864+
textureFiltering: p5.NEAREST
865+
});
866+
positionData.loadPixels();
867+
for (let i = 0; i < 3; i++) {
868+
positionData.pixels[i * 4] = i / 3;
869+
positionData.pixels[i * 4 + 1] = 0;
870+
positionData.pixels[i * 4 + 2] = 0;
871+
positionData.pixels[i * 4 + 3] = 1;
872+
}
873+
positionData.updatePixels();
874+
const sh = p5.baseMaterialShader().modify(() => {
875+
const data = p5.uniformTexture(() => positionData);
876+
p5.getWorldInputs((inputs) => {
877+
const angle = p5.getTexture(data, [p5.instanceID()/3, 0]).r * p5.TWO_PI;
878+
inputs.position.xy += [p5.cos(angle) * 10, p5.sin(angle) * 10];
879+
return inputs;
880+
});
881+
}, { p5, positionData });
882+
const instance = p5.buildGeometry(() => p5.sphere(3));
883+
884+
p5.noStroke();
885+
p5.fill('red');
886+
p5.shader(sh);
887+
p5.model(instance, 3);
888+
screenshot();
889+
});
855890
});
856891

857892
visualSuite("Image Based Lighting", function () {
481 Bytes
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"numScreenshots": 1
3+
}

0 commit comments

Comments
 (0)