forked from quag/mcobj
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmtl.go
More file actions
82 lines (68 loc) · 1.57 KB
/
mtl.go
File metadata and controls
82 lines (68 loc) · 1.57 KB
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
package main
import (
"fmt"
"io"
"os"
)
func printMtl(w io.Writer, blockId uint16) {
if !noColor {
var idByte = byte(blockId & 0xff)
var extraValue, extraPresent = extraData[idByte]
if extraValue && extraPresent {
fmt.Fprintf(w, "usemtl %d_%d", idByte, blockId>>8)
} else {
fmt.Fprintln(w, "usemtl", idByte)
}
}
}
func writeMtlFile(filename string) os.Error {
if noColor {
return nil
}
var outFile, outErr = os.Open(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if outErr != nil {
return outErr
}
defer outFile.Close()
for _, color := range colors {
color.Print(outFile)
}
return nil
}
type MTL struct {
blockId byte
metadata byte
color uint32
name string
}
func (mtl *MTL) Print(w io.Writer) {
var (
r = mtl.color >> 24
g = mtl.color >> 16 & 0xff
b = mtl.color >> 8 & 0xff
a = mtl.color & 0xff
)
if mtl.metadata == 255 {
fmt.Fprintf(w, "# %s\nnewmtl %d\nKd %.4f %.4f %.4f\nd %.4f\nillum 1\n\n", mtl.name, mtl.blockId, float64(r)/255, float64(g)/255, float64(b)/255, float64(a)/255)
} else {
fmt.Fprintf(w, "# %s\nnewmtl %d_%d\nKd %.4f %.4f %.4f\nd %.4f\nillum 1\n\n", mtl.name, mtl.blockId, mtl.metadata, float64(r)/255, float64(g)/255, float64(b)/255, float64(a)/255)
}
}
func (mtl *MTL) colorId() uint16 {
var id = uint16(mtl.blockId)
if mtl.metadata != 255 {
id += uint16(mtl.metadata) << 8
}
return id
}
func init() {
colors = make([]MTL, 256)
for i, _ := range colors {
colors[i] = MTL{byte(i), 255, 0x7f7f7f, "Unknown"}
}
extraData = make(map[byte]bool)
}
var (
extraData map[byte]bool
colors []MTL
)