【发布时间】:2017-06-17 10:33:24
【问题描述】:
我正在使用 thebennybox 制作的 java 游戏引擎来创建自己的引擎。之前我使用 UE4,您可以在其中拥有静态网格体 Actor,在他的引擎中,这可能是一个附有 MeshRenderer 组件的游戏对象:
GameObject staticMeshActor_Grass = new GameObject();
AddObject(staticMeshActor_Grass.addComponent(new MeshRenderer(
new Mesh("plane.obj"), new Material(
new Texture("T_Grass_BC.png"), 1, 8,
new Texture("T_Grass_N.png")))));
现在我正在尝试创建一大片这些草块,但由于在 16x16 块之后这些都是它们各自的游戏对象(或 UE4 中的 Actor),它开始削弱帧速率!
我相信如果我在一个 GameObject 中创建多个静态网格实例以使其性能友好,我可以解决这个问题。在 UE4 中,此功能称为“Instanced Static Mesh Component”,它允许这样做,我希望有类似的东西。
这是 thebennybox 制作的 GameObject 类:
public class GameObject
{
private ArrayList<GameObject> m_children;
private ArrayList<GameComponent> m_components;
private Transform m_transform;
private CoreEngine m_engine;
public GameObject()
{
m_children = new ArrayList<GameObject>();
m_components = new ArrayList<GameComponent>();
m_transform = new Transform();
m_engine = null;
}
public GameObject AddChild(GameObject child)
{
m_children.add(child);
child.SetEngine(m_engine);
child.GetTransform().SetParent(m_transform);
return this;
}
public GameObject AddComponent(GameComponent component)
{
m_components.add(component);
component.SetParent(this);
return this;
}
public void InputAll(float delta)
{
Input(delta);
for(GameObject child : m_children)
child.InputAll(delta);
}
public void UpdateAll(float delta)
{
Update(delta);
for(GameObject child : m_children)
child.UpdateAll(delta);
}
public void RenderAll(Shader shader, RenderingEngine renderingEngine)
{
Render(shader, renderingEngine);
for(GameObject child : m_children)
child.RenderAll(shader, renderingEngine);
}
public void Input(float delta)
{
m_transform.Update();
for(GameComponent component : m_components)
component.Input(delta);
}
public void Update(float delta)
{
for(GameComponent component : m_components)
component.Update(delta);
}
public void Render(Shader shader, RenderingEngine renderingEngine)
{
for(GameComponent component : m_components)
component.Render(shader, renderingEngine);
}
public ArrayList<GameObject> GetAllAttached()
{
ArrayList<GameObject> result = new ArrayList<GameObject>();
for(GameObject child : m_children)
result.addAll(child.GetAllAttached());
result.add(this);
return result;
}
public Transform GetTransform()
{
return m_transform;
}
public void SetEngine(CoreEngine engine)
{
if(this.m_engine != engine)
{
this.m_engine = engine;
for(GameComponent component : m_components)
component.AddToEngine(engine);
for(GameObject child : m_children)
child.SetEngine(engine);
}
}
}
第一部分的AddObject()方法来自一个抽象类,最终调用GameObject类内部的AddChild(GameObject child)方法。如您所见,我们有一个 GameObjects 和 GameComponents 的 ArrayList。现在我必须实现一些方法来支持在一个 GameObject 中创建多个网格实例。
我已经尝试将多个 MeshRenderer 组件添加到一个游戏对象,但没有成功。也许有人建议我如何解决这个问题?
如果你真的感兴趣并想帮助我,你可以在这里下载引擎:https://github.com/BennyQBD/3DGameEngine
只需在eclipse中使用'Open Projects From File System..',选择这个文件夹的目录,一旦你完成了,你应该可以运行程序了。
非常感谢您的帮助!
【问题讨论】: