No, We do not subclass Geometry at all.
We do use UrhoSharp.Forms. If this is not the place for questions related to UrhoSharp, is there any place you can recommend asking our question?
The function creating our geometry objects is listed below. It’s now marked as ‘unsafe’ because we’ve tried moving it to the iOS layer to have it insert the vertices with a fixed() statement, but that doesn’t appear to make any kind of difference. Behaviour remains the same.
PS: I have no clue on how to check if this is caused by the Urho3D core. Any suggestions?
Here’s the code converting our vertices and indices to a geometry object:
public unsafe Node Mesh2UrhoModel(Context Context, Scene scene, Node PlotNode, Material material, float[] verticesArray, Int32[] indicesArray, string globalID, string IFC_Type, string ObjectType)
{
// called very often
// DebugMessage("private void Mesh2UrhoModel(float[] verticesArray, long[] indicesArray, string globalID, string IFC_Type, string ObjectType)");
VertexBuffer vertexBuffer = new VertexBuffer(Context, false); // false
IndexBuffer indicesBuffer = new IndexBuffer(Context, false); // false
uint numberOfVertices = (uint)verticesArray.Length;
uint numberOfIndices = (uint)indicesArray.Length;
// Shadowed buffer needed for raycasts to work, and so that data can be automatically restored on device loss
vertexBuffer.Shadowed = true;
vertexBuffer.SetSize(numberOfVertices / 6, ElementMask.Position | ElementMask.Normal, false);
try
{
fixed (float* p = &verticesArray[0])
vertexBuffer.SetData(p);
}
catch (Exception e)
{
Debug.WriteLine("Thrown: " + e.Message);
}
indicesBuffer.Shadowed = true;
indicesBuffer.SetSize(numberOfIndices, false, false); // indicesBuffer.SetSize(numberOfIndices, false, false);
//Reversing the indices array
//Polygon direction is dependant on the direction in which you reference the vertices
//So instead of making a polygon between vertices 0-1-2, it should be made between 2-1-0
//This prevents backfacing; geometry displaying inside out
// this seems to undo an already executed inversion! see SwapIndices in IFCgeometry.cs
var indices = new short[numberOfIndices];
for (var i = numberOfIndices; i > 0; i--)
{
var index = numberOfIndices - i;
indices[index] = (short)indicesArray[i - 1];
}
//var indices = new short[numberOfIndices];
//for (var i = numberOfIndices; i < numberOfIndices; i++)
// indices[i] = (short)indicesArray[i];
// indicesBuffer.SetData(indices); // cannot convert int[] to void *. ask Peter.
try
{
fixed (short* p = &indices[0])
indicesBuffer.SetData(p);
}
catch (Exception e)
{
Debug.WriteLine("Thrown: " + e.Message);
}
var geometry = new Geometry();
geometry.SetNumVertexBuffers(1); // added Sander
geometry.SetVertexBuffer(0, vertexBuffer);
geometry.IndexBuffer = indicesBuffer;
// geometry.SetDrawRange(PrimitiveType.TriangleList, 0, numberOfIndices, true);
geometry.SetDrawRange(PrimitiveType.TriangleList, 0, numberOfIndices, 0, numberOfVertices / 6, true);
var model = new Urho.Model();
model.NumGeometries = 1;
model.SetGeometry(0, 0, geometry);
Vector3 minVector = new Vector3(1000000, 1000000, 1000000);
Vector3 maxVector = new Vector3(-1000000, -1000000, -1000000);
for (int i = 0; i < verticesArray.Length; i += 3)
{
float x = verticesArray[i];
float y = verticesArray[i + 1];
float z = verticesArray[i + 2];
if (x < minVector.X) minVector.X = x;
if (y < minVector.Y) minVector.Y = y;
if (z < minVector.Z) minVector.Z = z;
if (x > maxVector.X) maxVector.X = x;
if (y > maxVector.Y) maxVector.Y = y;
if (z > maxVector.Z) maxVector.Z = z;
}
model.BoundingBox = new BoundingBox(minVector, maxVector);
//model.BoundingBox = new BoundingBox(new Vector3(-100f, -100f, -100f), new Vector3(100f, 100f, 100f));
var node = PlotNode.CreateChild(globalID); // scene.CreateChild(); //
node.Position = new Vector3(0.0f, 0.0f, 0.0f);
node.SetScale(1.0f);
var nodeObject = node.CreateComponent<StaticModel>();
nodeObject.Model = model;
nodeObject.SetMaterial(material);
return (node);
}