Skip to content

Commit

Permalink
Merge pull request #157 from defold/spinevertex-format-fix
Browse files Browse the repository at this point in the history
Updated the editor/pipeline parts with the page_index vertex attribute
  • Loading branch information
JCash authored Jan 14, 2024
2 parents 835679d + 2fdea90 commit 14f4d07
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 20 deletions.
26 changes: 17 additions & 9 deletions defold-spine/editor/src/spineext.clj
Original file line number Diff line number Diff line change
Expand Up @@ -200,44 +200,52 @@
;; (.transform transform p)
;; [(.x p) (.y p) (.z p)])))))))


(shader/defshader spine-id-vertex-shader
(uniform mat4 view_proj)
(attribute vec4 position)
(attribute vec2 texcoord0)
(attribute float page_index)
(varying vec2 var_texcoord0)
(varying float var_page_index)
(defn void main []
(setq gl_Position (* gl_ModelViewProjectionMatrix position))
(setq var_texcoord0 texcoord0)))
(setq gl_Position (* view_proj (vec4 position.xyz 1.0)))
(setq var_texcoord0 texcoord0)
(setq var_page_index page_index)))

(shader/defshader spine-id-fragment-shader
(varying vec2 var_texcoord0)
(uniform sampler2D texture_sampler)
(varying float var_page_index)
(uniform vec4 id)
(uniform sampler2DArray texture_sampler)
(defn void main []
(setq vec4 color (texture2D texture_sampler var_texcoord0.xy))
(setq vec4 color (texture2DArray texture_sampler (vec3 var_texcoord0 var_page_index)))
(if (> color.a 0.05)
(setq gl_FragColor id)
(discard))))

(def spine-id-shader (shader/make-shader ::id-shader spine-id-vertex-shader spine-id-fragment-shader {"id" :id}))

(vtx/defvertex vtx-pos-tex-col
; See SpineVertex
(vtx/defvertex vtx-pos-tex-col-index
(vec3 position)
(vec2 texcoord0)
(vec4 color))
(vec4 color)
(vec1 page_index))

(defn generate-vertex-buffer [verts]
; verts should be in the format [[x y z u v r g b a] [x y z...] ...]
; verts should be in the format [[x y z u v r g b a p] [x y z...] ...]
(let [vcount (count verts)]
(when (> vcount 0)
(let [vb (->vtx-pos-tex-col vcount)
(let [vb (->vtx-pos-tex-col-index vcount)
vb-out (persistent! (reduce conj! vb verts))]
vb-out))))

(set! *warn-on-reflection* false)

(defn transform-vertices-as-vec [vertices]
; vertices is a SpineVertex array
(map (fn [vert] [(.x vert) (.y vert) (.z vert) (.u vert) (.v vert) (.r vert) (.g vert) (.b vert) (.a vert)]) vertices))
(map (fn [vert] [(.x vert) (.y vert) (.z vert) (.u vert) (.v vert) (.r vert) (.g vert) (.b vert) (.a vert) (.page_index vert)]) vertices))

(set! *warn-on-reflection* true)

Expand Down
18 changes: 9 additions & 9 deletions defold-spine/editor/src/spineguiext.clj
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,19 @@

(defn- transform-vtx [^Matrix4d m4d color vtx]
(let [[cr cg cb ca] color
[x y z u v r g b a] vtx
[x y z u v r g b a page_index] vtx
p (Point3d.)
_ (.set p x y z)
_ (.transform m4d p)]
[(.x p) (.y p) (.z p) u v (* r cr) (* g cg) (* b cb) (* a ca)]))
[(.x p) (.y p) (.z p) u v (* r cr) (* g cg) (* b cb) (* a ca) page_index]))

(defn- produce-local-vertices [handle skin anim dt]
(if (not= handle nil)
(let [_ (if (not (str/blank? skin)) (spineext/plugin-set-skin handle skin))
_ (if (not (str/blank? anim)) (spineext/plugin-set-animation handle anim))
_ (spineext/plugin-update-vertices handle dt)
vb-data (spineext/plugin-get-vertex-buffer-data handle) ; list of SpineVertex
vb-data-vec (spineext/transform-vertices-as-vec vb-data)] ; unpacked into lists of lists [[x y z u v r g b a]])
vb-data-vec (spineext/transform-vertices-as-vec vb-data)] ; unpacked into lists of lists [[x y z u v r g b a page_index]])
vb-data-vec)
[]))

Expand Down Expand Up @@ -153,15 +153,15 @@
(output spine-scene-pb g/Any (g/fnk [spine-scene-infos spine-scene]
(:spine-scene-pb (or (spine-scene-infos spine-scene)
(spine-scene-infos "")))))

;; The handle to the C++ resource
(output spine-data-handle g/Any (g/fnk [spine-scene-infos spine-scene]
(:spine-data-handle (or (spine-scene-infos spine-scene)
(spine-scene-infos "")))))

(output spine-vertex-buffer g/Any :cached (g/fnk [spine-scene spine-data-handle spine-skin spine-default-animation]
(produce-local-vertices spine-data-handle spine-skin spine-default-animation 0.0)))

(output aabb g/Any (g/fnk [spine-scene-infos spine-scene spine-skin pivot]
(or (get-in spine-scene-infos [spine-scene :spine-skin-aabbs (if (= spine-skin "") "default" spine-skin)])
geom/empty-bounding-box)))
Expand All @@ -177,7 +177,7 @@
(cond-> user-data
(not= :clipping-mode-none clipping-mode)
(assoc :clipping {:mode clipping-mode :inverted clipping-inverted :visible clipping-visible})))))

(output own-build-errors g/Any (g/fnk [_node-id build-errors-visual-node spine-anim-ids spine-default-animation spine-skin-ids spine-skin spine-scene spine-scene-names]
(g/package-errors _node-id
build-errors-visual-node
Expand Down Expand Up @@ -239,7 +239,7 @@
(input dep-build-targets g/Any)
(input name-counts gui/NameCounts)
(input spine-scene-resource resource/Resource)

(input spine-data-handle g/Any :substitute nil) ; The c++ pointer
(output spine-data-handle g/Any (gu/passthrough spine-data-handle))

Expand Down Expand Up @@ -344,7 +344,7 @@
(attach-spine-scene self spine-scenes-node spine-scene))))]
(concat old-spine-scenes new-spine-scenes))))


;;//////////////////////////////////////////////////////////////////////////////////////////////

(defn- fixup-spine-node [node-type-info node-desc]
Expand Down
Binary file modified defold-spine/plugins/lib/arm64-osx/libSpineExt.dylib
Binary file not shown.
Binary file modified defold-spine/plugins/lib/x86_64-linux/libSpineExt.so
Binary file not shown.
Binary file modified defold-spine/plugins/lib/x86_64-osx/libSpineExt.dylib
Binary file not shown.
Binary file modified defold-spine/plugins/lib/x86_64-win32/libSpineExt.dll
Binary file not shown.
Binary file modified defold-spine/plugins/share/pluginSpineExt.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions defold-spine/pluginsrc/com/defold/bob/pipeline/Spine.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ public static Bone[] SPINE_GetBones(SpinePointer spine) {

// Matching the struct in vertices.h
static public class SpineVertex extends Structure {
public float x, y, z, u, v, r, g, b, a;
public float x, y, z, u, v, r, g, b, a, page_index;
protected List getFieldOrder() {
return Arrays.asList(new String[] {"x", "y", "z", "u", "v", "r", "g", "b", "a"});
return Arrays.asList(new String[] {"x", "y", "z", "u", "v", "r", "g", "b", "a", "page_index"});
}
}

Expand Down
6 changes: 6 additions & 0 deletions defold-spine/pluginsrc/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -605,20 +605,23 @@ static void CreateAABB(SpineFile* file)
v->z = 0;
v->u = v->v = 0;
v->r = v->g = v->b = v->a = 1;
v->page_index = 0;
++v;

v->x = maxx;
v->y = miny;
v->z = 0;
v->u = v->v = 0;
v->r = v->g = v->b = v->a = 1;
v->page_index = 0;
++v;

v->x = maxx;
v->y = maxy;
v->z = 0;
v->u = v->v = 0;
v->r = v->g = v->b = v->a = 1;
v->page_index = 0;
++v;


Expand All @@ -627,20 +630,23 @@ static void CreateAABB(SpineFile* file)
v->z = 0;
v->u = v->v = 0;
v->r = v->g = v->b = v->a = 1;
v->page_index = 0;
++v;

v->x = minx;
v->y = maxy;
v->z = 0;
v->u = v->v = 0;
v->r = v->g = v->b = v->a = 1;
v->page_index = 0;
++v;

v->x = minx;
v->y = miny;
v->z = 0;
v->u = v->v = 0;
v->r = v->g = v->b = v->a = 1;
v->page_index = 0;
++v;
}

Expand Down

0 comments on commit 14f4d07

Please sign in to comment.