forked from g3n/engine
/
khr_materials_common.go
163 lines (143 loc) · 3.76 KB
/
khr_materials_common.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
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
package gltf
import (
"engine/material"
"engine/math32"
"engine/texture"
)
// loadMaterialCommon receives an interface value describing a KHR_materials_common extension,
// decodes it and returns a Material closest to the specified description.
// The specification of this extension is at:
// https://github.com/KhronosGroup/glTF/tree/master/extensions/1.0/Khronos/KHR_materials_common
func (g *GLTF) loadMaterialCommon(ext interface{}) (material.IMaterial, error) {
// The extension must be an object
m := ext.(map[string]interface{})
// Double sided
doubleSided := false
val, ok := m["doubleSided"]
if ok {
doubleSided = val.(bool)
}
// Technique
technique := ""
val, ok = m["technique"]
if ok {
technique = val.(string)
}
// Transparent
transparent := false
val, ok = m["transparent"]
if ok {
transparent = val.(bool)
}
// Defaul values
ambient := []float32{0, 0, 0, 1}
diffuse := []float32{0, 0, 0, 1}
emission := []float32{0, 0, 0, 1}
specular := []float32{0, 0, 0, 1}
shininess := float32(0)
transparency := float32(1)
var texDiffuse *texture.Texture2D
// Converts a slice of interface values which should be float64
// to a slice of float32
convIF32 := func(v interface{}) []float32 {
si := v.([]interface{})
res := make([]float32, 0)
for i := 0; i < len(si); i++ {
res = append(res, float32(si[i].(float64)))
}
return res
}
// Values
values, ok := m["values"].(map[string]interface{})
if ok {
// Ambient light
val, ok = values["ambient"]
if ok {
ambient = convIF32(val)
}
// Diffuse light
val, ok = values["diffuse"]
if ok {
v := convIF32(val)
// Checks for texture index
if len(v) == 1 {
var err error
texDiffuse, err = g.LoadTexture(int(v[0]))
if err != nil {
return nil, err
}
diffuse = []float32{1, 1, 1, 1}
}
}
// Emission light
val, ok = values["emission"]
if ok {
emission = convIF32(val)
}
// Specular light
val, ok = values["specular"]
if ok {
specular = convIF32(val)
}
// Shininess
val, ok = values["shininess"]
if ok {
s := convIF32(val)
shininess = s[0]
}
// Transparency
val, ok = values["transparency"]
if ok {
s := convIF32(val)
transparency = s[0]
}
}
//log.Error("doubleSided:%v", doubleSided)
//log.Error("technique:%v", technique)
//log.Error("transparent:%v", transparent)
//log.Error("values:%v", values)
//log.Error("ambient:%v", ambient)
//log.Error("diffuse:%v", diffuse)
//log.Error("emission:%v", emission)
//log.Error("specular:%v", specular)
//log.Error("shininess:%v", shininess)
//log.Error("transparency:%v", transparency)
var imat material.IMaterial
if technique == "PHONG" {
pm := material.NewPhong(&math32.Color{diffuse[0], diffuse[1], diffuse[2]})
pm.SetAmbientColor(&math32.Color{ambient[0], ambient[1], ambient[2]})
pm.SetEmissiveColor(&math32.Color{emission[0], emission[1], emission[2]})
pm.SetSpecularColor(&math32.Color{specular[0], specular[1], specular[2]})
pm.SetShininess(shininess)
pm.SetOpacity(transparency)
if texDiffuse != nil {
pm.AddTexture(texDiffuse)
}
imat = pm
} else {
sm := material.NewStandard(&math32.Color{diffuse[0], diffuse[1], diffuse[2]})
sm.SetAmbientColor(&math32.Color{ambient[0], ambient[1], ambient[2]})
sm.SetEmissiveColor(&math32.Color{emission[0], emission[1], emission[2]})
sm.SetSpecularColor(&math32.Color{specular[0], specular[1], specular[2]})
sm.SetShininess(shininess)
sm.SetOpacity(transparency)
if texDiffuse != nil {
sm.AddTexture(texDiffuse)
}
imat = sm
}
// Double Sided
mat := imat.GetMaterial()
if doubleSided {
mat.SetSide(material.SideDouble)
} else {
mat.SetSide(material.SideFront)
}
// Transparency
if transparent {
mat.SetDepthMask(true)
} else {
mat.SetDepthMask(false)
}
return imat, nil
}