small opti
This commit is contained in:
parent
0cf71d7abb
commit
a6a3785a9f
5 changed files with 23 additions and 15 deletions
|
|
@ -13,8 +13,8 @@ import (
|
||||||
rl "github.com/gen2brain/raylib-go/raylib"
|
rl "github.com/gen2brain/raylib-go/raylib"
|
||||||
)
|
)
|
||||||
|
|
||||||
const WIDTH = 100
|
const WIDTH = 200
|
||||||
const HEIGHT = 100
|
const HEIGHT = 200
|
||||||
const TEXTURE_SCALE = 4
|
const TEXTURE_SCALE = 4
|
||||||
|
|
||||||
const MAX_DIST = 500.0
|
const MAX_DIST = 500.0
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "math"
|
import (
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
type Color interface {
|
type Color interface {
|
||||||
GetColor(Vector3) Vector3
|
GetColor(Vector3) Vector3
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@ func shadow(point Vector3) float64 {
|
||||||
}
|
}
|
||||||
res = min(res, SOFT_SHADOW_COEFF*dist/dist_total)
|
res = min(res, SOFT_SHADOW_COEFF*dist/dist_total)
|
||||||
dist_total += dist
|
dist_total += dist
|
||||||
p = p.Add(direction.Scale(dist))
|
p.Radd(direction.Scale(dist))
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
@ -55,7 +55,7 @@ func rayMarch(origin Vector3, direction Vector3) (bool, Vector3) {
|
||||||
if dist > MAX_DIST {
|
if dist > MAX_DIST {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
p = p.Add(direction.Scale(dist))
|
p.Radd(direction.Scale(dist))
|
||||||
}
|
}
|
||||||
return false, GREY
|
return false, GREY
|
||||||
}
|
}
|
||||||
|
|
|
||||||
18
src/sdf.go
18
src/sdf.go
|
|
@ -73,7 +73,7 @@ type UnionSDF struct {
|
||||||
func (s UnionSDF) Distance(p Vector3) (float64, *Material) {
|
func (s UnionSDF) Distance(p Vector3) (float64, *Material) {
|
||||||
d1, color1 := s.primitive1.Distance(p)
|
d1, color1 := s.primitive1.Distance(p)
|
||||||
d2, color2 := s.primitive2.Distance(p)
|
d2, color2 := s.primitive2.Distance(p)
|
||||||
d := math.Min(d1, d2)
|
d := min(d1, d2)
|
||||||
|
|
||||||
color := color1
|
color := color1
|
||||||
if d2 < d1 {
|
if d2 < d1 {
|
||||||
|
|
@ -91,7 +91,7 @@ func (s SubstractionSDF) Distance(p Vector3) (float64, *Material) {
|
||||||
d1, color1 := s.primitive1.Distance(p)
|
d1, color1 := s.primitive1.Distance(p)
|
||||||
d2, _ := s.primitive2.Distance(p)
|
d2, _ := s.primitive2.Distance(p)
|
||||||
|
|
||||||
d := math.Max(d1, -d2)
|
d := max(d1, -d2)
|
||||||
return d, color1
|
return d, color1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -104,7 +104,7 @@ func (s IntersectionSDF) Distance(p Vector3) (float64, *Material) {
|
||||||
|
|
||||||
d1, mat1 := s.primitive1.Distance(p)
|
d1, mat1 := s.primitive1.Distance(p)
|
||||||
d2, mat2 := s.primitive2.Distance(p)
|
d2, mat2 := s.primitive2.Distance(p)
|
||||||
d := math.Max(d1, d2)
|
d := max(d1, d2)
|
||||||
|
|
||||||
var mat Material = MixedMaterial{mat1, mat2, 0.5, p}
|
var mat Material = MixedMaterial{mat1, mat2, 0.5, p}
|
||||||
return d, &mat
|
return d, &mat
|
||||||
|
|
@ -120,8 +120,8 @@ func (s SmoothUnionSDF) Distance(p Vector3) (float64, *Material) {
|
||||||
k := 4 * s.k
|
k := 4 * s.k
|
||||||
d1, mat1 := s.primitive1.Distance(p)
|
d1, mat1 := s.primitive1.Distance(p)
|
||||||
d2, mat2 := s.primitive2.Distance(p)
|
d2, mat2 := s.primitive2.Distance(p)
|
||||||
h := math.Max(k-math.Abs(d1-d2), 0.0)
|
h := max(k-math.Abs(d1-d2), 0.0)
|
||||||
d := math.Min(d1, d2) - h*h*0.25/k
|
d := min(d1, d2) - h*h*0.25/k
|
||||||
t := SmoothStep(d2-d1, -k, k)
|
t := SmoothStep(d2-d1, -k, k)
|
||||||
var mat Material = MixedMaterial{mat2, mat1, t, p}
|
var mat Material = MixedMaterial{mat2, mat1, t, p}
|
||||||
return d, &mat
|
return d, &mat
|
||||||
|
|
@ -137,8 +137,8 @@ func (s SmoothSubstractionSDF) Distance(p Vector3) (float64, *Material) {
|
||||||
k := 4 * s.k
|
k := 4 * s.k
|
||||||
d1, mat1 := s.primitive1.Distance(p)
|
d1, mat1 := s.primitive1.Distance(p)
|
||||||
d2, mat2 := s.primitive2.Distance(p)
|
d2, mat2 := s.primitive2.Distance(p)
|
||||||
h := math.Max(k-math.Abs(-d1-d2), 0.0)
|
h := max(k-math.Abs(-d1-d2), 0.0)
|
||||||
d := math.Max(d1, -d2) + h*h*0.25/k
|
d := max(d1, -d2) + h*h*0.25/k
|
||||||
t := SmoothStep(d1-d2, -k, k)
|
t := SmoothStep(d1-d2, -k, k)
|
||||||
var mat Material = MixedMaterial{mat1, mat2, t, p}
|
var mat Material = MixedMaterial{mat1, mat2, t, p}
|
||||||
return d, &mat
|
return d, &mat
|
||||||
|
|
@ -154,8 +154,8 @@ func (s SmoothIntersectionSDF) Distance(p Vector3) (float64, *Material) {
|
||||||
k := 4 * s.k
|
k := 4 * s.k
|
||||||
d1, mat1 := s.primitive1.Distance(p)
|
d1, mat1 := s.primitive1.Distance(p)
|
||||||
d2, mat2 := s.primitive2.Distance(p)
|
d2, mat2 := s.primitive2.Distance(p)
|
||||||
h := math.Max(k-math.Abs(d1-d2), 0.0)
|
h := max(k-math.Abs(d1-d2), 0.0)
|
||||||
d := math.Max(d1, d2) + h*h*0.25/k
|
d := max(d1, d2) + h*h*0.25/k
|
||||||
t := SmoothStep(d2-d1, -k, k)
|
t := SmoothStep(d2-d1, -k, k)
|
||||||
var mat Material = MixedMaterial{mat1, mat2, t, p}
|
var mat Material = MixedMaterial{mat1, mat2, t, p}
|
||||||
return d, &mat
|
return d, &mat
|
||||||
|
|
|
||||||
|
|
@ -16,12 +16,18 @@ func (u Vector3) Add(v Vector3) Vector3 {
|
||||||
return Vector3{u.X + v.X, u.Y + v.Y, u.Z + v.Z}
|
return Vector3{u.X + v.X, u.Y + v.Y, u.Z + v.Z}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *Vector3) Radd(v Vector3) {
|
||||||
|
u.X += v.X
|
||||||
|
u.Y += v.Y
|
||||||
|
u.Z += v.Z
|
||||||
|
}
|
||||||
|
|
||||||
func (u Vector3) Neg() Vector3 {
|
func (u Vector3) Neg() Vector3 {
|
||||||
return Vector3{-u.X, -u.Y, -u.Z}
|
return Vector3{-u.X, -u.Y, -u.Z}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u Vector3) Sub(v Vector3) Vector3 {
|
func (u Vector3) Sub(v Vector3) Vector3 {
|
||||||
return u.Add(v.Neg())
|
return Vector3{u.X - v.X, u.Y - v.Y, u.Z - v.Z}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u Vector3) Scale(a float64) Vector3 {
|
func (u Vector3) Scale(a float64) Vector3 {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue