Skip to content

Instantly share code, notes, and snippets.

@EgorBo
Created September 13, 2016 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EgorBo/59dc057a4e3b66f38b801fa0b5e249bd to your computer and use it in GitHub Desktop.
Save EgorBo/59dc057a4e3b66f38b801fa0b5e249bd to your computer and use it in GitHub Desktop.
//1. TriangleIndices
short[] indecies = new short[surf.TrianglesData.Length];
for (int n = 0; n < surf.TrianglesData.Length; n += 2)
{
//DirectXPixelFormat.R16UInt
var index = BitConverter.ToInt16(surf.TrianglesData, n);
indecies[n] = index;
}
float[] vertexData = new float[surf.VertexesCount * 6];
//2,3 - VertexPositions and Normals
for (int i = 0; i < surf.VertexesCount; i++)
{
//VertexPositions: DirectXPixelFormat.R16G16B16A16IntNormalized
short x = BitConverter.ToInt16(surf.VertexesData, i*8 + 0);
short y = BitConverter.ToInt16(surf.VertexesData, i*8 + 2);
short z = BitConverter.ToInt16(surf.VertexesData, i*8 + 4);
short w = BitConverter.ToInt16(surf.VertexesData, i*8 + 6);
//short to float:
float xx = (x == -32768) ? -1.0f : x*1.0f/(32767.0f);
float yy = (y == -32768) ? -1.0f : y*1.0f/(32767.0f);
float zz = (z == -32768) ? -1.0f : z*1.0f/(32767.0f);
float ww = (w == -32768) ? -1.0f : w*1.0f/(32767.0f);
//Normals: DirectXPixelFormat.R8G8B8A8IntNormalized
var normalX = surf.NormalsData[i * 4 + 0];
var normalY = surf.NormalsData[i * 4 + 1];
var normalZ = surf.NormalsData[i * 4 + 2];
//merge vertex+normals for Urho3D (also, change RH to LH coordinate systems)
vertexData[i * 6 + 0] = xx * surf.VertexesScale.X;
vertexData[i * 6 + 1] = yy * surf.VertexesScale.Y;
vertexData[i * 6 + 2] = -zz * surf.VertexesScale.Z;
vertexData[i*6 + 3] = normalX == 0 ? 0 : 255 / normalX;
vertexData[i*6 + 4] = normalY == 0 ? 0 : 255 / normalY;
vertexData[i*6 + 5] = normalZ == 0 ? 0 : -255 / normalZ;
}
Model geometryModel = new Model();
VertexBuffer vertexBuffer = new VertexBuffer(Context, false);
IndexBuffer indexBuffer = new IndexBuffer(Context, false);
Geometry geometry = new Geometry();
// Shadowed buffer needed for raycasts to work, and so that data can be automatically restored on device loss
vertexBuffer.Shadowed = true;
vertexBuffer.SetSize(surf.VertexesCount, ElementMask.Position | ElementMask.Normal, false);
vertexBuffer.SetData(vertexData);
indexBuffer.Shadowed = true;
indexBuffer.SetSize(surf.TrianglesCount, false, false);
indexBuffer.SetData(indecies);
geometry.SetVertexBuffer(0, vertexBuffer);
geometry.IndexBuffer = indexBuffer;
geometry.SetDrawRange(PrimitiveType.TriangleList, 0, surf.TrianglesCount, true);
geometryModel.NumGeometries = 1;
geometryModel.SetGeometry(0, 0, geometry);
geometryModel.BoundingBox = new BoundingBox(new Vector3(-1.0f, -1.0f, -1.0f), new Vector3(1.0f, 1.0f, 1.0f));
Node geometryNode = Scene.CreateChild();
geometryNode.Position = new Vector3(surf.BoundsCenter.X, surf.BoundsCenter.Y, -surf.BoundsCenter.Z);
var rot = new Quaternion(surf.BoundsOrientation.X, surf.BoundsOrientation.Y, surf.BoundsOrientation.Z,
surf.BoundsOrientation.W);
var rotAngles = rot.ToEulerAngles();
geometryNode.Rotation = new Quaternion(-rotAngles.X, -rotAngles.Y, rotAngles.Z);
StaticModel staticModel = geometryNode.CreateComponent<StaticModel>();
staticModel.Model = geometryModel;
staticModel.CastShadows = true;
var material = new Material();
material.CullMode = CullMode.None;
material.ShadowCullMode = CullMode.None;
material.SetTechnique(0, CoreAssets.Techniques.NoTexture, 1, 1);
material.SetShaderParameter("MatDiffColor", new Color(Randoms.Next(), Randoms.Next(), Randoms.Next()));
material.SetShaderParameter("MatSpecColor", new Color(Randoms.Next(), Randoms.Next(), Randoms.Next()));
staticModel.SetMaterial(material);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment