35
35
import com .google .ar .core .Trackable ;
36
36
import com .google .ar .core .TrackingState ;
37
37
38
+ import java .net .URL ;
38
39
import java .nio .FloatBuffer ;
39
40
import java .util .ArrayList ;
40
41
import java .util .Collection ;
49
50
import processing .opengl .PGLES ;
50
51
import processing .opengl .PGraphics3D ;
51
52
import processing .opengl .PGraphicsOpenGL ;
53
+ import processing .opengl .PShader ;
52
54
53
55
public class PGraphicsAR extends PGraphics3D {
54
56
// 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 {
59
61
protected float [] projMatrix = new float [16 ];
60
62
protected float [] viewMatrix = new float [16 ];
61
63
protected float [] anchorMatrix = new float [16 ];
62
- protected float [] colorCorrectionRgba = new float [4 ];
64
+ protected float [] colorCorrection = new float [4 ];
63
65
64
66
protected ArrayList <Plane > trackPlanes = new ArrayList <Plane >();
65
67
protected HashMap <Plane , float []> trackMatrices = new HashMap <Plane , float []>();
@@ -78,6 +80,17 @@ public class PGraphicsAR extends PGraphics3D {
78
80
protected int lastTrackableId = 0 ;
79
81
protected int lastAnchorId = 0 ;
80
82
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 ;
81
94
82
95
public PGraphicsAR () {
83
96
}
@@ -377,12 +390,6 @@ public void anchor(int i) {
377
390
anchorMatrix [3 ], anchorMatrix [7 ], anchorMatrix [11 ], anchorMatrix [15 ]);
378
391
}
379
392
380
- @ Override
381
- public void lights () {
382
- // TODO <---------------------------------------------------------------------------------------
383
- super .lights ();
384
- }
385
-
386
393
387
394
protected void createBackgroundRenderer () {
388
395
backgroundRenderer = new BackgroundRenderer (surfar .getActivity ());
@@ -395,6 +402,7 @@ protected void setCameraTexture() {
395
402
protected void updateMatrices () {
396
403
surfar .camera .getProjectionMatrix (projMatrix , 0 , 0.1f , 100.0f );
397
404
surfar .camera .getViewMatrix (viewMatrix , 0 );
405
+ surfar .frame .getLightEstimate ().getColorCorrection (colorCorrection , 0 );
398
406
}
399
407
400
408
@@ -411,7 +419,6 @@ protected void updateTrackables() {
411
419
trackPlanes .add (plane );
412
420
trackIds .put (plane , ++lastTrackableId );
413
421
newPlanes .add (plane );
414
- System .out .println ("-------------> ADDED TRACKING PLANE " + plane .hashCode ());
415
422
}
416
423
Pose pose = plane .getCenterPose ();
417
424
pose .toMatrix (mat , 0 );
@@ -425,7 +432,6 @@ protected void updateTrackables() {
425
432
trackPlanes .remove (i );
426
433
trackMatrices .remove (plane );
427
434
trackIds .remove (plane );
428
- System .out .println ("-------------> REMOVED TRACKING PLANE " + plane .hashCode ());
429
435
}
430
436
}
431
437
}
@@ -439,8 +445,52 @@ protected void cleanup() {
439
445
anchor .detach ();
440
446
anchorIds .remove (anchor );
441
447
anchors .remove (anchor );
442
- System .out .println ("-------------> REMOVED ANCHOR PLANE " + anchor .hashCode ());
443
448
}
444
449
delAnchors .clear ();
445
450
}
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
+ }
446
496
}
0 commit comments