Materials are working

This commit is contained in:
Jeremy Penner 2013-11-22 21:07:58 -05:00
parent 5357157f11
commit 7940bb61d5

View file

@ -62,6 +62,10 @@
(recur i (next rgval) mpval_i rgval_result))) (recur i (next rgval) mpval_i rgval_result)))
[mpval_i rgval_result]))) [mpval_i rgval_result])))
(defn dae-merge-libs [& rglibs]
(apply merge-with #(concat %1 %2) rglibs))
(defn dae-vec3-array [rgvec3 id x y z] (defn dae-vec3-array [rgvec3 id x y z]
(let [rgfloat (apply concat (map (fn [{:keys [x y z]}] [x y z]) rgvec3))] (let [rgfloat (apply concat (map (fn [{:keys [x y z]}] [x y z]) rgvec3))]
[:source {:id id} [:source {:id id}
@ -73,20 +77,56 @@
[:param {:name y :type "float"}] [:param {:name y :type "float"}]
[:param {:name z :type "float"}]]]])) [:param {:name z :type "float"}]]]]))
(defn id-from-mat [mat]
(str "mat_" (:colour mat)))
(defn dae-colour-from-mat [mat]
(let [colour (:colour mat)
rgbaHex [(subs colour 2 4) (subs colour 4 6) (subs colour 6 8) (subs colour 0 2)]
rgba (for [hex rgbaHex]
(-> (Integer/parseInt hex 16)
(/ 255.0)))]
[:color {} (clojure.string/join " " rgba)]))
(defn dae-mat-from-mat [mat]
(if mat
(let [idMat (id-from-mat mat)
idFx (str idMat "_fx")]
{:materials
[[:material {:id idMat :name idFx}
[:instance_effect {:url (str "#" idFx)}]]]
:effects
[[:effect {:id idFx :name idFx}
[:profile_COMMON {}
[:technique {:sid "common"}
[:phong {}
[:diffuse {} (dae-colour-from-mat mat)]
[:ambient {} (dae-colour-from-mat mat)]
[:specular {} (dae-colour-from-mat mat)]]]]]]})))
(defn rgmat-from-rgpoly [rgpoly]
(set (for [{mat :mat} rgpoly :when (not (nil? mat))] mat)))
(defn dae-mat-from-rgpoly [rgpoly]
(reduce dae-merge-libs
{}
(for [mat (rgmat-from-rgpoly rgpoly)] (dae-mat-from-mat mat))))
(defn dae-geom-from-rgpoly [rgpoly id name] (defn dae-geom-from-rgpoly [rgpoly id name]
(let [rgtri (apply concat (map :rgvec3 rgpoly)) (let [rgtri (apply concat (map :rgvec3 rgpoly))
[mpvec3_i rgvec3] (dedup rgtri) [mpvec3_i rgvec3] (dedup rgtri)
points_id (str id "_points") points_id (str id "_points")
vertices_id (str id "_vertices")] vertices_id (str id "_vertices")]
[:geometry {:id id :name name} {:geometries
[[:geometry {:id id :name name}
[:mesh {} [:mesh {}
(dae-vec3-array rgvec3 points_id "X" "Y" "Z") (dae-vec3-array rgvec3 points_id "X" "Y" "Z")
;todo: normals, textures ;todo: normals, textures
[:vertices {:id vertices_id} [:vertices {:id vertices_id}
[:input {:semantic "POSITION" :source (str "#" points_id)}]] [:input {:semantic "POSITION" :source (str "#" points_id)}]]
[:triangles {:count (count rgpoly)} [:triangles {:count (count rgpoly) :material (id-from-mat (:mat (first rgpoly)))}
[:input {:semantic "VERTEX" :source (str "#" vertices_id) :offset 0}] [:input {:semantic "VERTEX" :source (str "#" vertices_id) :offset 0}]
[:p {} (clojure.string/join " " (map #(get mpvec3_i %) rgtri))]]]])) [:p {} (clojure.string/join " " (map #(get mpvec3_i %) rgtri))]]]]]}))
(defn now (defn now
"Returns current ISO 8601 compliant date." "Returns current ISO 8601 compliant date."
@ -107,11 +147,19 @@
(conj [:scene {} scene]))) (conj [:scene {} scene])))
(defn dae-from-rgpoly [rgpoly name] (defn dae-from-rgpoly [rgpoly name]
(dae-from-desc {:geometries [(dae-geom-from-rgpoly rgpoly (str "id_" name) name)] (dae-from-desc
:visual_scenes [[:visual_scene {:id (str "id_" name "_scene") :name (str name "_scene")} (dae-merge-libs
[:node {:id (str name "_node") :name name :type "NODE"} (dae-geom-from-rgpoly rgpoly (str "id_" name) name)
[:instance_geometry {:url (str "#id_" name)}]]]]} (dae-mat-from-rgpoly rgpoly)
[:instance_visual_scene {:url (str "#id_" name "_scene")}])) {:visual_scenes [[:visual_scene {:id (str "id_" name "_scene") :name (str name "_scene")}
[:node {:id (str name "_node") :name name :type "NODE"}
[:instance_geometry {:url (str "#id_" name)}
[:bind_material {}
[:technique_common {}
(for [mat (rgmat-from-rgpoly rgpoly)]
[:instance_material {:material (id-from-mat mat)
:target (str "#" (id-from-mat mat))}])]]]]]]})
[:instance_visual_scene {:url (str "#id_" name "_scene")}]))
(defn parse-number (defn parse-number
"Reads a number from a string. Returns nil if not a number." "Reads a number from a string. Returns nil if not a number."
@ -163,5 +211,5 @@
(doseq [filename args] (doseq [filename args]
(convert-cko-to-dae filename))) (convert-cko-to-dae filename)))
;(convert-cko-to-dae "C:/dev/unity/genie-hand.cko") (convert-cko-to-dae "C:/dev/unity/8.cko")
;(write-test-cube) ;(write-test-cube)