sliva/src/sliva/core.cljs

147 lines
4.2 KiB
Clojure

(ns sliva.core
(:require [sliva.ctl :refer [websocket-init vtic]]
[cljs.core.async :refer [>!]])
(:require-macros [cljs.core.async.macros :refer [go go-loop]]))
(enable-console-print!)
(console.log "Hello, Sky!")
;;;;; ☭☭☭☭☭☭☭☭☭☭☭☭☭☭ ;;;;;;
;; ☭☭☭☭ Parametri razni ☭☭☭☭ ;;
;;;;; ☭☭☭☭☭☭☭☭☭☭☭☭☭☭ ;;;;;;
(def odmik-kamere 100)
(def rotacija-kamere 1)
(def FOV 140)
(def lik-sirina 2)
(def obj-limit 1000)
(def objekti (atom []))
(def stevec (atom 0))
(def rotacija-x 0.006)
(def rotacija-y 0.001)
(def rotacija-z 0.003)
(def zamik-barve 0.0000666)
(def zacetna-barva 0.333)
(def saturacija 1)
(def svetlost 0.4)
(def w-diff 0.5)
(def gostota-obj 2)
;; Inicializacija ;;
(def scena (THREE.Scene.))
(def kamera (THREE.PerspectiveCamera. FOV (/ (.-innerWidth js/window)
(.-innerHeight js/window)) 0.1 2000))
(aset kamera "position" "z" odmik-kamere)
(aset kamera "aspect" (/ (.-innerWidth js/window)
(.-innerHeight js/window)))
(.updateProjectionMatrix kamera)
(def izris (THREE.WebGLRenderer. (js-obj "alpha" true)))
(.setSize izris (.-innerWidth js/window) (.-innerHeight js/window))
;; Crno ozadje
(.setClearColor izris 0x000000 1)
(defn dodaj-obj [sirina]
(let [barva (js/THREE.Color.)
mat (js/THREE.LineBasicMaterial.)
geo (js/THREE.Geometry.)
koti (.-vertices geo)
zamik (/ sirina 2)
-zamik (* -1 zamik)]
;; Nastavi barvo novega objekta
(.setHSL barva (* @stevec zacetna-barva) saturacija svetlost)
(aset mat "color" barva)
;; Kvadratek (za nov objekt)
(.push koti (js/THREE.Vector3. -zamik 0 0) (js/THREE.Vector3. 0 zamik 0))
(.push koti (js/THREE.Vector3. -zamik 0 0) (js/THREE.Vector3. 0 -zamik 0))
(.push koti (js/THREE.Vector3. zamik 0 0) (js/THREE.Vector3. 0 zamik 0))
(.push koti (js/THREE.Vector3. zamik 0 0) (js/THREE.Vector3. 0 -zamik 0))
(let [obj (js/THREE.Line. geo mat (.-LineSegments js/THREE))
novi-objekti (conj (take obj-limit @objekti)
obj)]
(.add scena obj)
;; pucaj sceno ane
(reset! objekti novi-objekti))))
(defn obj-anim [obj]
(.rotateY obj rotacija-y)
(.rotateZ obj rotacija-z)
(.rotateX obj rotacija-x)
(let [new-scale (+ (aget obj "scale" "x")
w-diff)]
(aset obj "scale" "x" new-scale)
(aset obj "scale" "y" new-scale)
(aset obj "scale" "z" new-scale))
(.offsetHSL (aget obj "material" "color") zamik-barve 0 0))
(defn cam-rotate []
(.translateX kamera rotacija-kamere)
(.translateZ kamera (- odmik-kamere
(.sqrt js/Math (+ (.pow js/Math odmik-kamere 2)
(.pow js/Math rotacija-kamere 2)))))
(.lookAt kamera scena.position))
(defn render []
(.requestAnimationFrame js/window render)
(reset! stevec (inc @stevec))
(if (= 0 (rem @stevec gostota-obj))
(dodaj-obj lik-sirina))
(.render izris scena kamera)
(run! obj-anim @objekti)
(cam-rotate))
(defn start-render []
(let [container (.getElementById js/document "container")]
(.appendChild container (.-domElement izris))))
(defn stop-render []
(let [container (.getElementById js/document "container")]
(.removeChild container (.-firstChild container))))
;; Zacetek ;;
(aset js/document "onreadystatechange"
(fn []
(if (= (.-readyState js/document) "complete")
(do
(start-render)
(render)
(websocket-init)))))
(defn on-js-reload []
(stop-render)
(start-render)
(websocket-init))
;; Hendlaj risajz
(defn on-window-resize[]
(let [sirina (.-innerWidth js/window)
visina (.-innerHeight js/window)]
(aset kamera "aspect" (/ sirina visina))
(.updateProjectionMatrix kamera)
(.setSize izris sirina visina)))
(.addEventListener js/window "resize" on-window-resize false)
(.addEventListener js/window "click" (fn []
(console.log "kleek")
(console.log @vtic)
(go-loop []
(>! @vtic "WAT"))))