Skip to content

Commit

Permalink
Merge branch 'main' of github.com:defold/extension-spine
Browse files Browse the repository at this point in the history
  • Loading branch information
JCash committed Nov 16, 2021
2 parents 7ae416c + eccd033 commit 6fcd0bb
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 49 deletions.
13 changes: 0 additions & 13 deletions defold-spine/commonsrc/spine_ddf.proto
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,3 @@ message SpineEvent
optional uint64 string = 7 [default = 0];
optional dmScriptDDF.LuaRef node = 8;
}

/* Function wrapper documented in gamesys_script.cpp */
message SetConstantSpineModel
{
required uint64 name_hash = 1;
required dmMath.Vector4 value = 2;
}

/* Function wrapper documented in gamesys_script.cpp */
message ResetConstantSpineModel
{
required uint64 name_hash = 1;
}
42 changes: 11 additions & 31 deletions defold-spine/src/comp_spine_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,13 @@
#include <dmsdk/gamesys/resources/res_animationset.h>
#include <dmsdk/gamesys/resources/res_textureset.h>

// The engine ddf formats aren't stored in the "dmsdk" folder (yet)
#include <gamesys/gamesys_ddf.h>

// #include <dmsdk/dlib/array.h>
// #include <dmsdk/dlib/hash.h>
#include <dmsdk/dlib/log.h>
#include <dmsdk/gamesys/property.h>
// #include <dmsdk/dlib/message.h>
// #include <dmsdk/dlib/profile.h>
// #include <dmsdk/dlib/dstrings.h>
// #include <dmsdk/dlib/object_pool.h>
// #include <dmsdk/dlib/math.h>
// #include <dmsdk/dlib/vmath.h>
// #include <graphics/graphics.h>
// #include <render/render.h>
//#include <rig/rig.h>
#include <gameobject/gameobject_ddf.h>

// #include "../gamesys.h"
// #include "../gamesys_private.h"
// #include "../resources/res_spine_model.h"


// #include "spine_ddf.h"
// #include "sprite_ddf.h"
// #include "tile_ddf.h"

// using namespace Vectormath::Aos;

namespace dmSpine
{
using namespace dmVMath;
Expand Down Expand Up @@ -708,12 +688,12 @@ namespace dmSpine
return component->m_RenderConstants && dmGameSystem::GetRenderConstant(component->m_RenderConstants, name_hash, out_constant);
}

static void CompSpineModelSetConstantCallback(void* user_data, dmhash_t name_hash, uint32_t* element_index, const dmGameObject::PropertyVar& var)
static void CompSpineModelSetConstantCallback(void* user_data, dmhash_t name_hash, int32_t value_index, uint32_t* element_index, const dmGameObject::PropertyVar& var)
{
SpineModelComponent* component = (SpineModelComponent*)user_data;
if (!component->m_RenderConstants)
component->m_RenderConstants = dmGameSystem::CreateRenderConstants();
dmGameSystem::SetRenderConstant(component->m_RenderConstants, GetMaterial(component, component->m_Resource), name_hash, element_index, var);
dmGameSystem::SetRenderConstant(component->m_RenderConstants, GetMaterial(component, component->m_Resource), name_hash, value_index, element_index, var);
component->m_ReHash = 1;
}

Expand Down Expand Up @@ -746,11 +726,11 @@ namespace dmSpine
{
dmRig::CancelAnimation(component->m_RigInstance);
}
else if (params.m_Message->m_Id == dmGameSystemDDF::SetConstantSpineModel::m_DDFDescriptor->m_NameHash)
else if (params.m_Message->m_Id == dmGameSystemDDF::SetConstant::m_DDFDescriptor->m_NameHash)
{
dmGameSystemDDF::SetConstantSpineModel* ddf = (dmGameSystemDDF::SetConstantSpineModel*)params.m_Message->m_Data;
dmGameSystemDDF::SetConstant* ddf = (dmGameSystemDDF::SetConstant*)params.m_Message->m_Data;
dmGameObject::PropertyResult result = dmGameSystem::SetMaterialConstant(GetMaterial(component, component->m_Resource), ddf->m_NameHash,
dmGameObject::PropertyVar(ddf->m_Value), CompSpineModelSetConstantCallback, component);
dmGameObject::PropertyVar(ddf->m_Value), ddf->m_Index, CompSpineModelSetConstantCallback, component);
if (result == dmGameObject::PROPERTY_RESULT_NOT_FOUND)
{
dmMessage::URL& receiver = params.m_Message->m_Receiver;
Expand All @@ -761,9 +741,9 @@ namespace dmSpine
dmHashReverseSafe64(ddf->m_NameHash));
}
}
else if (params.m_Message->m_Id == dmGameSystemDDF::ResetConstantSpineModel::m_DDFDescriptor->m_NameHash)
else if (params.m_Message->m_Id == dmGameSystemDDF::ResetConstant::m_DDFDescriptor->m_NameHash)
{
dmGameSystemDDF::ResetConstantSpineModel* ddf = (dmGameSystemDDF::ResetConstantSpineModel*)params.m_Message->m_Data;
dmGameSystemDDF::ResetConstant* ddf = (dmGameSystemDDF::ResetConstant*)params.m_Message->m_Data;
if (component->m_RenderConstants)
{
component->m_ReHash |= dmGameSystem::ClearRenderConstant(component->m_RenderConstants, ddf->m_NameHash);
Expand Down Expand Up @@ -870,7 +850,7 @@ namespace dmSpine
dmRender::HMaterial material = GetMaterial(component, component->m_Resource);
return dmGameSystem::GetResourceProperty(context->m_Factory, material, out_value);
}
return dmGameSystem::GetMaterialConstant(GetMaterial(component, component->m_Resource), params.m_PropertyId, out_value, true, CompSpineModelGetConstantCallback, component);
return dmGameSystem::GetMaterialConstant(GetMaterial(component, component->m_Resource), params.m_PropertyId, params.m_Options.m_Index, out_value, true, CompSpineModelGetConstantCallback, component);
}

dmGameObject::PropertyResult CompSpineModelSetProperty(const dmGameObject::ComponentSetPropertyParams& params)
Expand Down Expand Up @@ -923,7 +903,7 @@ namespace dmSpine
component->m_ReHash |= res == dmGameObject::PROPERTY_RESULT_OK;
return res;
}
return dmGameSystem::SetMaterialConstant(GetMaterial(component, component->m_Resource), params.m_PropertyId, params.m_Value, CompSpineModelSetConstantCallback, component);
return dmGameSystem::SetMaterialConstant(GetMaterial(component, component->m_Resource), params.m_PropertyId, params.m_Value, params.m_Options.m_Index, CompSpineModelSetConstantCallback, component);
}

static void ResourceReloadedCallback(const dmResource::ResourceReloadedParams& params)
Expand Down
9 changes: 5 additions & 4 deletions defold-spine/src/script_spine_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,15 +659,16 @@ namespace dmSpine
dmhash_t name_hash = dmScript::CheckHashOrString(L, 2);
Vectormath::Aos::Vector4* value = dmScript::CheckVector4(L, 3);

dmGameSystemDDF::SetConstantSpineModel msg;
dmGameSystemDDF::SetConstant msg;
msg.m_NameHash = name_hash;
msg.m_Value = *value;
msg.m_Index = 0;

dmMessage::URL receiver;
dmMessage::URL sender;
dmScript::ResolveURL(L, 1, &receiver, &sender);

dmMessage::Post(&sender, &receiver, dmGameSystemDDF::SetConstantSpineModel::m_DDFDescriptor->m_NameHash, (uintptr_t)instance, 0, (uintptr_t)dmGameSystemDDF::SetConstantSpineModel::m_DDFDescriptor, &msg, sizeof(msg), 0);
dmMessage::Post(&sender, &receiver, dmGameSystemDDF::SetConstant::m_DDFDescriptor->m_NameHash, (uintptr_t)instance, 0, (uintptr_t)dmGameSystemDDF::SetConstant::m_DDFDescriptor, &msg, sizeof(msg), 0);
return 0;
}

