Created
November 15, 2014 00:36
-
-
Save nasser/59bb0621ab257be8aa10 to your computer and use it in GitHub Desktop.
pure functional composable mesh generation
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
(import Selection MeshFilter Vector3 Mesh Mathf) | |
(use 'clojure.repl) | |
;; mesh updates | |
(defn update-mesh! [^MeshFilter mf vs tris] | |
(set! (.. mf mesh vertices) | |
(into-array Vector3 vs)) | |
(set! (.. mf mesh triangles) | |
(into-array System.Int32 tris))) | |
(defn swap-mesh! [^MeshFilter mf vs tris] | |
(set! (.. mf mesh) nil) | |
(update-mesh! mf vs tris)) | |
;; triangle index functions | |
(defn tri-strip | |
([size] (tri-strip 0 size)) | |
([start size] | |
(->> (range start (+ start size)) | |
(partition 4 1) | |
(map (fn [[a b c d]] [a b c | |
c b d])) | |
flatten))) | |
(defn tri-fan | |
([size] (tri-fan 0 size)) | |
([start size] | |
(->> (range (inc start) (+ start size)) | |
(partition 2 1) | |
(map #(conj % start)) | |
flatten))) | |
(defn tris-disjoint | |
([size] (tris 0 size)) | |
([start size] | |
(->> (range start (+ start size)) | |
(partition 3) | |
flatten))) | |
;; mesh constructor | |
(defn mesh* [vs trifn] | |
[vs (trifn (count vs))]) | |
;; spiral mesh | |
(defn spiral [size outer-r inner-r resolution z-speed] | |
(let [vs (reduce (fn [acc i] | |
(let [j (* resolution i) | |
x0 (* inner-r (Mathf/Cos j)) | |
y0 (* inner-r (Mathf/Sin j)) | |
x1 (* outer-r (Mathf/Cos j)) | |
y1 (* outer-r (Mathf/Sin j)) | |
z (* z-speed j)] | |
(concat acc | |
[(Vector3. x0 y0 z) | |
(Vector3. x1 y1 z)]))) | |
[] (range size))] | |
(mesh* vs tri-strip))) | |
;; select object w/mesh filter and meshrenderer and eval this to see the mesh | |
(let [obj Selection/activeObject | |
mf (.. obj (GetComponent MeshFilter))] | |
(apply swap-mesh! mf (spiral 140 100 50 2 10))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment