forked from JeremyHeleine/Photo-Sphere-Viewer
-
-
Notifications
You must be signed in to change notification settings - Fork 706
/
Copy pathEquirectangularAdapter.ts
268 lines (222 loc) · 9.2 KB
/
EquirectangularAdapter.ts
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
import { MathUtils, Mesh, MeshBasicMaterial, SphereGeometry, Texture } from 'three';
import { PSVError } from '../PSVError';
import type { Viewer } from '../Viewer';
import { SPHERE_RADIUS } from '../data/constants';
import { SYSTEM } from '../data/system';
import { EquirectangularPanorama, PanoData, PanoDataProvider, PanoramaPosition, Position, TextureData } from '../model';
import { createTexture, getConfigParser, getXMPValue, isNil, mergePanoData } from '../utils';
import { AbstractAdapter } from './AbstractAdapter';
/**
* Configuration for {@link EquirectangularAdapter}
*/
export type EquirectangularAdapterConfig = {
/**
* number of faces of the sphere geometry, higher values may decrease performances
* @default 64
*/
resolution?: number;
/**
* read real image size from XMP data
* @default true
*/
useXmpData?: boolean;
/**
* used for equirectangular tiles adapter
* @internal
*/
blur?: boolean;
};
export type EquirectangularMesh = Mesh<SphereGeometry, MeshBasicMaterial>;
export type EquirectangularTextureData = TextureData<Texture, string | EquirectangularPanorama, PanoData>;
const getConfig = getConfigParser<EquirectangularAdapterConfig>(
{
resolution: 64,
useXmpData: true,
blur: false,
},
{
resolution: (resolution) => {
if (!resolution || !MathUtils.isPowerOfTwo(resolution)) {
throw new PSVError('EquirectangularAdapter resolution must be power of two.');
}
return resolution;
},
},
);
/**
* Adapter for equirectangular panoramas
*/
export class EquirectangularAdapter extends AbstractAdapter<string | EquirectangularPanorama, PanoData, Texture, EquirectangularMesh> {
static override readonly id: string = 'equirectangular';
static override readonly VERSION = PKG_VERSION;
static override readonly supportsDownload: boolean = true;
private readonly config: EquirectangularAdapterConfig;
// @internal
public readonly SPHERE_SEGMENTS: number;
// @internal
public readonly SPHERE_HORIZONTAL_SEGMENTS: number;
constructor(viewer: Viewer, config?: EquirectangularAdapterConfig) {
super(viewer);
this.config = getConfig(config);
this.SPHERE_SEGMENTS = this.config.resolution;
this.SPHERE_HORIZONTAL_SEGMENTS = this.SPHERE_SEGMENTS / 2;
}
override supportsTransition() {
return true;
}
override supportsPreload() {
return true;
}
override textureCoordsToSphericalCoords(point: PanoramaPosition, data: PanoData): Position {
if (isNil(point.textureX) || isNil(point.textureY)) {
throw new PSVError(`Texture position is missing 'textureX' or 'textureY'`);
}
const relativeX = ((point.textureX + data.croppedX) / data.fullWidth) * Math.PI * 2;
const relativeY = ((point.textureY + data.croppedY) / data.fullHeight) * Math.PI;
return {
yaw: relativeX >= Math.PI ? relativeX - Math.PI : relativeX + Math.PI,
pitch: Math.PI / 2 - relativeY,
};
}
override sphericalCoordsToTextureCoords(position: Position, data: PanoData): PanoramaPosition {
const relativeLong = (position.yaw / Math.PI / 2) * data.fullWidth;
const relativeLat = (position.pitch / Math.PI) * data.fullHeight;
let textureX = Math.round(position.yaw < Math.PI ? relativeLong + data.fullWidth / 2 : relativeLong - data.fullWidth / 2) - data.croppedX;
let textureY = Math.round(data.fullHeight / 2 - relativeLat) - data.croppedY;
if (textureX < 0 || textureX > data.croppedWidth || textureY < 0 || textureY > data.croppedHeight) {
textureX = textureY = undefined;
}
return { textureX, textureY };
}
async loadTexture(
panorama: string | EquirectangularPanorama,
loader = true,
newPanoData?: PanoData | PanoDataProvider,
useXmpPanoData = this.config.useXmpData,
): Promise<EquirectangularTextureData> {
if (typeof panorama !== 'string' && (typeof panorama !== 'object' || !panorama.path)) {
return Promise.reject(new PSVError('Invalid panorama url, are you using the right adapter?'));
}
let cleanPanorama: EquirectangularPanorama;
if (typeof panorama === 'string') {
cleanPanorama = {
path: panorama,
data: newPanoData,
};
} else {
cleanPanorama = {
data: newPanoData,
...panorama,
};
}
const blob = await this.viewer.textureLoader.loadFile(
cleanPanorama.path,
loader ? p => this.viewer.loader.setProgress(p) : null,
cleanPanorama.path,
);
const xmpPanoData = useXmpPanoData ? await this.loadXMP(blob) : null;
const img = await this.viewer.textureLoader.blobToImage(blob);
if (typeof cleanPanorama.data === 'function') {
cleanPanorama.data = cleanPanorama.data(img, xmpPanoData);
}
const panoData = mergePanoData(img.width, img.height, cleanPanorama.data, xmpPanoData);
const texture = this.createEquirectangularTexture(img);
return {
panorama,
texture,
panoData,
cacheKey: cleanPanorama.path,
};
}
/**
* Loads the XMP data of an image
*/
private async loadXMP(blob: Blob): Promise<PanoData> {
const binary = await this.loadBlobAsString(blob);
const a = binary.indexOf('<x:xmpmeta');
if (a === -1) {
return null;
}
const b = binary.indexOf('</x:xmpmeta>', a);
if (b === -1) {
return null;
}
const data = binary.substring(a, b);
if (!data.includes('GPano:')) {
return null;
}
return {
fullWidth: getXMPValue(data, 'FullPanoWidthPixels'),
fullHeight: getXMPValue(data, 'FullPanoHeightPixels'),
croppedWidth: getXMPValue(data, 'CroppedAreaImageWidthPixels'),
croppedHeight: getXMPValue(data, 'CroppedAreaImageHeightPixels'),
croppedX: getXMPValue(data, 'CroppedAreaLeftPixels'),
croppedY: getXMPValue(data, 'CroppedAreaTopPixels'),
poseHeading: getXMPValue(data, 'PoseHeadingDegrees', false),
posePitch: getXMPValue(data, 'PosePitchDegrees', false),
poseRoll: getXMPValue(data, 'PoseRollDegrees', false),
initialHeading: getXMPValue(data, 'InitialViewHeadingDegrees', false),
initialPitch: getXMPValue(data, 'InitialViewPitchDegrees', false),
initialFov: getXMPValue(data, 'InitialHorizontalFOVDegrees', false),
};
}
/**
* Reads a Blob as a string
*/
private loadBlobAsString(blob: Blob): Promise<string> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result as string);
reader.onerror = reject;
reader.readAsText(blob);
});
}
/**
* Creates the final texture from image and panorama data
*/
private createEquirectangularTexture(img: HTMLImageElement): Texture {
if (this.config.blur || img.width > SYSTEM.maxTextureWidth) {
const ratio = Math.min(1, SYSTEM.maxCanvasWidth / img.width);
const buffer = new OffscreenCanvas(Math.floor(img.width * ratio), Math.floor(img.height * ratio));
const ctx = buffer.getContext('2d');
if (this.config.blur) {
ctx.filter = `blur(${buffer.width / 2048}px)`;
}
ctx.drawImage(img, 0, 0, buffer.width, buffer.height);
return createTexture(buffer);
}
return createTexture(img);
}
createMesh(panoData: PanoData): EquirectangularMesh {
const hStart = (panoData.croppedX / panoData.fullWidth) * 2 * Math.PI;
const hLength = (panoData.croppedWidth / panoData.fullWidth) * 2 * Math.PI;
const vStart = (panoData.croppedY / panoData.fullHeight) * Math.PI;
const vLength = (panoData.croppedHeight / panoData.fullHeight) * Math.PI;
// The middle of the panorama is placed at yaw=0
const geometry = new SphereGeometry(
SPHERE_RADIUS,
Math.round((this.SPHERE_SEGMENTS / (2 * Math.PI)) * hLength),
Math.round((this.SPHERE_HORIZONTAL_SEGMENTS / Math.PI) * vLength),
-Math.PI / 2 + hStart,
hLength,
vStart,
vLength,
).scale(-1, 1, 1);
const material = new MeshBasicMaterial({ depthTest: false, depthWrite: false });
return new Mesh(geometry, material);
}
setTexture(mesh: EquirectangularMesh, textureData: EquirectangularTextureData) {
mesh.material.map = textureData.texture;
}
setTextureOpacity(mesh: EquirectangularMesh, opacity: number) {
mesh.material.opacity = opacity;
mesh.material.transparent = opacity < 1;
}
disposeTexture({ texture }: EquirectangularTextureData) {
texture.dispose();
}
disposeMesh(mesh: EquirectangularMesh) {
mesh.geometry.dispose();
mesh.material.dispose();
}
}