Expand Down Expand Up @@ -700,14 +701,14 @@ namespace dmSpine
dmGameObject::HInstance instance = dmScript::CheckGOInstance(L);
dmhash_t name_hash = dmScript::CheckHashOrString(L, 2);

dmGameSystemDDF::ResetConstantSpineModel msg;
dmGameSystemDDF::ResetConstant msg;
msg.m_NameHash = name_hash;

dmMessage::URL receiver;
dmMessage::URL sender;
dmScript::ResolveURL(L, 1, &receiver, &sender);

dmMessage::Post(&sender, &receiver, dmGameSystemDDF::ResetConstantSpineModel::m_DDFDescriptor->m_NameHash, (uintptr_t)instance, 0, (uintptr_t)dmGameSystemDDF::ResetConstantSpineModel::m_DDFDescriptor, &msg, sizeof(msg), 0);
dmMessage::Post(&sender, &receiver, dmGameSystemDDF::ResetConstant::m_DDFDescriptor->m_NameHash, (uintptr_t)instance, 0, (uintptr_t)dmGameSystemDDF::ResetConstant::m_DDFDescriptor, &msg, sizeof(msg), 0);
return 0;
}

Expand Down
17 changes: 16 additions & 1 deletion main/main.collection
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,22 @@ name: "main"
scale_along_z: 0
embedded_instances {
id: "go"
data: "embedded_components {\n"
data: "components {\n"
" id: \"main\"\n"
" component: \"/main/main.script\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
"embedded_components {\n"
" id: \"spinemodel\"\n"
" type: \"spinemodel\"\n"
" data: \"spine_scene: \\\"/assets/spineboy.spinescene\\\"\\n"
Expand Down
78 changes: 78 additions & 0 deletions main/main.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
go.property("test_var", vmath.vector4(1,2,3,4))

local function callback(self, message_id, message, sender)
pprint("GO callback", message_id, message, sender)

spine.reset_constant(self.url, "tint")
end

function init(self)
msg.post(".", "acquire_input_focus")

--local bone_go = spine.get_go("#spinemodel", "torso")
--print("Bone", bone_go)

self.countdown = 2
self.url = "#spinemodel"

--spine.set_skin("#spinemodel", "default")
--spine.play_anim("#spinemodel", "run", go.PLAYBACK_ONCE_FORWARD)

spine.play_anim(self.url, "run", go.PLAYBACK_ONCE_FORWARD, {}, callback)
--spine.play_anim(self.url, "run", go.PLAYBACK_ONCE_FORWARD)

--constants
print("getting constants")

print("tint:", go.get(self.url, "tint"))
print("set tint", go.set(self.url, "tint", vmath.vector4(1,2,3,4)))
print("tint:", go.get(self.url, "tint"))
print("set tint", go.set(self.url, "tint.y", 0.5))
print("tint:", go.get(self.url, "tint"))

print("setting constants")
--spine.set_constant(self.url, "tint", vmath.vector4(1,0,0,1))
--spine.reset_constant(self.url, "tint")


-- errors
--spine.play_anim(self.url, "not_exist", go.PLAYBACK_ONCE_FORWARD, {})
--spine.play_anim("#not_exist", "run", go.PLAYBACK_ONCE_FORWARD, {})


end

function final(self)
-- Add finalization code here
-- Learn more: https://defold.com/manuals/script/
-- Remove this function if not needed
end

function update(self, dt)
self.countdown = self.countdown - 1
if self.countdown == 0 then
--spine.cancel("#spinemodel")
end
end

function on_message(self, message_id, message, sender)
pprint("message", message_id, message)
end

function on_input(self, action_id, action)
-- Add input-handling code here. The game object this script is attached to
-- must have acquired input focus:
--
-- msg.post(".", "acquire_input_focus")
--
-- All mapped input bindings will be received. Mouse and touch input will
-- be received regardless of where on the screen it happened.
-- Learn more: https://defold.com/manuals/input/
-- Remove this function if not needed
end

function on_reload(self)
-- Add reload-handling code here
-- Learn more: https://defold.com/manuals/hot-reload/
-- Remove this function if not needed
end

0 comments on commit 6fcd0bb

Please sign in to comment.