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

10
src/go.mod Normal file
View file

@ -0,0 +1,10 @@
module test_raylib
go 1.24.7
require (
github.com/ebitengine/purego v0.9.0 // indirect
github.com/gen2brain/raylib-go/raylib v0.55.1 // indirect
golang.org/x/exp v0.0.0-20250911091902-df9299821621 // indirect
golang.org/x/sys v0.36.0 // indirect
)

8
src/go.sum Normal file
View file

@ -0,0 +1,8 @@
github.com/ebitengine/purego v0.9.0 h1:mh0zpKBIXDceC63hpvPuGLiJ8ZAa3DfrFTudmfi8A4k=
github.com/ebitengine/purego v0.9.0/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
github.com/gen2brain/raylib-go/raylib v0.55.1 h1:1rdc10WvvYjtj7qijHnV9T38/WuvlT6IIL+PaZ6cNA8=
github.com/gen2brain/raylib-go/raylib v0.55.1/go.mod h1:BaY76bZk7nw1/kVOSQObPY1v1iwVE1KHAGMfvI6oK1Q=
golang.org/x/exp v0.0.0-20250911091902-df9299821621 h1:2id6c1/gto0kaHYyrixvknJ8tUK/Qs5IsmBtrc+FtgU=
golang.org/x/exp v0.0.0-20250911091902-df9299821621/go.mod h1:TwQYMMnGpvZyc+JpB/UAuTNIsVJifOlSkrZkhcvpVUk=
golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=

92
src/material.go Normal file
View file

@ -0,0 +1,92 @@
package main
import "math"
type Color interface {
GetColor(Vector3) Vector3
}
type GridColor struct {
color1 Color
color2 Color
size float64
}
func (grid GridColor) GetColor(p Vector3) Vector3 {
ix := int(math.Floor(p.X / grid.size))
iy := int(math.Floor(p.Y / grid.size))
iz := int(math.Floor(p.Z/grid.size)) * 0
if (ix+iy+iz)%2 == 0 {
return grid.color1.GetColor(p)
}
return grid.color2.GetColor(p)
}
// Ambre je te copie
type Material struct {
ambiantColor Color
diffuseColor Color
diffuseFac float64
specularColor Color
specularFac float64
specularExp float64
reflectanceFac float64
reflectanceTint Color
refractFac float64
refractIndice float64
}
func DefaultMaterial(diffuseColor Color) Material {
return Material{
ambiantColor: Vector3{0, 0, 0},
diffuseColor: diffuseColor,
diffuseFac: 1,
specularColor: Vector3{255, 255, 255},
specularFac: 1,
specularExp: 35.0,
reflectanceFac: 0.0,
reflectanceTint: Vector3{255, 255, 255},
refractFac: 0.0,
refractIndice: 1.0,
}
}
func MixMat(mat1 Material, mat2 Material, t float64, p Vector3) Material {
return Material{
(mat1.ambiantColor.GetColor(p).Scale(1 - t)).Add((mat2.ambiantColor.GetColor(p)).Scale(t)),
(mat1.diffuseColor.GetColor(p).Scale(1 - t)).Add((mat2.diffuseColor.GetColor(p)).Scale(t)),
mat1.diffuseFac*(1-t) + mat2.diffuseFac*t,
mat1.specularColor.GetColor(p).Scale(1 - t).Add((mat2.specularColor.GetColor(p)).Scale(t)),
mat1.specularFac*(1-t) + mat2.specularFac*t,
mat1.specularExp*(1-t) + mat2.specularExp*t,
(mat1.reflectanceFac*(1-t) + mat2.reflectanceFac) * t,
mat1.reflectanceTint.GetColor(p).Scale(1 - t).Add((mat2.reflectanceTint.GetColor(p)).Scale(t)),
mat1.refractFac*(1-t) + mat2.refractFac*t,
mat1.refractIndice*(1-t) + mat2.refractIndice*t,
}
}
// Colors
var RED = Vector3{255, 0, 0}
var GREEN = Vector3{0, 255, 0}
var BLUE = Vector3{0, 0, 255}
var WHITE = Vector3{255, 255, 255}
var GREY = Vector3{127, 127, 127}
var WHITE_GREY_GRID = GridColor{WHITE, GREY, 5}
var RED_BLUE_GRID = GridColor{RED, BLUE, 5}
var GRID_OF_GRID = GridColor{WHITE_GREY_GRID, RED_BLUE_GRID, 10}
// Materials
var RED_MAT = DefaultMaterial(RED)
var GREEN_MAT = DefaultMaterial(GREEN)
var BLUE_MAT = DefaultMaterial(BLUE)
var WHITE_MAT = DefaultMaterial(WHITE)
var GREY_MAT = DefaultMaterial(GREY)
var WHITE_GREY_GRID_MAT = DefaultMaterial(WHITE_GREY_GRID)
var GRID_OF_GRID_MAT = DefaultMaterial(GRID_OF_GRID)

