Last active
August 19, 2020 01:47
-
-
Save postspectacular/94a9b9ace2fd6ed57534c3d9d45bafc5 to your computer and use it in GitHub Desktop.
STL mesh viewer & WebWorker example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; base.js contains all of CLJS & thi.ng | |
(.importScripts js/self "base.js") | |
(ns meshworker | |
(:require-macros | |
[cljs-log.core :refer [debug info warn]]) | |
(:require | |
[thi.ng.math.core :as m] | |
[thi.ng.geom.core :as g] | |
[thi.ng.geom.matrix :as mat] | |
[thi.ng.geom.mesh.io :as mio] | |
[thi.ng.geom.gl.glmesh :as glm] | |
[thi.ng.strf.core :as f])) | |
(defn load-binary | |
[uri onload onerror] | |
(let [xhr (js/XMLHttpRequest.)] | |
(.open xhr "GET" uri true) | |
(set! (.-responseType xhr) "arraybuffer") | |
(set! (.-onload xhr) | |
(fn [e] | |
(if-let [buf (.-response xhr)] | |
(onload buf) | |
(when onerror (onerror xhr e))))) | |
(.send xhr))) | |
(defn build-mesh | |
[buf] | |
(let [t0 (f/timestamp) | |
mesh (mio/read-stl | |
(mio/wrapped-input-stream buf) | |
#(glm/gl-mesh % #{:fnorm})) | |
bounds (g/bounds mesh) | |
tx (-> mat/M44 | |
(g/scale (/ 1.0 (-> bounds :size :y))) | |
(g/translate (m/- (g/centroid bounds)))) | |
vertices (-> mesh .-vertices .-buffer) | |
fnormals (-> mesh .-fnormals .-buffer) | |
num (.-id mesh)] | |
(debug (- (f/timestamp) t0) "ms," num "triangles") | |
(.postMessage | |
js/self | |
;; message payload | |
#js [vertices fnormals num tx] | |
;; this is the list of buffers to transfer w/o copy | |
#js [vertices fnormals]))) | |
(defn load-mesh | |
[msg] | |
(load-binary | |
(.-data msg) | |
build-mesh | |
#(warn "error loading mesh: " (.-data msg)))) | |
(set! (.-onmessage js/self) load-mesh) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment