This commit is contained in:
Crizomb 2025-09-27 04:09:39 +02:00
parent f5c912c489
commit a7f8b46910
8 changed files with 28 additions and 52 deletions

2
README.md Normal file
View file

@ -0,0 +1,2 @@
Simple cpu ray-marching in go
Focus on Lisiblity & Features, sacrifice performance

View file

View file

View file

@ -43,10 +43,10 @@ func DefaultMaterial(diffuseColor Color) Material {
diffuseColor: diffuseColor, diffuseColor: diffuseColor,
diffuseFac: 1, diffuseFac: 1,
specularColor: Vector3{255, 255, 255}, specularColor: Vector3{255, 255, 255},
specularFac: 2, specularFac: 1,
specularExp: 35.0, specularExp: 35.0,
reflectanceFac: 0.0, reflectanceFac: 0.0,
reflectanceTint: Vector3{1.0, 1.0, 1.0}, reflectanceTint: Vector3{255, 255, 255},
refractFac: 0.0, refractFac: 0.0,
refractIndice: 1.0, refractIndice: 1.0,
} }
@ -67,32 +67,6 @@ func MixMat(mat1 Material, mat2 Material, t float64, p Vector3) Material {
} }
} }
// Setters
func ChangeAmbiantColor(m *Material, newAmbiantColor Color) {
m.ambiantColor = newAmbiantColor
}
func ChangeDiffuseColor(m *Material, newDiffuseColor Color) {
m.diffuseColor = newDiffuseColor
}
func ChangeSpecularColor(m *Material, newSpecularColor Color) {
m.specularColor = newSpecularColor
}
func ChangeDiffuseFac(m *Material, newDiffuseFac float64) {
m.diffuseFac = newDiffuseFac
}
func ChangeSpecularFac(m *Material, newSpecularFac float64) {
m.specularFac = newSpecularFac
}
func ChangeSpecularExp(m *Material, newSpecularExp float64) {
m.specularExp = newSpecularExp
}
// Colors // Colors
var RED = Vector3{255, 0, 0} var RED = Vector3{255, 0, 0}

View file

@ -4,11 +4,11 @@ package main
type Sphere struct { type Sphere struct {
center Vector3 center Vector3
radius float64 radius float64
material *Material material Material
} }
func (s Sphere) Distance(p Vector3) (float64, Material) { func (s Sphere) Distance(p Vector3) (float64, Material) {
return p.Sub(s.center).Length() - s.radius, *s.material return p.Sub(s.center).Length() - s.radius, s.material
} }
// Box // Box
@ -23,6 +23,7 @@ func (s Box) Distance(p Vector3) (float64, Material) {
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
type Plane struct { type Plane struct {
normal Vector3 normal Vector3
height float64 height float64

View file

@ -17,18 +17,29 @@ const WIDTH = 500
const HEIGHT = 500 const HEIGHT = 500
const PI = math.Pi const PI = math.Pi
var sphereMat = RED_MAT var lightPos Vector3
var lightPos = Vector3{0, -200, 800} var scene SmoothUnionSDF
var sphere = Sphere{Vector3{0, 100, 5}, 10, &sphereMat}
var plane = Plane{Vector3{0, 0, 1}, 0, WHITE_GREY_GRID_MAT}
// var scene UnionSDF = UnionSDF{sphere, plane}
var scene = SmoothUnionSDF{sphere, plane, 2}
var cameraPos Vector3 = Vector3{0, -10, 30}
// radius, theta, phi // radius, theta, phi
var screenSpherical Vector3 = Vector3{10, PI/2 + 0.2, PI / 2} var screenSpherical Vector3
var screenPhysicalSize float64 = 5 var screenPhysicalSize float64
var cameraPos Vector3
func init() {
lightPos = Vector3{0, -200, 800}
cameraPos = Vector3{0, -10, 30}
screenSpherical = Vector3{10, PI/2 + 0.2, PI / 2}
screenPhysicalSize = 5
sphereMat := RED_MAT
sphereMat.specularFac = 0.5
sphere := Sphere{Vector3{0, 100, 5}, 10, sphereMat}
plane := Plane{Vector3{0, 0, 1}, 0, WHITE_GREY_GRID_MAT}
scene = SmoothUnionSDF{sphere, plane, 2}
}
func phongShading(point Vector3, normal Vector3, mat Material) Vector3 { func phongShading(point Vector3, normal Vector3, mat Material) Vector3 {
lightVec := (lightPos.Sub(point)).Normalized() lightVec := (lightPos.Sub(point)).Normalized()
@ -49,20 +60,14 @@ func rayMarch(origin Vector3, direction Vector3) (bool, Vector3) {
p := origin p := origin
for range MAX_STEP { for range MAX_STEP {
dist, mat := scene.Distance(p) dist, mat := scene.Distance(p)
// fmt.Println(dist)
if dist < EPS { if dist < EPS {
// fmt.Println("hit")
normal := Gradient(scene, p, EPS).Normalized() normal := Gradient(scene, p, EPS).Normalized()
return true, phongShading(p, normal, mat) return true, phongShading(p, normal, mat)
} }
if dist > MAX_DIST { if dist > MAX_DIST {
break break
} }
// fmt.Println("Before")
// fmt.Println(p, dist)
p = p.Add(direction.Scale(dist)) p = p.Add(direction.Scale(dist))
// fmt.Println("After")
// fmt.Println(p, DistanceOnly(scene, p))
} }
return false, GREY return false, GREY
} }
@ -70,7 +75,6 @@ func rayMarch(origin Vector3, direction Vector3) (bool, Vector3) {
func genImage() *rl.Image { func genImage() *rl.Image {
screenCartesian, _, theta_vec, phi_vec := screenSpherical.SphericalToCartesian() screenCartesian, _, theta_vec, phi_vec := screenSpherical.SphericalToCartesian()
screenCenter := cameraPos.Add(screenCartesian) screenCenter := cameraPos.Add(screenCartesian)
// fmt.Println(theta_vec, phi_vec)
image := rl.GenImageColor(WIDTH, HEIGHT, rl.Black) image := rl.GenImageColor(WIDTH, HEIGHT, rl.Black)
@ -81,7 +85,6 @@ func genImage() *rl.Image {
origin := screenCenter.Add(phi_vec.Scale(-sx)).Add(theta_vec.Scale(sy)) origin := screenCenter.Add(phi_vec.Scale(-sx)).Add(theta_vec.Scale(sy))
direction := (origin.Sub(cameraPos)).Normalized() direction := (origin.Sub(cameraPos)).Normalized()
// fmt.Println(origin, direction)
hit, color_vec := rayMarch(origin, direction) hit, color_vec := rayMarch(origin, direction)
var fr, fg, fb float64 var fr, fg, fb float64
@ -100,10 +103,6 @@ func genImage() *rl.Image {
} }
func main() { func main() {
// ChangeDiffuseColor(&sphereMat, Vector3{0, 0, 255})
ChangeSpecularFac(&sphereMat, 0.5)
ChangeSpecularExp(&sphereMat, 20)
// ChangeSpecularColor(&sphereMat, Vector3{0, 0, 255})
rl.InitWindow(WIDTH, HEIGHT, "raymarching") rl.InitWindow(WIDTH, HEIGHT, "raymarching")
texture := rl.LoadTextureFromImage(genImage()) texture := rl.LoadTextureFromImage(genImage())

View file