-
Notifications
You must be signed in to change notification settings - Fork 0
/
solstrale-json.go
572 lines (479 loc) · 12.4 KB
/
solstrale-json.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// Package solstralejson provides functions to convert json data to a solstrale scene object
package solstralejson
import (
"context"
_ "embed"
"encoding/json"
"errors"
"fmt"
"image"
_ "image/jpeg"
_ "image/png"
"os"
"strings"
"github.com/DanielPettersson/solstrale/camera"
"github.com/DanielPettersson/solstrale/geo"
"github.com/DanielPettersson/solstrale/hittable"
"github.com/DanielPettersson/solstrale/material"
"github.com/DanielPettersson/solstrale/post"
"github.com/DanielPettersson/solstrale/renderer"
"github.com/qri-io/jsonschema"
)
var (
//go:embed schema.json
schemaBytes []byte
schema *jsonschema.Schema = &jsonschema.Schema{}
schemCtx context.Context = context.Background()
imageCache map[string]image.Image = make(map[string]image.Image)
)
func init() {
if err := json.Unmarshal(schemaBytes, schema); err != nil {
panic("unmarshal schema: " + err.Error())
}
}
// ToScene takes a slice of bytes representing json as input and returns a scene.
// If json is not properly formatted an error is returned describing the formatting issue.
func ToScene(jsonBytes []byte) (*renderer.Scene, error) {
err := validateSchema(jsonBytes)
if err != nil {
return nil, err
}
var data map[string]interface{}
json.Unmarshal(jsonBytes, &data)
return toScene(data)
}
func validateSchema(jsonBytes []byte) error {
context.Background()
result, err := schema.ValidateBytes(schemCtx, jsonBytes)
if err != nil {
return err
}
if len(result) == 0 {
return nil
} else {
var msgs []string
for _, e := range result {
msgs = append(msgs, e.Message)
}
return errors.New(strings.Join(msgs, "\n"))
}
}
func toScene(data map[string]interface{}) (*renderer.Scene, error) {
world, err := getHittable(data, "world")
if err != nil {
return nil, err
}
renderConfig := toRenderConfig(getObject(data, "renderConfig"))
camera := toCamera(getObject(data, "camera"), renderConfig.ImageWidth, renderConfig.ImageHeight)
return &renderer.Scene{
World: world,
Cam: camera,
BackgroundColor: getColor(data, "background"),
RenderConfig: renderConfig,
}, nil
}
func toRenderConfig(data map[string]interface{}) renderer.RenderConfig {
return renderer.RenderConfig{
ImageWidth: getInt(data, "imageWidth"),
ImageHeight: getInt(data, "imageHeight"),
SamplesPerPixel: getInt(data, "samplesPerPixel"),
Shader: toShader(getObject(data, "shader")),
PostProcessor: toPostProcessor(getNillableObject(data, "postProcessor")),
}
}
func toShader(data map[string]interface{}) renderer.Shader {
switch getString(data, "type") {
case "pathTracing":
return toPathTracing(data)
case "albedo":
return renderer.AlbedoShader{}
case "normal":
return renderer.NormalShader{}
default: // simple
return renderer.SimpleShader{}
}
}
func toPathTracing(data map[string]interface{}) renderer.Shader {
return renderer.PathTracingShader{
MaxDepth: getInt(data, "maxDepth"),
}
}
func toPostProcessor(data map[string]interface{}) post.PostProcessor {
if data == nil {
return nil
}
switch getString(data, "type") {
default: // oidn
return toOidn(data)
}
}
func toOidn(data map[string]interface{}) post.PostProcessor {
return post.OidnPostProcessor{
OidnDenoiseExecutablePath: getString(data, "oidnDenoiseExecutablePath"),
}
}
func getHittable(data map[string]interface{}, key string) (hittable.Hittable, error) {
object := getObject(data, key)
return toHittable(object)
}
func toHittable(data map[string]interface{}) (hittable.Hittable, error) {
switch getString(data, "type") {
case "bvh":
return toBvh(data)
case "constantMedium":
return toConstantMedium(data)
case "hittableList":
return toHittableList(data)
case "motionBlur":
return toMotionBlur(data)
case "quad":
return toQuad(data)
case "box":
return toBox(data)
case "rotationY":
return toRotationY(data)
case "sphere":
return toSphere(data)
case "triangle":
return toTriangle(data)
case "objModel":
return toObjModel(data)
default: // translation
return toTranslation(data)
}
}
func toBvh(data map[string]interface{}) (hittable.Hittable, error) {
list := data["list"].([]interface{})
items := make([]hittable.Hittable, 0, len(list))
for _, itemData := range list {
item := toObject(itemData)
hittable, err := toHittable(item)
if err != nil {
return nil, err
}
items = append(items, hittable)
}
bvh := hittable.NewBoundingVolumeHierarchy(items)
return bvh, nil
}
func toConstantMedium(data map[string]interface{}) (hittable.Hittable, error) {
object, err := getHittable(data, "object")
if err != nil {
return nil, err
}
texture, err := getTexture(data, "texture")
if err != nil {
return nil, err
}
constantMedium := hittable.NewConstantMedium(
object,
getFloat(data, "density"),
texture,
)
return constantMedium, nil
}
func toHittableList(data map[string]interface{}) (hittable.Hittable, error) {
list := data["list"].([]interface{})
items := hittable.NewHittableList()
for _, itemData := range list {
item := toObject(itemData)
hittable, err := toHittable(item)
if err != nil {
return nil, err
}
items.Add(hittable)
}
return &items, nil
}
func toMotionBlur(data map[string]interface{}) (hittable.Hittable, error) {
object, err := getHittable(data, "object")
if err != nil {
return nil, err
}
motionBlur := hittable.NewMotionBlur(
object,
getVec(data, "blurDirection"),
)
return motionBlur, nil
}
func toQuad(data map[string]interface{}) (hittable.Hittable, error) {
mat, err := getMaterial(data, "mat")
if err != nil {
return nil, err
}
quad := hittable.NewQuad(
getVec(data, "corner"),
getVec(data, "dirU"),
getVec(data, "dirV"),
mat,
)
return quad, nil
}
func toBox(data map[string]interface{}) (hittable.Hittable, error) {
mat, err := getMaterial(data, "mat")
if err != nil {
return nil, err
}
box := hittable.NewBox(
getVec(data, "corner"),
getVec(data, "diagonalCorner"),
mat,
)
return box, nil
}
func toRotationY(data map[string]interface{}) (hittable.Hittable, error) {
object, err := getHittable(data, "object")
if err != nil {
return nil, err
}
rotationY := hittable.NewRotationY(
object,
getFloat(data, "angle"),
)
return rotationY, nil
}
func toSphere(data map[string]interface{}) (hittable.Hittable, error) {
mat, err := getMaterial(data, "mat")
if err != nil {
return nil, err
}
sphere := hittable.NewSphere(
getVec(data, "center"),
getFloat(data, "radius"),
mat,
)
return sphere, nil
}
func toTriangle(data map[string]interface{}) (hittable.Hittable, error) {
mat, err := getMaterial(data, "mat")
if err != nil {
return nil, err
}
triangle := hittable.NewTriangle(
getVec(data, "v0"),
getVec(data, "v1"),
getVec(data, "v2"),
mat,
)
return triangle, nil
}
func toObjModel(data map[string]interface{}) (hittable.Hittable, error) {
matData := getNillableObject(data, "mat")
path := getString(data, "path")
if matData != nil {
mat, err := toMaterial(matData)
if err != nil {
return nil, err
}
objModel, err := hittable.NewObjModelWithDefaultMaterial(path, mat)
if err != nil {
return nil, err
}
return objModel, nil
}
objModel, err := hittable.NewObjModel(path)
if err != nil {
return nil, err
}
return objModel, nil
}
func toTranslation(data map[string]interface{}) (hittable.Hittable, error) {
object, err := getHittable(data, "object")
if err != nil {
return nil, err
}
translation := hittable.NewTranslation(
object,
getVec(data, "offset"),
)
return translation, nil
}
func getTexture(data map[string]interface{}, key string) (material.Texture, error) {
object := getObject(data, key)
return toTexture(object)
}
func toTexture(data map[string]interface{}) (material.Texture, error) {
t := getString(data, "type")
switch t {
case "solidColor":
return toSolidColor(data), nil
case "checker":
return toChecker(data)
case "image":
return toImage(data)
default: // noise
return toNoise(data), nil
}
}
func toSolidColor(data map[string]interface{}) material.Texture {
color := getColor(data, "color")
return material.SolidColor{
ColorValue: color,
}
}
func toChecker(data map[string]interface{}) (material.Texture, error) {
even, err := getTexture(data, "even")
if err != nil {
return nil, err
}
odd, err := getTexture(data, "odd")
if err != nil {
return nil, err
}
return material.CheckerTexture{
Scale: getFloat(data, "scale"),
Even: even,
Odd: odd,
}, nil
}
func toImage(data map[string]interface{}) (material.Texture, error) {
path := getString(data, "path")
var im image.Image
cachedImage, cached := imageCache[path]
if cached {
im = cachedImage
} else {
f, err := os.Open(path)
if err != nil {
return nil, errors.New(fmt.Sprintf("Failed loading image: %v", err.Error()))
}
defer f.Close()
im, _, err = image.Decode(f)
if err != nil {
return nil, err
}
imageCache[path] = im
}
imageTexture := material.ImageTexture{
Image: im,
Mirror: getBool(data, "mirror"),
}
return imageTexture, nil
}
func toNoise(data map[string]interface{}) material.Texture {
return material.NoiseTexture{
ColorValue: getColor(data, "color"),
Scale: getFloat(data, "scale"),
}
}
func getMaterial(data map[string]interface{}, key string) (material.Material, error) {
object := getObject(data, key)
return toMaterial(object)
}
func toMaterial(data map[string]interface{}) (material.Material, error) {
switch getString(data, "type") {
case "lambertian":
return toLambertian(data)
case "metal":
return toMetal(data)
case "dielectric":
return toDielectric(data)
default: // "diffuseLight"
return toDiffuseLight(data), nil
}
}
func toLambertian(data map[string]interface{}) (material.Material, error) {
texture, err := getTexture(data, "texture")
if err != nil {
return nil, err
}
lambertian := material.Lambertian{
Tex: texture,
}
return lambertian, nil
}
func toMetal(data map[string]interface{}) (material.Material, error) {
texture, err := getTexture(data, "texture")
if err != nil {
return nil, err
}
metal := material.Metal{
Tex: texture,
Fuzz: getFloat(data, "fuzz"),
}
return metal, nil
}
func toDielectric(data map[string]interface{}) (material.Material, error) {
texture, err := getTexture(data, "texture")
if err != nil {
return nil, err
}
metal := material.Dielectric{
Tex: texture,
IndexOfRefraction: getFloat(data, "indexOfRefraction"),
}
return metal, nil
}
func toDiffuseLight(data map[string]interface{}) material.Material {
return material.DiffuseLight{
Emit: material.SolidColor{
ColorValue: getColor(data, "color"),
},
}
}
func toCamera(data map[string]interface{}, imageWidth, imageHeight int) camera.Camera {
verticalFovDegrees := getFloat(data, "verticalFovDegrees")
apertureSize := getFloat(data, "apertureSize")
focusDistance := getFloat(data, "focusDistance")
lookFrom := getVec(data, "lookFrom")
lookAt := getVec(data, "lookAt")
vup := getVec(data, "vup")
return camera.New(
imageWidth,
imageHeight,
verticalFovDegrees,
apertureSize,
focusDistance,
lookFrom,
lookAt,
vup,
)
}
func getVec(data map[string]interface{}, key string) geo.Vec3 {
vecData := getObject(data, key)
return toVec(vecData)
}
func toVec(data map[string]interface{}) geo.Vec3 {
x := getFloat(data, "x")
y := getFloat(data, "y")
z := getFloat(data, "z")
return geo.NewVec3(x, y, z)
}
func getColor(data map[string]interface{}, key string) geo.Vec3 {
colorData := getObject(data, key)
return toColor(colorData)
}
func toColor(data map[string]interface{}) geo.Vec3 {
r := getFloat(data, "r")
g := getFloat(data, "g")
b := getFloat(data, "b")
return geo.NewVec3(r, g, b)
}
func getFloat(data map[string]interface{}, key string) float64 {
return data[key].(float64)
}
func getInt(data map[string]interface{}, key string) int {
return int(data[key].(float64))
}
func getString(data map[string]interface{}, key string) string {
return data[key].(string)
}
func getBool(data map[string]interface{}, key string) bool {
return data[key].(bool)
}
func getObject(data map[string]interface{}, key string) map[string]interface{} {
return toObject(data[key])
}
func toObject(data interface{}) map[string]interface{} {
return data.(map[string]interface{})
}
func getNillableObject(data map[string]interface{}, key string) map[string]interface{} {
return toNillableObject(data[key])
}
func toNillableObject(data interface{}) map[string]interface{} {
if data == nil {
return nil
} else {
return data.(map[string]interface{})
}
}