void InitChildren() { MeshDesc bigMeshDesc = new MeshDesc(); FillMeshDesc(bigMeshDesc); List<MeshDesc> meshDescs = new List<MeshDesc>(); MeshUtils.Split(bigMeshDesc, meshDescs, MeshUtils.nbMaxVerticesPerMesh); for (int i = 0; i < meshDescs.Count; ++i) { MeshDesc meshDesc = meshDescs[i]; Mesh mesh = new Mesh(); MeshUtils.ApplyToMesh(mesh, meshDesc); GameObject sub = GameObject.Instantiate(blockPrefab); MeshFilter meshFilter = sub.GetComponent<MeshFilter>(); if (meshFilter != null) { meshFilter.mesh = mesh; } sub.transform.parent = transform; sub.transform.localPosition = Vector3.zero; sub.transform.localRotation = Quaternion.identity; } }
public static void ApplyToMesh(Mesh mesh, MeshDesc desc) { mesh.Clear(); mesh.vertices = desc.positions.ToArray(); mesh.normals = desc.normals.ToArray(); mesh.triangles = desc.indices.ToArray(); mesh.Optimize(); mesh.RecalculateBounds(); }
public void Build(MeshDesc desc, OctreeNode root) { desc.Clear(); meshDesc = desc; if (root != null) { GenerateVertices(root); ContourCellProc(root); } }
public void Execute() { OctreeBuilder.Build(node, shape, minSize); OctreeMeshBuilder meshBuilder = new OctreeMeshBuilder(); MeshDesc meshDesc = new MeshDesc(); meshBuilder.Build(meshDesc, node); List<MeshDesc> descs = new List<MeshDesc>(); MeshUtils.Split(meshDesc, descs, MeshUtils.nbMaxVerticesPerMesh); meshDescs = descs.ToArray(); }
private bool TryGetMeshByPtr(Object addr, out MeshDesc outDesc) { bool ret = false; MeshDesc foundDesc = new MeshDesc(); foreach (MeshDesc md in Meshes.Values) { if (md.ptr == addr) { foundDesc = md; ret = true; break; } } outDesc = foundDesc; return(ret); }
private bool TryGetMeshByPtr(BulletShape shape, out MeshDesc outDesc) { bool ret = false; MeshDesc foundDesc = new MeshDesc(); foreach (MeshDesc md in Meshes.Values) { if (md.shape.ReferenceSame(shape)) { foundDesc = md; ret = true; break; } } outDesc = foundDesc; return(ret); }
public static void Split(MeshDesc desc, IList<MeshDesc> outDescs, int nbMaxVerticesPerDesc) { int nbPositions = desc.positions.Count; int nbIndices = desc.indices.Count; int nbTriangles = nbIndices / 3; int[] vertexBatch = new int[desc.positions.Count]; int[] vertexBatchIndex = new int[desc.positions.Count]; for (int i = 0; i < vertexBatch.Length; ++i) { vertexBatch[i] = -1; } MeshDesc currentDesc = new MeshDesc(); int currentBatchIndex = outDescs.Count; for (int i = 0; i < nbTriangles; ++i) { for (int j = 0; j < 3; ++j) { int indexIndex = i * 3 + j; int vertIndex = desc.indices[indexIndex]; int batchIndex = vertexBatch[vertIndex]; if (batchIndex != currentBatchIndex) { vertexBatch[vertIndex] = currentBatchIndex; vertexBatchIndex[vertIndex] = currentDesc.positions.Count; currentDesc.positions.Add(desc.positions[vertIndex]); currentDesc.normals.Add(desc.normals[vertIndex]); } int vertBatchIndex = vertexBatchIndex[vertIndex]; currentDesc.indices.Add(vertBatchIndex); } if (currentDesc.positions.Count > nbMaxVerticesPerDesc - 3) { outDescs.Add(currentDesc); currentDesc = new MeshDesc(); ++currentBatchIndex; } } if (currentDesc.indices.Count > 0) { outDescs.Add(currentDesc); } }
void AddCube(MeshDesc meshDesc, CubeDesc desc, Vector3 position) { int positionsStartIndex = meshDesc.positions.Count; int normalsStartIndex = meshDesc.normals.Count; int indicesStartIndex = meshDesc.indices.Count; meshDesc.positions.AddRange(desc.positions); meshDesc.normals.AddRange(desc.normals); meshDesc.indices.AddRange(desc.indices); for (int i = positionsStartIndex; i < meshDesc.positions.Count; ++i) { meshDesc.positions[i] += position; } for (int i = indicesStartIndex; i < meshDesc.indices.Count; ++i) { meshDesc.indices[i] += positionsStartIndex; } }
void FillMeshDesc(MeshDesc desc) { CubeDesc cubeDesc = new CubeDesc(); InitCubeDesc(cubeDesc); Vector3 position = new Vector3(); for (int z = 0; z < size; ++z) { for (int y = 0; y < size; ++y) { for (int x = 0; x < size; ++x) { position.x = x * cubeSize; position.y = y * cubeSize; position.z = z * cubeSize; AddCube(desc, cubeDesc, position); } } } }
unsafe static void Main(string[] args) { var mesh = new MeshDesc(); var buffer = new byte[Marshal.SizeOf(mesh)]; // Set where NameLen will be read from. buffer[0] = 12; // Use Buffer.BlockCopy to raw copy data across arrays of primitives. // Note we copy to offset 2 here: char's have alignment of 2, so there is // a padding byte after NameLen: just like in C. Buffer.BlockCopy("Hello!".ToCharArray(), 0, buffer, 2, 12); // Copy data to struct Read(buffer, out mesh); // Print the Name we wrote above: var name = new char[mesh.NameLen]; // Use Marsal.Copy to copy between arrays and pointers to arrays. unsafe { Marshal.Copy((IntPtr)mesh.Name, name, 0, mesh.NameLen); } // Note you can also use the String.String(char*) overloads Console.WriteLine("Name: " + new string(name)); // If Erik Myers likes it... mesh.VertexCount = 4711; // Copy data from struct: // MeshDesc is a struct, and is on the stack, so it's // memory is effectively pinned by the stack pointer. // This means '&' is sufficient to get a pointer. Write(&mesh, buffer); // Watch for alignment again, and note you have endianess to worry about... int vc = buffer[100] | (buffer[101] << 8) | (buffer[102] << 16) | (buffer[103] << 24); Console.WriteLine("VertexCount = " + vc); }
private bool TryGetMeshByPtr(BulletShape shape, out MeshDesc outDesc) { bool ret = false; MeshDesc foundDesc = new MeshDesc(); foreach (MeshDesc md in Meshes.Values) { if (md.shape.ReferenceSame(shape)) { foundDesc = md; ret = true; break; } } outDesc = foundDesc; return ret; }
unsafe static void Read(byte[] buffer, out MeshDesc mesh) { fixed(byte *pBuffer = buffer) mesh = *(MeshDesc *)pBuffer; }
private bool TryGetMeshByPtr(Object addr, out MeshDesc outDesc) { bool ret = false; MeshDesc foundDesc = new MeshDesc(); foreach (MeshDesc md in Meshes.Values) { if (md.ptr == addr) { foundDesc = md; ret = true; break; } } outDesc = foundDesc; return ret; }