完成动画在设备上播放和切换

This commit is contained in:
xsl
2025-11-01 14:56:03 +08:00
parent 263a061d27
commit 6495a27658
8 changed files with 92 additions and 76 deletions
@@ -1,4 +1,5 @@
package com.hmwl.face_sdk; package com.hmwl.face_sdk;
import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
@@ -12,7 +13,11 @@ public class Motion {
try { try {
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
jsonObject.put("type", type); jsonObject.put("type", type);
jsonObject.put("png_names", png_names); JSONArray ja = new JSONArray();
for(int i = 0; i < png_names.size(); ++i){
ja.put(png_names.get(i));
}
jsonObject.put("png_names", ja);
return jsonObject; return jsonObject;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
+1 -1
View File
@@ -7,7 +7,7 @@ android {
compileSdk 36 compileSdk 36
defaultConfig { defaultConfig {
applicationId "com.hmwl.f20251030" applicationId "com.hmwl.f20251101"
minSdk 30 minSdk 30
targetSdk 36 targetSdk 36
versionCode 1 versionCode 1
+27 -27
View File
@@ -2,6 +2,33 @@
"11": [ "11": [
"11.png" "11.png"
], ],
"action24": [
"000000.png",
"000001.png",
"000002.png",
"000003.png",
"000004.png",
"000005.png",
"000006.png",
"000007.png",
"000008.png",
"000009.png",
"000010.png",
"000011.png",
"000012.png",
"000013.png",
"000014.png",
"000015.png",
"000016.png",
"000017.png",
"000018.png",
"000019.png",
"000020.png",
"000021.png",
"000022.png",
"000023.png",
"000024.png"
],
"12": [ "12": [
"12.png" "12.png"
], ],
@@ -94,32 +121,5 @@
], ],
"81": [ "81": [
"81.png" "81.png"
],
"action24": [
"000000.png",
"000001.png",
"000002.png",
"000003.png",
"000004.png",
"000005.png",
"000006.png",
"000007.png",
"000008.png",
"000009.png",
"000010.png",
"000011.png",
"000012.png",
"000013.png",
"000014.png",
"000015.png",
"000016.png",
"000017.png",
"000018.png",
"000019.png",
"000020.png",
"000021.png",
"000022.png",
"000023.png",
"000024.png"
] ]
} }
@@ -33,10 +33,10 @@ public class MainActivity extends FaceActivity
protected void onCreate(Bundle savedInstanceState) protected void onCreate(Bundle savedInstanceState)
{ {
Motion motion = new Motion(); Motion motion = new Motion();
motion.type = "4"; motion.type = "11";
motion.png_names.add("4.png"); motion.png_names.add("11.png");
InitArg initArg = new InitArg(); InitArg initArg = new InitArg();
initArg.action_fps = 3; initArg.action_fps = 10;
initArg.zoom = 1.5f; initArg.zoom = 1.5f;
initArg.motion = motion; initArg.motion = motion;
SetInitArg(initArg.toJson()); SetInitArg(initArg.toJson());
@@ -44,12 +44,13 @@ public class MainActivity extends FaceActivity
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
FaceInit(this); FaceInit(this);
Toast.makeText(this, "4.png", Toast.LENGTH_LONG).show(); Toast.makeText(this, "11.png", Toast.LENGTH_LONG).show();
} }
String GetCurMotionState(int index) String GetCurMotionState(int index)
{ {
Motion motion = new Motion(); Motion motion = new Motion();
motion.type = pngFilesByFolder.get(index).folderName;
motion.png_names = pngFilesByFolder.get(index).getFileList(); motion.png_names = pngFilesByFolder.get(index).getFileList();
return motion.toJson(); return motion.toJson();
} }
+1 -1
View File
@@ -1,3 +1,3 @@
<resources> <resources>
<string name="app_name">f20251030</string> <string name="app_name">f20251101</string>
</resources> </resources>
+3 -1
View File
@@ -39,7 +39,7 @@ public:
void createRenderPass(); void createRenderPass();
void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex, long long frameTime); void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex, long long frameTime);
void createSyncObjects(); void createSyncObjects();
void drawFrame(long long frameTime); virtual void drawFrame(long long frameTime);
virtual void render(VkCommandBuffer commandBuffer, long long frameTime); virtual void render(VkCommandBuffer commandBuffer, long long frameTime);
virtual bool isInited() { return inited; } virtual bool isInited() { return inited; }
@@ -89,4 +89,6 @@ protected:
void destroy_texture(Texture texture); void destroy_texture(Texture texture);
std::mutex mtx; std::mutex mtx;
long long _lastDrawFrameTime; long long _lastDrawFrameTime;
std::mutex changeMotionMtx;
}; };
+46 -40
View File
@@ -456,34 +456,7 @@ void FaceApp::create_face_pipelines()
VK_CHECK(vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipeline_create_info, nullptr, &m_graphicsPipeline)); VK_CHECK(vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipeline_create_info, nullptr, &m_graphicsPipeline));
} }
void FaceApp::setup_descriptor_pool()
{
// 保守估计:假设每个描述符集可能有多个绑定
const uint32_t setsCount = kMaxTexture;
//const uint32_t multiplier = 3; // 安全系数
std::vector<VkDescriptorPoolSize> pool_sizes = {
{
.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.descriptorCount = 3
},
{
.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.descriptorCount = setsCount + 4
},
// 如果需要其他类型的描述符,在这里添加
};
VkDescriptorPoolCreateInfo descriptor_pool_info{};
descriptor_pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
descriptor_pool_info.poolSizeCount = static_cast<uint32_t>(pool_sizes.size());
descriptor_pool_info.pPoolSizes = pool_sizes.data();
descriptor_pool_info.maxSets = setsCount + 7; // 额外预留一些集合
descriptor_pool_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
VK_CHECK(vkCreateDescriptorPool(device, &descriptor_pool_info, nullptr, &descriptor_pool));
}
@@ -553,6 +526,32 @@ void FaceApp::setup_descriptor_set_layout()
VK_CHECK(vkCreatePipelineLayout(device, &pipeline_layout_create_info, nullptr, &m_pipelineLayout)); VK_CHECK(vkCreatePipelineLayout(device, &pipeline_layout_create_info, nullptr, &m_pipelineLayout));
} }
void FaceApp::setup_descriptor_pool()
{
std::vector<VkDescriptorPoolSize> pool_sizes = {
{
.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.descriptorCount = kMaxTexture
},
{
.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.descriptorCount = (kMaxTexture + 1)
},
// 如果需要其他类型的描述符,在这里添加
};
VkDescriptorPoolCreateInfo descriptor_pool_info{};
descriptor_pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
descriptor_pool_info.poolSizeCount = static_cast<uint32_t>(pool_sizes.size());
descriptor_pool_info.pPoolSizes = pool_sizes.data();
descriptor_pool_info.maxSets = kMaxTexture + (kMaxTexture + 1);
descriptor_pool_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
VK_CHECK(vkCreateDescriptorPool(device, &descriptor_pool_info, nullptr, &descriptor_pool));
}
void FaceApp::setup_descriptor_set() void FaceApp::setup_descriptor_set()
{ {
m_descriptor_sets.resize(kMaxTexture); m_descriptor_sets.resize(kMaxTexture);
@@ -646,21 +645,10 @@ void FaceApp::setup_descriptor_set()
void FaceApp::render(VkCommandBuffer commandBuffer, long long frameTime) void FaceApp::render(VkCommandBuffer commandBuffer, long long frameTime)
{ {
static long long game_time = 0;
game_time += frameTime;
long long actionTime = (1000 / _initArg.action_fps);
if (game_time > actionTime)
{
_curTexIndex++;
if (_curTexIndex >= _curMotion.png_names.size())
{
_curTexIndex = 0;
}
game_time = game_time - actionTime;
}
std::unique_lock<std::mutex> lock(mtx); std::unique_lock<std::mutex> lock(mtx);
std::unique_lock<std::mutex> lock_point(mtx_point); std::unique_lock<std::mutex> lock_point(mtx_point);
//Application::render(commandBuffer); //Application::render(commandBuffer);
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineLayout_bg, 0, 1, &m_descriptor_set_bg, 0, NULL); vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineLayout_bg, 0, 1, &m_descriptor_set_bg, 0, NULL);
@@ -926,7 +914,7 @@ void FaceApp::createUniformBuffer()
void FaceApp::create_pipelines_bg() void FaceApp::create_pipelines_bg()
{ {
VkPipelineInputAssemblyStateCreateInfo input_assembly_state{}; VkPipelineInputAssemblyStateCreateInfo input_assembly_state{};
input_assembly_state.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; input_assembly_state.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
input_assembly_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; input_assembly_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
@@ -1137,6 +1125,23 @@ void FaceApp::SetInitArg(const char* arg)
} }
void FaceApp::drawFrame(long long frameTime)
{
std::unique_lock lock(changeMotionMtx);
static long long game_time = 0;
game_time += frameTime;
long long actionTime = (1000 / _initArg.action_fps);
if (game_time > actionTime)
{
_curTexIndex++;
if (_curTexIndex >= _curMotion.png_names.size())
{
_curTexIndex = 0;
}
game_time = game_time - actionTime;
}
Application::drawFrame(frameTime);
}
void FaceApp::changeMotion(const char* json) void FaceApp::changeMotion(const char* json)
{ {
@@ -1150,6 +1155,7 @@ void FaceApp::changeMotion(Motion& motion)
{ {
return; return;
} }
std::unique_lock<std::mutex> lock_changeMotion(changeMotionMtx);
_curMotion = motion; _curMotion = motion;
for (int i = 0; i < _curMotion.png_names.size(); ++i) for (int i = 0; i < _curMotion.png_names.size(); ++i)
{ {
+3 -1
View File
@@ -100,7 +100,7 @@ private:
VkPipelineLayout m_pipelineLayout = VK_NULL_HANDLE; VkPipelineLayout m_pipelineLayout = VK_NULL_HANDLE;
VkDescriptorSetLayout m_descriptorSetLayout = VK_NULL_HANDLE; VkDescriptorSetLayout m_descriptorSetLayout = VK_NULL_HANDLE;
vector<VkDescriptorSet> m_descriptor_sets; vector<VkDescriptorSet> m_descriptor_sets;
const int kMaxTexture = 30; const uint32_t kMaxTexture = 30;
vector<Texture> m_texs; vector<Texture> m_texs;
vector<Texture> m_texs_ex; vector<Texture> m_texs_ex;
@@ -141,6 +141,8 @@ public:
Motion _curMotion; Motion _curMotion;
int _curTexIndex = 0; int _curTexIndex = 0;
void SetInitArg(const char* arg); void SetInitArg(const char* arg);
void drawFrame(long long frameTime)override;
}; };
#endif #endif