Skip to content

Instantly share code, notes, and snippets.

@Aeva
Created June 12, 2022 05:19
Show Gist options
  • Save Aeva/b5ae89232ec7e5bb7cbfb989f16348ae to your computer and use it in GitHub Desktop.
Save Aeva/b5ae89232ec7e5bb7cbfb989f16348ae to your computer and use it in GitHub Desktop.
Seaside Town
#lang racket/base
; Copyright 2022 Aeva Palecek
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
; http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and
; limitations under the License.
(require racket/list)
(require tangerine vec)
(require tangerine/eval)
(provide model)
(define clip (cube 10))
(define seascape
(let* ([sea (align 0 0 1 (box 10 10 50))]
[land (blend inter 1
(rotate-y 10
(rotate-x -10
(move 6 7 0 (cylinder 20 10))))
(move 20 20 -42
(sphere 100)))]
[fill (paint #x5f4d2e (move-z -.1 land))]
[crust (paint #x3da447 (diff land fill))]
[land (union crust fill)]
[shore (paint 'green (diff land sea))]
[seabed (paint 'red (inter land sea))]
[depths-dist -.05]
[depths (move-z depths-dist sea)]
[depths-cut (move-z depths-dist (plane 0 0 1))]
[water (union
(paint #x4974fc (diff sea land depths-cut))
(paint #x0c32ab (diff depths land)))])
(inter clip
(union water land))))
(define (tree x y z size)
(let ([half (* size .5)]
[third (* size 1/3)]
[tuft
(paint 'green
(move x y z
(align 0 0 (* -1. (random))
(sphere size))))])
(if (size . < . 0.1)
tuft
(blend union third tuft
(tree x y (+ z half) half)))))
(define (house x y z side height)
(move x y z
(align 0 0 -1
(rotate-z (+ 44 (* (random) 2.))
(box side side height)))))
(define cityscape
(inter clip
(let* ([evaluator (sdf-build seascape)])
(begin0
(apply union
(filter
(λ (x) (not (null? x)))
(for*/list ([y (in-range -5.125 5.125 .25)]
[x (in-range -5.125 5.125 .25)])
(let-values ([(hit hit-x hit-y hit-z) (sdf-ray-march evaluator x y 50. 0. 0. -1.)])
(if (and hit (hit-z . > . 0.1))
(let* ([side (* (random) .25)]
[height (+ (* (random) side 1.1) side)]
[tree-size (+ (* (random) .25) .15)])
(if (side . > . 0.125)
(house x y hit-z side height)
(tree x y hit-z tree-size)))
null)))))
(sdf-free evaluator)))))
(define model (union cityscape seascape))
@Aeva
Copy link
Author

Aeva commented Jun 12, 2022

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment