Hopefully before really raymarching testing
This commit is contained in:
parent
d2f0abff6a
commit
6ed5b21890
5 changed files with 206 additions and 27 deletions
41
vec3.go
41
vec3.go
|
|
@ -8,6 +8,10 @@ type Vector3 struct {
|
|||
X, Y, Z float64
|
||||
}
|
||||
|
||||
func (u Vector3) GetColor(p Vector3) Vector3 {
|
||||
return u
|
||||
}
|
||||
|
||||
func (u Vector3) Add(v Vector3) Vector3 {
|
||||
return Vector3{u.X + v.X, u.Y + v.Y, u.Z + v.Z}
|
||||
}
|
||||
|
|
@ -49,6 +53,19 @@ func (u Vector3) Round() Vector3 {
|
|||
return Vector3{round(u.X), round(u.Y), round(u.Z)}
|
||||
}
|
||||
|
||||
func (u Vector3) Abs() Vector3 {
|
||||
abs := math.Abs
|
||||
return Vector3{abs(u.X), abs(u.Y), abs(u.Z)}
|
||||
}
|
||||
|
||||
func (u Vector3) Max(x float64) Vector3 {
|
||||
return Vector3{max(u.X, x), max(u.Y, x), max(u.Z, x)}
|
||||
}
|
||||
|
||||
func (u Vector3) Min(x float64) Vector3 {
|
||||
return Vector3{min(u.X, x), min(u.Y, x), min(u.Z, x)}
|
||||
}
|
||||
|
||||
// i incident, n normal. Both vector should be normalized
|
||||
func Reflect(i Vector3, n Vector3) Vector3 {
|
||||
y := i.Dot(n)
|
||||
|
|
@ -58,7 +75,7 @@ func Reflect(i Vector3, n Vector3) Vector3 {
|
|||
// Todo : Refract
|
||||
|
||||
// Rodrigues' rotation formula. rotVector should be normalized
|
||||
func rotate(u Vector3, rotVector Vector3, angle float64) Vector3 {
|
||||
func Rotate(u Vector3, rotVector Vector3, angle float64) Vector3 {
|
||||
cos, sin := math.Cos(angle), math.Sin(angle)
|
||||
vec1 := u.Scale(cos)
|
||||
vec2 := rotVector.Cross(u).Scale(sin)
|
||||
|
|
@ -66,6 +83,28 @@ func rotate(u Vector3, rotVector Vector3, angle float64) Vector3 {
|
|||
return vec1.Add(vec2).Add(vec3)
|
||||
}
|
||||
|
||||
func Mix(u Vector3, v Vector3, k float64) Vector3 {
|
||||
l := (1 - k)
|
||||
return Vector3{u.X*l + v.X*k, u.Y*l + v.Y*k, u.Z*l + v.Z*k}
|
||||
}
|
||||
|
||||
func (v Vector3) Unpack() (float64, float64, float64) {
|
||||
return v.X, v.Y, v.Z
|
||||
}
|
||||
|
||||
// Others maths thingies
|
||||
|
||||
func Clamp(x float64, a float64, b float64) float64 {
|
||||
return min(max(x, a), b)
|
||||
}
|
||||
|
||||
func (v Vector3) Clamp(a float64, b float64) Vector3 {
|
||||
x, y, z := v.Unpack()
|
||||
x, y, z = Clamp(x, a, b), Clamp(y, a, b), Clamp(z, a, b)
|
||||
return Vector3{x, y, z}
|
||||
}
|
||||
|
||||
func SmoothStep(x float64, a float64, b float64) float64 {
|
||||
x = Clamp((x-a)/(b-a), 0, 1)
|
||||
return x * x * (3.0 - 2.0*x)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue