Skip to content

Instantly share code, notes, and snippets.

@timsgardner
Last active September 3, 2015 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timsgardner/929ab154f9335f7c057b to your computer and use it in GitHub Desktop.
Save timsgardner/929ab154f9335f7c057b to your computer and use it in GitHub Desktop.
seascape snippet
// from "Seascape" by Alexander Alekseev aka TDM - 2014
// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
float heightMapTracing(vec3 ori, vec3 dir, out vec3 p) { ```
float tm = 0.0;
float tx = 1000.0;
float hx = map(ori + dir * tx);
if(hx > 0.0) return tx;
float hm = map(ori + dir * tm);
float tmid = 0.0;
for(int i = 0; i < NUM_STEPS; i++) {
tmid = mix(tm,tx, hm/(hm-hx));
p = ori + dir * tmid;
float hmid = map(p);
if(hmid < 0.0) {
tx = tmid;
hx = hmid;
} else {
tm = tmid;
hm = hmid;
}
}
return tmid;
}
(defn height-map-tracing [ori ; vec3
dir ; vec3
p ; vec3 <- "OUT"
{:keys [num-steps]
:as opts}]
(if (> hx 0)
tx
(loop [i 0,
p p,
;; using a vector so we only need 1 g/if. benchmarks would be nice.
tx-hx-tm-hm (g/vec4
;; maybe unity profiler supports this?
1000
(sea-map (g/+ ori dir tx) opts)
0
(sea-map (g/+ ori (g/* dir tm))))]
(let [[tx hx tm hm] (gvdest tx-hx-tm-hm)]
(if (< i num-steps)
(let [tmid (mix tm, tx, (g/div hm (g/- hm hx)))
p2 (g/+ ori (g/* dir tmid))
hmid (sea-map p opts)]
(recur
(inc i),
p2,
(g/if (g/< hmid 0)
(g/vec4 tmid hmid tm hm)
(g/vec4 tx hx tmid hmid))))
{:p p, :tmid tmid})))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment