|
1 | 1 | package vrcube;
|
2 | 2 |
|
3 | 3 | import processing.core.PApplet;
|
| 4 | +import processing.core.PMatrix3D; |
| 5 | +import processing.core.PVector; |
4 | 6 | import processing.vr.*;
|
5 | 7 |
|
6 | 8 | public class Sketch extends PApplet {
|
7 | 9 | float boxSize = 140;
|
8 | 10 | VRCamera vrcam;
|
9 |
| - VRSelector vrsel; |
| 11 | + Selector vrsel; |
10 | 12 |
|
11 | 13 | public void settings() {
|
12 | 14 | fullScreen(VR);
|
13 | 15 | }
|
14 | 16 |
|
15 | 17 | public void setup() {
|
16 | 18 | vrcam = new VRCamera(this);
|
17 |
| - vrsel = new VRSelector(this); |
| 19 | + vrsel = new Selector(this); |
| 20 | +// vrcam.setNear(1000); |
| 21 | +// vrcam.setFar(1100); |
18 | 22 | }
|
19 | 23 |
|
20 | 24 | public void draw() {
|
@@ -53,14 +57,110 @@ void drawGrid() {
|
53 | 57 | }
|
54 | 58 |
|
55 | 59 | void drawAim() {
|
56 |
| - vrcam.begin(); |
| 60 | + vrcam.sticky(); |
57 | 61 | stroke(47, 177, 234, 150);
|
58 | 62 | strokeWeight(50);
|
59 | 63 | point(0, 0, 100);
|
60 |
| - vrcam.end(); |
| 64 | + vrcam.noSticky(); |
61 | 65 | }
|
62 | 66 |
|
| 67 | + class Selector { |
| 68 | + protected PApplet parent; |
63 | 69 |
|
| 70 | + protected PVector dir = new PVector(); |
| 71 | + protected PVector cam = new PVector(); |
| 72 | + |
| 73 | + protected PMatrix3D eyeMat = new PMatrix3D(); |
| 74 | + protected PMatrix3D objMat = new PMatrix3D(); |
| 75 | + |
| 76 | + protected PVector front = new PVector(); |
| 77 | + protected PVector objCam = new PVector(); |
| 78 | + protected PVector objFront = new PVector(); |
| 79 | + protected PVector objDir = new PVector(); |
| 80 | + |
| 81 | + protected PVector hit = new PVector(); |
| 82 | + |
| 83 | + public Selector(PApplet parent) { |
| 84 | + this.parent = parent; |
| 85 | + } |
| 86 | + |
| 87 | + public void update() { |
| 88 | + parent.getEyeMatrix(eyeMat); |
| 89 | + cam.set(eyeMat.m03, eyeMat.m13, eyeMat.m23); |
| 90 | + dir.set(eyeMat.m02, eyeMat.m12, eyeMat.m22); |
| 91 | + PVector.add(cam, dir, front); |
| 92 | + } |
| 93 | + |
| 94 | + public boolean hit(PMatrix3D mat, float boxSize) { |
| 95 | + objMat.set(mat); |
| 96 | + return hitImpl(boxSize); |
| 97 | + } |
| 98 | + |
| 99 | + public boolean hit(float boxSize) { |
| 100 | + parent.getObjectMatrix(objMat); |
| 101 | + return hitImpl(boxSize); |
| 102 | + } |
| 103 | + |
| 104 | + protected boolean hitImpl(float boxSize) { |
| 105 | + objMat.mult(cam, objCam); |
| 106 | + objMat.mult(front, objFront); |
| 107 | + PVector.sub(objFront, objCam, objDir); |
| 108 | + PVector boxMin = new PVector(-boxSize/2, -boxSize/2, -boxSize/2); |
| 109 | + PVector boxMax = new PVector(+boxSize/2, +boxSize/2, +boxSize/2); |
| 110 | + return intersectsLine(objCam, objDir, boxMin, boxMax, 0, 1000, hit); |
| 111 | + } |
| 112 | + |
| 113 | + protected boolean intersectsLine(PVector orig, PVector dir, |
| 114 | + PVector minPos, PVector maxPos, float minDist, float maxDist, PVector hit) { |
| 115 | + PVector bbox; |
| 116 | + PVector invDir = new PVector(1/dir.x, 1/dir.y, 1/dir.z); |
| 117 | + |
| 118 | + boolean signDirX = invDir.x < 0; |
| 119 | + boolean signDirY = invDir.y < 0; |
| 120 | + boolean signDirZ = invDir.z < 0; |
| 121 | + |
| 122 | + bbox = signDirX ? maxPos : minPos; |
| 123 | + float txmin = (bbox.x - orig.x) * invDir.x; |
| 124 | + bbox = signDirX ? minPos : maxPos; |
| 125 | + float txmax = (bbox.x - orig.x) * invDir.x; |
| 126 | + bbox = signDirY ? maxPos : minPos; |
| 127 | + float tymin = (bbox.y - orig.y) * invDir.y; |
| 128 | + bbox = signDirY ? minPos : maxPos; |
| 129 | + float tymax = (bbox.y - orig.y) * invDir.y; |
| 130 | + |
| 131 | + if ((txmin > tymax) || (tymin > txmax)) { |
| 132 | + return false; |
| 133 | + } |
| 134 | + if (tymin > txmin) { |
| 135 | + txmin = tymin; |
| 136 | + } |
| 137 | + if (tymax < txmax) { |
| 138 | + txmax = tymax; |
| 139 | + } |
| 140 | + |
| 141 | + bbox = signDirZ ? maxPos : minPos; |
| 142 | + float tzmin = (bbox.z - orig.z) * invDir.z; |
| 143 | + bbox = signDirZ ? minPos : maxPos; |
| 144 | + float tzmax = (bbox.z - orig.z) * invDir.z; |
| 145 | + |
| 146 | + if ((txmin > tzmax) || (tzmin > txmax)) { |
| 147 | + return false; |
| 148 | + } |
| 149 | + if (tzmin > txmin) { |
| 150 | + txmin = tzmin; |
| 151 | + } |
| 152 | + if (tzmax < txmax) { |
| 153 | + txmax = tzmax; |
| 154 | + } |
| 155 | + if ((txmin < maxDist) && (txmax > minDist)) { |
| 156 | + hit.x = orig.x + txmin * dir.x; |
| 157 | + hit.y = orig.y + txmin * dir.y; |
| 158 | + hit.z = orig.z + txmin * dir.z; |
| 159 | + return true; |
| 160 | + } |
| 161 | + return false; |
| 162 | + } |
| 163 | + } |
64 | 164 |
|
65 | 165 | /*
|
66 | 166 | public void settings() {
|
|
0 commit comments