-
Notifications
You must be signed in to change notification settings - Fork 16
/
Track.kt
391 lines (342 loc) · 13.3 KB
/
Track.kt
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package de.fabmax.kool.demo.physics.vehicle
import de.fabmax.kool.math.*
import de.fabmax.kool.math.spatial.KdTree
import de.fabmax.kool.math.spatial.NearestTraverser
import de.fabmax.kool.math.spatial.pointKdTree
import de.fabmax.kool.physics.RigidStatic
import de.fabmax.kool.pipeline.*
import de.fabmax.kool.pipeline.deferred.deferredKslPbrShader
import de.fabmax.kool.scene.Mesh
import de.fabmax.kool.scene.Node
import de.fabmax.kool.scene.geometry.IndexedVertexList
import de.fabmax.kool.scene.geometry.MeshBuilder
import de.fabmax.kool.scene.geometry.multiShape
import de.fabmax.kool.scene.geometry.simpleShape
import de.fabmax.kool.util.*
import kotlin.math.*
import kotlin.random.Random
class Track(val world: VehicleWorld) : Node() {
private val spline = SimpleSpline3f()
private val numSamples = mutableListOf<Int>()
private val trackPoints = mutableListOf<TrackPoint>()
private val trackPointsMap = TreeMap<Float, TrackPoint>()
private var trackPointTree: KdTree<Vec3f>? = null
private val nearestTrav = NearestTraverser<Vec3f>()
private val guardRails = mutableListOf<GuardRailSection>()
var subdivs = 1
var columnDist = 64.5f
var curbLen = 2.511f
val trackMesh = Mesh(Attribute.POSITIONS, Attribute.NORMALS, Attribute.TEXTURE_COORDS, Attribute.TANGENTS)
val trackSupportMesh = Mesh(Attribute.POSITIONS, Attribute.NORMALS, Attribute.COLORS, ATTRIBUTE_ROUGHNESS)
lateinit var trackActor: RigidStatic
private set
val guardRail = GuardRail()
init {
isFrustumChecked = false
addNode(trackMesh)
addNode(trackSupportMesh)
addNode(guardRail.guardRailMesh)
}
fun addControlPoint(ctrlPt: SimpleSpline3f.CtrlPoint, numSamples: Int = 20) {
if (spline.ctrlPoints.isNotEmpty()) {
this.numSamples += numSamples
}
spline.ctrlPoints += ctrlPt
}
fun addGuardRailSection(from: Float, to: Float, isLeft: Boolean) {
guardRails += GuardRailSection(from, to, isLeft)
}
fun distanceToTrack(point: Vec3f): Float {
val tree = trackPointTree ?: return -1f
nearestTrav.setup(point).traverse(tree)
return sqrt(nearestTrav.sqrDist).toFloat()
}
fun cleanUp() {
guardRail.cleanUp()
}
private fun computeFrameAt(pos: Float, result: MutableMat4f) {
val prevKey = trackPointsMap.lowerKey(pos) ?: trackPointsMap.firstKey()
val nextKey = trackPointsMap.higherKey(pos) ?: trackPointsMap.lastKey()
val prev = trackPointsMap[prevKey]!!
val next = trackPointsMap[nextKey]!!
val wPrev = if (nextKey == prevKey) {
1f
} else {
(nextKey - pos) / (nextKey - prevKey)
}
val wNext = 1f - wPrev
val vec = MutableVec3f(prev).mul(wPrev).add(MutableVec3f(next).mul(wNext))
val up = MutableVec3f(prev.up).mul(wPrev).add(MutableVec3f(next.up).mul(wNext))
val z = MutableVec3f(prev.dir).mul(wPrev).add(MutableVec3f(next.dir).mul(wNext))
val x = MutableVec3f(z).apply { y = 0f }
x.rotate(90f.deg, up).norm()
val y = z.cross(x, MutableVec3f()).norm()
result.setColumn(0, x, 0f)
result.setColumn(1, y, 0f)
result.setColumn(2, z, 0f)
result.setColumn(3, vec, 1f)
}
private fun sampleTrack() {
trackPoints.clear()
var pos = 0f
for (i in 0 .. (spline.ctrlPoints.size - 2)) {
val samples = numSamples[i] * subdivs
for (j in 0 until samples) {
val pt = spline.evaluate(i + j / samples.toFloat(), MutableVec3f())
if (trackPoints.isNotEmpty()) {
pos += pt.distance(trackPoints.last())
}
val trackPt = TrackPoint(pt, pos, trackPoints.size)
trackPoints += trackPt
trackPointsMap[trackPt.pos] = trackPt
}
}
for (i in trackPoints.indices) {
val prev = if (i == 0) trackPoints.last() else trackPoints[i-1]
val pt = trackPoints[i % trackPoints.size]
val next = trackPoints[(i + 1) % trackPoints.size]
next.subtract(prev, pt.dir).norm()
}
trackPointTree = pointKdTree(trackPoints)
}
private fun build() {
sampleTrack()
val columnPts = mutableListOf<Vec4f>()
var columnL = columnDist
trackMesh.generate {
var texU = 0f
vertexModFun = {
val texScale = 25f
texCoord.x = texU
texCoord.mul(1f / texScale)
}
color = MdColor.ORANGE toneLin 100
profile {
multiShape {
simpleShape(false) {
xy(7.5f, 0f)
xy(2.5f, 0f)
xy(-2.5f, 0f)
xy(-7.5f, 0f)
uv(0f, 7.5f)
uv(0f, 2.5f)
uv(0f, -2.5f)
uv(0f, -7.5f)
}
simpleShape(false) {
xy(-7.5f, 0f)
xy(-7.5f, -1f)
uv(0f, 0f)
uv(0f, -1f)
}
simpleShape(false) {
xy(-7.5f, -1f)
xy(7.5f, -1f)
uv(0f, -7.5f)
uv(0f, 7.5f)
}
simpleShape(false) {
xy(7.5f, -1f)
xy(7.5f, 0f)
uv(0f, -1f)
uv(0f, 0f)
}
}
withTransform {
for (i in 0 .. trackPoints.size) {
val prev = if (i == 0) trackPoints.last() else trackPoints[i-1]
val pt = trackPoints[i % trackPoints.size]
pt.computeFrame(transform)
val adv = pt.distance(prev)
texU += adv
sample()
columnL -= adv
if (columnL < 0 && pt.y > 5f) {
val ang = atan2(pt.dir.x, pt.dir.z).toDeg()
columnPts += Vec4f(pt, ang)
columnL = columnDist
}
}
}
geometry.generateNormals()
geometry.generateTangents()
}
}
trackSupportMesh.generate {
var roughness = 0.3f
vertexModFun = {
getFloatAttribute(ATTRIBUTE_ROUGHNESS)?.f = roughness
}
generateCurbs()
roughness = 0.8f
color = VehicleDemo.color(400)
columnPts.forEach { pt ->
val base = MutableVec3f(pt.x, 0f, pt.z)
generateColumn(base, pt.y - 0.7f, pt.w)
}
geometry.generateNormals()
}
val collisionMesh = IndexedVertexList(Attribute.POSITIONS)
collisionMesh.addGeometry(trackMesh.geometry)
collisionMesh.addGeometry(trackSupportMesh.geometry)
trackActor = world.addStaticCollisionBody(collisionMesh)
buildGuardRails()
}
private fun buildGuardRails() {
guardRails.forEach { guardRail ->
sampleGuardRailTrack(guardRail)
}
}
private fun sampleGuardRailTrack(guardRailSec: GuardRailSection) {
val steps = ((guardRailSec.to - guardRailSec.from) / 3f).roundToInt()
val step = (guardRailSec.to - guardRailSec.from) / steps
val frame = MutableMat4f()
for (i in 0 .. steps) {
computeFrameAt(guardRailSec.from + step * i, frame)
val leftRightSign = if (guardRailSec.isLeft) { -1f } else { 1f }
frame.translate(-7.65f * leftRightSign, 0f, 0f)
.translate(0f, 1.5f, 0f)
.rotate((90f * leftRightSign).deg, Vec3f.Y_AXIS)
guardRail.signs += GuardRail.SignInstance(guardRail.signs.size, guardRailSec.isLeft, frame, this, world)
}
}
private fun MeshBuilder.generateCurbs() {
val curbColors = listOf(VehicleDemo.color(150), VehicleDemo.color(600))
color = curbColors[0]
withTransform {
profile {
multiShape {
simpleShape(false) {
xy(6.7f, -1.0f)
xy(6.75f, -1.05f)
xy(7.6f, -1.05f)
xy(7.65f, -1.0f)
xy(7.65f, 0.0f)
xy(7.6f, 0.05f)
xy(6.75f, 0.05f)
xy(6.7f, 0.0f)
}
simpleShape(false) {
xy(-6.7f, 0.0f)
xy(-6.75f, 0.05f)
xy(-7.6f, 0.05f)
xy(-7.65f, 0.0f)
xy(-7.65f, -1.0f)
xy(-7.6f, -1.05f)
xy(-6.75f, -1.05f)
xy(-6.7f, -1.0f)
}
}
var pos = 0f
var iColor = 0
while (pos < trackPoints.last().pos) {
iColor = (iColor + 1) % curbColors.size
color = curbColors[iColor]
computeFrameAt(pos, transform)
sample(connect = false)
pos += curbLen
computeFrameAt(pos, transform)
sample()
}
computeFrameAt(0f, transform)
sample()
}
}
}
private fun MeshBuilder.generateColumn(center: Vec3f, height: Float, dir: Float) {
withTransform {
translate(center)
rotate(dir.deg, Vec3f.Y_AXIS)
profile {
multiShape {
simpleShape(true) {
for (i in 0 until 16) {
val a = i / 16f * 2f * PI.toFloat()
xz(sin(a) - 4, cos(a))
}
}
simpleShape(true) {
for (i in 0 until 16) {
val a = i / 16f * 2f * PI.toFloat()
xz(sin(a) + 4, cos(a))
}
}
}
for (i in 0..10) {
val s = abs((i - 5f) / 5f).pow(2) * 0.4f + 0.6f
scale(s, 1f, 1f)
sample()
scale(1f / s, 1f, 1f)
translate(0f, height / 10f, 0f)
}
}
}
}
fun generate(block: Track.() -> Unit): Track {
apply(block)
build()
makeTrackShader()
makeSupportMeshShader()
return this
}
private fun makeTrackShader() {
val texProps = TextureProps(
generateMipMaps = false,
defaultSamplerSettings = SamplerSettings().nearest()
)
val rand = Random(1337)
val gradient = ColorGradient(VehicleDemo.color(50, false), VehicleDemo.color(300, false))
val sz = 128
val colorData = Uint8Buffer(sz * sz * 4)
val roughnessData = Uint8Buffer(sz * sz)
var c = gradient.getColor(rand.randomF())
var len = rand.randomI(2, 5)
for (i in 0 until sz * sz) {
if (--len == 0) {
c = gradient.getColor(rand.randomF())
len = rand.randomI(2, 5)
}
colorData[i * 4 + 0] = (c.r * 255f).toInt().toUByte()
colorData[i * 4 + 1] = (c.g * 255f).toInt().toUByte()
colorData[i * 4 + 2] = (c.b * 255f).toInt().toUByte()
colorData[i * 4 + 3] = (c.a * 255f).toInt().toUByte()
roughnessData[i] = ((1f - c.brightness + 0.2f).clamp(0f, 1f) * 255f).toInt().toUByte()
}
val albedoMap = Texture2d(texProps, "track-color") {
TextureData2d(colorData, sz, sz, TexFormat.RGBA)
}
val roughnessMap = Texture2d(texProps, "track-roughness") {
TextureData2d(roughnessData, sz, sz, TexFormat.R)
}
albedoMap.releaseWith(trackMesh)
roughnessMap.releaseWith(trackMesh)
trackMesh.shader = deferredKslPbrShader {
color { textureColor(albedoMap) }
roughness { textureProperty(roughnessMap) }
}
}
private fun makeSupportMeshShader() {
trackSupportMesh.shader = deferredKslPbrShader {
color { vertexColor() }
roughness { vertexProperty(ATTRIBUTE_ROUGHNESS) }
}
}
private class GuardRailSection(val from: Float, val to: Float, val isLeft: Boolean)
private class TrackPoint(pt: Vec3f, val pos: Float, val index: Int) : Vec3f(pt) {
val dir = MutableVec3f()
val up = MutableVec3f(0f, 1f, 0f)
fun computeFrame(result: MutableMat4f) {
val z = dir
val x = MutableVec3f(z).apply { y = 0f }
x.rotate(90f.deg, up).norm()
val y = z.cross(x, MutableVec3f()).norm()
result.setColumn(0, x, 0f)
result.setColumn(1, y, 0f)
result.setColumn(2, z, 0f)
result.setColumn(3, this, 1f)
}
}
companion object {
val ATTRIBUTE_ROUGHNESS = Attribute("aRoughness", GpuType.FLOAT1)
}
}