├── .gitignore ├── screenshot.jpg ├── src ├── cljs │ └── resonate2015 │ │ └── day2 │ │ ├── components │ │ ├── counter.cljs │ │ ├── add-shape.cljs │ │ ├── shader_select.cljs │ │ ├── canvas.cljs │ │ └── fps.cljs │ │ ├── derivedviews.cljs │ │ ├── core.cljs │ │ ├── tick.cljs │ │ ├── handlers.cljs │ │ ├── ecs.cljs │ │ └── demo.cljs └── clj │ └── resonate2015 │ └── day1 │ ├── core.clj │ └── ecs.clj ├── dev_src └── resonate2015 │ └── dev.cljs ├── resources └── public │ ├── index.html │ └── css │ └── site.css ├── project.clj ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | resources/public/js 2 | target 3 | -------------------------------------------------------------------------------- /screenshot.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learn-postspectacular/resonate-workshop-2015/HEAD/screenshot.jpg -------------------------------------------------------------------------------- /src/cljs/resonate2015/day2/components/counter.cljs: -------------------------------------------------------------------------------- 1 | (ns resonate2015.day2.components.counter 2 | (:require 3 | [re-frame.core :refer [subscribe]])) 4 | 5 | (defn particle-count 6 | [] 7 | (let [num (subscribe [:particle-count])] 8 | [:span (str "Particles: " @num)])) 9 | -------------------------------------------------------------------------------- /dev_src/resonate2015/dev.cljs: -------------------------------------------------------------------------------- 1 | (ns resonate2015.dev 2 | (:require 3 | [resonate2015.day2.core] 4 | [figwheel.client :as fw])) 5 | 6 | (fw/start { 7 | :websocket-url "ws://localhost:3449/figwheel-ws" 8 | :on-jsload (fn [] 9 | ;; (stop-and-start-my app) 10 | )}) 11 | -------------------------------------------------------------------------------- /resources/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Resonate workshop 2015 5 | 6 | 7 | 8 | 9 |

Loading...

