Skip to content

Instantly share code, notes, and snippets.

@selfsame
Created September 6, 2014 23:13
Show Gist options
  • Save selfsame/ca198aa74677fd2408d4 to your computer and use it in GitHub Desktop.
Save selfsame/ca198aa74677fd2408d4 to your computer and use it in GitHub Desktop.
(ns hard.core
(:import
[UnityEngine Debug Resources PrimitiveType Application]))
(defn log [x]
(UnityEngine.Debug/Log x))
;(defn math [f & more]
; (. UnityEngine.Mathf (f more)))
(defn resource [s] (UnityEngine.Resources/Load s))
;(. UnityEngine.Resources (Load "bike"))
(defn vector3? [x]
(if (instance? UnityEngine.Vector3 x) true false))
(defn transform? [x]
(if (instance? UnityEngine.Transform x) true false))
(defn gameobject? [x]
(if (instance? UnityEngine.GameObject x) true false))
(def primitive-map {
:cube UnityEngine.PrimitiveType/Cube
:plane UnityEngine.PrimitiveType/Plane
:sphere UnityEngine.PrimitiveType/Sphere
:cylinder UnityEngine.PrimitiveType/Cylinder
:quad UnityEngine.PrimitiveType/Quad
:capsule UnityEngine.PrimitiveType/Capsule})
(defn primitive [kw]
(GameObject/CreatePrimitive (kw primitive-map)))
(defn find-name [str] (. GameObject (Find str)))
(defn clone
([ref] (clone ref false))
([ref pos]
(let [source (if (string? ref) (find-name ref) ref)
gob (. GameObject (Instantiate source))]
(when pos (position! gob pos))
gob)))
(defn vec3 [o]
(cond (vector3? o) o
(sequential? o) (let [[x y z] o] (Vector3. (or x 0) (or y 0) (or z 0)))
(transform? o) (.position o)
(gameobject? o) (.position (.transform o))))
(defn position! [o pos]
(cond (gameobject? o) (set! (.position (.transform o)) (vec3 pos))))
(defn local-scale [o]
(cond (gameobject? o) (.localScale (.transform o) )))
(defn local-scale! [o v]
(cond (gameobject? o) (set! (.localScale (.transform o)) (vec3 v))))
(defn rotate-around! [o point axis angle]
(when (gameobject? o)
(. (.transform o) (RotateAround (vec3 point) (vec3 axis) angle))))
(defn destroy! [o]
(cond (gameobject? o)
(if (. Application isEditor)
(. GameObject (DestroyImmediate o))
(. GameObject (Destroy o)))
(sequential? o)
(mapv destroy! o)) nil)
(def cube (primitive :cube))
(def pattern (vec (for [x (range 1 20)
z (range 1 20)
:when (== 0 (mod x z))
:let [c (clone cube)]]
(do (position! c (mapv * [x 0 z] [1.5 0 1.5])) c))))
(defn vec3*n [o n]
(Vector3/Scale (vec3 o) (vec3 [n n n])))
;(set! (.color (.material (.renderer cube))) (Vector4. 1.0 0.2 0.5 1.0))
(defn mud [n1 n2]
(let [clean (fn [x] (inc (. UnityEngine.Mathf (Abs x))))]
(mod (clean n1) (clean n2))))
(defn gen-bug []
{:form (rand-nth [:cube :sphere :cylinder])
:scale [(+ (rand) 0.2)
(+ (rand) 0.2)
(+ (rand) 0.2)]
:symetry (rand-nth [:radial :bilateral])
:subdivide (int (* (rand) 10))})
(defn make-part [part pos angle parent]
(let [ob (primitive (:form part))]
(local-scale! ob (:scale part))
(position! ob pos) ob))
(doall
(for [x (range 20)
z (range 20)
:let [bug (gen-bug)
loc (mapv * [x 0 z] [3 0 3])
base (make-part bug loc [0 0 1] false)
bscale (:scale bug)
arm (make-part
bug
(mapv + loc [0 (* 0.5 (get bscale 1)) 0])
[0 0 1] bug)]]
[base arm]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment