-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathport.js
More file actions
275 lines (232 loc) · 7.69 KB
/
port.js
File metadata and controls
275 lines (232 loc) · 7.69 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import * as THREE from "three";
import { textSplats } from "@sparkjsdev/spark";
import { SparkRing } from "./sparkring.js";
import { SparkDisk, PortalEffects } from "./sparkdisk.js";
// Re-export for convenience
export { PortalEffects };
export class ProtoPortal {
constructor(portalPair, destinationUrl, scene, portals) {
this.pair = portalPair;
this.destinationUrl = destinationUrl;
this.scene = scene;
this.portals = portals;
this.entryLabel = null; // Label on entry side (shows destination name)
this.exitLabel = null; // Label on exit side (shows source name)
this.entryRing = null; // Ring on entry side
this.exitRing = null; // Ring on exit side
this.entryDisk = null; // Disk on entry side (for VR mode)
this.exitDisk = null; // Disk on exit side (for VR mode)
}
/**
* Create labels on both sides of the portal
* @param {string} entryLabelText - Text shown on entry side (destination name)
* @param {string} exitLabelText - Text shown on exit side (source name)
* @param {boolean} enabled - Whether to create labels (default true)
*/
createLabels(entryLabelText, exitLabelText, enabled = true) {
if (!enabled) {
return;
}
// Get positions and rotations from the portal pair
const entryPos = this.pair.entryPortal.position;
const entryRot = this.pair.entryPortal.quaternion;
const exitPos = this.pair.exitPortal.position;
const exitRot = this.pair.exitPortal.quaternion;
// Create entry label (shows where portal leads to)
this.entryLabel = this._createTextSplat(entryLabelText, entryPos, entryRot);
// Create exit label (shows where you came from)
this.exitLabel = this._createTextSplat(exitLabelText, exitPos, exitRot);
}
_createTextSplat(text, position, quaternion) {
const textMesh = textSplats({
text: text,
font: "Arial",
fontSize: 60,
color: new THREE.Color(0xffffff),
});
// Scale to appropriate size (reduced by 50%)
textMesh.scale.setScalar(0.25 / 80);
// Position above portal
textMesh.position.copy(position);
textMesh.position.y += 1.3;
// Apply same rotation as portal
textMesh.quaternion.copy(quaternion);
this.scene.add(textMesh);
return textMesh;
}
updateLabelRotation(time) {
const rotationSpeed = 0.0005; // radians per millisecond
if (this.entryLabel) {
this.entryLabel.rotation.y = time * rotationSpeed;
}
if (this.exitLabel) {
this.exitLabel.rotation.y = time * rotationSpeed;
}
}
/**
* Create rings on both sides of the portal
* @param {number} radius - Radius of the rings (default 1.0)
*/
createRings(radius = 1.0) {
// Get positions and rotations from the portal pair
const entryPos = this.pair.entryPortal.position;
const entryRot = this.pair.entryPortal.quaternion;
const exitPos = this.pair.exitPortal.position;
const exitRot = this.pair.exitPortal.quaternion;
// Create entry ring
this.entryRing = this._createRing(entryPos, entryRot, radius);
// Create exit ring
this.exitRing = this._createRing(exitPos, exitRot, radius);
}
_createRing(position, quaternion, radius) {
// Create a ring using procedural splats
const sparkRing = new SparkRing({
radius: radius,
tubeRadius: 0.05,
radialSegments: 64,
tubularSegments: 16,
color: new THREE.Color(0xffd700), // Gold color
opacity: 1.0
});
const ringMesh = sparkRing.getMesh();
// Position at portal location
ringMesh.position.copy(position);
// Apply portal rotation
ringMesh.quaternion.copy(quaternion);
this.scene.add(ringMesh);
return sparkRing;
}
/**
* Create disks on both sides of the portal (for VR mode)
* Disks are created hidden and should be shown when in VR
* @param {number} radius - Radius of the disks (default 1.0)
* @param {string} effect - Effect type from PortalEffects (default 'swirl')
*/
createDisks(radius = 1.0, effect = PortalEffects.SWIRL) {
// Store the effect type for later reference
this.diskEffect = effect;
// Get positions and rotations from the portal pair
const entryPos = this.pair.entryPortal.position;
const entryRot = this.pair.entryPortal.quaternion;
const exitPos = this.pair.exitPortal.position;
const exitRot = this.pair.exitPortal.quaternion;
// Create entry disk
this.entryDisk = this._createDisk(entryPos, entryRot, radius, effect);
// Create exit disk
this.exitDisk = this._createDisk(exitPos, exitRot, radius, effect);
// Start hidden (only shown in VR mode)
this.setDisksVisible(false);
}
_createDisk(position, quaternion, radius, effect) {
// Create a disk using procedural splats
const sparkDisk = new SparkDisk({
radius: radius,
radialSegments: 32,
concentricRings: 16,
color: new THREE.Color(0x000000), // Black color
opacity: 1.0,
effect: effect
});
const diskMesh = sparkDisk.getMesh();
// Position at portal location
diskMesh.position.copy(position);
// Apply portal rotation
diskMesh.quaternion.copy(quaternion);
this.scene.add(diskMesh);
return sparkDisk;
}
/**
* Change the effect type for both disks
* @param {string} effect - Effect type from PortalEffects
*/
setDiskEffect(effect) {
this.diskEffect = effect;
if (this.entryDisk) {
this.entryDisk.setEffect(effect);
}
if (this.exitDisk) {
this.exitDisk.setEffect(effect);
}
}
/**
* Show or hide the VR disks
* @param {boolean} visible
*/
setDisksVisible(visible) {
if (this.entryDisk) {
this.entryDisk.setVisible(visible);
}
if (this.exitDisk) {
this.exitDisk.setVisible(visible);
}
}
/**
* Update disk animations - call every frame when disks are visible
*/
updateDisks() {
if (this.entryDisk) {
this.entryDisk.update();
}
if (this.exitDisk) {
this.exitDisk.update();
}
}
dispose() {
// Remove portal pair
if (this.pair && this.portals) {
this.portals.removePortalPair(this.pair);
}
// Remove and dispose entry label
if (this.entryLabel) {
this.scene.remove(this.entryLabel);
if (this.entryLabel.dispose) {
this.entryLabel.dispose();
}
this.entryLabel = null;
}
// Remove and dispose exit label
if (this.exitLabel) {
this.scene.remove(this.exitLabel);
if (this.exitLabel.dispose) {
this.exitLabel.dispose();
}
this.exitLabel = null;
}
// Remove and dispose entry ring
if (this.entryRing) {
const ringMesh = this.entryRing.getMesh();
this.scene.remove(ringMesh);
this.entryRing.dispose();
this.entryRing = null;
}
// Remove and dispose exit ring
if (this.exitRing) {
const ringMesh = this.exitRing.getMesh();
this.scene.remove(ringMesh);
this.exitRing.dispose();
this.exitRing = null;
}
// Remove and dispose entry disk
if (this.entryDisk) {
const diskMesh = this.entryDisk.getMesh();
this.scene.remove(diskMesh);
this.entryDisk.dispose();
this.entryDisk = null;
}
// Remove and dispose exit disk
if (this.exitDisk) {
const diskMesh = this.exitDisk.getMesh();
this.scene.remove(diskMesh);
this.exitDisk.dispose();
this.exitDisk = null;
}
}
}
// Setup lighting for portal materials
export function setupPortalLighting(scene, camera) {
// Add lighting for metallic materials
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
const pointLight = new THREE.PointLight(0xffffff, 1, 100);
camera.add(pointLight); // Light follows camera
}