10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/cljs/resonate2015/day2/components/add-shape.cljs: -------------------------------------------------------------------------------- 1 | (ns resonate2015.day2.components.add-shape 2 | (:require 3 | [resonate2015.day2.components.counter :refer [particle-count]] 4 | [re-frame.core :refer [dispatch]])) 5 | 6 | (defn add-shape-button 7 | [type] 8 | [:button {:on-click #(dispatch [:add-shape type])} "+ " (name type)]) 9 | 10 | (defn add-shape-ops 11 | [types] 12 | [:div 13 | (for [type types] ^{:key (str "bt-" type)} [add-shape-button type]) 14 | [particle-count]]) 15 | -------------------------------------------------------------------------------- /src/cljs/resonate2015/day2/components/shader_select.cljs: -------------------------------------------------------------------------------- 1 | (ns resonate2015.day2.components.shader-select 2 | (:require 3 | [clojure.string :as str] 4 | [re-frame.core :refer [subscribe dispatch]])) 5 | 6 | (defn shader-selector 7 | [ids] 8 | (let [shader (subscribe [:current-shader])] 9 | (fn [] 10 | [:select 11 | {:on-change #(dispatch [:set-shader (-> % .-target .-value)]) 12 | :value @shader} 13 | (for [id ids] 14 | [:option {:key id :value id} (str/capitalize (name id))])]))) 15 | -------------------------------------------------------------------------------- /src/cljs/resonate2015/day2/derivedviews.cljs: -------------------------------------------------------------------------------- 1 | (ns resonate2015.day2.derivedviews 2 | (:require-macros 3 | [reagent.ratom :refer [reaction]]) 4 | (:require 5 | [re-frame.core :refer [register-sub]])) 6 | 7 | (register-sub 8 | :app-initialized? 9 | (fn [db _] (reaction (-> @db :initialized?)))) 10 | 11 | (register-sub 12 | :window-size 13 | (fn [db _] (reaction (-> @db :window-size)))) 14 | 15 | (register-sub 16 | :particle-count 17 | (fn [db _] (reaction (count (:entities @db))))) 18 | 19 | (register-sub 20 | :current-shader 21 | (fn [db _] (reaction (:curr-shader @db)))) 22 | -------------------------------------------------------------------------------- /src/cljs/resonate2015/day2/components/canvas.cljs: -------------------------------------------------------------------------------- 1 | (ns resonate2015.day2.components.canvas 2 | (:require-macros 3 | [cljs-log.core :refer [info warn]]) 4 | (:require 5 | [reagent.core :as reagent] 6 | [re-frame.core :refer [subscribe dispatch]])) 7 | 8 | (defn canvas 9 | [] 10 | (let [size (subscribe [:window-size])] 11 | (reagent/create-class 12 | {:display-name "canvas" 13 | :component-did-mount 14 | (fn [this] 15 | (dispatch [:canvas-mounted (.getContext (reagent/dom-node this) "webgl")])) 16 | :reagent-render 17 | (fn [] 18 | [:canvas 19 | {:key "main-canvas" 20 | :width (@size 0) 21 | :height (@size 1)}])}))) 22 | -------------------------------------------------------------------------------- /src/cljs/resonate2015/day2/core.cljs: -------------------------------------------------------------------------------- 1 | (ns resonate2015.day2.core 2 | (:require 3 | [resonate2015.day2.handlers] 4 | [resonate2015.day2.derivedviews] 5 | [resonate2015.day2.demo :as demo] 6 | [resonate2015.day2.components.add-shape :refer [add-shape-ops]] 7 | [resonate2015.day2.components.shader-select :refer [shader-selector]] 8 | [resonate2015.day2.components.canvas :refer [canvas]] 9 | [resonate2015.day2.components.fps :refer [fps-panel]] 10 | [cljsjs.react :as react] 11 | [reagent.core :as reagent] 12 | [re-frame.core :refer [subscribe dispatch]])) 13 | 14 | (defn main-panel 15 | [] 16 | (let [init? (subscribe [:app-initialized?])] 17 | #(if @init? 18 | [:div 19 | [canvas] 20 | [fps-panel {:id :fps :mode :fps :width 100 :col "limegreen"}] 21 | [:div#hud.animated.bounceInDown 22 | [:h1 "Resonate workshop 2015" 23 | [:span.small 24 | "[" [:a {:href "https://github.com/learn-postspectacular/resonate-workshop-2015"} 25 | "GitHub"] "]"]] 26 | [add-shape-ops demo/shape-types] 27 | [:div [shader-selector (keys demo/shader-uniforms)]]]] 28 | [:div#loading 29 | [:h1 "Loading..."]]))) 30 | 31 | (defn main 32 | [] 33 | (dispatch [:init-app]) 34 | (reagent/render-component 35 | [main-panel] (.getElementById js/document "app"))) 36 | 37 | (main) 38 | -------------------------------------------------------------------------------- /src/clj/resonate2015/day1/core.clj: -------------------------------------------------------------------------------- 1 | (ns resonate2015.day1.core) 2 | 3 | (defn hello 4 | [] (prn "Zdravo!")) 5 | 6 | (def my-map {:a {:b {:c [1 2 3] :d "string"}}}) 7 | 8 | (get-in my-map [:a :b :d 2]) 9 | 10 | (def my-set #{}) 11 | 12 | ;; 13 | (defn test-it 14 | [coll] 15 | (filter #{:age :name} (keys coll))) 16 | 17 | 18 | [] ; vector 19 | () ; list 20 | {} ; hash-map / array-map 21 | #{} ; hash-set 22 | 23 | (filter odd? #{1 2 3 4 5}) 24 | (take 3 #{1 2 3 4 5}) ;; takes from beginning 25 | (drop 3 #{1 2 3 4 5}) 26 | (take-last 10 (range 100)) 27 | (drop-while (fn [x] (< x 20)) (range 100)) 28 | 29 | ;;(last (take 5 (iterate subdivision mesh))) 30 | 31 | (->> mesh (iterate subdiv) (take 5) last) 32 | 33 | (defprotocol Proto 34 | (as-mesh [this opts])) 35 | 36 | (deftype FooType [a b c] 37 | Proto 38 | (as-mesh [this opts] :as-mesh)) 39 | 40 | (require '[thi.ng.geom.core :as g]) 41 | (require '[thi.ng.geom.aabb :as a]) 42 | (require '[thi.ng.geom.circle :as c]) 43 | (require '[thi.ng.geom.basicmesh :as bm]) 44 | (require '[thi.ng.geom.gmesh :as gm]) 45 | (require '[thi.ng.geom.mesh.io :as mio]) 46 | (require '[thi.ng.geom.mesh.subdivision :as sd]) 47 | (require '[clojure.java.io :as io]) 48 | 49 | (with-open [o (io/output-stream "foo.stl")] 50 | (let [mesh (-> (c/circle 1) 51 | (g/extrude {:depth 1 :res 6 :mesh (gm/gmesh)}))] 52 | (->> mesh 53 | ;;(iterate sd/catmull-clark) 54 | (iterate sd/doo-sabin) 55 | (take 4) 56 | (last) 57 | (g/tessellate) 58 | (mio/write-stl o)))) 59 | 60 | 61 | (reductions (fn [acc x] (+ acc x)) 0 (filter f (range 10))) 62 | 63 | (apply + (range 10)) 64 | 65 | (hash-seq) 66 | -------------------------------------------------------------------------------- /resources/public/css/site.css: -------------------------------------------------------------------------------- 1 | html, body { 2 | margin:0; 3 | padding:0; 4 | overflow: hidden; 5 | font-size: 14px; 6 | } 7 | 8 | body, button, select { 9 | font-family: Inconsolata, monospace; 10 | } 11 | 12 | #loading { 13 | margin: 1em; 14 | } 15 | 16 | #hud { 17 | position: fixed; 18 | top: 20px; 19 | left: 20px; 20 | z-index: 1000; 21 | } 22 | 23 | #hud div { 24 | margin-bottom: 1em; 25 | } 26 | 27 | #fps { 28 | position: fixed; 29 | top: 0px; 30 | right: 0px; 31 | z-index: 1000; 32 | } 33 | 34 | h1 { 35 | margin: 0 0 10px 0; 36 | } 37 | 38 | .small { 39 | margin-left: 1em; 40 | font-size: 14px; 41 | } 42 | 43 | a:link, a:visited { color: #000; } 44 | 45 | button, select { 46 | background: #333; 47 | color: #fff; 48 | padding: 1em; 49 | border: 0; 50 | border-radius: 4px; 51 | margin-right: 10px; 52 | } 53 | 54 | select { 55 | width: 10em; 56 | height: 3em; 57 | } 58 | 59 | .animated { 60 | -webkit-animation-duration: 1s; 61 | animation-duration: 1s; 62 | -webkit-animation-fill-mode: both; 63 | animation-fill-mode: both; 64 | } 65 | 66 | @-webkit-keyframes bounceInDown { 67 | 0% { 68 | opacity: 0; 69 | -webkit-transform: translateY(-200px); 70 | } 71 | 60% { 72 | opacity: 1; 73 | -webkit-transform: translateY(30px); 74 | } 75 | 80% { 76 | -webkit-transform: translateY(-10px); 77 | } 78 | 100% { 79 | -webkit-transform: translateY(0); 80 | } 81 | } 82 | 83 | @keyframes bounceInDown { 84 | 0% { 85 | opacity: 0; 86 | transform: translateY(-200px); 87 | } 88 | 60% { 89 | opacity: 1; 90 | transform: translateY(30px); 91 | } 92 | 80% { 93 | transform: translateY(-10px); 94 | } 95 | 100% { 96 | transform: translateY(0); 97 | } 98 | } 99 | 100 | .bounceInDown { 101 | -webkit-animation-name: bounceInDown; 102 | animation-name: bounceInDown; 103 | } -------------------------------------------------------------------------------- /src/cljs/resonate2015/day2/tick.cljs: -------------------------------------------------------------------------------- 1 | (ns resonate2015.day2.tick 2 | (:require-macros 3 | [cljs-log.core :refer [info warn]]) 4 | (:require 5 | [reagent.core :as reagent] 6 | [re-frame.core :refer [register-handler dispatch trim-v]])) 7 | 8 | (defprotocol PTickHandler 9 | (init-state 10 | [_ db] 11 | "Called to during :add-tick-handlers event handling to allow 12 | modification of app-db. Must return updated db.") 13 | (tick 14 | [_ db] 15 | "Called at each tick w/ current app-db. 16 | Must return updated db.")) 17 | 18 | (defn- re-trigger-ticker 19 | "Dispatches :next-tick event at next React redraw cycle (usually 20 | every 16ms, but depending on CPU load)." 21 | [] (reagent/next-tick (fn [] (dispatch [:next-tick])))) 22 | 23 | (defn init-ticker 24 | "MUST be called with app-db map from a pure re-frame handler (e.g. 25 | during initial app init event). Registers tick related events & 26 | handlers and adds initial ::tick state to given db map. 27 | 28 | Tick handlers can be added via :add-tick-handlers event. These 29 | handlers are NOT re-frame handlers and MUST implement the 30 | PTickHandler protocol instead, for example: 31 | 32 | (dispatch 33 | [:add-tick-handlers 34 | {:foo (reify PTickHandler 35 | (init-state [_ db] (assoc db :foo {:state 0})) 36 | (tick [_ db] (update-in db [:foo :state] inc)))}])" 37 | [db] 38 | (register-handler 39 | :add-tick-handlers trim-v 40 | (fn [db [handlers]] 41 | (info "adding tick handlers:" (keys handlers)) 42 | (reduce-kv 43 | (fn [db id handler] (init-state handler db)) 44 | (update-in db [::tick :handlers] merge handlers) 45 | handlers))) 46 | 47 | (register-handler 48 | :remove-tick-handlers trim-v 49 | (fn [db [ids]] 50 | (info "removing tick handlers:" ids) 51 | (update-in db [::tick :handlers] #(apply dissoc % ids)))) 52 | 53 | (register-handler 54 | :next-tick trim-v 55 | (fn [{ticker ::tick :as db} _] 56 | (if-not (:paused? ticker) 57 | (do (re-trigger-ticker) 58 | (reduce-kv 59 | (fn [db id handler] (tick handler db)) 60 | db (:handlers ticker))) 61 | db))) 62 | 63 | (re-trigger-ticker) 64 | 65 | (assoc db ::tick {:handlers {} :paused? false})) 66 | -------------------------------------------------------------------------------- /src/cljs/resonate2015/day2/handlers.cljs: -------------------------------------------------------------------------------- 1 | (ns resonate2015.day2.handlers 2 | (:require-macros 3 | [cljs-log.core :refer [info warn]]) 4 | (:require 5 | [resonate2015.day2.tick :as tick] 6 | [resonate2015.day2.demo :as demo] 7 | [resonate2015.day2.components.fps :as fps] 8 | [thi.ng.geom.core.vector :refer [vec2]] 9 | [thi.ng.geom.rect :as r] 10 | [re-frame.core :refer [register-handler dispatch]])) 11 | 12 | (defn window-size 13 | [] [(.-innerWidth js/window) (.-innerHeight js/window)]) 14 | 15 | (defn dispatch-resize 16 | [e] (dispatch [:resize-window (window-size)])) 17 | 18 | (defn dispatch-keydown 19 | [e] (dispatch [:keydown (.-keyCode e)])) 20 | 21 | (defn dispatch-mousemove 22 | [e] (dispatch [:set-mouse-pos (vec2 (.-clientX e) (.-clientY e))])) 23 | 24 | (defn add-dom-listener-if-missing 25 | [db id fn] 26 | (if-not (db id) 27 | (do (.addEventListener js/window (name id) fn) 28 | (assoc db id fn)) 29 | db)) 30 | 31 | (defn init-dom-events 32 | [db] 33 | (-> db 34 | (add-dom-listener-if-missing :resize dispatch-resize) 35 | (add-dom-listener-if-missing :keydown dispatch-keydown) 36 | (add-dom-listener-if-missing :mousemove dispatch-mousemove))) 37 | 38 | (register-handler 39 | :init-app 40 | (fn [db _] 41 | (let [db (-> db 42 | (init-dom-events) 43 | (assoc :window-size (window-size) 44 | :mouse-pos (vec2) 45 | :initialized? true)) 46 | db (if-not (:tick/tick db) (tick/init-ticker db) db)] 47 | (fps/register-fps-counter :fps-counter) 48 | db))) 49 | 50 | (register-handler 51 | :resize-window 52 | (fn [db [_ size]] 53 | (let [size (or size (window-size))] 54 | (assoc db 55 | :window-size size 56 | :view-rect (apply r/rect size))))) 57 | 58 | (register-handler 59 | :canvas-mounted (fn [db [_ ctx]] (demo/start db ctx))) 60 | 61 | (register-handler 62 | :set-shader (fn [db [_ id]] (assoc db :curr-shader (keyword id)))) 63 | 64 | (register-handler 65 | :set-mouse-pos (fn [db [_ mpos]] (assoc db :mouse-pos mpos))) 66 | 67 | (register-handler 68 | :add-shape (fn [db [_ type]] (demo/make-shape db type))) 69 | 70 | (register-handler 71 | :keydown 72 | (fn [db [_ id]] 73 | (case id 74 | 0x20 (dispatch [:add-shape :particles]) ;; space 75 | 0x4c (dispatch [:set-shader :lambert]) ;; L 76 | 0x50 (dispatch [:set-shader :phong]) ;; P 77 | nil) 78 | db)) 79 | -------------------------------------------------------------------------------- /src/clj/resonate2015/day1/ecs.clj: -------------------------------------------------------------------------------- 1 | (ns resonate2015.day1.ecs 2 | (:require 3 | [clojure.set :as set])) 4 | 5 | (require '[thi.ng.geom.mesh.io :as mio]) 6 | (require '[clojure.java.io :as io]) 7 | 8 | ;; (component :position {:x 0 :y 10}) 9 | 10 | ;; 1 [pos col vel renderable] ;; player 11 | ;; 2 [skill pos vel] 12 | 13 | ;; 1 {:pos [0 0] :vel [1 -1] :color :red :renderable true} 14 | ;; 2 {:pos {:x ...} :vel {....} 15 | 16 | ;; color 17 | ;; vel 18 | 19 | ;; {1 [{:name :x :y} {:name :color} ...] 20 | ;; 2 [{:name :x :y}] 21 | ;; } 22 | 23 | (defn make-app 24 | [] 25 | (atom 26 | {:uuid-counter 0 27 | :entities {} 28 | :systems {}})) 29 | 30 | (defn new-entity! 31 | "Defines a new entity ID" 32 | [app-state] 33 | (-> app-state 34 | (swap! update-in [:uuid-counter] inc) 35 | (:uuid-counter))) 36 | 37 | (defn component 38 | "Defines a new component" 39 | [app-state entity-id name state] 40 | (assoc-in app-state [:entities entity-id name] state)) 41 | 42 | (defn add-components-for-eid 43 | "Updates a app state map with new entity states." 44 | [app-state entity-id states] 45 | (update-in app-state [:entities entity-id] 46 | merge states)) 47 | 48 | (defn add-components-for-eid! 49 | "Swaps a app state atom with new entity states." 50 | ([app-state entity-id states] 51 | (swap! app-state 52 | update-in [:entities entity-id] 53 | merge states)) 54 | ([app-state states] 55 | (add-components-for-eid! 56 | app-state 57 | (new-entity! app-state) 58 | states))) 59 | 60 | (defn new-system! 61 | "Defines a new system in the given app-state atom. 62 | Asks for system name, a set of required component IDs and 63 | a system function to process all matching entities." 64 | [app-state name comp-ids f] 65 | (swap! app-state assoc-in 66 | [:systems name] 67 | {:fn f 68 | :comp-ids comp-ids})) 69 | 70 | (defn make-entity-validator 71 | [comp-ids] 72 | (let [valid-entity-keys? (partial set/subset? comp-ids)] 73 | (fn [[_ v]] 74 | (valid-entity-keys? (set (keys v)))))) 75 | 76 | (defn run-system 77 | "Runs a single system fn on matching components" 78 | [app-state name] 79 | (let [{:keys [entities systems]} (deref app-state) 80 | {sys-fn :fn comp-ids :comp-ids} (systems name) 81 | valid-entity? (make-entity-validator comp-ids) 82 | matching-entities (filter valid-entity? entities)] 83 | (dorun 84 | (map 85 | (fn [[eid estate]] (sys-fn app-state eid estate)) 86 | matching-entities)))) 87 | 88 | (defn movement-system 89 | [app-state eid estate] 90 | (let [{:keys [pos vel]} estate 91 | pos' (mapv + pos vel)] 92 | (swap! app-state assoc-in [:entities eid :pos] pos'))) 93 | -------------------------------------------------------------------------------- /src/cljs/resonate2015/day2/ecs.cljs: -------------------------------------------------------------------------------- 1 | (ns resonate2015.day2.ecs 2 | (:require-macros 3 | [cljs-log.core :refer [info warn]]) 4 | (:require 5 | [clojure.set :as set])) 6 | 7 | (defn make-ecs 8 | "Returns a fresh entity-component-system map." 9 | [] 10 | {:eid 0 11 | :entities {} 12 | :systems {}}) 13 | 14 | (defn set-component 15 | "Takes an ECS map, entity ID, component name and component state, 16 | sets component for entity and returns updated map." 17 | [ecs entity-id name state] 18 | (assoc-in ecs [:entities entity-id name] state)) 19 | 20 | (defn update-entity-in-systems 21 | [ecs eid] 22 | (let [estate (get-in ecs [:entities eid])] 23 | (update ecs :systems 24 | #(reduce-kv 25 | (fn [acc sid sys] 26 | (let [in? ((:entities sys) eid)] 27 | (if ((:valid? sys) estate) 28 | (if-not in? 29 | (update-in acc [sid :entities] conj eid) 30 | acc) 31 | (if in? 32 | (update-in acc [sid :entities] disj eid) 33 | acc)))) 34 | % %)))) 35 | 36 | (defn set-entity 37 | "Takes an ECS map, entity ID and entity state which is to be used as 38 | the entity's new state." 39 | [ecs eid state] 40 | (assoc-in ecs [:entities eid] state)) 41 | 42 | (defn update-entity 43 | "Takes an ECS map, entity ID and an update fn with optional args. 44 | Applies fn and args via update-in to entity's current state." 45 | [ecs eid f & args] 46 | (apply update-in ecs [:entities eid] f args)) 47 | 48 | (defn register-entity 49 | "Takes an ECS map and optional map of component states. 50 | Creates a new unique entity ID and associates state with entity. 51 | Returns updated map." 52 | ([ecs] 53 | (register-entity ecs {})) 54 | ([ecs comps] 55 | (let [eid (inc (:eid ecs))] 56 | (-> ecs 57 | (assoc :eid eid) 58 | (update-entity eid merge comps) 59 | (update-entity-in-systems eid))))) 60 | 61 | (defn remove-entity 62 | "Takes an ECS map and removes given entity ID from entities." 63 | [ecs eid] (update ecs :entities dissoc eid)) 64 | 65 | (defn entity-validator 66 | "Takes a seq or set of component IDs, returns a predicate fn which, 67 | when called will check if an entity has given components. 68 | Used by run-system." 69 | [comp-ids] 70 | (let [valid-entity-keys? #(set/subset? comp-ids %)] 71 | (fn [v] (valid-entity-keys? (set (keys v)))))) 72 | 73 | (defn register-system 74 | "Defines a new system in the given ECS map. Asks for system name, 75 | a set of required component IDs and a system function to process 76 | a matching entity." 77 | [ecs name comp-ids sys-fn] 78 | (let [valid? (entity-validator comp-ids) 79 | ids (into #{} 80 | (comp (filter (comp valid? val)) 81 | (map key)) 82 | (:entities ecs))] 83 | (assoc-in ecs [:systems name] 84 | {:fn sys-fn 85 | :valid? valid? 86 | :entities ids}))) 87 | 88 | (defn run-system 89 | "Runs a single system fn on matching components." 90 | [{:keys [entities systems] :as ecs} name] 91 | (let [{sys-fn :fn sys-entity-ids :entities} (systems name)] 92 | (as-> ecs ecs 93 | (reduce 94 | (fn [acc eid] (sys-fn acc eid (entities eid))) 95 | ecs sys-entity-ids) 96 | (reduce 97 | update-entity-in-systems 98 | ecs sys-entity-ids)))) 99 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject com.postspectacular/resonate-workshop-2015 "0.1.0-SNAPSHOT" 2 | :description "Clojurescript, reagent & core.async workshop at Resonate festival 2015" 3 | :url "https://github.com/learn-postspectacular/resonate-workshop-2015" 4 | :license {:name "Apache Software License v2.0" 5 | :url "https://www.apache.org/licenses/LICENSE-2.0"} 6 | 7 | :dependencies [[org.clojure/clojure "1.7.0-RC1"] 8 | [org.clojure/clojurescript "0.0-3297"] 9 | [thi.ng/geom "0.0.803"] 10 | [thi.ng/domus "0.1.0"] 11 | [cljsjs/react "0.12.2-5"] 12 | [org.clojars.toxi/re-frame "0.2.0"] 13 | [cljs-log "0.2.1"] 14 | [reagent "0.5.0"] 15 | [figwheel "0.2.5" :exclusions [org.clojure/clojure org.clojure/clojurescript]] 16 | [org.clojure/core.async "0.1.346.0-17112a-alpha"]] 17 | 18 | :plugins [[lein-cljsbuild "1.0.5"] 19 | [lein-figwheel "0.2.5" :exclusions [org.clojure/clojure org.clojure/clojurescript]]] 20 | 21 | :source-paths ["src/clj" "src/cljs"] 22 | 23 | :clean-targets ^{:protect false} ["resources/public/js"] 24 | 25 | :cljsbuild {:builds [{:id "dev" 26 | :source-paths ["src/cljs" "dev_src"] 27 | :compiler {:output-to "resources/public/js/app.js" 28 | :output-dir "resources/public/js/out" 29 | :optimizations :none 30 | :main resonate2015.dev 31 | :asset-path "js/out" 32 | :source-map true 33 | :source-map-timestamp true 34 | :cache-analysis true }} 35 | {:id "min" 36 | :source-paths ["src/cljs"] 37 | :compiler {:output-to "resources/public/js/app.js" 38 | :main resonate2015.day2.core 39 | :optimizations :advanced 40 | :pretty-print false}}]} 41 | 42 | :figwheel {:http-server-root "public" ;; default and assumes "resources" 43 | :server-port 3449 ;; default 44 | :css-dirs ["resources/public/css"] ;; watch and update CSS 45 | 46 | ;; Start an nREPL server into the running figwheel process 47 | ;; :nrepl-port 7888 48 | 49 | ;; Server Ring Handler (optional) 50 | ;; if you want to embed a ring handler into the figwheel http-kit 51 | ;; server, this is simple ring servers, if this 52 | ;; doesn't work for you just run your own server :) 53 | ;; :ring-handler hello_world.server/handler 54 | 55 | ;; To be able to open files in your editor from the heads up display 56 | ;; you will need to put a script on your path. 57 | ;; that script will have to take a file path and a line number 58 | ;; ie. in ~/bin/myfile-opener 59 | ;; #! /bin/sh 60 | ;; emacsclient -n +$2 $1 61 | ;; 62 | ;; :open-file-command "myfile-opener" 63 | 64 | ;; if you want to disable the REPL 65 | ;; :repl false 66 | 67 | ;; to configure a different figwheel logfile path 68 | ;; :server-logfile "tmp/logs/figwheel-logfile.log" 69 | }) 70 | -------------------------------------------------------------------------------- /src/cljs/resonate2015/day2/components/fps.cljs: -------------------------------------------------------------------------------- 1 | (ns resonate2015.day2.components.fps 2 | (:require-macros 3 | [reagent.ratom :refer [reaction]] 4 | [cljs-log.core :refer [info]]) 5 | (:require 6 | [resonate2015.day2.tick :as tick] 7 | [reagent.core :as reagent] 8 | [re-frame.core :refer [register-sub subscribe dispatch]])) 9 | 10 | (defn register-fps-counter 11 | "Sets up an FPS counter tick handler & subscriptions for current & 12 | average framerates. Current framerate (:fps) is updated every 13 | second, the average (:avg-fps) every tick and is the total avg. rate 14 | since the beginning." 15 | [db-root] 16 | (register-sub 17 | :fps (fn [db _] (reaction (get-in @db [db-root :fps])))) 18 | (register-sub 19 | :avg-fps (fn [db _] (reaction (get-in @db [db-root :avg-fps])))) 20 | (dispatch 21 | [:add-tick-handlers 22 | {:update-fps 23 | (reify tick/PTickHandler 24 | (init-state [_ db] 25 | (assoc db db-root 26 | {:frame 0 :total 0 :fps 0 :avg-fps 0 27 | :start (.getTime (js/Date.)) 28 | :last (.getTime (js/Date.))})) 29 | (tick [_ db] 30 | (let [{:keys [frame total start last]} (db db-root) 31 | now (.getTime (js/Date.)) 32 | age (- now start) 33 | frame (inc frame) 34 | total (inc total) 35 | db (update db db-root assoc 36 | :frame frame 37 | :total total 38 | :avg-fps (/ total (* age 1e-3)))] 39 | (if (> (- now last) 1000) 40 | (update db db-root assoc 41 | :last now 42 | :frame 0 43 | :fps (/ frame (* (- now last) 1e-3))) 44 | db))))}])) 45 | 46 | (defn- fps-graph 47 | "Updates canvas visualization using given component state & opts. 48 | Called from fps-panel render fn." 49 | [state fps width height col grid-col] 50 | (let [w' (- width 4) 51 | s (/ (- height 20) 60) 52 | h' (- height 3)] 53 | (when-let [ctx (:ctx @state)] 54 | (swap! state update :history 55 | #(conj (if (< (count %) w') % (->> % (drop 1) vec)) (- h' (* s fps)))) 56 | (let [history (:history @state) 57 | x (dec (count history)) 58 | w'' (inc w') 59 | g1 (- h' (* s 20)) 60 | g2 (- h' (* s 40)) 61 | g3 (- h' (* s 60)) 62 | fps' (.toFixed (js/Number. fps) 2)] 63 | (.clearRect ctx 0 0 width height) 64 | (set! (.-strokeStyle ctx) grid-col) 65 | (set! (.-fillStyle ctx) col) 66 | (doto ctx 67 | (.beginPath) 68 | (.moveTo 2 h') (.lineTo w'' h') 69 | (.moveTo 2 g1) (.lineTo w'' g1) 70 | (.moveTo 2 g2) (.lineTo w'' g2) 71 | (.moveTo 2 g3) (.lineTo w'' g3) 72 | (.stroke)) 73 | (set! (.-strokeStyle ctx) col) 74 | (.beginPath ctx) 75 | (.moveTo ctx (+ x 2) (get history x)) 76 | (loop [x (dec x)] 77 | (when-not (neg? x) 78 | (.lineTo ctx (+ x 2) (get history x)) 79 | (recur (dec x)))) 80 | (.stroke ctx) 81 | (.fillText ctx (str "fps: " fps') 4 11))))) 82 | 83 | (defn fps-panel 84 | "Framerate visualization component. Takes options map with following 85 | supported keys: 86 | 87 | :mode - visualization mode (:fps or :avg-fps) 88 | :width - canvas width (default 100) 89 | :height - canvas height (default 76) 90 | :col - graph color (#f0f) 91 | :grid-col - grid color (20/40/60 fps markers, #ccc) 92 | 93 | Furthermore, the map can contain :id, :class or :style keys. All 94 | other keys will be ignored. 95 | 96 | Example: [fps-panel {:mode :fps :width 200 :col \"limegreen\"}]" 97 | [& [opts]] 98 | (let [fps (subscribe [(opts :mode :fps)]) 99 | state (atom {})] 100 | (reagent/create-class 101 | {:display-name "fps-panel" 102 | :component-did-mount 103 | #(reset! state 104 | {:ctx (.getContext (reagent/dom-node %) "2d") 105 | :history []}) 106 | :reagent-render 107 | (fn [& [{:keys [width height col grid-col] 108 | :or {width 100 height 76 col "#f0f" grid-col "#ccc"} 109 | :as opts}]] 110 | (fps-graph state @fps width height col grid-col) 111 | [:canvas 112 | (merge {:width width :height height} 113 | (select-keys opts [:id :class :style]))])}))) 114 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # resonate-workshop-2015 2 | 3 | ![screenshot](screenshot.jpg) 4 | 5 | [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/learn-postspectacular/resonate-workshop-2015?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | [interactive version @ demo.thi.ng](http://demo.thi.ng/geom/resonate-2015/index.html) 6 | 7 | April 13-15, 2015 Belgrade 8 | 9 | ## Preparations / requirements 10 | 11 | To make the most of our time next week and since online connectivity 12 | at the workshop venue has been traditionally sporadic, please ensure 13 | you have the following requirements met before the workshop begins on 14 | Monday: 15 | 16 | - [Java JDK 7 or 8](http://www.oracle.com/technetwork/java/javase/downloads/index.html) 17 | - [Leiningen](http://leiningen.org) 18 | - [Counterclockwise (CCW)](http://doc.ccw-ide.org/documentation.html#install-as-standalone-product) 19 | 20 | The last tool (CCW) is an Eclipse based Clojure IDE, which is 21 | x-platform, easy to install and we already used successfully in 22 | [last year's workshop](https://github.com/learn-postspectacular/resonate-workshop-2014). 23 | However, this just a recommendation and you're of course free to use 24 | any other editor. 25 | 26 | Please also already clone this repo and execute the following to force 27 | downloading most (if not all) other necessary library dependencies: 28 | 29 | ``` 30 | cd resonate-workshop-2015 31 | lein deps 32 | ``` 33 | 34 | ## Running the demo 35 | 36 | The WebGL example shown above (and described below) can be run like this: 37 | 38 | ``` 39 | cd resonate-workshop-2015 40 | lein figwheel 41 | ``` 42 | 43 | The above is building the development version and starts a server on 44 | http://localhost:3449/ 45 | 46 | Whilst the figwheel server is running, any changes done to the source 47 | code are automatically recompiled & applied to the running browser 48 | session. 49 | 50 | To build a minified & optimized JS version of this code, stop the 51 | figwheel server (`Ctrl+D`) and run these commands: 52 | 53 | ``` 54 | lein do clean, cljsbuild once min 55 | open resources/public/index.html 56 | ``` 57 | 58 | The demo is partially interactive: 59 | 60 | - Press `SPACE` to spawn 10 more random particles (see note below) 61 | - Click any of the buttons to create specific particles 62 | - Use mouse to rotate 3D view 63 | 64 | *Note*: The performance of this example is not amazing and the code is 65 | not very optimized (we were focused on learning new concepts after 66 | all). Furthermore, WebGL itself is not happy with drawing hundreds of 67 | tiny VBOs each with potentially different shader instances. In an 68 | ideal world, the mesh drawing/handling would use large vertex buffers 69 | for multiple meshes, each with more attributes to specify the unique 70 | for data of each mesh instance. Instead, we're currently bombarding 71 | the WebGL driver with a multitude of render states and performance 72 | goes down v.quickly... 73 | 74 | ## Daily reports 75 | 76 | ### Day 1 77 | 78 | - Clojure introduction 79 | - Simple project setup via CCW 80 | - Working with the REPL 81 | - Clojure sequence API 82 | - Development of a simple Entity-component system (ECS) as excuse to 83 | introduce many important Clojure concepts 84 | 85 | ### Day 2 & 3 86 | 87 | We spent these last two days developing a little WebGL demo based 88 | around a refined version of the ECS developed a day earlier and spent 89 | much time talking about the many new concepts around: 90 | 91 | - Clojurescript (the language, cross-compilation, tooling (i.e. 92 | Figwheel, Cljsbuild etc.) 93 | - ReactJS integration (via the awesome reagent & re-frame) 94 | - WebGL basics (vector & matrix algebra, shaders, VBOs) 95 | - Basic channel operations w/ core.async 96 | - Encoding HTML/SVG as Clojure datastructures (hiccup format) 97 | 98 | ## Libraries 99 | 100 | - [thi.ng/geom](http://thi.ng/geom) 101 | - [reagent](http://reagent-project.github.io/) 102 | - [re-frame](https://github.com/Day8/re-frame) 103 | - [figwheel](https://github.com/bhauman/lein-figwheel) 104 | - [ReactJS](http://facebook.github.io/react/) 105 | 106 | ## Reading list & references 107 | 108 | ### Clojure & ClojureScript introduction 109 | 110 | - [Clojure](http://clojure.org) 111 | - [ClojureScript](https://github.com/clojure/clojurescript) ([quickstart](https://github.com/clojure/clojurescript/wiki/Quick-Start), [wiki](https://github.com/clojure/clojurescript/wiki)) 112 | - [Leiningen sample project (full options)](https://github.com/technomancy/leiningen/blob/master/sample.project.clj) 113 | 114 | #### Tutorials 115 | 116 | - [Tutorial @ CAN](http://www.creativeapplications.net/tutorials/introduction-to-clojure-part-1/) 117 | - [Brave Clojure](http://www.braveclojure.com/) 118 | - [Clojure from the ground up](https://aphyr.com/posts/301-clojure-from-the-ground-up-welcome) 119 | 120 | ### Entity Component Systems 121 | 122 | - [Understanding ECS](http://www.gamedev.net/page/resources/_/technical/game-programming/understanding-component-entity-systems-r3013) 123 | - [T-Machine ECS blog series](http://t-machine.org/index.php/2007/09/03/entity-systems-are-the-future-of-mmog-development-part-1/) 124 | - [Chris Granger's Chromashift](http://www.chris-granger.com/2012/12/11/anatomy-of-a-knockout/) 125 | 126 | ### Reactive programming 127 | 128 | - [core.async](https://github.com/clojure/core.async) ([docs](http://clojure.github.io/core.async/), [walkthrough](https://github.com/clojure/core.async/blob/master/examples/walkthrough.clj)) 129 | - [React.js](http://facebook.github.io/react/) 130 | - [Reagent](http://reagent-project.github.io) 131 | - [Re-frame](https://github.com/Day8/re-frame/) 132 | 133 | ### Data transformations 134 | 135 | - [Rich Hickey's Transducers talk](https://www.youtube.com/watch?v=6mTbuzafcII) 136 | 137 | ### Data, data, data (derived views, compute graphs etc.) 138 | 139 | - [Turning the DB inside out](https://www.youtube.com/watch?v=fU9hR3kiOK0) 140 | - [Signal/Collect](http://www.signalcollect.com) 141 | 142 | ## Get in touch 143 | 144 | If you run into any problems, please get in touch via the above 145 | [gitter.im room](https://gitter.im/learn-postspectacular/resonate-workshop-2015), 146 | which we will use as backchannel. 147 | -------------------------------------------------------------------------------- /src/cljs/resonate2015/day2/demo.cljs: -------------------------------------------------------------------------------- 1 | (ns resonate2015.day2.demo 2 | (:require-macros 3 | [cljs-log.core :refer [info warn]]) 4 | (:require 5 | [resonate2015.day2.ecs :as ecs] 6 | [resonate2015.day2.tick :as tick] 7 | [thi.ng.geom.core :as g] 8 | [thi.ng.geom.core.vector :as v :refer [vec3 randvec3]] 9 | [thi.ng.geom.core.matrix :as mat :refer [M44]] 10 | [thi.ng.geom.aabb :as a] 11 | [thi.ng.geom.basicmesh :as bm] 12 | [thi.ng.geom.rect :as r] 13 | [thi.ng.geom.polygon :as p] 14 | [thi.ng.geom.mesh.polyhedra :as polyhedra] 15 | [thi.ng.geom.mesh.subdivision :as sd] 16 | [thi.ng.geom.webgl.core :as gl] 17 | [thi.ng.geom.webgl.buffers :as buffers] 18 | [thi.ng.geom.webgl.shaders :as shaders] 19 | [thi.ng.geom.webgl.shaders.lambert :as lambert] 20 | [thi.ng.geom.webgl.shaders.phong :as phong] 21 | [thi.ng.color.core :as col] 22 | [thi.ng.common.math.core :as m] 23 | [re-frame.core :refer [dispatch]])) 24 | 25 | (def shape-types [:particles :sphere :ico :box]) 26 | 27 | (def shader-uniforms 28 | {:lambert {:lightCol [1 0.8 0.5] 29 | :lightDir (g/normalize (vec3 0 1 0.5)) 30 | :alpha 1} 31 | :phong {:shininess 32 32 | :specularCol [1 1 1] 33 | :lightPos [-100 100 150] 34 | :useBlinnPhong true 35 | :wrap 0}}) 36 | 37 | (defn box-mesh 38 | [] (->> (a/aabb 1) (g/center) (g/as-mesh))) 39 | 40 | (defn ico-mesh 41 | [iter] (polyhedra/polyhedron-mesh polyhedra/icosahedron sd/catmull-clark 1 iter)) 42 | 43 | (defn init-shaders 44 | [ctx] 45 | {:lambert (shaders/make-shader-from-spec ctx lambert/shader-spec-two-sided) 46 | :phong (shaders/make-shader-from-spec ctx phong/shader-spec)}) 47 | 48 | (defn webgl-shape-spec 49 | [ctx mesh] 50 | (-> mesh 51 | (gl/as-webgl-buffer-spec {:fnormals true :tessellate true}) 52 | (buffers/make-attribute-buffers-in-spec ctx gl/static-draw) 53 | (assoc :uniforms {:ambientCol [0.2 0.2 0.25]}))) 54 | 55 | (defn update-shape-protos 56 | [{[x y] :mouse-pos 57 | [w h] :window-size :as db}] 58 | (let [proj (gl/perspective 45 (/ w h) 0.1 1000) 59 | view (-> (mat/look-at (vec3 0 0 100) (vec3 0 1 0) (vec3 0 1 0)) 60 | (g/rotate-x (* 0.01 y)) 61 | (g/rotate-y (* 0.01 x)))] 62 | (update db :shape-protos 63 | #(reduce-kv 64 | (fn [acc id spec] 65 | (update-in acc [id :uniforms] merge 66 | {:proj proj 67 | :view view})) 68 | % %)))) 69 | 70 | (defn make-hover-shape 71 | [db type color] 72 | (ecs/register-entity 73 | db {:orig-pos (randvec3 (m/random 80)) 74 | :render type 75 | :color color 76 | :scale (m/random 1 10) 77 | :hover (m/random m/TWO_PI) 78 | :hover-speed (m/random 0.01 0.05)})) 79 | 80 | (defmulti make-shape (fn [db type] type)) 81 | 82 | (defmethod make-shape :default 83 | [db type] (warn "unknown shape type:" type) db) 84 | 85 | (defmethod make-shape :particles 86 | [{[w h] :window-size :as db} _] 87 | (let [origin (randvec3)] 88 | (reduce 89 | (fn [db _] 90 | (ecs/register-entity 91 | db {:pos (g/+ origin (randvec3 10)) 92 | :vel (randvec3 (m/random 0.25 1)) 93 | :render (rand-nth [:sphere :ico :box]) 94 | :scale (m/random 1 5) 95 | :color (col/hsv->rgb (rand) 1 1) 96 | :spin {:axis (randvec3) 97 | :theta (m/random m/TWO_PI) 98 | :speed (m/random -0.2 0.2)}})) 99 | db (range 10)))) 100 | 101 | (defmethod make-shape :sphere 102 | [db type] (make-hover-shape db type (col/hsv->rgb 0.08 0.33 1))) 103 | 104 | (defmethod make-shape :ico 105 | [db type] (make-hover-shape db type (col/hsv->rgb 0.5 0.33 1))) 106 | 107 | (defmethod make-shape :box 108 | [db type] (make-hover-shape db type (col/hsv->rgb 0.92 0.33 1))) 109 | 110 | (defn move-entity 111 | [{bounds :world-bounds :as ecs} eid {:keys [pos vel] :as state}] 112 | (let [pos (g/+ pos vel)] 113 | (if (g/contains-point? bounds pos) 114 | (ecs/set-entity ecs eid (assoc state :pos pos)) 115 | (ecs/remove-entity ecs eid)))) 116 | 117 | (defn spin-entity 118 | [ecs eid {{:keys [speed]} :spin :as state}] 119 | (ecs/set-entity 120 | ecs eid (update-in state [:spin :theta] + speed))) 121 | 122 | (defn hover-entity 123 | [ecs eid {:keys [orig-pos hover hover-speed] :as state}] 124 | (ecs/set-entity 125 | ecs eid 126 | (assoc state 127 | :pos (g/+ orig-pos 0 (* (Math/sin hover) 5) 0) 128 | :hover (+ hover hover-speed)))) 129 | 130 | (defn webgl-render-entity 131 | [{ctx :canvas-ctx [w h] :window-size :as db} 132 | eid {:keys [pos render color scale spin] :as state}] 133 | (when ctx 134 | (let [model (-> db :shape-protos render) 135 | shader-type (db :curr-shader :lambert) 136 | model-mat (g/translate M44 pos) 137 | model-mat (if spin 138 | (-> model-mat 139 | (g/rotate-around-axis (:axis spin) (:theta spin)) 140 | (g/scale scale)) 141 | (g/scale model-mat scale))] 142 | (buffers/draw-arrays-with-shader 143 | ctx 144 | (-> model 145 | (assoc :shader (-> db :shaders shader-type)) 146 | (update-in [:uniforms] merge 147 | (shader-uniforms shader-type) 148 | {:model model-mat 149 | :diffuseCol color}) 150 | (shaders/inject-normal-matrix :model :view :normalMat))))) 151 | db) 152 | 153 | (def demo-handler 154 | (reify tick/PTickHandler 155 | (init-state 156 | [_ db] 157 | (-> db 158 | (merge (ecs/make-ecs)) 159 | (ecs/register-system :move #{:pos :vel} move-entity) 160 | (ecs/register-system :spin #{:spin} spin-entity) 161 | (ecs/register-system :hover #{:hover} hover-entity) 162 | (ecs/register-system :render #{:render} webgl-render-entity))) 163 | (tick 164 | [_ {ctx :canvas-ctx [w h] :window-size :as db}] 165 | (if ctx 166 | (do 167 | (doto ctx 168 | (gl/set-viewport 0 0 w h) 169 | (gl/clear-color-buffer 0.9 0.9 0.9 1.0) 170 | (gl/enable gl/depth-test)) 171 | (reduce 172 | ecs/run-system 173 | (update-shape-protos db) 174 | [:move :spin :hover :render])) 175 | db)))) 176 | 177 | (defn start 178 | [db ctx] 179 | (dispatch [:add-tick-handlers {:demo demo-handler}]) 180 | (-> db 181 | (assoc :canvas-ctx ctx 182 | :curr-shader :phong 183 | :view-rect (apply r/rect (:window-size db)) 184 | :world-bounds (g/center (a/aabb 150)) 185 | :shaders (init-shaders ctx) 186 | :shape-protos {:sphere (webgl-shape-spec ctx (ico-mesh 1)) 187 | :ico (webgl-shape-spec ctx (ico-mesh 0)) 188 | :box (webgl-shape-spec ctx (box-mesh))}))) 189 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | --------------------------------------------------------------------------------