[geom] Render a mesh (or similar) to SVG #545
-
|
Hi! 👋 My goal is to define a basic shape in 3D coords, and turn it into an SVG from an isometric perspective. I'm hoping the result will be something like: I think I've overcomplicated things but this is what I tried (copying from examples): (ns my-ns
(:require
[thi.ng.geom.core :as g]
[thi.ng.geom.matrix :as mat]
[thi.ng.geom.svg.core :as svg]
[thi.ng.geom.svg.adapter :as adapt]
[thi.ng.geom.quad :as q]
[thi.ng.geom.svg.renderer :as render]
[thi.ng.geom.svg.shaders :as shader]
[thi.ng.math.core :as m]))
(def width 640)
(def height 480)
(def model (-> (mat/matrix44) (g/rotate-x m/HALF_PI) (g/rotate-z m/SIXTH_PI)))
(def view (apply mat/look-at (mat/look-at-vectors 0 0 -2 0 0 0)))
(def proj (mat/perspective 60 (/ width height) 0.1 2))
(def mvp (->> model (m/* view) (m/* proj)))
(def col-tx (g/rotate-x (mat/matrix44) (- m/HALF_PI)))
(def mesh (g/as-mesh (q/quad3 10)))
(def screen (mat/viewport-matrix width height))
(def shader
(shader/shader
{:fill (shader/phong
{:model model
:view view
:light-pos [0 -2 1]
:light-col [1 1 1]
:diffuse (shader/normal-rgb col-tx)
:ambient [0.1 0.1 0.2]
:specular 1.0
:shininess 6.0})
:uniforms {:stroke "black" :stroke-width 0.25}
:flags {:solid true}}))
(render/mesh mesh mvp screen nil)
;;=> [:g {:stroke "black", :stroke-width 0.25} ()]It made a group, which is great, but the contents are empty. I'm not attached to using a shader, it was a required argument (fails when nil), so I copied it from an example. Any pointers would help, thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
Messing around a bit more... (defn render-mesh
[mesh mvp screen]
(let [faces (render/project-faces mvp screen (map #(g/vertices % mesh) (g/faces mesh false)))
faces (->> faces
(render/z-map-faces)
(render/z-sort-faces first))]
(map (fn [face] (svg/polygon (second face))) faces)))
(render-mesh mesh mvp screen)
;;=> ([:polygon {:points "0.00,0.00 10.00,0.00 10.00,10.00 0.00,10.00"}])Which I think sets me on the path to victory. 🤔 |
Beta Was this translation helpful? Give feedback.
-
|
Also just curious if there is a way to go from data to objects: {:vertices #{[0.0 10.0 0.0]
[10.0 0.0 0.0]
[10.0 10.0 0.0]
[0.0 0.0 0.0]},
:faces #{[0.0 0.0 0.0] [10.0 0.0 0.0] [10.0 10.0 0.0] [0.0 10.0 0.0]]}}or similar and create a |
Beta Was this translation helpful? Give feedback.
-
|
thinking about it a bit more, a tree walk would do it conveniently. |
Beta Was this translation helpful? Give feedback.
Messing around a bit more...
Which I think sets me on the path to victory. 🤔