-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjection.js
More file actions
168 lines (141 loc) · 3.55 KB
/
Projection.js
File metadata and controls
168 lines (141 loc) · 3.55 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import {
PerspectiveCamera,
CameraHelper,
Color,
MeshBasicMaterial,
Mesh,
PlaneGeometry,
Vector2,
DoubleSide,
Object3D,
TextureLoader,
} from "three";
// import ProjectedMaterial from "three-projected-material";
export default class Projection extends Object3D {
camera;
#layers;
#layerNames;
#bounds;
#texture;
#screen;
material = {};
helper = {};
renderer;
scene;
renderTarget;
plane;
textureSource;
attributes;
ready = false;
#focus;
#opacity;
#index;
constructor({ quaternion, position, camera, file }, dir) {
super();
const fov = 2 * Math.atan(camera.width / (2 * camera.f)) * (180 / Math.PI);
this.camera = new PerspectiveCamera(
fov,
camera.width / camera.height,
1,
2,
);
this.position.set(...position);
this.setRotationFromQuaternion(quaternion);
const textureLoader = new TextureLoader();
const texture = textureLoader.load(`${dir}/images/${file}`);
this.plane = new Mesh(
new PlaneGeometry(1, 1),
new MeshBasicMaterial({
// transparent: true,
// opacity: 0.4,
map: texture,
side: DoubleSide,
}),
);
const scale = this.camera.getViewSize(this.camera.far, new Vector2());
this.plane.scale.set(...scale, 1);
this.plane.translateZ(-this.camera.far);
this.helper = new CameraHelper(this.camera);
this.#setHelperColor(0x00ff00);
// this.add(this.helper);
this.add(this.plane);
}
set index(index) {
this.#index = index + 1;
Object.keys(this.material).forEach((layer) => {
if (layer === "vantage-renderer") return;
this.#layers[layer].material[index + 1] = this.material[layer];
});
}
get index() {
return this.#index - 1;
}
set bounds({ bounds, auto }) {
if (this.projectionType !== "map" || bounds == null) return;
if (!auto) this.#bounds = bounds;
this.camera.left = bounds[0];
this.camera.right = bounds[1];
this.camera.top = bounds[2];
this.camera.bottom = bounds[3];
this.update();
}
get bounds() {
return this.#bounds;
}
set position(position) {
this.camera.position.set(...position);
this.update();
}
get position() {
return this.camera.position;
}
set rotation(rotation) {
this.camera.rotation.set(...rotation);
this.update();
}
get rotation() {
return this.camera.rotation;
}
set fov(fov) {
this.camera.fov = fov;
this.camera.updateProjectionMatrix();
this.update();
}
get fov() {
return this.camera.fov;
}
set far(far) {
this.camera.far = far;
this.camera.updateProjectionMatrix();
this.update();
}
get far() {
return this.camera.far;
}
#setHelperColor = (color) => {
const c = new Color(color);
this.helper.setColors(c, c, c, c, c);
};
updatePlane = () => {
if (this.plane == null) return;
this.plane.rotation.set(...this.camera.rotation);
const position =
this.projectionType === "map"
? [
(this.camera.top + this.camera.bottom) / 2,
this.camera.position.y,
(this.camera.right + this.camera.left) / 2,
]
: this.camera.position;
this.plane.position.set(...position);
const scale =
this.projectionType === "map"
? [
this.camera.right - this.camera.left,
this.camera.top - this.camera.bottom,
]
: this.camera.getViewSize(this.camera.far, new Vector2());
this.plane.scale.set(...scale, 1);
this.plane.translateZ(-this.camera.far + this.camera.far * 0.001);
};
}