-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmodelui.go
120 lines (110 loc) · 3.01 KB
/
modelui.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
// Copyright 2015 Matthew Collins
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package steven
import (
"math"
"github.com/go-gl/mathgl/mgl32"
"github.com/thinkofdeath/steven/type/direction"
"github.com/thinkofdeath/steven/ui"
"github.com/thinkofdeath/steven/world/biome"
)
var modelCache = map[string]*model{}
func getModel(name string) *model {
if mdl, ok := modelCache[name]; ok {
return mdl
}
js := &jsModel{}
err := loadJSON("minecraft", "models/item/"+name+".json", js)
if err != nil {
modelCache[name] = nil
return nil
}
mdl := parseModel("minecraft", js)
modelCache[name] = mdl
return mdl
}
func modelToUI(mdl *model, block Block) *ui.Model {
mat := mgl32.Ident4()
if gui, ok := mdl.display["gui"]; ok {
if gui.Scale != nil {
mat = mat.Mul4(mgl32.Scale3D(
float32(gui.Scale[0]),
float32(gui.Scale[1]),
float32(gui.Scale[2]),
))
}
if gui.Translation != nil {
mat = mat.Mul4(mgl32.Translate3D(
float32(gui.Translation[0]/16),
float32(gui.Translation[1]/16),
float32(gui.Translation[2]/16),
))
}
if gui.Rotation != nil {
mat = mat.Mul4(mgl32.Rotate3DX(float32(gui.Rotation[0]/180) * math.Pi).Mat4())
mat = mat.Mul4(mgl32.Rotate3DZ(float32(gui.Rotation[2]/180) * math.Pi).Mat4())
mat = mat.Mul4(mgl32.Rotate3DY(float32(gui.Rotation[1]/180) * math.Pi).Mat4())
}
}
var verts []*ui.ModelVertex
p := precomputeModel(mdl)
for _, f := range p.faces {
var cr, cg, cb byte
cr = 255
cg = 255
cb = 255
if block != nil && block.TintImage() != nil {
switch f.tintIndex {
case 0:
bi := biome.Plains
ix := bi.ColorIndex & 0xFF
iy := bi.ColorIndex >> 8
col := block.TintImage().NRGBAAt(ix, iy)
cr = byte(col.R)
cg = byte(col.G)
cb = byte(col.B)
}
}
if f.facing == direction.East || f.facing == direction.West {
cr = byte(float64(cr) * 0.8)
cg = byte(float64(cg) * 0.8)
cb = byte(float64(cb) * 0.8)
}
if f.facing == direction.North || f.facing == direction.South {
cr = byte(float64(cr) * 0.6)
cg = byte(float64(cg) * 0.6)
cb = byte(float64(cb) * 0.6)
}
for _, vert := range f.vertices {
vert := &ui.ModelVertex{
X: vert.X,
Y: vert.Y,
Z: vert.Z,
TOffsetX: vert.TOffsetX,
TOffsetY: vert.TOffsetY,
R: cr,
G: cg,
B: cb,
A: 255,
TX: vert.TX,
TY: vert.TY,
TW: vert.TW,
TH: vert.TH,
TAtlas: vert.TAtlas,
}
verts = append(verts, vert)
}
}
return ui.NewModel(0, 0, verts, mat)
}