/
material_colors.go
66 lines (59 loc) · 1.52 KB
/
material_colors.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 g2d
import (
"fmt"
"github.com/go4orward/gigl/common"
)
type MaterialColors struct {
colors [4][4]float32 // color ([0]:common, [1]:vert, [2]:edge, [3]:face)
}
func NewMaterialColors(colors ...any) *MaterialColors {
mc := MaterialColors{}
for i := 0; i < 4; i++ {
rgba := [4]float32{0, 0, 0, 0}
if i < len(colors) {
switch colors[i].(type) {
case [3]float32:
rgb := colors[i].([3]float32)
rgba = [4]float32{rgb[0], rgb[1], rgb[2], 1.0}
case [4]float32:
rgba = colors[i].([4]float32)
case string:
rgba = common.RGBAFromHexString(colors[i].(string))
}
mc.colors[i] = rgba
} else if i > 0 {
mc.colors[i] = mc.colors[0]
}
}
return &mc
}
func (self *MaterialColors) MaterialSummary() string {
colors := ""
for i := 0; i < len(self.colors); i++ {
colors += common.HexStringFromRGBA(self.colors[i]) + " "
}
return fmt.Sprintf("MaterialColor %s", colors)
}
func (self *MaterialColors) GetDrawModeColor(draw_mode int) [4]float32 {
return self.colors[draw_mode]
}
func (self *MaterialColors) SetColorForDrawMode(draw_mode int, color string) *MaterialColors {
// 'draw_mode' : 0:common, 1:vertex, 2:edges, 3:faces
if color != "" {
rgba := common.RGBAFromHexString(color)
switch draw_mode {
case 1:
self.colors[1] = rgba // vertex color
case 2:
self.colors[2] = rgba // edge color
case 3:
self.colors[3] = rgba // face color
default:
self.colors[0] = rgba // otherwise
self.colors[1] = rgba
self.colors[2] = rgba
self.colors[3] = rgba
}
}
return self
}