-
Notifications
You must be signed in to change notification settings - Fork 71
/
color.go
96 lines (81 loc) · 3.37 KB
/
color.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
// Copyright 2022 Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package vphong
import (
"fmt"
"image/color"
"log"
"cogentcore.org/core/math32"
)
// Colors are the material colors with padding for direct uploading to shader
type Colors struct {
// main color of surface, used for both ambient and diffuse color in standard Phong model -- alpha component determines transparency -- note that transparent objects require more complex rendering
Color math32.Vector4
// X = shininess spread factor, Y = shine reflection factor, Z = brightness factor: shiny = specular shininess factor -- how focally the surface shines back directional light -- this is an exponential factor, with 0 = very broad diffuse reflection, and higher values (typically max of 128) having a smaller more focal specular reflection. Shine reflect = 1 for full shine white reflection (specular) color, 0 = no shine reflection. bright = overall multiplier on final computed color value -- can be used to tune the overall brightness of various surfaces relative to each other for a given set of lighting parameters. W is used for Tex idx.
ShinyBright math32.Vector4
// color that surface emits independent of any lighting -- i.e., glow -- can be used for marking lights with an object
Emissive math32.Vector4
}
// NewGoColor sets the colors from standard Go colors
func NewColors(clr, emis color.Color, shiny, reflect, bright float32) *Colors {
cl := &Colors{}
cl.SetColors(clr, emis, shiny, reflect, bright)
return cl
}
// SetColors sets the colors from standard Go colors
func (cl *Colors) SetColors(clr, emis color.Color, shiny, reflect, bright float32) {
cl.Color = math32.NewVector4Color(clr).SRGBToLinear()
cl.Emissive = math32.NewVector4Color(emis).SRGBToLinear()
cl.ShinyBright.X = shiny
cl.ShinyBright.Y = reflect
cl.ShinyBright.Z = bright
}
// AddColor adds to list of colors, which can be use for a materials library
func (ph *Phong) AddColor(name string, clr *Colors) {
ph.Colors.Add(name, clr)
}
// UseColorIndex selects color by index for current render step
func (ph *Phong) UseColorIndex(idx int) error {
if err := ph.Colors.IndexIsValid(idx); err != nil {
log.Println(err)
return err
}
clr := ph.Colors.ValueByIndex(idx)
ph.Cur.Color = *clr
return nil
}
// UseColorName selects color by name for current render step
func (ph *Phong) UseColorName(name string) error {
idx, ok := ph.Colors.IndexByKeyTry(name)
if !ok {
err := fmt.Errorf("vphong:UseColorName -- name not found: %s", name)
log.Println(err)
return err
}
clr := ph.Colors.ValueByIndex(idx)
ph.Cur.Color = *clr
return nil
}
// UseColors sets the color values for current render step
func (ph *Phong) UseColor(clr, emis color.Color, shiny, reflect, bright float32) {
ph.Cur.Color.SetColors(clr, emis, shiny, reflect, bright)
}
// RenderOnecolor renders current settings to onecolor pipeline
func (ph *Phong) RenderOnecolor() {
sy := &ph.Sys
cmd := sy.CmdPool.Buff
pl := sy.PipelineMap["onecolor"]
push := ph.Cur.NewPush()
ph.Push(pl, push)
pl.BindDrawVertex(cmd, ph.Cur.DescIndex)
}
// RenderVtxColor renders current settings to vertexcolor pipeline
func (ph *Phong) RenderVtxColor() {
sy := &ph.Sys
cmd := sy.CmdPool.Buff
pl := sy.PipelineMap["pervertex"]
push := ph.Cur.NewPush()
ph.Push(pl, push)
pl.BindDrawVertex(cmd, ph.Cur.DescIndex)
}