Skip to content

Commit 2d10678

Browse files
committed
Fix #7030
1 parent e9f400d commit 2d10678

File tree

4 files changed

+80
-22
lines changed

4 files changed

+80
-22
lines changed

preview/index.html

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,37 @@
2020
import p5 from '../src/app.js';
2121

2222
const sketch = function (p) {
23+
let myShader
24+
let tex
2325
p.setup = function () {
24-
p.createCanvas(200, 200);
25-
};
26-
27-
p.draw = function () {
28-
p.background(0, 50, 50);
29-
p.circle(100, 100, 50);
30-
31-
p.fill('white');
32-
p.textSize(30);
33-
p.text('hello', 10, 30);
26+
p.createCanvas(20, 10, p.WEBGL);
27+
p.background(255);
28+
29+
myShader = p.baseMaterialShader().modify({
30+
uniforms: {
31+
'sampler2D myTex': undefined,
32+
},
33+
'Inputs getPixelInputs': `(Inputs inputs) {
34+
inputs.color = texture(myTex, inputs.texCoord);
35+
return inputs;
36+
}`
37+
})
38+
39+
// Make a red texture
40+
tex = p.createFramebuffer();
41+
tex.draw(() => p.background('red'));
42+
43+
p.translate(-p.width/2, -p.height/2)
44+
p.shader(myShader);
45+
p.noStroke();
46+
myShader.setUniform('myTex', tex);
47+
48+
// Draw once to the left
49+
p.rect(0, 0, 10, 10);
50+
51+
// Draw once to the right
52+
p.rect(10, 0, 10, 10);
53+
console.log(p.canvas.toDataURL())
3454
};
3555
};
3656

src/webgl/p5.RendererGL.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,10 @@ class RendererGL extends Renderer {
551551
const shader = this._drawingFilter && this.states.userFillShader
552552
? this.states.userFillShader
553553
: this._getFillShader();
554+
shader.bindShader();
554555
this._setGlobalUniforms(shader);
555556
this._setFillUniforms(shader);
557+
shader.bindTextures();
556558

557559
for (const buff of this.buffers.fill) {
558560
buff._prepareBuffer(geometry, shader);
@@ -576,8 +578,10 @@ class RendererGL extends Renderer {
576578
this._useLineColor = geometry.vertexStrokeColors.length > 0;
577579

578580
const shader = this._getStrokeShader();
581+
shader.bindShader();
579582
this._setGlobalUniforms(shader);
580583
this._setStrokeUniforms(shader);
584+
shader.bindTextures();
581585

582586
for (const buff of this.buffers.stroke) {
583587
buff._prepareBuffer(geometry, shader);
@@ -615,8 +619,10 @@ class RendererGL extends Renderer {
615619
_drawPoints(vertices, vertexBuffer) {
616620
const gl = this.GL;
617621
const pointShader = this._getPointShader();
622+
pointShader.bindShader();
618623
this._setGlobalUniforms(pointShader);
619624
this._setPointUniforms(pointShader);
625+
pointShader.bindTextures();
620626

621627
this._bindBuffer(
622628
vertexBuffer,
@@ -2104,8 +2110,6 @@ class RendererGL extends Renderer {
21042110
}
21052111

21062112
_setGlobalUniforms(shader) {
2107-
shader.bindShader();
2108-
21092113
const modelMatrix = this.states.uModelMatrix;
21102114
const viewMatrix = this.states.uViewMatrix;
21112115
const projectionMatrix = this.states.uPMatrix;
@@ -2139,8 +2143,6 @@ class RendererGL extends Renderer {
21392143
}
21402144

21412145
_setStrokeUniforms(strokeShader) {
2142-
strokeShader.bindShader();
2143-
21442146
// set the uniform values
21452147
strokeShader.setUniform('uUseLineColor', this._useLineColor);
21462148
strokeShader.setUniform('uMaterialColor', this.states.curStrokeColor);
@@ -2150,8 +2152,6 @@ class RendererGL extends Renderer {
21502152
}
21512153

21522154
_setFillUniforms(fillShader) {
2153-
fillShader.bindShader();
2154-
21552155
this.mixedSpecularColor = [...this.states.curSpecularColor];
21562156

21572157
if (this.states._useMetalness > 0) {
@@ -2236,8 +2236,6 @@ class RendererGL extends Renderer {
22362236
fillShader.setUniform('uConstantAttenuation', this.states.constantAttenuation);
22372237
fillShader.setUniform('uLinearAttenuation', this.states.linearAttenuation);
22382238
fillShader.setUniform('uQuadraticAttenuation', this.states.quadraticAttenuation);
2239-
2240-
fillShader.bindTextures();
22412239
}
22422240

22432241
// getting called from _setFillUniforms
@@ -2257,8 +2255,6 @@ class RendererGL extends Renderer {
22572255
}
22582256

22592257
_setPointUniforms(pointShader) {
2260-
pointShader.bindShader();
2261-
22622258
// set the uniform values
22632259
pointShader.setUniform('uMaterialColor', this.states.curStrokeColor);
22642260
// @todo is there an instance where this isn't stroke weight?

src/webgl/p5.Shader.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,6 @@ class Shader {
747747
unbindShader() {
748748
if (this._bound) {
749749
this.unbindTextures();
750-
//this._renderer.GL.useProgram(0); ??
751750
this._bound = false;
752751
}
753752
return this;
@@ -781,8 +780,12 @@ class Shader {
781780
}
782781

783782
unbindTextures() {
783+
const gl = this._renderer.GL;
784+
const empty = this._renderer._getEmptyTexture();
784785
for (const uniform of this.samplers) {
785-
this.setUniform(uniform.name, this._renderer._getEmptyTexture());
786+
gl.activeTexture(gl.TEXTURE0 + uniform.samplerIndex);
787+
empty.bindTexture();
788+
gl.uniform1i(uniform.location, uniform.samplerIndex);
786789
}
787790
}
788791

test/unit/webgl/p5.RendererGL.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,45 @@ suite('p5.RendererGL', function() {
6969
});
7070
});
7171

72+
suite('texture binding', function() {
73+
test('textures remain bound after each draw call', function() {
74+
myp5.createCanvas(20, 10, myp5.WEBGL);
75+
myp5.background(255);
76+
77+
const myShader = myp5.baseMaterialShader().modify({
78+
uniforms: {
79+
'sampler2D myTex': undefined,
80+
},
81+
'Inputs getPixelInputs': `(Inputs inputs) {
82+
inputs.color = texture(myTex, inputs.texCoord);
83+
return inputs;
84+
}`
85+
})
86+
87+
// Make a red texture
88+
const tex = myp5.createFramebuffer();
89+
tex.draw(() => myp5.background('red'));
90+
91+
myp5.shader(myShader);
92+
// myp5.fill('red');
93+
myp5.noStroke();
94+
myShader.setUniform('myTex', tex);
95+
96+
myp5.translate(-myp5.width/2, -myp5.height/2);
97+
myp5.rectMode(myp5.CORNER);
98+
99+
// Draw once to the left
100+
myp5.rect(0, 0, 10, 10);
101+
102+
// Draw once to the right
103+
myp5.rect(10, 0, 10, 10);
104+
105+
// Both rectangles should be red
106+
assert.deepEqual(myp5.get(5, 5), [255, 0, 0, 255]);
107+
assert.deepEqual(myp5.get(15, 5), [255, 0, 0, 255]);
108+
})
109+
});
110+
72111
suite('default stroke shader', function() {
73112
test('check activate and deactivating fill and stroke', function() {
74113
myp5.noStroke();

0 commit comments

Comments
 (0)