-
Notifications
You must be signed in to change notification settings - Fork 21
/
resources.cljs
207 lines (195 loc) · 7.16 KB
/
resources.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
(ns ^:figwheel-always game.client.resources
(:require
[cljs.pprint :as pprint]
[clojure.string :as string :refer [replace]]
[com.stuartsierra.component :as component]
[promesa.core :as p]
[game.client.math :as math :refer [pi]]
[game.client.config :as config]
[game.client.models :as models :refer [transform-geometry post-transform-geometry]]
[game.client.voxelize :as voxelize]
[game.client.progress-manager
:as progress-manager
:refer [update-progress-item]])
(:require-macros [game.shared.macros :as macros :refer [defcom]]))
(def should-load-voxels? false)
(defn
load-json
[path on-success on-progress on-error]
(let
[xhr (new js/XMLHttpRequest)]
(doto
xhr
(.open "GET" path true)
(aset "responseType" "json")
(aset "onerror" on-error)
(aset "onload" on-success)
(aset "onprogress" on-progress)
(.send))))
(defn
load-resource
[path on-success on-progress on-error]
(let
[xhr (new js/XMLHttpRequest)]
(doto
xhr
(.open "GET" path true)
(aset "responseType" "arraybuffer")
(aset "onerror" on-error)
(aset "onload" on-success)
(aset "onprogress" on-progress)
(.send))))
(defn load-texture
[model path on-progress]
(p/promise
(fn [resolve reject]
(let
[repeat-x (first (:texture-repeat model))
repeat-y (second (:texture-repeat model))
on-success
(fn []
(this-as
this
(let
[img (new js/Image)
blob (new js/Blob #js [(.-response this)])]
(aset img "onload"
(fn []
; (println "resolve image" path)
(let
[texture (new js/THREE.Texture img)]
(-> texture .-wrapS (set! js/THREE.RepeatWrapping))
(-> texture .-wrapT (set! js/THREE.RepeatWrapping))
(-> texture .-repeat (.set repeat-x repeat-y))
(-> texture .-needsUpdate (set! true))
(resolve texture))))
(aset img "onerror" #(reject (str "image load error: " path)))
(aset img "src" (-> js/window .-URL (.createObjectURL blob))))))]
(load-resource path on-success on-progress reject)))))
(defn load-voxels
[model path on-progress]
(p/promise
(fn [resolve reject]
(let
[on-success
(fn []
(this-as
this
(let
[buffer (.-response this)
uint8array (new js/Uint8Array buffer)
uint8array (-> js/pako (.inflate uint8array))
decoded (-> js/msgpack (.decode uint8array))
;_ (.log js/console "uvs" (aget decoded "uvs"))
voxel-dict (js->clj decoded :keywordize-keys true)
; _ (.log js/console "uvs" (:uvs voxel-dict))
;_ (println "voxels" (:voxels voxel-dict))
; voxel-dict (assoc voxel-dict :uvs (new js/Float32Array (flatten (:uvs voxel-dict))))
voxel-geometry (voxelize/voxelize-output voxel-dict)
scale (:post-scale model)
bbox (-> voxel-geometry .-boundingBox)
bsphere (-> voxel-geometry .-boundingSphere)
mat (new js/THREE.Matrix4)
_ (-> mat (.makeScale scale scale scale))
_ (-> bbox (.applyMatrix4 mat))
_ (-> bsphere (.applyMatrix4 mat))
; TODO: post-transform-geometry is just a hack
;voxel-geometry (post-transform-geometry model voxel-geometry)
voxel-dict (assoc voxel-dict :geometry voxel-geometry)]
; (.log js/console "uvs" (-> voxel-geometry (.getAttribute "uv")))
(resolve voxel-dict))))]
(load-resource path on-success on-progress reject)))))
(defn load-geometry
[model path on-progress]
(p/promise
(fn [resolve reject]
(let
[on-success
(fn []
(this-as
this
(let
[buffer (.-response this)
uint8array (new js/Uint8Array buffer)
uint8array (-> js/pako (.inflate uint8array))
decoded (-> js/msgpack (.decode uint8array))
geo-loader (new js/THREE.BufferGeometryLoader)
geo (-> geo-loader (.parse decoded))
geo (post-transform-geometry model geo)]
(-> geo (.computeBoundingBox))
(-> geo (.computeBoundingSphere))
(-> geo (.computeFaceNormals))
(-> geo (.computeVertexNormals))
(resolve geo))))]
(load-resource path on-success on-progress reject)))))
;(defn load-geometry
; [model path on-progress]
; (p/promise
; (fn [resolve reject]
; (let
; [on-success #(resolve (transform-geometry model %))
; on-error reject
; geo-loader (new js/THREE.BufferGeometryLoader)]
; (-> geo-loader
; (.load path on-success on-progress on-error))))))
(defn on-progress
[progress-manager resource xhr]
(update-progress-item
progress-manager
resource
(-> xhr .-loaded)
(-> xhr .-total)))
(defcom
new-resources
[progress-manager]
[resource-list all-promise]
(fn [component]
(let
[resource-list
(into
[]
(for
[model (models/get-models)]
(let
[
path (:path model)
texture-path (:texture-path model)
voxels-path
(or
(:voxels-path model)
(replace (replace path #"/3d/" "/voxels/") #"\.json$" ".msgpack.gz"))
path
(if
(string/includes? path "models-dinosaurs")
"models/3d/missile.json"
path)
path (replace path #"\.json$" ".msgpack.gz")
on-geo-progress (partial on-progress progress-manager path)
on-texture-progress (partial on-progress progress-manager texture-path)
on-voxels-progress (partial on-progress progress-manager voxels-path)
load-promise (load-geometry model path on-geo-progress)
texture-load-promise (load-texture model texture-path on-texture-progress)
voxels-load-promise
(if
should-load-voxels?
(load-voxels model voxels-path on-voxels-progress))]
(merge
model
{
:load-promise load-promise
:texture-load-promise texture-load-promise
:voxels-load-promise voxels-load-promise}))))]
(-> component
(assoc
:all-promise
(p/all
(into
[]
(concat
(map
#(vector
(:load-promise %)
(:texture-load-promise %)
(:voxels-load-promise %)) resource-list)))))
(assoc :resource-list resource-list))))
(fn [component] component))