2x Perf, because wtf *Material is useless, Material is an interface so it's already a ref
This commit is contained in:
parent
a6a3785a9f
commit
0ded879dce
4 changed files with 26 additions and 25 deletions
|
|
@ -70,14 +70,14 @@ func MixMat(mat1 MaterialBrut, mat2 MaterialBrut, t float64, p Vector3) Material
|
||||||
}
|
}
|
||||||
|
|
||||||
type MixedMaterial struct {
|
type MixedMaterial struct {
|
||||||
mat1 *Material
|
mat1 Material
|
||||||
mat2 *Material
|
mat2 Material
|
||||||
t float64
|
t float64
|
||||||
p Vector3
|
p Vector3
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mixedMat MixedMaterial) GetMaterialBrut() MaterialBrut {
|
func (mixedMat MixedMaterial) GetMaterialBrut() MaterialBrut {
|
||||||
return MixMat((*mixedMat.mat1).GetMaterialBrut(), (*mixedMat.mat2).GetMaterialBrut(), mixedMat.t, mixedMat.p)
|
return MixMat((mixedMat.mat1).GetMaterialBrut(), (mixedMat.mat2).GetMaterialBrut(), mixedMat.t, mixedMat.p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func DefaultMaterial(diffuseColor Color) MaterialBrut {
|
func DefaultMaterial(diffuseColor Color) MaterialBrut {
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,8 @@ type Sphere struct {
|
||||||
material Material
|
material Material
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Sphere) Distance(p Vector3) (float64, *Material) {
|
func (s Sphere) Distance(p Vector3) (float64, Material) {
|
||||||
return p.Length() - s.radius, &s.material
|
return p.Length() - s.radius, s.material
|
||||||
}
|
}
|
||||||
|
|
||||||
// Box
|
// Box
|
||||||
|
|
@ -16,9 +16,9 @@ type Box struct {
|
||||||
material Material
|
material Material
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Box) Distance(p Vector3) (float64, *Material) {
|
func (s Box) Distance(p Vector3) (float64, Material) {
|
||||||
q := p.Abs().Sub(s.dimensions)
|
q := p.Abs().Sub(s.dimensions)
|
||||||
return q.Max(0.0).Length() + min(max(q.X, max(q.Y, q.Z)), 0.0), &s.material
|
return q.Max(0.0).Length() + min(max(q.X, max(q.Y, q.Z)), 0.0), s.material
|
||||||
}
|
}
|
||||||
|
|
||||||
// Plane
|
// Plane
|
||||||
|
|
@ -27,6 +27,6 @@ type Plane struct {
|
||||||
material Material
|
material Material
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Plane) Distance(p Vector3) (float64, *Material) {
|
func (s Plane) Distance(p Vector3) (float64, Material) {
|
||||||
return p.Dot(s.normal), &s.material
|
return p.Dot(s.normal), s.material
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,8 @@ func rayMarch(origin Vector3, direction Vector3) (bool, Vector3) {
|
||||||
dist, mat := scene.Distance(p)
|
dist, mat := scene.Distance(p)
|
||||||
if dist < EPS {
|
if dist < EPS {
|
||||||
normal := Gradient(scene, p, EPS).Normalized()
|
normal := Gradient(scene, p, EPS).Normalized()
|
||||||
return true, phongShading(p, normal, *mat).Scale(max(shadow(p), 0.3))
|
return true, phongShading(p, normal, mat).Scale(max(shadow(p), 0.3))
|
||||||
|
// return true, phongShading(p, normal, *mat)
|
||||||
}
|
}
|
||||||
if dist > MAX_DIST {
|
if dist > MAX_DIST {
|
||||||
break
|
break
|
||||||
|
|
|
||||||
30
src/sdf.go
30
src/sdf.go
|
|
@ -5,7 +5,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type SDF interface {
|
type SDF interface {
|
||||||
Distance(Vector3) (float64, *Material)
|
Distance(Vector3) (float64, Material)
|
||||||
}
|
}
|
||||||
|
|
||||||
func DistanceOnly(sdf SDF, p Vector3) float64 {
|
func DistanceOnly(sdf SDF, p Vector3) float64 {
|
||||||
|
|
@ -27,7 +27,7 @@ type TranslatedSDF struct {
|
||||||
translate Vector3
|
translate Vector3
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s TranslatedSDF) Distance(p Vector3) (float64, *Material) {
|
func (s TranslatedSDF) Distance(p Vector3) (float64, Material) {
|
||||||
return s.primitive.Distance(p.Sub(s.translate))
|
return s.primitive.Distance(p.Sub(s.translate))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -37,7 +37,7 @@ type RotatedSDF struct {
|
||||||
angle float64
|
angle float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s RotatedSDF) Distance(p Vector3) (float64, *Material) {
|
func (s RotatedSDF) Distance(p Vector3) (float64, Material) {
|
||||||
rotated_p := Rotate(p, s.rotVector, s.angle)
|
rotated_p := Rotate(p, s.rotVector, s.angle)
|
||||||
return s.primitive.Distance(rotated_p)
|
return s.primitive.Distance(rotated_p)
|
||||||
}
|
}
|
||||||
|
|
@ -47,7 +47,7 @@ type ScaledSDF struct {
|
||||||
scale float64
|
scale float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s ScaledSDF) Distance(p Vector3) (float64, *Material) {
|
func (s ScaledSDF) Distance(p Vector3) (float64, Material) {
|
||||||
dist, color := s.primitive.Distance(p.Scale(1 / s.scale))
|
dist, color := s.primitive.Distance(p.Scale(1 / s.scale))
|
||||||
return dist * s.scale, color
|
return dist * s.scale, color
|
||||||
}
|
}
|
||||||
|
|
@ -57,7 +57,7 @@ type RepeatSDF struct {
|
||||||
cellSize Vector3
|
cellSize Vector3
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s RepeatSDF) Distance(p Vector3) (float64, *Material) {
|
func (s RepeatSDF) Distance(p Vector3) (float64, Material) {
|
||||||
x, y, z := p.Unpack()
|
x, y, z := p.Unpack()
|
||||||
sx, sy, sz := s.cellSize.Unpack()
|
sx, sy, sz := s.cellSize.Unpack()
|
||||||
round := math.RoundToEven
|
round := math.RoundToEven
|
||||||
|
|
@ -70,7 +70,7 @@ type UnionSDF struct {
|
||||||
primitive2 SDF
|
primitive2 SDF
|
||||||
}
|
}
|
||||||
|
|
||||||
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 := min(d1, d2)
|
d := min(d1, d2)
|
||||||
|
|
@ -87,7 +87,7 @@ type SubstractionSDF struct {
|
||||||
primitive2 SDF
|
primitive2 SDF
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s SubstractionSDF) Distance(p Vector3) (float64, *Material) {
|
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)
|
||||||
|
|
||||||
|
|
@ -100,14 +100,14 @@ type IntersectionSDF struct {
|
||||||
primitive2 SDF
|
primitive2 SDF
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s IntersectionSDF) Distance(p Vector3) (float64, *Material) {
|
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 := 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
|
||||||
}
|
}
|
||||||
|
|
||||||
type SmoothUnionSDF struct {
|
type SmoothUnionSDF struct {
|
||||||
|
|
@ -116,7 +116,7 @@ type SmoothUnionSDF struct {
|
||||||
k float64
|
k float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s SmoothUnionSDF) Distance(p Vector3) (float64, *Material) {
|
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)
|
||||||
|
|
@ -124,7 +124,7 @@ func (s SmoothUnionSDF) Distance(p Vector3) (float64, *Material) {
|
||||||
d := 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
|
||||||
}
|
}
|
||||||
|
|
||||||
type SmoothSubstractionSDF struct {
|
type SmoothSubstractionSDF struct {
|
||||||
|
|
@ -133,7 +133,7 @@ type SmoothSubstractionSDF struct {
|
||||||
k float64
|
k float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s SmoothSubstractionSDF) Distance(p Vector3) (float64, *Material) {
|
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)
|
||||||
|
|
@ -141,7 +141,7 @@ func (s SmoothSubstractionSDF) Distance(p Vector3) (float64, *Material) {
|
||||||
d := 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
|
||||||
}
|
}
|
||||||
|
|
||||||
type SmoothIntersectionSDF struct {
|
type SmoothIntersectionSDF struct {
|
||||||
|
|
@ -150,7 +150,7 @@ type SmoothIntersectionSDF struct {
|
||||||
k float64
|
k float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s SmoothIntersectionSDF) Distance(p Vector3) (float64, *Material) {
|
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)
|
||||||
|
|
@ -158,5 +158,5 @@ func (s SmoothIntersectionSDF) Distance(p Vector3) (float64, *Material) {
|
||||||
d := 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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue