-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyTrack.js
155 lines (120 loc) · 4.51 KB
/
MyTrack.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
import * as THREE from 'three';
import {MyContentBuilder} from './MyContentBuilder.js';
/**
* This class contains the contents of out application
*/
class MyTrack {
/**
constructs the object
@param {MyApp} app The application object
*/
constructor(app) {
this.app = app
this.builder = new MyContentBuilder(this.app);
this.track1 = {
segments: 100,
width: 1.9,
scale: [1,0.1,1],
textureRepeat: 1,
showWireframe: false,
showMesh: true,
showLine: true,
closedCurve: true,
path: [
new THREE.Vector3(-8, 0, 5),
new THREE.Vector3(-8, 0, -6),
new THREE.Vector3(-8, 0, -8),
new THREE.Vector3(-6, 0, -10),
new THREE.Vector3(2, 0, -10),
new THREE.Vector3(4, 0, -8),
new THREE.Vector3(4, 0, -6),
new THREE.Vector3(2, 0, -4),
new THREE.Vector3(0, 0, -2),
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(2, 0, 2),
new THREE.Vector3(4, 0, 2),
new THREE.Vector3(6, 0, 4),
new THREE.Vector3(6, 0, 8),
new THREE.Vector3(4, 0, 10),
new THREE.Vector3(2, 0, 12),
new THREE.Vector3(-4, 0, 12),
new THREE.Vector3(-8, 0, 10),
new THREE.Vector3(-8, 0, 5),
]
};
}
/**
* initializes the contents
*/
init() {
}
buildTrack() {
let track = this.track1;
// Textured material
const trackTex = new THREE.TextureLoader().load("./scenes/t01g03/textures/track.jpg");
let trackMaterial = new THREE.MeshStandardMaterial({
map: trackTex,
});
trackMaterial.map.repeat.set(6, 3);
trackMaterial.map.wrapS = THREE.RepeatWrapping;
trackMaterial.map.wrapT = THREE.RepeatWrapping;
// Adjust the emissive color for additional visibility
trackMaterial.emissive.setHex(0x111111);
this.curve = this.buildCurve(track, trackMaterial);
this.app.scene.add(this.curve); // show track in the scene
const startTex = new THREE.TextureLoader().load("./scenes/t01g03/textures/start.jpg");
let startMaterial = new THREE.MeshStandardMaterial({ map: startTex });
// Adjust the emissive color for additional visibility
startMaterial.emissive.setHex(0x111111);
let plane = new THREE.PlaneGeometry(1, 3);
let startMesh = new THREE.Mesh(plane, startMaterial)
startMesh.rotation.set(-Math.PI/2,0,Math.PI/2);
startMesh.position.set(8,0.1,3.5);
startMesh.name = "track";
this.app.scene.add(startMesh); // show start in the scene track
return track;
}
buildCurve(track, material) {
let materials = this.createCurveMaterialsTextures(material);
return this.createCurveObjects(track, materials);
}
createCurveMaterialsTextures(material) {
let wireframeMaterial = new THREE.MeshBasicMaterial({
color: 0x0000ff,
opacity: 0.3,
wireframe: true,
transparent: true,
});
let lineMaterial = new THREE.LineBasicMaterial({ color: 0xff0000 });
return { material: material, wireframeMaterial: wireframeMaterial, lineMaterial: lineMaterial};
}
createCurveObjects(track, materials) {
let catmull = new THREE.CatmullRomCurve3(track.path);
console.log(track.path)
let geometry = new THREE.TubeGeometry(
catmull,
track.segments,
track.width,
3,
track.closedCurve
);
let mesh = new THREE.Mesh(geometry, materials.material);
let wireframe = new THREE.Mesh(geometry, materials.wireframeMaterial);
let sampledPoints = catmull.getPoints(track.segments);
let bGeometry = new THREE.BufferGeometry().setFromPoints(sampledPoints);
// Create the final object to add to the scene
let curve_line_obj = new THREE.Line(bGeometry, materials.lineMaterial);
let curve = new THREE.Group();
mesh.visible = track.showMesh;
mesh.name = "track";
wireframe.visible = track.showWireframe;
curve_line_obj.visible = track.showLine;
curve.add(mesh);
curve.add(wireframe);
curve.add(curve_line_obj);
curve.scale.set(track.scale[0], track.scale[1], track.scale[2]);
curve.rotateZ(Math.PI)
return curve;
}
}
export {MyTrack};