-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzine-utils.js
156 lines (137 loc) · 3.95 KB
/
zine-utils.js
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
import * as THREE from 'three';
import {GLTFLoader} from 'three/examples/jsm/loaders/GLTFLoader.js';
import {GLTFExporter} from 'three/examples/jsm/exporters/GLTFExporter.js';
import {DRACOLoader} from 'three/examples/jsm/loaders/DRACOLoader.js';
import {
defaultCameraFov,
floorNetWorldSize,
floorNetWorldDepth,
} from './zine-constants.js';
//
export const makePromise = () => {
let resolve = null;
let reject = null;
const promise = new Promise((a, b) => {
resolve = a;
reject = b;
});
promise.resolve = resolve;
promise.reject = reject;
return promise;
}
//
export const makeRenderer = canvas => {
const renderer = new THREE.WebGLRenderer({
canvas,
alpha: true,
antialias: true,
preserveDrawingBuffer: true,
});
renderer.sortObjects = false;
renderer.physicallyCorrectLights = true;
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.setClearColor(0x000000, 0);
return renderer;
};
//
export const makeGltfLoader = () => {
const gltfLoader = new GLTFLoader();
const dracoLoader = new DRACOLoader();
dracoLoader.setDecoderPath('/three/draco/');
gltfLoader.setDRACOLoader(dracoLoader);
return gltfLoader;
};
//
export const makeGltfExporter = () => {
const gltfExporter = new GLTFExporter();
return gltfExporter;
};
//
export const makeDefaultCamera = () => new THREE.PerspectiveCamera(defaultCameraFov, 1, 0.1, 1000);
export const makeFloorNetCamera = () => {
const floorNetCamera = new THREE.OrthographicCamera(
-floorNetWorldSize / 2,
floorNetWorldSize / 2,
floorNetWorldSize / 2,
-floorNetWorldSize / 2,
0,
floorNetWorldDepth
);
floorNetCamera.position.set(0, -floorNetWorldDepth/2, 0);
floorNetCamera.quaternion.setFromAxisAngle(new THREE.Vector3(1, 0, 0), -Math.PI/2)
.multiply(
new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 1, 0), Math.PI)
);
floorNetCamera.updateMatrixWorld();
return floorNetCamera;
};
export const makeMapIndexCamera = () => {
const floorNetCamera = new THREE.OrthographicCamera(
-floorNetWorldSize / 2,
floorNetWorldSize / 2,
floorNetWorldSize / 2,
-floorNetWorldSize / 2,
0,
floorNetWorldDepth
);
floorNetCamera.position.set(0, -floorNetWorldDepth/2, 0);
floorNetCamera.quaternion.setFromAxisAngle(new THREE.Vector3(1, 0, 0), -Math.PI/2)
.multiply(
new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 1, 0), Math.PI)
);
floorNetCamera.updateMatrixWorld();
return floorNetCamera;
};
//
export const pushMeshes = (scene, meshes, options = {}) => {
const originalSpecs = meshes.map(mesh => {
const {parent, frustumCulled} = mesh;
scene.add(mesh);
if (options.frustumCulled !== undefined) {
mesh.frustumCulled = options.frustumCulled;
}
return {
parent,
frustumCulled,
};
});
return () => {
for (let i = 0; i < meshes.length; i++) {
const mesh = meshes[i];
const originalSpec = originalSpecs[i];
if (originalSpec.parent) {
originalSpec.parent.add(mesh);
} else {
scene.remove(mesh);
}
mesh.frustumCulled = originalSpec.frustumCulled;
}
};
};
//
export const normalToQuaternion = (() => {
const localVector = new THREE.Vector3();
// const localVector2 = new THREE.Vector3();
const localMatrix = new THREE.Matrix4();
return (normal, quaternion, up) => {
return quaternion.setFromRotationMatrix(
localMatrix.lookAt(
normal,
localVector.set(0, 0, 0),
up
)
);
};
})();
//
export function range(value, min, max) {
return Math.min(Math.max(value, min), max);
}
//
export const align = (v, N) => {
const r = v % N;
return r === 0 ? v : v - r + N;
};
export const align4 = v => align(v, 4);