35
src/primitives_sdf.go Normal file
View file

@ -0,0 +1,35 @@
package main
// Sphere
type Sphere struct {
center Vector3
radius float64
material Material
}
func (s Sphere) Distance(p Vector3) (float64, Material) {
return p.Sub(s.center).Length() - s.radius, s.material
}
// Box
type Box struct {
center Vector3
dimensions Vector3
material Material
}
func (s Box) Distance(p Vector3) (float64, Material) {
q := p.Sub(s.center).Abs().Sub(s.dimensions)
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
material Material
}
func (s Plane) Distance(p Vector3) (float64, Material) {
return p.Dot(s.normal) - s.height, s.material
}

115
src/ray_marching.go Normal file
View file

@ -0,0 +1,115 @@
package main
// RIGHT : x
// FORWARD : Y
// UP : Z
import (
"math"
rl "github.com/gen2brain/raylib-go/raylib"
)
const MAX_DIST = 1000.0
const MAX_STEP = 1000
const EPS = 0.01
const WIDTH = 500
const HEIGHT = 500
const PI = math.Pi
var lightPos Vector3
var scene SmoothUnionSDF
// radius, theta, phi
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()
reflectLight := Reflect(lightVec, normal)
eyeVec := (cameraPos.Sub(point)).Normalized()
ambiant := mat.ambiantColor.GetColor(point).Scale(0.1)
diffusePower := mat.diffuseFac * max(0, normal.Dot(lightVec))
diffuse := mat.diffuseColor.GetColor(point).Scale(diffusePower)
specularPower := mat.specularFac * math.Pow(max(0, reflectLight.Dot(eyeVec)), mat.specularExp)
specular := mat.specularColor.GetColor(point).Scale(specularPower)
return ambiant.Add(diffuse).Add(specular)
}
func rayMarch(origin Vector3, direction Vector3) (bool, Vector3) {
p := origin
for range MAX_STEP {
dist, mat := scene.Distance(p)
if dist < EPS {
normal := Gradient(scene, p, EPS).Normalized()
return true, phongShading(p, normal, mat)
}
if dist > MAX_DIST {
break
}
p = p.Add(direction.Scale(dist))
}
return false, GREY
}
func genImage() *rl.Image {
screenCartesian, _, theta_vec, phi_vec := screenSpherical.SphericalToCartesian()
screenCenter := cameraPos.Add(screenCartesian)
image := rl.GenImageColor(WIDTH, HEIGHT, rl.Black)
for pixel_x := range WIDTH {
for pixel_y := range HEIGHT {
sx := screenPhysicalSize * (float64(pixel_x)/WIDTH - 0.5)
sy := screenPhysicalSize * (float64(pixel_y)/WIDTH - 0.5)
origin := screenCenter.Add(phi_vec.Scale(-sx)).Add(theta_vec.Scale(sy))
direction := (origin.Sub(cameraPos)).Normalized()
hit, color_vec := rayMarch(origin, direction)
var fr, fg, fb float64
if hit {
fr, fg, fb = color_vec.Unpack()
} else {
fr, fg, fb = 0, 0, 0
}
fr, fg, fb = Clamp(fr, 0, 255), Clamp(fg, 0, 255), Clamp(fb, 0, 255)
r, g, b := uint8(fr), uint8(fg), uint8(fb)
rl.ImageDrawPixel(image, int32(pixel_x), int32(pixel_y), rl.Color{R: r, G: g, B: b, A: 255})
}
}
return image
}
func main() {
rl.InitWindow(WIDTH, HEIGHT, "raymarching")
texture := rl.LoadTextureFromImage(genImage())
for !rl.WindowShouldClose() {
rl.BeginDrawing()
rl.ClearBackground(rl.Black)
rl.DrawTexture(texture, 0, 0, rl.White)
rl.EndDrawing()
}
}

