Skip to content

Commit 0c3ca97

Browse files
committed
load custom AR shaders
1 parent db4f48e commit 0c3ca97

File tree

1 file changed

+60
-10
lines changed

1 file changed

+60
-10
lines changed

mode/libraries/ar/src/processing/ar/PGraphicsAR.java

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.google.ar.core.Trackable;
3636
import com.google.ar.core.TrackingState;
3737

38+
import java.net.URL;
3839
import java.nio.FloatBuffer;
3940
import java.util.ArrayList;
4041
import java.util.Collection;
@@ -49,6 +50,7 @@
4950
import processing.opengl.PGLES;
5051
import processing.opengl.PGraphics3D;
5152
import processing.opengl.PGraphicsOpenGL;
53+
import processing.opengl.PShader;
5254

5355
public class PGraphicsAR extends PGraphics3D {
5456
// Convenience reference to the AR surface. It is the same object one gets from PApplet.getSurface().
@@ -59,7 +61,7 @@ public class PGraphicsAR extends PGraphics3D {
5961
protected float[] projMatrix = new float[16];
6062
protected float[] viewMatrix = new float[16];
6163
protected float[] anchorMatrix = new float[16];
62-
protected float[] colorCorrectionRgba = new float[4];
64+
protected float[] colorCorrection = new float[4];
6365

6466
protected ArrayList<Plane> trackPlanes = new ArrayList<Plane>();
6567
protected HashMap<Plane, float[]> trackMatrices = new HashMap<Plane, float[]>();
@@ -78,6 +80,17 @@ public class PGraphicsAR extends PGraphics3D {
7880
protected int lastTrackableId = 0;
7981
protected int lastAnchorId = 0;
8082

83+
static protected URL arLightShaderVertURL =
84+
PGraphicsOpenGL.class.getResource("/assets/shaders/ARLightVert.glsl");
85+
static protected URL arTexlightShaderVertURL =
86+
PGraphicsOpenGL.class.getResource("/assets/shaders/ARTexLightVert.glsl");
87+
static protected URL arLightShaderFragURL =
88+
PGraphicsOpenGL.class.getResource("/assets/shaders/ARLightFrag.glsl");
89+
static protected URL arTexlightShaderFragURL =
90+
PGraphicsOpenGL.class.getResource("/assets/shaders/ARTexLightFrag.glsl");
91+
92+
protected PShader arLightShader;
93+
protected PShader arTexlightShader;
8194

8295
public PGraphicsAR() {
8396
}
@@ -377,12 +390,6 @@ public void anchor(int i) {
377390
anchorMatrix[3], anchorMatrix[7], anchorMatrix[11], anchorMatrix[15]);
378391
}
379392

380-
@Override
381-
public void lights() {
382-
// TODO <---------------------------------------------------------------------------------------
383-
super.lights();
384-
}
385-
386393

387394
protected void createBackgroundRenderer() {
388395
backgroundRenderer = new BackgroundRenderer(surfar.getActivity());
@@ -395,6 +402,7 @@ protected void setCameraTexture() {
395402
protected void updateMatrices() {
396403
surfar.camera.getProjectionMatrix(projMatrix, 0, 0.1f, 100.0f);
397404
surfar.camera.getViewMatrix(viewMatrix, 0);
405+
surfar.frame.getLightEstimate().getColorCorrection(colorCorrection, 0);
398406
}
399407

400408

@@ -411,7 +419,6 @@ protected void updateTrackables() {
411419
trackPlanes.add(plane);
412420
trackIds.put(plane, ++lastTrackableId);
413421
newPlanes.add(plane);
414-
System.out.println("-------------> ADDED TRACKING PLANE " + plane.hashCode());
415422
}
416423
Pose pose = plane.getCenterPose();
417424
pose.toMatrix(mat, 0);
@@ -425,7 +432,6 @@ protected void updateTrackables() {
425432
trackPlanes.remove(i);
426433
trackMatrices.remove(plane);
427434
trackIds.remove(plane);
428-
System.out.println("-------------> REMOVED TRACKING PLANE " + plane.hashCode());
429435
}
430436
}
431437
}
@@ -439,8 +445,52 @@ protected void cleanup() {
439445
anchor.detach();
440446
anchorIds.remove(anchor);
441447
anchors.remove(anchor);
442-
System.out.println("-------------> REMOVED ANCHOR PLANE " + anchor.hashCode());
443448
}
444449
delAnchors.clear();
445450
}
451+
452+
453+
@Override
454+
protected PShader getPolyShader(boolean lit, boolean tex) {
455+
if (getPrimaryPG() != this) {
456+
// An offscreen surface will use the default shaders from the parent OpenGL renderer
457+
return super.getPolyShader(lit, tex);
458+
}
459+
460+
PShader shader;
461+
boolean useDefault = polyShader == null;
462+
if (lit) {
463+
if (tex) {
464+
if (useDefault || !isPolyShaderTexLight(polyShader)) {
465+
if (arTexlightShader == null) {
466+
arTexlightShader = loadShaderFromURL(arTexlightShaderFragURL, arTexlightShaderVertURL);
467+
}
468+
shader = arTexlightShader;
469+
} else {
470+
shader = polyShader;
471+
}
472+
} else {
473+
if (useDefault || !isPolyShaderLight(polyShader)) {
474+
if (arLightShader == null) {
475+
arLightShader = loadShaderFromURL(arLightShaderFragURL, arLightShaderVertURL);
476+
}
477+
shader = arLightShader;
478+
} else {
479+
shader = polyShader;
480+
}
481+
}
482+
updateShader(shader);
483+
return shader;
484+
} else {
485+
// Non-lit shaders use the default shaders from the parent OpenGL renderer
486+
return super.getPolyShader(lit, tex);
487+
}
488+
}
489+
490+
491+
@Override
492+
protected void updateShader(PShader shader) {
493+
super.updateShader(shader);
494+
shader.set("colorCorrection", colorCorrection, 4);
495+
}
446496
}

0 commit comments

Comments
 (0)