forked from dskinner/htm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
texcoords.go
55 lines (49 loc) · 1.09 KB
/
texcoords.go
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
package htm
import (
"math"
"azul3d.org/lmath.v1"
)
// TexCoords returns a slice of UV coordinates for texture mapping.
// TODO(d) seam does not wrap correctly.
// TODO(d) allow user to declare which axis is up.
func TexCoords(verts []lmath.Vec3) []float32 {
var tc []float32
for _, v0 := range verts {
u := 0.5 + math.Atan2(v0.Y, v0.X)/(math.Pi*2)
v := 0.5 - math.Asin(v0.Z)/math.Pi
tc = append(tc, float32(u), float32(v))
}
return tc
}
// TexCoordsPlanar returns a slice of UV coordinates mapped against
// a 2-dimensional plane.
// TODO(d) allow user to declare up axis.
func TexCoordsPlanar(verts []lmath.Vec3) []float32 {
var xlo, xhi, zlo, zhi float64
for _, v0 := range verts {
if v0.X < xlo {
xlo = v0.X
}
if v0.X > xhi {
xhi = v0.X
}
if v0.Z < zlo {
zlo = v0.Z
}
if v0.Z > zhi {
zhi = v0.Z
}
}
xrng := (xlo - xhi) * -1
xoffset := 0 - xlo
zrng := (zlo - zhi) * -1
zoffset := 0 - zlo
//
var tc []float32
for _, v0 := range verts {
u := (v0.X + xoffset) / xrng
v := (v0.Z + zoffset) / zrng
tc = append(tc, float32(u), float32(v))
}
return tc
}