-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
vertex_color_space.go
66 lines (53 loc) · 1.66 KB
/
vertex_color_space.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
56
57
58
59
60
61
62
63
64
65
66
package meshops
import (
"github.com/EliCDavis/polyform/math/colors"
"github.com/EliCDavis/polyform/modeling"
"github.com/EliCDavis/vector/vector3"
)
type VertexColorSpaceTransformation int
const (
VertexColorSpaceSRGBToLinear VertexColorSpaceTransformation = iota
VertexColorSpaceLinearToSRGB
)
type VertexColorSpaceTransformer struct {
Attribute string
Transformation VertexColorSpaceTransformation
}
func (vcst VertexColorSpaceTransformer) attribute() string {
return vcst.Attribute
}
func (vcst VertexColorSpaceTransformer) Transform(m modeling.Mesh) (results modeling.Mesh, err error) {
attribute := getAttribute(vcst, modeling.ColorAttribute)
if err = RequireV3Attribute(m, attribute); err != nil {
return
}
return VertexColorSpace(m, attribute, vcst.Transformation), nil
}
func VertexColorSpace(m modeling.Mesh, attribute string, transformation VertexColorSpaceTransformation) modeling.Mesh {
if err := RequireV3Attribute(m, attribute); err != nil {
panic(err)
}
oldData := m.Float3Attribute(attribute)
transferredData := make([]vector3.Float64, oldData.Len())
switch transformation {
case VertexColorSpaceLinearToSRGB:
for i := 0; i < oldData.Len(); i++ {
entry := oldData.At(i)
transferredData[i] = vector3.New(
colors.LinearToSRGB(entry.X()),
colors.LinearToSRGB(entry.Y()),
colors.LinearToSRGB(entry.Z()),
)
}
case VertexColorSpaceSRGBToLinear:
for i := 0; i < oldData.Len(); i++ {
entry := oldData.At(i)
transferredData[i] = vector3.New(
colors.SRGBToLinear(entry.X()),
colors.SRGBToLinear(entry.Y()),
colors.SRGBToLinear(entry.Z()),
)
}
}
return m.SetFloat3Attribute(attribute, transferredData)
}