-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDOF.java
More file actions
144 lines (131 loc) · 3.93 KB
/
DOF.java
File metadata and controls
144 lines (131 loc) · 3.93 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import nub.core.Node;
import nub.processing.Scene;
import processing.core.PApplet;
import processing.core.PGraphics;
import processing.core.PShape;
import processing.event.MouseEvent;
import processing.opengl.PShader;
import java.nio.file.Paths;
/**
* Depth-Of-Filed (DOF) post-processing effect
* https://t.ly/-ADs
*/
public class DOF extends PApplet {
String depthPath;
PShader depthShader, dofShader;
PGraphics dofPGraphics;
Scene scene, depthScene;
Node[] models;
int mode = 2;
boolean exact = true;
@Override
public void settings() {
size(1400, 1400, P3D);
}
@Override
public void setup() {
scene = new Scene(createGraphics(width, height, P3D), 1000);
scene.fit(1000);
models = new Node[100];
for (int i = 0; i < models.length; i++) {
models[i] = new Node(boxShape());
//models[i].setBullsEyeSize(0.7f);
scene.randomize(models[i]);
}
// Depth shader
// Test all the different versions
depthPath = Paths.get("testing/data/depth/depth_linear.glsl").toAbsolutePath().toString();
//depthPath = Paths.get("testing/data/depth/depth_nonlinear.glsl").toAbsolutePath().toString();
//depthPath = Paths.get("testing/data/depth/depth_frag.glsl").toAbsolutePath().toString();
depthShader = loadShader(depthPath);
// TODO add proper constructor to share eye node
depthScene = new Scene(createGraphics(width, height, P3D), scene.eye(), 1000);
//depthScene.fit();
depthScene.context().shader(depthShader);
// TODO make API more consistent
depthScene.picking = false;
// DOF shader
dofShader = loadShader(Paths.get("testing/data/dof/dof.glsl").toAbsolutePath().toString());
dofShader.set("aspect", width / (float) height);
dofShader.set("maxBlur", (float) 0.015);
dofShader.set("aperture", (float) 0.02);
dofPGraphics = createGraphics(width, height, P3D);
dofPGraphics.shader(dofShader);
frameRate(1000);
}
@Override
public void draw() {
// 1. Render into main buffer
scene.openContext();
scene.context().background(0);
scene.render();
scene.closeContext();
// 2. Draw into depth buffer
depthScene.openContext();
depthScene.context().background(0);
depthShader.set("near", depthScene.zNear());
depthShader.set("far", depthScene.zFar());
depthScene.render();
depthScene.closeContext();
// 3. Draw destination buffer
dofPGraphics.beginDraw();
dofShader.set("focus", map(mouseX, 0, width, -0.5f, 1.5f));
dofShader.set("tDepth", depthScene.context());
dofPGraphics.image(scene.context(), 0, 0);
dofPGraphics.endDraw();
// display one of the 3 buffers
if (mode == 0)
scene.image();
else if (mode == 1)
depthScene.image();
//image(depthScene.context(), 0, 0);
else
image(dofPGraphics, 0, 0);
// println("-> frameRate: " + Scene.TimingHandler.frameRate + " (nub) " + frameRate + " (p5)");
}
PShape boxShape() {
PShape box = createShape(BOX, 60);
box.setFill(color(random(0, 255), random(0, 255), random(0, 255)));
return box;
}
@Override
public void keyPressed() {
if (key == '0') mode = 0;
if (key == '1') mode = 1;
if (key == '2') mode = 2;
if (key == 't')
scene.togglePerspective();
if (key == 'f')
scene.fit(1000);
if (key == 'F')
scene.fit();
}
@Override
public void mouseMoved() {
scene.tag();
}
@Override
public void mouseDragged() {
if (mouseButton == LEFT)
scene.spin();
else if (mouseButton == RIGHT)
scene.shift();
else
scene.moveForward(scene.mouseDX());
}
@Override
public void mouseWheel(MouseEvent event) {
scene.zoom(event.getCount() * 20);
}
@Override
public void mouseClicked(MouseEvent event) {
if (event.getCount() == 2)
if (event.getButton() == LEFT)
scene.focus();
else
scene.align();
}
public static void main(String[] args) {
PApplet.main(new String[]{"DOF"});
}
}