-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmaterial_basic.go
More file actions
75 lines (65 loc) · 2.18 KB
/
Copy pathmaterial_basic.go
File metadata and controls
75 lines (65 loc) · 2.18 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
package g3d
import (
"encoding/binary"
"math"
)
// BasicMaterial is an unlit material that renders a solid color.
// Suitable for prototyping, data visualization, wireframe overlays,
// and any situation where lighting calculations are not needed.
//
// The GPU uniform layout is 16 bytes:
//
// offset 0: color RGBA [4]float32
type BasicMaterial struct {
color Color
opacity float32
doubleSided bool
wireframe bool
}
// NewBasicMaterial creates an unlit material with the given options.
// Defaults: color=White, opacity=1.
func NewBasicMaterial(opts ...MaterialOption) *BasicMaterial {
m := &BasicMaterial{
color: White,
opacity: 1,
}
for _, opt := range opts {
opt.applyBasic(m)
}
return m
}
// ShaderID returns "basic", identifying the unlit shader pipeline.
func (m *BasicMaterial) ShaderID() string { return shaderBasic }
// RenderBucket returns RenderBucketTransparent if opacity < 1,
// otherwise RenderBucketOpaque.
func (m *BasicMaterial) RenderBucket() RenderBucket {
if m.opacity < 1 {
return RenderBucketTransparent
}
return RenderBucketOpaque
}
// DoubleSided reports whether back-face culling is disabled.
func (m *BasicMaterial) DoubleSided() bool { return m.doubleSided }
// Wireframe reports whether the material renders as wireframe.
func (m *BasicMaterial) Wireframe() bool { return m.wireframe }
// Color returns the base color.
func (m *BasicMaterial) Color() Color { return m.color }
// Opacity returns the opacity value.
func (m *BasicMaterial) Opacity() float32 { return m.opacity }
// UniformData returns 16 bytes encoding the material color as RGBA float32 values.
// The alpha component is pre-multiplied with opacity.
//
// Layout (matches WGSL vec4<f32>):
//
// offset 0: R float32
// offset 4: G float32
// offset 8: B float32
// offset 12: A float32 (color.A * opacity)
func (m *BasicMaterial) UniformData() []byte {
var buf [16]byte
binary.LittleEndian.PutUint32(buf[0:4], math.Float32bits(m.color.R))
binary.LittleEndian.PutUint32(buf[4:8], math.Float32bits(m.color.G))
binary.LittleEndian.PutUint32(buf[8:12], math.Float32bits(m.color.B))
binary.LittleEndian.PutUint32(buf[12:16], math.Float32bits(m.color.A*m.opacity))
return buf[:]
}