162
src/sdf.go Normal file
View file

@ -0,0 +1,162 @@
package main
import (
"math"
)
type SDF interface {
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 := (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}
}
// Some transformations see https://iquilezles.org/articles/distfunctions/
type TranslatedSDF struct {
primitive SDF
translate Vector3
}
func (s TranslatedSDF) Distance(p Vector3) (float64, Material) {
return s.primitive.Distance(p.Sub(s.translate))
}
type RotatedSDF struct {
primitive SDF
rotVector Vector3
angle float64
}
func (s RotatedSDF) Distance(p Vector3) (float64, Material) {
rotated_p := Rotate(p, s.rotVector, s.angle)
return s.primitive.Distance(rotated_p)
}
type ScaledSDF struct {
primitive SDF
scale float64
}
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 {
primitive SDF
cellSize Vector3
}
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))
}
type UnionSDF struct {
primitive1 SDF
primitive2 SDF
}
func (s UnionSDF) Distance(p Vector3) (float64, Material) {
d1, color1 := s.primitive1.Distance(p)
d2, color2 := s.primitive2.Distance(p)
d := math.Min(d1, d2)
color := color1
if d2 < d1 {
color = color2
}
return d, color
}
type SubstractionSDF struct {
primitive1 SDF
primitive2 SDF
}
func (s SubstractionSDF) Distance(p Vector3) (float64, Material) {
d1, color1 := s.primitive1.Distance(p)
d2, _ := s.primitive2.Distance(p)
d := math.Max(-d1, d2)
return d, color1
}
type IntersectionSDF struct {
primitive1 SDF
primitive2 SDF
}
func (s IntersectionSDF) Distance(p Vector3) (float64, Material) {
d1, mat1 := s.primitive1.Distance(p)
d2, mat2 := s.primitive2.Distance(p)
d := math.Max(d1, d2)
mat := MixMat(mat1, mat2, 0.5, p)
return d, mat
}
type SmoothUnionSDF struct {
primitive1 SDF
primitive2 SDF
k float64
}
func (s SmoothUnionSDF) Distance(p Vector3) (float64, Material) {
k := 4 * s.k
d1, mat1 := s.primitive1.Distance(p)
d2, mat2 := s.primitive2.Distance(p)
h := math.Max(k-math.Abs(d1-d2), 0.0)
d := math.Min(d1, d2) - h*h*0.25/k
t := SmoothStep(d2-d1, -k, k)
mat := MixMat(mat2, mat1, t, p)
return d, mat
}
type SmoothSubstractionSDF struct {
primitive1 SDF
primitive2 SDF
k float64
}
func (s SmoothSubstractionSDF) Distance(p Vector3) (float64, Material) {
k := 4 * s.k
d1, mat1 := s.primitive1.Distance(p)
d2, mat2 := s.primitive2.Distance(p)
h := math.Max(k-math.Abs(-d1-d2), 0.0)
d := math.Max(-d1, d2) + h*h*0.25/k
t := SmoothStep(d2-d1, -k, k)
mat := MixMat(mat2, mat1, t, p)
return d, mat
}
type SmoothIntersectionSDF struct {
primitive1 SDF
primitive2 SDF
k float64
}
func (s SmoothIntersectionSDF) Distance(p Vector3) (float64, Material) {
k := 4 * s.k
d1, mat1 := s.primitive1.Distance(p)
d2, mat2 := s.primitive2.Distance(p)
h := math.Max(k-math.Abs(d1-d2), 0.0)
d := math.Max(d1, d2) + h*h*0.25/k
t := SmoothStep(d2-d1, -k, k)
mat := MixMat(mat2, mat1, t, p)
return d, mat
}

122
src/vec3.go Normal file
View file

