repl.it
@turbio/

console ray tracer

Go

a ray tracer rendering to ascii characters

fork
loading
Files
  • main.go
  • color.go
  • main
  • screen.go
  • sphere.go
  • vec.go
  • Packager files
  • go.mod
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package main

import (
	"math"
	"time"
)

const recurDepth = 2
const samples = 1

var black = &Color{0, 0, 0}

type Scene struct {
	Camera  *Vec
	Spheres []*Sphere
	Lights  []*Light

	X1 *Vec
	X2 *Vec
	X3 *Vec
	X4 *Vec

	Ambient *Color
}

func occluded(scene *Scene, sphere *Sphere, shadowRay *Ray) bool {
	for _, s2 := range scene.Spheres {
		if s2 == sphere {
			continue
		}

		t2, ok := s2.Intersects(shadowRay)

		if !ok {
			continue
		}

		if t2 < 1 && t2 > 0 {
			return true
		}
	}

	return false
}

func rayColor(ray *Ray, scene *Scene, depth int) *Color {
	min := struct {
		*Sphere
		t float64
	}{}

	for _, s := range scene.Spheres {
		t, ok := s.Intersects(ray)

		if !ok {
			continue
		}

		if min.Sphere == nil || t < min.t {
			min.Sphere = s
			min.t = t
		}
	}

	if min.Sphere == nil {
		return black
	}

	color := min.Sphere.Material.Ambient.Times(scene.Ambient)

	intersectPoint := ray.Point.Add(ray.Direction.Scale(min.t))
	normalVec := intersectPoint.Sub(min.Sphere.Center)

	for _, l := range scene.Lights {
		lightVec := l.Position.Sub(intersectPoint).Normalize()
		normalLightDot := normalVec.Dot(lightVec)

		if normalLightDot < 0 {
			continue
		}

		shadowRay := &Ray{
			Point:     intersectPoint,
			Direction: l.Position.Sub(intersectPoint),
		}

		if occluded(scene, min.Sphere, shadowRay) {
			continue
		}

		color = color.Add(
			min.Sphere.Material.Diffuse.
				Times(l.Diffuse).
				Scale(normalLightDot),
		)

		reflectance := normalVec.Scale(2 * normalLightDot).Sub(lightVec)
		viewVec := scene.Camera.Sub(intersectPoint).Normalize()

		specular := l.Specular.
			Times(min.Sphere.Material.Specular).
			Scale(
				math.Pow(reflectance.Dot(viewVec),
					min.Sphere.Material.Shininess),
			)

		color = color.Add(specular)

	}

	if depth < recurDepth {
		v := ray.Direction.Scale(-1).Normalize()
		reflectance := normalVec.Scale(2 * v.Dot(normalVec)).Sub(v)

		newRay := &Ray{
			Point:     intersectPoint.Add(&Vec{.0001, .0001, .0001}),
			Direction: reflectance,
		}

		color = color.Add(
			rayColor(newRay, scene, depth+1).
				Times(min.Sphere.Material.Reflectivity),
		)
	}

	return color.Clamp()
}

func colorAt(scene *Scene, alpha, beta float64) *Color {
	top := scene.X1.Lerp(scene.X2, alpha)
	bottom := scene.X3.Lerp(scene.X4, alpha)

	pointOnPlane := top.Lerp(bottom, beta)

	ray := &Ray{
		pointOnPlane,
		pointOnPlane.Sub(scene.Camera),
	}

	return rayColor(ray, scene, 0)
}

func renderAA(minAlpha, maxAlpha, minBeta, maxBeta float64, scene *Scene) *Color {
	color := &Color{}

	for sampX := 0; sampX < samples; sampX++ {
		for sampY := 0; sampY < samples; sampY++ {
			xt := float64(sampX) / float64(samples)
			yt := float64(sampY) / float64(samples)

			alpha := ((1 - xt) * minAlpha) + (maxAlpha * xt)
			beta := ((1 - yt) * minBeta) + (maxBeta * yt)

			color = color.Add(colorAt(scene, alpha, beta))
		}
	}

	color = color.Div(math.Pow(samples, 2))

	return color

}

func render(scene *Scene, screen *Screen) {
	type Job struct {
		minAlpha float64
		maxAlpha float64
		minBeta  float64
		maxBeta  float64
		x        int
		y        int
	}

	toRender := make(chan *Job)

	for i := 0; i < 8; i++ {
		go func() {
			for {
				j := <-toRender
				screen.Set(
					j.x,
					j.y,
					renderAA(j.minAlpha, j.maxAlpha, j.minBeta, j.maxBeta, scene),
				)
			}
		}()
	}

	for x := 0; x < screen.W; x++ {
		for y := 0; y < screen.H; y++ {
			minAlpha := float64(x) / float64(screen.W)
			minBeta := float64(y) / float64(screen.H)
			maxAlpha := float64(x+1) / float64(screen.W)
			maxBeta := float64(y+1) / float64(screen.H)

			//fmt.Println(minAlpha, minBeta, maxAlpha, maxBeta, toRender)

			toRender <- &Job{
				minAlpha: minAlpha,
				maxAlpha: maxAlpha,
				minBeta:  minBeta,
				maxBeta:  maxBeta,
				x:        x,
				y:        y,
			}
		}
	}
}

func run(screen *Screen) {
	for {
		time.Sleep(100 * time.Millisecond)
		t := float64(time.Now().UnixNano()/1000000) / 1000.0

		ratio := (float64(screen.W) / float64(screen.H)) / 1.5

		scene := &Scene{
			Camera: &Vec{0, 0, -1},
			Spheres: []*Sphere{
				&Sphere{
					Center: &Vec{0, 0, 2},
					Radius: 2,
					Material: &Material{
						Ambient:      &Color{0.1, 0.1, 0.0},
						Diffuse:      &Color{.05, 1, .5},
						Specular:     &Color{.2, .2, .2},
						Reflectivity: &Color{0, 0, 0},
						Shininess:    0,
					},
				},
			},
			Lights: []*Light{
				&Light{
					Position: &Vec{math.Cos(t), 1.0, math.Sin(t)},
					Diffuse:  &Color{1, 1, 1},
					Specular: &Color{.1, .1, .1},
				},
				&Light{
					Position: &Vec{math.Cos(t + 3.14), 1.0, math.Sin(t + 3.14)},
					Diffuse:  &Color{1, 1, 1},
					Specular: &Color{.1, .1, .1},
				},
			},
			X1:      &Vec{1, ratio, 0},
			X2:      &Vec{-1, ratio, 0},
			X3:      &Vec{1, -ratio, 0},
			X4:      &Vec{-1, -ratio, 0},
			Ambient: &Color{.3, .3, .3},
		}

		renderStart := time.Now()
		render(scene, screen)

		screen.Push()

		_ = time.Since(renderStart).Nanoseconds()
	}
}

func main() {
	const width = 33
	const height = 20

	screen := NewScreen(width, height)
	run(screen)
}
Fetching token
?