-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
executable file
·253 lines (229 loc) · 7.33 KB
/
index.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
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
/* global AFRAME, THREE */
if (typeof AFRAME === "undefined") {
throw new Error(
"Component attempted to register before AFRAME was available."
);
}
require("./lib/terrainloader.js");
/**
* Terrain model component.
*/
AFRAME.registerComponent("terrain-model", {
schema: {
planeHeight: {
type: "number",
default: 346,
},
planeWidth: {
type: "number",
default: 346,
},
segmentsHeight: {
type: "number",
default: 199,
},
segmentsWidth: {
type: "number",
default: 199,
},
zPosition: {
type: "number",
default: 1.5,
},
dem: {
type: "asset",
},
map: {
type: "asset",
},
alphaMap: {
type: "asset",
},
wireframe: {
type: "boolean",
default: false,
},
},
init: function () {
const el = this.el;
const data = this.data;
this.heightData = null;
this.terrainLoader = new THREE.TerrainLoader();
this.textureLoader = new THREE.TextureLoader();
this._replaceTexture = this._replaceTexture.bind(this);
this._updatePositionBuffer = this._updatePositionBuffer.bind(this);
this._toggleWireframe = this._toggleWireframe.bind(this);
// Setup geometry.
const { planeWidth, planeHeight, segmentsHeight, segmentsWidth } = data;
this.geometry = new THREE.PlaneBufferGeometry(
planeWidth,
planeHeight,
segmentsWidth,
segmentsHeight
);
// Setup material.
this.material = new THREE.MeshLambertMaterial();
// Create the terrain mesh; rotate it to be parallel with the ground.
this.mesh = new THREE.Mesh(this.geometry, this.material);
this.mesh.rotation.x = -90 * (Math.PI / 180);
el.setObject3D("terrain", this.mesh);
},
update: function (oldData) {
const data = this.data;
const dem = data.dem;
const map = data.map;
const alphaMap = data.alphaMap;
const zPosition = data.zPosition;
const wireframe = data.wireframe;
/**
* Callback for THREE.TerrainLoader().
* @param {number[]} heightData
*/
function onTerrainLoad(heightData) {
this.heightData = heightData;
this._updatePositionBuffer();
this.el.emit("demLoaded", { dem });
}
/**
* This is a curried function that returns a callback to pass to THREE.TextureLoader.load()
* This callback updates the terrain material as necessary. 'map' and 'alphaMap'
* texture loads are handled almost identically.
* @param {string} materialProp either 'map' (for this.material.map) or 'alphaMap' (for this.material.alphaMap)
* @returns {function}
*/
const curriedOnTextureLoad = (materialProp) => {
return (loadedTexture) => {
if (materialProp === "map" && this.data.map !== map) {
// The texture took too long to load. The entity now has a different
// map, so the map that was loaded cannot be used.
loadedTexture.dispose();
return;
}
if (materialProp === "alphaMap" && this.data.alphaMap !== alphaMap) {
// Same idea as above, except the loaded texture is an alphaMap.
loadedTexture.dispose();
return;
}
loadedTexture.anisotropy = 16;
this._replaceTexture(materialProp, loadedTexture);
this.el.emit("textureLoaded", { type: materialProp });
};
};
if (dem !== oldData.dem) {
// DEM has updated, so load the new one.
this.terrainLoader.load(dem, onTerrainLoad.bind(this));
}
if (map !== oldData.map) {
// When map is not set, it defaults to ""
if (!map) {
this._replaceTexture("map", null);
} else {
// Texture has updated, so load the new one.
this.textureLoader.load(map, curriedOnTextureLoad("map"));
}
}
if (alphaMap !== oldData.alphaMap) {
// When alphaMap is not set, it defaults to ""
if (!alphaMap) {
this._replaceTexture("alphaMap", null);
} else {
// Alpha map has updated, so load the new one.
// Also turn on material transparency.
this.material.transparent = true;
this.textureLoader.load(alphaMap, curriedOnTextureLoad("alphaMap"));
}
}
if (zPosition !== oldData.zPosition) {
// zPosition has changed, so re-scale the mesh.
this.mesh.scale.set(1, 1, zPosition);
}
if (wireframe !== oldData.wireframe) {
// wire mode has either been activated or disactivated.
this._toggleWireframe();
}
},
/**
* A helper for swapping out a material texture.
* Ensures the old texture is disposed.
* @param {string} materialProp e.g. 'map' or 'alphaMap'
* @param {(THREE.Texture|null)} newTexture
*/
_replaceTexture: function (materialProp, newTexture) {
const oldTexture = this.material[materialProp];
this.material[materialProp] = newTexture;
this.material.needsUpdate = true;
if (oldTexture) {
oldTexture.dispose();
}
},
/**
* Sets the z-component of every vector in the position attribute buffer
* to the (adjusted) height value from the DEM. Also adjusts the wireframe.
* Note:
* positionBuffer.count === the number of vertices in the plane
*/
_updatePositionBuffer: function () {
if (!this.heightData) {
return;
}
let positionBuffer = this.geometry.getAttribute("position");
for (let i = 0; i < positionBuffer.count; i++) {
let heightValue = this.heightData[i] / 65535;
positionBuffer.setZ(i, heightValue);
}
positionBuffer.needsUpdate = true;
// Update wireframe
let oldWireMesh = this.mesh.getObjectByName("terrain-wireframe");
if (oldWireMesh) {
oldWireMesh.geometry.dispose();
const wireGeometry = new THREE.WireframeGeometry(this.geometry);
oldWireMesh.geometry = wireGeometry;
}
this.el.emit("positionBufferUpdated");
},
/**
* Creates or destroys a terrain wireframe mesh.
* A wireframe can be useful for debugging or for visualizing a terrain DEM
* without a texture.
*/
_toggleWireframe: function () {
// TODO: Consider creating wireframe on init and hiding/revealing rather
// than creating the wireframe mesh on demand. Cons: wastes resources for
// people who don't care about wireframe. Pros: less lag when wireframe is
// turned on.
let oldWireMesh = this.mesh.getObjectByName("terrain-wireframe");
if (!oldWireMesh) {
// This is a somewhat inelegant way to prevent adding a wireframe when the
// component initializes.
if (!this.data.wireframe) {
return;
}
// Add wireframe.
const wireGeometry = new THREE.WireframeGeometry(this.geometry);
const wireMaterial = new THREE.LineBasicMaterial({
color: 0x808080,
linewidth: 1,
});
let wireMesh = new THREE.LineSegments(wireGeometry, wireMaterial);
wireMesh.name = "terrain-wireframe";
wireMesh.material.opacity = 0.3;
wireMesh.material.transparent = true;
this.mesh.add(wireMesh);
return;
}
// Remove wireframe
oldWireMesh.geometry.dispose();
oldWireMesh.material.dispose();
this.mesh.remove(oldWireMesh);
},
remove: function () {
this.geometry.dispose();
this.material.map.dispose();
this.material.alphaMap.dispose();
this.material.dispose();
if (this.data.wireframe) {
this._toggleWireframe();
}
this.el.removeObject3D("terrain");
},
});