Compare commits
No commits in common. "master" and "distance_return_material" have entirely different histories.
master
...
distance_r
9 changed files with 61 additions and 138 deletions
|
|
@ -1,26 +0,0 @@
|
|||
name: Build Go (Linux + Windows)
|
||||
|
||||
on:
|
||||
push:
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: docker
|
||||
container:
|
||||
image: git.rufous-trench.ts.net/crizomb/forgejo-action-goraylib:deb13-node20-go1.24
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Build binaries
|
||||
run: |
|
||||
cd src
|
||||
mkdir -p builds_artifact
|
||||
GOOS=linux GOARCH=amd64 go build -o builds_artifact/raymarchgo-linux-amd64
|
||||
GOOS=windows GOARCH=amd64 go build -o builds_artifact/raymarchgo-windows-amd64.exe
|
||||
|
||||
- name: Upload artifacts
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: DebugBuilds
|
||||
path: src/builds_artifact
|
||||
|
|
@ -1,7 +1,2 @@
|
|||
Simple cpu ray-marching in go
|
||||
Focus on Lisiblity & Features
|
||||
|
||||
<video src="https://git.rufous-trench.ts.net/Crizomb/Medias/raw/branch/main/rayMarchingGo.mp4" controls></video>
|
||||
|
||||
also used to test CI/CD things currently
|
||||
inc=2
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -1,4 +1,4 @@
|
|||
module raymarching_go
|
||||
module test_raylib
|
||||
|
||||
go 1.24.7
|
||||
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ import (
|
|||
|
||||
const WIDTH = 200
|
||||
const HEIGHT = 200
|
||||
const TEXTURE_SCALE = 4
|
||||
const TEXTURE_SCALE = 2
|
||||
|
||||
const MAX_DIST = 500.0
|
||||
const MAX_STEP = 100
|
||||
|
|
@ -43,11 +43,9 @@ func init() {
|
|||
func update(t float64) {
|
||||
|
||||
sphereMat := RED_MAT
|
||||
sphereMat2 := GREEN_MAT
|
||||
sphereMat.specularFac = 0.5
|
||||
sphereMat2.specularFac = 0.5
|
||||
sphere := TranslatedSDF{Sphere{10, sphereMat}, Vector3{-15, 100, 10}}
|
||||
sphere2 := TranslatedSDF{Sphere{10, sphereMat2}, Vector3{15, 100, 10}}
|
||||
sphere2 := TranslatedSDF{Sphere{10, sphereMat}, Vector3{15, 100, 10}}
|
||||
|
||||
// boxMat := BLUE_MAT
|
||||
// box := TranslatedSDF{Box{Vector3{10, 2, 10}, boxMat}, Vector3{0, 100, 10}}
|
||||
|
|
|
|||
|
|
@ -6,12 +6,8 @@ type Sphere struct {
|
|||
material Material
|
||||
}
|
||||
|
||||
func (s Sphere) Distance(p Vector3) float64 {
|
||||
return p.Length() - s.radius
|
||||
}
|
||||
|
||||
func (s Sphere) GetMaterial(p Vector3) Material {
|
||||
return s.material
|
||||
func (s Sphere) Distance(p Vector3) (float64, Material) {
|
||||
return p.Length() - s.radius, s.material
|
||||
}
|
||||
|
||||
// Box
|
||||
|
|
@ -20,13 +16,9 @@ type Box struct {
|
|||
material Material
|
||||
}
|
||||
|
||||
func (s Box) Distance(p Vector3) float64 {
|
||||
func (s Box) Distance(p Vector3) (float64, Material) {
|
||||
q := p.Abs().Sub(s.dimensions)
|
||||
return q.Max(0.0).Length() + min(max(q.X, max(q.Y, q.Z)), 0.0)
|
||||
}
|
||||
|
||||
func (s Box) GetMaterial(p Vector3) Material {
|
||||
return s.material
|
||||
return q.Max(0.0).Length() + min(max(q.X, max(q.Y, q.Z)), 0.0), s.material
|
||||
}
|
||||
|
||||
// Plane
|
||||
|
|
@ -35,10 +27,6 @@ type Plane struct {
|
|||
material Material
|
||||
}
|
||||
|
||||
func (s Plane) Distance(p Vector3) float64 {
|
||||
return p.Dot(s.normal)
|
||||
}
|
||||
|
||||
func (s Plane) GetMaterial(p Vector3) Material {
|
||||
return s.material
|
||||
func (s Plane) Distance(p Vector3) (float64, Material) {
|
||||
return p.Dot(s.normal), s.material
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ func shadow(point Vector3) float64 {
|
|||
res := 1.0
|
||||
dist_total := 0.0
|
||||
for range MAX_STEP {
|
||||
dist := scene.Distance(p)
|
||||
dist, _ := scene.Distance(p)
|
||||
if dist < EPS {
|
||||
return 0
|
||||
}
|
||||
|
|
@ -62,9 +62,8 @@ func reflect(point Vector3, direction Vector3, normal Vector3, mat Material, ref
|
|||
func rayMarch(origin Vector3, direction Vector3, reflectNumber int) (bool, Vector3) {
|
||||
p := origin
|
||||
for range MAX_STEP {
|
||||
dist := scene.Distance(p)
|
||||
dist, mat := scene.Distance(p)
|
||||
if dist < EPS {
|
||||
mat := scene.GetMaterial(p)
|
||||
normal := Gradient(scene, p, EPS).Normalized()
|
||||
phongShade := phongShading(p, normal, mat)
|
||||
shadowCoeff := max(shadow(p), 0.3)
|
||||
|
|
|
|||
Binary file not shown.
131
src/sdf.go
131
src/sdf.go
|
|
@ -5,14 +5,18 @@ import (
|
|||
)
|
||||
|
||||
type SDF interface {
|
||||
Distance(Vector3) float64
|
||||
GetMaterial(Vector3) Material
|
||||
Distance(Vector3) (float64, Material)
|
||||
}
|
||||
|
||||
func DistanceOnly(sdf SDF, p Vector3) float64 {
|
||||
dist, _ := sdf.Distance(p)
|
||||
return dist
|
||||
}
|
||||
|
||||
func Gradient(sdf SDF, p Vector3, eps float64) Vector3 {
|
||||
dx := (sdf.Distance(p.Add(Vector3{X: eps})) - sdf.Distance(p.Add(Vector3{X: -eps}))) / (2 * eps)
|
||||
dy := (sdf.Distance(p.Add(Vector3{Y: eps})) - sdf.Distance(p.Add(Vector3{Y: -eps}))) / (2 * eps)
|
||||
dz := (sdf.Distance(p.Add(Vector3{Z: eps})) - sdf.Distance(p.Add(Vector3{Z: -eps}))) / (2 * eps)
|
||||
dx := (DistanceOnly(sdf, p.Add(Vector3{X: eps})) - DistanceOnly(sdf, p.Add(Vector3{X: -eps}))) / (2 * eps)
|
||||
dy := (DistanceOnly(sdf, p.Add(Vector3{Y: eps})) - DistanceOnly(sdf, p.Add(Vector3{Y: -eps}))) / (2 * eps)
|
||||
dz := (DistanceOnly(sdf, p.Add(Vector3{Z: eps})) - DistanceOnly(sdf, p.Add(Vector3{Z: -eps}))) / (2 * eps)
|
||||
return Vector3{X: dx, Y: dy, Z: dz}
|
||||
}
|
||||
|
||||
|
|
@ -23,40 +27,29 @@ type TranslatedSDF struct {
|
|||
translate Vector3
|
||||
}
|
||||
|
||||
func (s TranslatedSDF) Distance(p Vector3) float64 {
|
||||
func (s TranslatedSDF) Distance(p Vector3) (float64, Material) {
|
||||
return s.primitive.Distance(p.Sub(s.translate))
|
||||
}
|
||||
|
||||
func (s TranslatedSDF) GetMaterial(p Vector3) Material {
|
||||
return s.primitive.GetMaterial(p.Sub(s.translate))
|
||||
}
|
||||
|
||||
type RotatedSDF struct {
|
||||
primitive SDF
|
||||
rotVector Vector3
|
||||
angle float64
|
||||
}
|
||||
|
||||
func (s RotatedSDF) Distance(p Vector3) float64 {
|
||||
func (s RotatedSDF) Distance(p Vector3) (float64, Material) {
|
||||
rotated_p := Rotate(p, s.rotVector, s.angle)
|
||||
return s.primitive.Distance(rotated_p)
|
||||
}
|
||||
func (s RotatedSDF) GetMaterial(p Vector3) Material {
|
||||
rotated_p := Rotate(p, s.rotVector, s.angle)
|
||||
return s.primitive.GetMaterial(rotated_p)
|
||||
}
|
||||
|
||||
type ScaledSDF struct {
|
||||
primitive SDF
|
||||
scale float64
|
||||
}
|
||||
|
||||
func (s ScaledSDF) Distance(p Vector3) float64 {
|
||||
return s.primitive.Distance(p.Scale(1 / s.scale))
|
||||
}
|
||||
|
||||
func (s ScaledSDF) GetMaterial(p Vector3) Material {
|
||||
return s.primitive.GetMaterial(p.Scale(1 / s.scale))
|
||||
func (s ScaledSDF) Distance(p Vector3) (float64, Material) {
|
||||
dist, color := s.primitive.Distance(p.Scale(1 / s.scale))
|
||||
return dist * s.scale, color
|
||||
}
|
||||
|
||||
type RepeatSDF struct {
|
||||
|
|
@ -64,39 +57,29 @@ type RepeatSDF struct {
|
|||
cellSize Vector3
|
||||
}
|
||||
|
||||
func (s RepeatSDF) Distance(p Vector3) float64 {
|
||||
func (s RepeatSDF) Distance(p Vector3) (float64, Material) {
|
||||
x, y, z := p.Unpack()
|
||||
sx, sy, sz := s.cellSize.Unpack()
|
||||
round := math.RoundToEven
|
||||
nearest_cell := Vector3{sx * round(x/sx), sy * round(y/sy), sz * round(z/sz)}
|
||||
return s.primitive.Distance(p.Sub(nearest_cell))
|
||||
}
|
||||
func (s RepeatSDF) GetMaterial(p Vector3) Material {
|
||||
x, y, z := p.Unpack()
|
||||
sx, sy, sz := s.cellSize.Unpack()
|
||||
round := math.RoundToEven
|
||||
nearest_cell := Vector3{sx * round(x/sx), sy * round(y/sy), sz * round(z/sz)}
|
||||
return s.primitive.GetMaterial(p.Sub(nearest_cell))
|
||||
}
|
||||
|
||||
type UnionSDF struct {
|
||||
primitive1 SDF
|
||||
primitive2 SDF
|
||||
}
|
||||
|
||||
func (s UnionSDF) Distance(p Vector3) float64 {
|
||||
return min(s.primitive1.Distance(p), s.primitive2.Distance(p))
|
||||
}
|
||||
|
||||
func (s UnionSDF) GetMaterial(p Vector3) Material {
|
||||
d1, color1 := s.primitive1.Distance(p), s.primitive1.GetMaterial(p)
|
||||
d2, color2 := s.primitive2.Distance(p), s.primitive2.GetMaterial(p)
|
||||
func (s UnionSDF) Distance(p Vector3) (float64, Material) {
|
||||
d1, color1 := s.primitive1.Distance(p)
|
||||
d2, color2 := s.primitive2.Distance(p)
|
||||
d := min(d1, d2)
|
||||
|
||||
color := color1
|
||||
if d2 < d1 {
|
||||
color = color2
|
||||
}
|
||||
return color
|
||||
return d, color
|
||||
}
|
||||
|
||||
type SubstractionSDF struct {
|
||||
|
|
@ -104,12 +87,12 @@ type SubstractionSDF struct {
|
|||
primitive2 SDF
|
||||
}
|
||||
|
||||
func (s SubstractionSDF) Distance(p Vector3) float64 {
|
||||
return max(s.primitive1.Distance(p), -s.primitive2.Distance(p))
|
||||
}
|
||||
func (s SubstractionSDF) Distance(p Vector3) (float64, Material) {
|
||||
d1, color1 := s.primitive1.Distance(p)
|
||||
d2, _ := s.primitive2.Distance(p)
|
||||
|
||||
func (s SubstractionSDF) GetMaterial(p Vector3) Material {
|
||||
return s.primitive1.GetMaterial(p)
|
||||
d := max(d1, -d2)
|
||||
return d, color1
|
||||
}
|
||||
|
||||
type IntersectionSDF struct {
|
||||
|
|
@ -117,13 +100,14 @@ type IntersectionSDF struct {
|
|||
primitive2 SDF
|
||||
}
|
||||
|
||||
func (s IntersectionSDF) Distance(p Vector3) float64 {
|
||||
return max(s.primitive1.Distance(p), s.primitive2.Distance(p))
|
||||
}
|
||||
func (s IntersectionSDF) Distance(p Vector3) (float64, Material) {
|
||||
|
||||
func (s IntersectionSDF) GetMaterial(p Vector3) Material {
|
||||
mat1, mat2 := s.primitive1.GetMaterial(p), s.primitive2.GetMaterial(p)
|
||||
return MixedMaterial{mat1, mat2, 0.5, p}
|
||||
d1, mat1 := s.primitive1.Distance(p)
|
||||
d2, mat2 := s.primitive2.Distance(p)
|
||||
d := max(d1, d2)
|
||||
|
||||
var mat Material = MixedMaterial{mat1, mat2, 0.5, p}
|
||||
return d, mat
|
||||
}
|
||||
|
||||
type SmoothUnionSDF struct {
|
||||
|
|
@ -132,20 +116,15 @@ type SmoothUnionSDF struct {
|
|||
k float64
|
||||
}
|
||||
|
||||
func (s SmoothUnionSDF) Distance(p Vector3) float64 {
|
||||
func (s SmoothUnionSDF) Distance(p Vector3) (float64, Material) {
|
||||
k := 4 * s.k
|
||||
d1 := s.primitive1.Distance(p)
|
||||
d2 := s.primitive2.Distance(p)
|
||||
d1, mat1 := s.primitive1.Distance(p)
|
||||
d2, mat2 := s.primitive2.Distance(p)
|
||||
h := max(k-math.Abs(d1-d2), 0.0)
|
||||
return min(d1, d2) - h*h*0.25/k
|
||||
}
|
||||
|
||||
func (s SmoothUnionSDF) GetMaterial(p Vector3) Material {
|
||||
k := 4 * s.k
|
||||
d1, mat1 := s.primitive1.Distance(p), s.primitive1.GetMaterial(p)
|
||||
d2, mat2 := s.primitive2.Distance(p), s.primitive2.GetMaterial(p)
|
||||
d := min(d1, d2) - h*h*0.25/k
|
||||
t := SmoothStep(d2-d1, -k, k)
|
||||
return MixedMaterial{mat2, mat1, t, p}
|
||||
var mat Material = MixedMaterial{mat2, mat1, t, p}
|
||||
return d, mat
|
||||
}
|
||||
|
||||
type SmoothSubstractionSDF struct {
|
||||
|
|
@ -154,20 +133,15 @@ type SmoothSubstractionSDF struct {
|
|||
k float64
|
||||
}
|
||||
|
||||
func (s SmoothSubstractionSDF) Distance(p Vector3) float64 {
|
||||
func (s SmoothSubstractionSDF) Distance(p Vector3) (float64, Material) {
|
||||
k := 4 * s.k
|
||||
d1 := s.primitive1.Distance(p)
|
||||
d2 := s.primitive2.Distance(p)
|
||||
d1, mat1 := s.primitive1.Distance(p)
|
||||
d2, mat2 := s.primitive2.Distance(p)
|
||||
h := max(k-math.Abs(-d1-d2), 0.0)
|
||||
return max(d1, -d2) + h*h*0.25/k
|
||||
}
|
||||
|
||||
func (s SmoothSubstractionSDF) GetMaterial(p Vector3) Material {
|
||||
k := 4 * s.k
|
||||
d1, mat1 := s.primitive1.Distance(p), s.primitive1.GetMaterial(p)
|
||||
d2, mat2 := s.primitive2.Distance(p), s.primitive2.GetMaterial(p)
|
||||
d := max(d1, -d2) + h*h*0.25/k
|
||||
t := SmoothStep(d1-d2, -k, k)
|
||||
return MixedMaterial{mat1, mat2, t, p}
|
||||
var mat Material = MixedMaterial{mat1, mat2, t, p}
|
||||
return d, mat
|
||||
}
|
||||
|
||||
type SmoothIntersectionSDF struct {
|
||||
|
|
@ -176,18 +150,13 @@ type SmoothIntersectionSDF struct {
|
|||
k float64
|
||||
}
|
||||
|
||||
func (s SmoothIntersectionSDF) Distance(p Vector3) float64 {
|
||||
func (s SmoothIntersectionSDF) Distance(p Vector3) (float64, Material) {
|
||||
k := 4 * s.k
|
||||
d1 := s.primitive1.Distance(p)
|
||||
d2 := s.primitive2.Distance(p)
|
||||
d1, mat1 := s.primitive1.Distance(p)
|
||||
d2, mat2 := s.primitive2.Distance(p)
|
||||
h := max(k-math.Abs(d1-d2), 0.0)
|
||||
return max(d1, d2) + h*h*0.25/k
|
||||
}
|
||||
|
||||
func (s SmoothIntersectionSDF) GetMaterial(p Vector3) Material {
|
||||
k := 4 * s.k
|
||||
d1, mat1 := s.primitive1.Distance(p), s.primitive1.GetMaterial(p)
|
||||
d2, mat2 := s.primitive2.Distance(p), s.primitive2.GetMaterial(p)
|
||||
d := max(d1, d2) + h*h*0.25/k
|
||||
t := SmoothStep(d2-d1, -k, k)
|
||||
return MixedMaterial{mat1, mat2, t, p}
|
||||
var mat Material = MixedMaterial{mat1, mat2, t, p}
|
||||
return d, mat
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue