-
Notifications
You must be signed in to change notification settings - Fork 3
/
SDFColliderTest.kt
150 lines (136 loc) · 4.55 KB
/
SDFColliderTest.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
package me.anno.tests.collider
import me.anno.Engine
import me.anno.ecs.Entity
import me.anno.ecs.components.collider.BoxCollider
import me.anno.ecs.components.collider.CapsuleCollider
import me.anno.ecs.components.collider.Collider
import me.anno.ecs.components.collider.ConeCollider
import me.anno.ecs.components.collider.CylinderCollider
import me.anno.ecs.components.collider.MeshCollider
import me.anno.ecs.components.collider.SphereCollider
import me.anno.ecs.components.mesh.Mesh
import me.anno.ecs.components.mesh.MeshComponent
import me.anno.engine.OfficialExtensions
import me.anno.engine.raycast.RayQueryLocal
import me.anno.image.ImageWriter
import me.anno.io.saveable.Saveable.Companion.registerCustomClass
import me.anno.maths.Maths.fract
import me.anno.tests.LOGGER
import me.anno.utils.Color.rgba
import me.anno.utils.OS.documents
import org.apache.logging.log4j.LogManager
import org.joml.Vector3f
val testShapes = listOf(
BoxCollider(),
BoxCollider().apply {
name = "BoxWithBorder"
roundness = 0.5
},
SphereCollider(),
CapsuleCollider().apply {
radius = 0.8
halfHeight = 0.8
},
CylinderCollider(),
CylinderCollider().apply {
name = "CylinderWithBorder"
roundness = 0.5
},
ConeCollider().apply {
name = "Cone"
},
ConeCollider().apply {
name = "ConeWithBorder"
roundness = 0.5
},
ConeCollider().apply {
name = "ConeSmall"
radius *= 0.5
height *= 0.5
},
ConeCollider().apply {
name = "ConeWide"
radius *= 1.5
height /= 1.5
},
MeshCollider().apply {
registerCustomClass(Entity())
registerCustomClass(MeshComponent())
registerCustomClass(Mesh())
name = "IcoSphere"
meshFile = documents.getChild("icosphere.obj")
},
MeshCollider().apply {
registerCustomClass(Entity())
registerCustomClass(MeshComponent())
registerCustomClass(Mesh())
name = "Triangle"
meshFile = documents.getChild("triangle.obj")
},
MeshCollider().apply {
registerCustomClass(Entity())
registerCustomClass(MeshComponent())
registerCustomClass(Mesh())
name = "Monkey"
meshFile = documents.getChild("monkey.obj")
}
)
fun renderSDF(collider: Collider, name: String) {
val logger = LogManager.getLogger("SDFTest")
val heavy = name == "Monkey"
val size = 8.0f
val res = if (heavy) 128 else 512
val scaleX = size / (res - 1)
val scale = Vector3f(scaleX, -scaleX, 1f)
val offset = Vector3f(size * 0.5f, -size * 0.5f, 0f)
// logger.info()
logger.info(collider.name.ifEmpty { collider.className })
/*logger.info(
"raycast from 1,0,0 to 0,0,0: " + collider.raycast(
Vector3f(1f, 0f, 0f),
Vector3f(-1f, 0f, 0f),
0f, 0f,
Vector3f(),
1f
)
)
logger.info("distance at 0,0,0: " + collider.getSignedDistance(Vector3f()))
logger.info("distance at 1,0,0: " + collider.getSignedDistance(Vector3f(1f, 0f, 0f)))*/
writeImage(res, res, heavy, name, false) { x, y ->
val pos = Vector3f(x, y, 0f)
pos.mul(scale)
pos.sub(offset)
val distance = collider.getSignedDistance(pos)
var color = fract(distance)
if (distance < 0f) color--
color
}
val name1 = name.substring(0, name.length - 4) + "-d.png"
writeImage(res, res, heavy, name1, false) { x, y ->
val pos = Vector3f(x, y, 0f).mul(scale).sub(offset)
val dir = Vector3f(pos).mul(-1f).normalize()
val query = RayQueryLocal(pos, dir, size * 2f, 0f, 0f)
val distance = collider.raycastClosestHit(query, null)
if (distance.isInfinite()) LOGGER.debug("{} {} -> {}, {} -> {}", x, y, pos, dir, distance)
if (distance < 0f) -255f else distance * (if (heavy) 0.25f else 2f) * 255f / size
}
val name2 = name.substring(0, name.length - 4) + "-n.png"
ImageWriter.writeRGBAImageInt(res, res, name2, 512) { x, y, _ ->
val pos = Vector3f(x.toFloat(), y.toFloat(), 0f)
val normal = Vector3f(0f, 0f, 1f)
pos.mul(scale)
pos.sub(offset)
collider.getSignedDistance(pos, normal)
normal.normalize(0.5f).add(0.5f, 0.5f, 0.5f)
rgba(normal.x, normal.y, normal.z, 1f)
}
}
fun main() {
OfficialExtensions.initForTests()
for (shape in testShapes) {
var name = shape.name
if (name.isEmpty()) name = shape.className
renderSDF(shape, "img/$name.png")
}
Engine.requestShutdown()
}