-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDepthMap.java
More file actions
129 lines (118 loc) · 3.58 KB
/
DepthMap.java
File metadata and controls
129 lines (118 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import nub.core.Node;
import nub.primitives.Quaternion;
import nub.primitives.Vector;
import nub.processing.Scene;
import processing.core.PApplet;
import processing.core.PGraphics;
import processing.event.MouseEvent;
import processing.opengl.PShader;
import java.nio.file.Paths;
/**
* Depth map shader useful for DOF and other post-processing effects
* https://t.ly/0Qea
*/
public class DepthMap extends PApplet {
Scene scene;
Scene depthMapScene;
Node[] shapes;
PGraphics depthMap;
PShader depthShader;
float zNear = 50;
float zFar = 700;
int w = 1400;
int h = 1400;
public void settings() {
size(w, h, P3D);
}
public void setup() {
depthMap = createGraphics(w / 2, h / 2, P3D);
depthShader = loadShader(Paths.get("testing/data/depth/depth_linear.glsl").toAbsolutePath().toString());
depthShader.set("near", zNear);
depthShader.set("far", zFar);
depthMap.shader(depthShader);
scene = new Scene(this, max(w, h));
scene.fit(1000);
shapes = new Node[20];
for (int i = 0; i < shapes.length; i++) {
shapes[i] = new Node(this::cube);
scene.randomize(shapes[i]);
shapes[i].setHighlight(0);
}
scene.tag("light", shapes[(int) random(0, shapes.length - 1)]);
scene.node("light").toggleHint(Node.SHAPE | Node.AXES | Node.BOUNDS);
scene.node("light").setWorldOrientation(Quaternion.from(Vector.plusK, scene.node("light").worldPosition()));
// scene.enablePicking(false);
depthMapScene = new Scene(depthMap, scene.node("light"));
depthMapScene.setZNear(() -> zNear);
depthMapScene.setZFar(() -> zFar);
depthMapScene.togglePerspective();
depthMapScene.picking = false;
frameRate(1000);
}
public void cube(PGraphics pg) {
pg.pushStyle();
if (pg == depthMap)
pg.noStroke();
else {
pg.strokeWeight(3);
pg.stroke(0, 255, 255);
}
pg.fill(255, 0, 0);
pg.box(80);
pg.popStyle();
}
public void draw() {
// 1. Fill in and display front-buffer
background(75, 25, 15);
scene.render();
// 2. Fill in shadow map using the light point of view
if (scene.isTagValid("light")) {
depthMapScene.openContext();
depthMapScene.context().background(140, 160, 125);
depthMapScene.drawAxes();
depthMapScene.render();
depthMapScene.closeContext();
depthMapScene.image(w / 2, h / 2);
}
}
public void mouseMoved(MouseEvent event) {
if (event.isShiftDown()) {
if (scene.isTagValid("light")) {
scene.node("light").toggleHint(Node.SHAPE | Node.AXES | Node.BOUNDS);
}
// no calling mouseTag since we need to immediately update the tagged node
scene.updateTag("light");
if (scene.isTagValid("light")) {
depthMapScene.setEye(scene.node("light"));
scene.node("light").toggleHint(Node.SHAPE | Node.AXES | Node.BOUNDS);
}
} else
scene.tag();
}
public void mouseDragged() {
if (mouseButton == LEFT)
scene.spin();
else if (mouseButton == RIGHT)
scene.shift();
else
scene.moveForward(mouseX - pmouseX);
}
public void mouseWheel(MouseEvent event) {
if (event.isShiftDown() && scene.isTagValid("light")) {
depthShader.set("far", zFar += event.getCount() * 20);
depthMapScene.setZFar(() -> zFar);
}
else
scene.zoom(event.getCount() * 20);
}
public void keyPressed() {
if (key == ' ' && scene.isTagValid("light")) {
depthMapScene.togglePerspective();
}
if (key == 'p')
scene.togglePerspective();
}
public static void main(String[] args) {
PApplet.main(new String[]{"DepthMap"});
}
}