@ -0,0 +1,122 @@
package main
import (
"math"
)
type Vector3 struct {
X, Y, Z float64
}
func (u Vector3) GetColor(p Vector3) Vector3 {
return u
}
func (u Vector3) Add(v Vector3) Vector3 {
return Vector3{u.X + v.X, u.Y + v.Y, u.Z + v.Z}
}
func (u Vector3) Neg() Vector3 {
return Vector3{-u.X, -u.Y, -u.Z}
}
func (u Vector3) Sub(v Vector3) Vector3 {
return u.Add(v.Neg())
}
func (u Vector3) Scale(a float64) Vector3 {
return Vector3{a * u.X, a * u.Y, a * u.Z}
}
func (u Vector3) Dot(v Vector3) float64 {
return u.X*v.X + u.Y*v.Y + u.Z*v.Z
}
func (u Vector3) Cross(v Vector3) Vector3 {
return Vector3{u.Y*v.Z - u.Z*v.Y, u.Z*v.X - u.X*v.Z, u.X*v.Y - u.Y*v.X}
}
func (u Vector3) LengthSquared() float64 {
return u.Dot(u)
}
func (u Vector3) Length() float64 {
return math.Sqrt(u.LengthSquared())
}
func (u Vector3) Normalized() Vector3 {
return u.Scale(1.0 / u.Length())
}
func (u Vector3) Round() Vector3 {
round := math.Round
return Vector3{round(u.X), round(u.Y), round(u.Z)}
}
func (u Vector3) Abs() Vector3 {
abs := math.Abs
return Vector3{abs(u.X), abs(u.Y), abs(u.Z)}
}
func (u Vector3) Max(x float64) Vector3 {
return Vector3{max(u.X, x), max(u.Y, x), max(u.Z, x)}
}
func (u Vector3) Min(x float64) Vector3 {
return Vector3{min(u.X, x), min(u.Y, x), min(u.Z, x)}
}
// i incident, n normal. Both vector should be normalized
func Reflect(i Vector3, n Vector3) Vector3 {
y := i.Dot(n)
return (n.Scale(2 * y)).Sub(i)
}
// Todo : Refract
// Rodrigues' rotation formula. rotVector should be normalized
func Rotate(u Vector3, rotVector Vector3, angle float64) Vector3 {
cos, sin := math.Cos(angle), math.Sin(angle)
vec1 := u.Scale(cos)
vec2 := rotVector.Cross(u).Scale(sin)
vec3 := rotVector.Scale(rotVector.Dot(u) * (1 - cos))
return vec1.Add(vec2).Add(vec3)
}
func Mix(u Vector3, v Vector3, k float64) Vector3 {
l := (1 - k)
return Vector3{u.X*l + v.X*k, u.Y*l + v.Y*k, u.Z*l + v.Z*k}
}
func (v Vector3) Unpack() (float64, float64, float64) {
return v.X, v.Y, v.Z
}
// Return position, and units vectors of spherical into cartesian coords (r, theta, phi) units vecs
func (v Vector3) SphericalToCartesian() (Vector3, Vector3, Vector3, Vector3) {
p, theta, phi := v.Unpack()
cos_theta, sin_theta := math.Cos(theta), math.Sin(theta)
cos_phi, sin_phi := math.Cos(phi), math.Sin(phi)
cart_pos := Vector3{p * cos_theta * cos_phi, p * sin_theta * sin_phi, p * cos_theta}
unit_vec_r := Vector3{sin_theta * cos_phi, sin_theta * cos_phi, cos_theta}
unit_vec_theta := Vector3{cos_theta * cos_phi, cos_theta * sin_phi, -sin_theta}
unit_vec_phi := Vector3{-sin_phi, cos_phi, 0}
return cart_pos, unit_vec_r, unit_vec_theta, unit_vec_phi
}
// Others maths thingies
func Clamp(x float64, a float64, b float64) float64 {
return min(max(x, a), b)
}
func (v Vector3) Clamp(a float64, b float64) Vector3 {
x, y, z := v.Unpack()
x, y, z = Clamp(x, a, b), Clamp(y, a, b), Clamp(z, a, b)
return Vector3{x, y, z}
}
func SmoothStep(x float64, a float64, b float64) float64 {
x = Clamp((x-a)/(b-a), 0, 1)
return x * x * (3.0 - 2.0*x)
}