-
Notifications
You must be signed in to change notification settings - Fork 2
How to define 3D object in code
If you refer to example, you may see that even simple planes are loaded from mdl files. This approach is quite simple, but it would not work if you are making a procedural levels. Generally a programmer should create a child of scene node; then create StaticModel component, define CustomGeometry object (may require some math, because normals should be calculated in program too); create model; assign it to node. There is more complex example of how to do it: https://github.com/urho3d/Urho3D/blob/master/Source/Samples/34_DynamicGeometry/DynamicGeometry.cpp (seek for pyramid shape generation).
An easier way is to create a CustomGeometry Component for Node (It is assumed implicitly in documentation. A Component may be constructed over a Drawable class, and Drawable is a parent for CustomGeometry).
{ //another try of static geometry. Create a plane consisting of two triangles.
Node* nodeDynamicMesh2 = scene_->CreateChild("Object");
nodeDynamicMesh2->SetPosition(Vector3(0.0f, 0.0f, 0.0f));
nodeDynamicMesh2->SetScale(Vector3(5,1,5));
CustomGeometry* _geometryDynamicMesh2 = nodeDynamicMesh2->CreateComponent<CustomGeometry>();
_geometryDynamicMesh2->BeginGeometry(0, TRIANGLE_LIST);
// tri 1
_geometryDynamicMesh2->DefineVertex(Vector3(1, 0, 1));
_geometryDynamicMesh2->DefineTexCoord(Vector2(2.0, 2.0));
_geometryDynamicMesh2->DefineNormal(Vector3(0, 1, 0));
_geometryDynamicMesh2->DefineVertex(Vector3(1, 0, 0));
_geometryDynamicMesh2->DefineTexCoord(Vector2(2.0, 1.0));
_geometryDynamicMesh2->DefineNormal(Vector3(0, 1, 0));
_geometryDynamicMesh2->DefineVertex(Vector3(-1, 0, 0));
_geometryDynamicMesh2->DefineTexCoord(Vector2(0.0, 1.0));
_geometryDynamicMesh2->DefineNormal(Vector3(0, 1, 0));
// tri 2
_geometryDynamicMesh2->DefineVertex(Vector3(-1, 0, 1));
_geometryDynamicMesh2->DefineNormal(Vector3(0, 1, 0));
_geometryDynamicMesh2->DefineTexCoord(Vector2(0.0, 2.0));
_geometryDynamicMesh2->DefineVertex(Vector3(1, 0, 1));
_geometryDynamicMesh2->DefineNormal(Vector3(0, 1, 0));
_geometryDynamicMesh2->DefineTexCoord(Vector2(2.0, 2.0));
_geometryDynamicMesh2->DefineVertex(Vector3(-1, 0, 0));
_geometryDynamicMesh2->DefineNormal(Vector3(0, 1, 0));
_geometryDynamicMesh2->DefineTexCoord(Vector2(0.0, 1.0));
_geometryDynamicMesh2->Commit();
_geometryDynamicMesh2->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml"));
}
Although this code is simpler than the code in example, but it would create less optimized unindexed geometry (the same vertices belonging to different triangles would be processed independently. This is a common CG concept )
It is possible to get this done by adding the following code:
PODVector<VertexElement> elements;
elements.Push(VertexElement(TYPE_VECTOR3, SEM_POSITION));
elements.Push(VertexElement(TYPE_VECTOR3, SEM_NORMAL));
After that it is possible to set up the texture coordinates.
Start with BeginGeometry, with a TRIANGLE_LIST parameter. One triangle is constructed over each of 3 points. Define each point with DefineVertex, which accepts coordinates in the 3d space of shape. Call DefineNormal to set a normal for this point: a vector (unit vector basically; if you calculate it manually then take some time to normalize it), which is perpendicular to the plane to which this point belongs. This vector should face outwards, defining a visible part of a plane. Call DefineTexCoord to point out which coordinates of your texture should correspond to this point. Also there is a SetColor routine which assigns a color to a point. End defining points with Commit() call.
Source: https://discourse.urho3d.io/t/runtime-geometry-creation/337/4 https://discourse.urho3d.io/t/a-mesh-generator/2361/4
Modify me