Skip to content

Instantly share code, notes, and snippets.

@timsgardner
Created July 18, 2015 23:01
Show Gist options
  • Save timsgardner/ba29f164e3248642244e to your computer and use it in GitHub Desktop.
Save timsgardner/ba29f164e3248642244e to your computer and use it in GitHub Desktop.
arcadia cube, vr demo
(let [ns 'clojure-west.demo]
(require ns)
(in-ns ns)
(use
'clojure.pprint
'clojure.repl
'clojure-west.freeze)
(require
;'[clojure-west.mesh :as mesh]
'[clojure-west.materials :as mat]
'[arcadia.introspection :as intro])
(import '[UnityEngine
Color
PhysicMaterial
Transform
Rigidbody
Time]
'[UnityEditor
Selection]))
(do
(def white
(mat/shader-material "Standard"))
(def black
(doto (mat/shader-material "Standard")
(set-with! [_ color]
(Color. 0 0 0))))
(defn physic-material ^PhysicMaterial
[{b :bounciness, sf :static-friction, df :dynamic-friction}]
(let [^PhysicMaterial pm (PhysicMaterial.)]
(when b (set-with! pm [_ bounciness] b))
(when sf (set-with! pm [_ staticFriction] sf))
(when df (set-with! pm [_ dynamicFriction] df))
pm))
(defn game-object-seq [x]
(tree-seq
#(instance? GameObject %)
(fn [^GameObject y]
(map (fn [^Transform tr]
(game-object tr))
(transform y)))
x))
(def ^GameObject sun
(object-named "sun"))
(defn aproprint [s]
(pprint (apropos s)))
(def bouncey-1
(physic-material
{:bounciness 0.7
:static-friction 0.8
:dynamic-friction 0.4}))
(def bounce-box
{:box-collider [{:material bouncey-1}]})
(defmacro def-tweak-children [name spec]
`(defn ~name [obj#]
(doseq [^Transform x# (get-component obj# UnityEngine.Transform)]
(populate! (.gameObject x#)
~spec))))
(def-tweak-children gravitate-children
{:rigidbody [{:use-gravity true}]})
(def-tweak-children ungravitate-children
{:rigidbody [{:use-gravity false}]})
(def-tweak-children bounce-children
bounce-box)
(def-compget rigidbody Rigidbody)
(def sphere-spec
(kill! (create-primitive :sphere))))
(defscn a-cubeq
(hydrate
(deep-merge-mv cubespec
{:transform [{:local-position (v3 0 200 20)
:local-scale (v3 1)}]
:rigidbody [{:mass (float 1)}]}
bounce-box)))
(defscn ramp
(let [length 100
length-tilt 10]
(hydrate
(deep-merge-mv cubespec
{:transform
[{:local-position (v3 0
(* length
(/ 2)
(Mathf/Sin
(* Mathf/Deg2Rad length-tilt)))
(- (/ length 2)))
:local-rotation (qq*
(aa 20 0 0 1)
(aa 10 1 0 0))
:local-scale (v3 20 1 length)}]}))))
(defscn cubes
(let [els (for [x (range 9)
y (range 90)]
(->
(deep-merge-mv cubespec bounce-box
{:transform
[{:local-position
(v3 x (+ 1 y) (- y))
:local-scale
(v3 1)}]
;:box-collider [{}]
:rigidbody [{:use-gravity true}]
})
(assoc :mesh-renderer [{:shared-material
(if (= (odd? x) (odd? y))
white
black)}])))]
(hydrate
{:name "cubes"
:transform [{:local-position (v3 1 1 10)}]
:children (vec els)})))
(defscn sky-cubes
(let [els (for [x (range 9)
y (range 90)]
(->
(deep-merge-mv cubespec ;bounce-box
{:transform
[{:local-position
(v3 x y 0)
:local-scale (v3 1.01 1.001 1)}]
:rigidbody [{:use-gravity false}]
})
(assoc :mesh-renderer [{:shared-material
(if (= (odd? x) (odd? y))
white
black)}])))]
(hydrate
{:name "sky-cubes"
:transform [{:local-position (v3 0 10 -20)}]
:children (vec els)})))
(defscn pyramids
(let [white (mat/shader-material "Standard")
black (doto (mat/shader-material "Standard")
(set-with! [_ color]
(Color. 0 0 0)))
els (for [x (range 2)
z (range 2)
y (range 50)]
(-> cubespec
(deep-merge-mv bounce-box
{:transform [{:local-position (v3
(* 10 x)
(+ 10 y )
(* 10 z))
:local-scale (v3
(- 10 (/ y 5))
1
(- 10 (/ y 5)))}]
;:box-collider [{}]
:rigidbody [{:use-gravity false
:mass (float 100)}]})
(assoc :mesh-renderer [{:shared-material
(if (odd? y)
white
black)}])))]
(hydrate
{:name "pyramids"
:transform [{:local-position (v3 1 1 70)
:local-rotation (aa 3 -1 0 1)
:local-scale (v3 1)}]
:children (vec els)})))
;; this one's fun
(gravitate-children pyramids)
(defscn cubes2
(let [els (for [x (range 10)
z (range 10)]
(deep-merge-mv cubespec
{:transform [{:local-position (v3 x 0 z)
:local-scale (v3 0.3)}]
:rigidbody [{
:use-gravity true
;:is-kinematic true
}]
}
))]
(hydrate
{:name "cubes"
:transform [{:local-position (v3 0 100 0)}]
:children (vec els)})))
(defscn stairs
(let [ymx 3000
stair-width 5
inner-width 1
dense? true
winds 50
stair-height 0.1
rot-ang (/ (* winds 360) ymx)
stair-depth (if dense?
(* (+ inner-width stair-width)
(Mathf/Sin (* Mathf/Deg2Rad rot-ang)))
1)
els (for [x [0]
y (range ymx)
:let [rot (aa (* y rot-ang)
0 1 0)]]
(->
(deep-merge-mv cubespec
{:transform [{:local-position
(->
(qv*
;(aa 45 0 0 1)
(qt)
(qv*
;(qt)
rot
(v3 (+ (/ stair-width 2) x inner-width)
(* stair-height y)
(- (/ stair-depth 2)))))
(v3scale (v3 1 1 1)))
:local-rotation rot
:local-scale
(v3
stair-width
stair-height
stair-depth)}]
:box-collider [{}]
:rigidbody [{:use-gravity false
:kinematic false}]})
(assoc :mesh-renderer [{:shared-material
(if (odd? y)
white
black)}])))]
(hydrate
{:name "stairs"
:transform [{:local-position (v3 0 0.6 20)}]
:children (vec els)})))
(do
(def sphere-spec
(kill! (create-primitive :sphere)))
(def bounce-sphere
{:sphere-collider [{:material bouncey-1}]}))
(defscn whacker
(hydrate
(deep-merge-mv
sphere-spec
bounce-sphere
{:name "whacker"
:transform [{:local-position (v3 0 400 20)
:local-scale (v3 10)}]
:rigidbody [{;:use-gravity true
:mass (float 10000)}]})))
(defscn whackers
(let [spacing 10
els (for [x (range 0 (* spacing 10) spacing)
z (range 0 (* spacing 10) spacing)]
(deep-merge-mv sphere-spec bounce-sphere
{:transform [{:local-position (v3 x 300 z)
:local-scale (v3scale
(v3 (/ spacing 2))
(v3 1 0.5 1))}]
:rigidbody [{:mass (float 1)}]}))]
(hydrate
{:name "whackers"
:transform [{:local-position [-50 0 -50]}]
:children (vec els)})))
(defscn stopblock
(let [els (map (fn [q]
(deep-merge-mv cubespec
{:transform [{:local-position (qv* q (v3 -0.5 0 0))
:local-scale (v3 0.1 1 1)
:local-rotation q}]
:rigidbody [{:use-gravity false
:is-kinematic true}]}))
[(qt)
(aa 90 0 1 0)
(aa 180 0 1 0)
(aa 270 0 1 0)
(aa 90 0 0 1)
(aa -90 0 0 1)])]
(hydrate
{:name "stopblock"
:transform [{:local-scale (v3 50)
:local-position (v3 0 500 0)}]
:children (vec els)})))
(defn position-you [x y z]
(populate! (.gameObject (object-named "NormalController"))
{:transform [{:local-position (v3 x y z)}]}))
(defn repon []
(.SetActive repl true))
(defn repoff []
(.SetActive repl false))
(defscn cube-it
(hydrate
(deep-merge-mv cubespec
{:transform [{:local-scale (v3 20)}]})))
(defscn nice-island
(let [c (doto (mat/shader-material "Standard")
(set-with! [_ color]
(Color. 0.1 0 0.2)))]
(hydrate
(->
(deep-merge-mv cubespec
{:name "nice-island"
:transform [{:local-position [200 400 200]
:local-scale [4 0.1 4]}]})
(assoc :mesh-renderer [{:shared-material c}])))))
(defn to-nice-island []
(let [p (.localPosition (transform nice-island))]
(position-you (.x p) (+ (.y p) 1) (.z p))))
(defn island-it
([] (island-it (.localPosition (transform (object-named "NormalController")))))
([v]
(set! (.localPosition (transform nice-island)) v)
(to-nice-island))
([x y z]
(island-it (v3 x y z))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment