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,
diffuseFac: 1,
specularColor: Vector3{255, 255, 255},
specularFac: 2,
specularFac: 1,
specularExp: 35.0,
reflectanceFac: 0.0,
reflectanceTint: Vector3{1.0, 1.0, 1.0},
reflectanceTint: Vector3{255, 255, 255},
refractFac: 0.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
var RED = Vector3{255, 0, 0}

View file

@ -4,11 +4,11 @@ package main
type Sphere struct {
center Vector3
radius float64
material *Material
material 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
@ -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
}
// Plane
type Plane struct {
normal Vector3
height float64

View file

@ -17,18 +17,29 @@ const WIDTH = 500
const HEIGHT = 500
const PI = math.Pi
var sphereMat = RED_MAT
var lightPos = Vector3{0, -200, 800}
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}
var lightPos Vector3
var scene SmoothUnionSDF
// radius, theta, phi
var screenSpherical Vector3 = Vector3{10, PI/2 + 0.2, PI / 2}
var screenPhysicalSize float64 = 5
var screenSpherical Vector3
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 {
lightVec := (lightPos.Sub(point)).Normalized()
@ -49,20 +60,14 @@ func rayMarch(origin Vector3, direction Vector3) (bool, Vector3) {
p := origin
for range MAX_STEP {
dist, mat := scene.Distance(p)
// fmt.Println(dist)
if dist < EPS {
// fmt.Println("hit")
normal := Gradient(scene, p, EPS).Normalized()
return true, phongShading(p, normal, mat)
}
if dist > MAX_DIST {
break
}
// fmt.Println("Before")
// fmt.Println(p, dist)
p = p.Add(direction.Scale(dist))
// fmt.Println("After")
// fmt.Println(p, DistanceOnly(scene, p))
}
return false, GREY
}
@ -70,7 +75,6 @@ func rayMarch(origin Vector3, direction Vector3) (bool, Vector3) {
func genImage() *rl.Image {
screenCartesian, _, theta_vec, phi_vec := screenSpherical.SphericalToCartesian()
screenCenter := cameraPos.Add(screenCartesian)
// fmt.Println(theta_vec, phi_vec)
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))
direction := (origin.Sub(cameraPos)).Normalized()
// fmt.Println(origin, direction)
hit, color_vec := rayMarch(origin, direction)
var fr, fg, fb float64
@ -100,10 +103,6 @@ func genImage() *rl.Image {
}
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")
texture := rl.LoadTextureFromImage(genImage())

View file