Daniele Bartolini 10 лет назад
Родитель
Сommit
76d45402d9
3 измененных файлов с 56 добавлено и 1 удалено
  1. 12 0
      src/lua/lua_api.cpp
  2. 40 1
      src/world/debug_line.cpp
  3. 4 0
      src/world/debug_line.h

+ 12 - 0
src/lua/lua_api.cpp

@@ -2800,6 +2800,17 @@ static int debug_line_add_obb(lua_State* L)
 	return 0;
 }
 
+static int debug_line_add_unit(lua_State* L)
+{
+	LuaStack stack(L);
+	stack.get_debug_line(1)->add_unit(*device()->resource_manager()
+		, stack.get_matrix4x4(2)
+		, stack.get_resource_id(3)
+		, stack.get_color4(4)
+		);
+	return 0;
+}
+
 static int debug_line_reset(lua_State* L)
 {
 	LuaStack stack(L);
@@ -3445,6 +3456,7 @@ void load_api(LuaEnvironment& env)
 	env.add_module_function("DebugLine", "add_cone",   debug_line_add_cone);
 	env.add_module_function("DebugLine", "add_sphere", debug_line_add_sphere);
 	env.add_module_function("DebugLine", "add_obb",    debug_line_add_obb);
+	env.add_module_function("DebugLine", "add_unit",   debug_line_add_unit);
 	env.add_module_function("DebugLine", "reset",      debug_line_reset);
 	env.add_module_function("DebugLine", "submit",     debug_line_submit);
 	env.add_module_function("DebugLine", "__index",    "DebugLine");

+ 40 - 1
src/world/debug_line.cpp

@@ -8,7 +8,10 @@
 #include "device.h"
 #include "math_utils.h"
 #include "matrix4x4.h"
+#include "mesh_resource.h"
+#include "resource_manager.h"
 #include "shader_manager.h"
+#include "unit_resource.h"
 #include "vector3.h"
 #include <string.h> // memcpy
 
@@ -138,7 +141,7 @@ void DebugLine::add_obb(const Matrix4x4& tm, const Vector3& half_extents, const
 
 void DebugLine::add_mesh(const Matrix4x4& tm, const void* vertices, u32 stride, const u16* indices, u32 num, const Color4& color)
 {
-	for (u32 i = 0; i < num; ++i)
+	for (u32 i = 0; i < num; i += 3)
 	{
 		const u32 i0 = indices[i + 0];
 		const u32 i1 = indices[i + 1];
@@ -154,6 +157,42 @@ void DebugLine::add_mesh(const Matrix4x4& tm, const void* vertices, u32 stride,
 	}
 }
 
+void DebugLine::add_unit(ResourceManager& rm, const Matrix4x4& tm, StringId64 name, const Color4& color)
+{
+	const UnitResource& ur = *(const UnitResource*)rm.get(RESOURCE_TYPE_UNIT, name);
+
+	const char* component_data = (const char*)(&ur + 1);
+
+	for (u32 cc = 0; cc < ur.num_component_types; ++cc)
+	{
+		const ComponentData* component = (const ComponentData*)component_data;
+		const u32* unit_index = (const u32*)(component + 1);
+		const char* data = (const char*)(unit_index + component->num_instances);
+
+		if (component->type == StringId32("mesh_renderer")._id)
+		{
+			const MeshRendererDesc* mrd = (const MeshRendererDesc*)data;
+			for (u32 i = 0; i < component->num_instances; ++i)
+			{
+				const MeshResource* mr = (const MeshResource*)rm.get(RESOURCE_TYPE_MESH, mrd->mesh_resource);
+				const MeshGeometry* mg = mr->geometry(mrd->geometry_name);
+
+				add_mesh(tm
+					, mg->vertices.data
+					, mg->vertices.stride
+					, (u16*)mg->indices.data
+					, mg->indices.num
+					, color
+					);
+
+				++mrd;
+			}
+		}
+
+		component_data += component->size + sizeof(ComponentData);
+	}
+}
+
 void DebugLine::reset()
 {
 	_num = 0;

+ 4 - 0
src/world/debug_line.h

@@ -6,6 +6,7 @@
 #pragma once
 
 #include "math_types.h"
+#include "resource_types.h"
 #include "string_id.h"
 #include "types.h"
 #include <bgfx/bgfx.h>
@@ -64,6 +65,9 @@ struct DebugLine
 	/// Adds the mesh described by (vertices, stride, indices, num).
 	void add_mesh(const Matrix4x4& tm, const void* vertices, u32 stride, const u16* indices, u32 num, const Color4& color);
 
+	/// Adds the meshes from the unit @a name.
+	void add_unit(ResourceManager& rm, const Matrix4x4& tm, StringId64 name, const Color4& color);
+
 	/// Resets all the lines.
 	void reset();