Przeglądaj źródła

world: add RenderWorld.mesh_material() and RenderWorld.sprite_material()

Daniele Bartolini 6 lat temu
rodzic
commit
feca236dfd
5 zmienionych plików z 40 dodań i 1 usunięć
  1. 1 1
      CHANGELOG.md
  2. 6 0
      docs/lua_api.rst
  3. 14 0
      src/lua/lua_api.cpp
  4. 13 0
      src/world/render_world.cpp
  5. 6 0
      src/world/render_world.h

+ 1 - 1
CHANGELOG.md

@@ -5,8 +5,8 @@ Changelog
 ------
 *DD MMM YYYY*
 
-* runtime: added RenderWorld.mesh_set_material()
 * runtime: added Material.set_vector4() and Material.set_matrix4x4()
+* runtime: added RenderWorld.mesh_material(), RenderWorld.mesh_set_material() and RenderWorld.sprite_material()
 * runtime: added the ability to scale the shape of colliders at Unit spawn time
 * runtime: added World.unit_by_name() to retrieve unit by its name in the Level Editor
 * runtime: fixed an issue that caused PhysicsWorld.set_gravity() to re-enable gravity to actors that previously disabled it with PhysicsWorld.actor_disable_gravity()

+ 6 - 0
docs/lua_api.rst

@@ -621,6 +621,9 @@ Mesh
 **mesh_instances** (rw, unit) : Id
 	Returns the IDs for all the meshes of the *unit*.
 
+**mesh_material** (rw, id) : Material
+	Returns the material of the mesh *id*.
+
 **mesh_set_material** (rw, id, material)
 	Sets the *material* of the mesh *id*.
 
@@ -645,6 +648,9 @@ Sprite
 **sprite_instances** (rw, unit) : Id
 	Returns the IDs for all the sprites of the *unit*.
 
+**sprite_material** (rw, unit) : Material
+	Returns the material of the sprite.
+
 **sprite_set_material** (rw, unit, material)
 	Sets the *material* of the sprite.
 

+ 14 - 0
src/lua/lua_api.cpp

@@ -1947,6 +1947,13 @@ void load_api(LuaEnvironment& env)
 			stack.push_float(t);
 			return 1;
 		});
+	env.add_module_function("RenderWorld", "mesh_material", [](lua_State* L)
+		{
+			LuaStack stack(L);
+			Material* material = stack.get_render_world(1)->mesh_material(stack.get_mesh_instance(2));
+			stack.push_pointer(material);
+			return 1;
+		});
 	env.add_module_function("RenderWorld", "mesh_set_material", [](lua_State* L)
 		{
 			LuaStack stack(L);
@@ -1994,6 +2001,13 @@ void load_api(LuaEnvironment& env)
 			stack.push_sprite_instance(inst);
 			return 1;
 		});
+	env.add_module_function("RenderWorld", "sprite_material", [](lua_State* L)
+		{
+			LuaStack stack(L);
+			Material* material = stack.get_render_world(1)->sprite_material(stack.get_unit(2));
+			stack.push_pointer(material);
+			return 1;
+		});
 	env.add_module_function("RenderWorld", "sprite_set_material", [](lua_State* L)
 		{
 			LuaStack stack(L);

+ 13 - 0
src/world/render_world.cpp

@@ -94,6 +94,12 @@ void RenderWorld::mesh_instances(UnitId id, Array<MeshInstance>& instances)
 	}
 }
 
+Material* RenderWorld::mesh_material(MeshInstance i)
+{
+	CE_ASSERT(i.i < _mesh_manager._data.size, "Index out of bounds");
+	return _material_manager->get(_mesh_manager._data.material[i.i]);
+}
+
 void RenderWorld::mesh_set_material(MeshInstance i, StringId64 id)
 {
 	CE_ASSERT(i.i < _mesh_manager._data.size, "Index out of bounds");
@@ -160,6 +166,13 @@ SpriteInstance RenderWorld::sprite_instances(UnitId unit)
 	return _sprite_manager.sprite(unit);
 }
 
+Material* RenderWorld::sprite_material(UnitId unit)
+{
+	SpriteInstance i = _sprite_manager.sprite(unit);
+	CE_ASSERT(i.i < _sprite_manager._data.size, "Index out of bounds");
+	return _material_manager->get(_sprite_manager._data.material[i.i]);
+}
+
 void RenderWorld::sprite_set_material(UnitId unit, StringId64 id)
 {
 	SpriteInstance i = _sprite_manager.sprite(unit);

+ 6 - 0
src/world/render_world.h

@@ -35,6 +35,9 @@ struct RenderWorld
 	/// Returns the mesh instances of the unit @a id.
 	void mesh_instances(UnitId id, Array<MeshInstance>& instances);
 
+	/// Returns the material of the mesh @a i.
+	Material* mesh_material(MeshInstance i);
+
 	/// Sets the material @a id of the mesh @a i.
 	void mesh_set_material(MeshInstance i, StringId64 id);
 
@@ -57,6 +60,9 @@ struct RenderWorld
 	/// Returns the sprite instances of the @a unit.
 	SpriteInstance sprite_instances(UnitId unit);
 
+	/// Returns the material of the sprite.
+	Material* sprite_material(UnitId unit);
+
 	/// Sets the material @a id of the sprite.
 	void sprite_set_material(UnitId unit, StringId64 id);