private void Start() { string fileName = Path.Combine(Application.streamingAssetsPath, objFile); if (!File.Exists(fileName)) { Debug.LogError("FILE COULD NOT BE FOUND!"); return; } string objData = File.ReadAllText(fileName); int objLen = (int)(objData.Split('o').Length * 1.2f); debugMeshes = new List <Mesh>(objLen); gameWorld = new world(objLen); MeshHelpers.CreateMeshesFromEpa(objData); Raycast.InitRaycastData(ref gameWorld); //Generate Ground plane. bounds ground = new bounds(); ground.minPoints = new float3(-1000, -.1f, -1000); ground.maxPoints = new float3(1000, .1f, 1000); groundMesh = MeshHelpers.MakeCubeFromBounds(ground); groundMesh.RecalculateBounds(); entity groundEntity = new entity(); groundEntity.bounds.minPoints = groundMesh.bounds.min; groundEntity.bounds.maxPoints = groundMesh.bounds.max; groundEntity.name = "Ground"; GameWorld.AddEntity(ref gameWorld, ref groundEntity); }
private void BuildScene03() { var mesh = SurfaceExamples.NonConvexPlanar(); var sphere = MeshHelpers.Sphere(1); var meshes = new List <Mesh> { mesh, sphere }; BuildScene(meshes); Camera = new ArcBallCamera(new Vector3(20, 20, 20), Vector3.Zero, -MathHelper.Pi * 100, MathHelper.Pi * 100, 1, 1000, GraphicsDevice); }
/// <summary> /// Initialize /// </summary> public void Awake() { // build the hashtable of targets foreach (BlendShape.Target t in targets) { _targetsByName.Add(t.name, t); } // acquire the _outMesh based on the type of renderer bool isSkin = renderer.GetType() == typeof(SkinnedMeshRenderer); if (_outMesh == null) { if (isSkin) { _outMesh = (meshRenderer as SkinnedMeshRenderer).sharedMesh; } else { _outMesh = GetComponent <MeshFilter>().mesh; } } // duplicate _outMesh to avoid modifying source asset _outMesh = MeshHelpers.DuplicateMesh(_outMesh, string.Format("{0} (Duplicate)", _outMesh.name)); if (isSkin) { (meshRenderer as SkinnedMeshRenderer).sharedMesh = _outMesh; } else { GetComponent <MeshFilter>().mesh = _outMesh; } // duplicate seamlessBaseMesh to avoid modifying source asset seamlessBaseMesh = MeshHelpers.DuplicateMesh(seamlessBaseMesh, string.Format("{0} (Duplicate)", seamlessBaseMesh.name)); // store necessary vertex and normal arrays _defaultNormals = _outMesh.normals; _seamlessNormals = seamlessBaseMesh.normals; _seamlessVertices = seamlessBaseMesh.vertices; _outNormals = _outMesh.normals; _outVertices = _outMesh.vertices; }
private void BuildScene01() { MeshGeometries = new List <MeshGeometry>(); Random rand = new Random(); int size = 10; int num = 10; int maxHeight = 100; for (int i = 0; i < num; i++) { for (int j = 0; j < num; j++) { var box = MeshHelpers.Box(size, size, rand.Next(maxHeight / 20, maxHeight)); var translate = Matrix4.Translation(i * 1.5 * size, j * 1.5 * size, 0); box.Transform(translate); var mg = new MeshGeometry(this, box); MeshGeometries.Add(mg); } } }
public static List <List <Point> > Tessellate(List <Surface> surfaces, double tolerance, int maxGridLines) { var rpfactory = new DefaultRenderPackageFactory(); var rp = rpfactory.CreateRenderPackage(); var tp = new TessellationParameters(); tp.MaxTessellationDivisions = maxGridLines; tp.Tolerance = tolerance; foreach (var surface in surfaces) { surface.Tessellate(rp, tp); } //grab double components from rp and subset them into points and further into triangles List <List <double> > pointdata = MeshHelpers.Split(rp.MeshVertices.ToList(), 3); List <Point> points = pointdata.Select(x => Point.ByCoordinates(x[0], x[1], x[2])).ToList(); List <List <Point> > tris = MeshHelpers.Split(points, 3); return(tris); }
public static Mesh MakePlanetaryMesh(Vector2 start, Vector2 sweep, float minRadius, int meshSegments = 32) { int neededVerts = (int)Pow(meshSegments * 2, 2); int neededTris = 2 * (int)Pow(meshSegments, 2); Vector3[] vertices = new Vector3[neededVerts]; Vector3[] normals = new Vector3[neededVerts]; Vector2[] uv = new Vector2[neededVerts]; int[] triangles = new int[neededTris * 3]; Vector2 sweepPer = sweep / meshSegments; int cnt = meshSegments * meshSegments; float xx, yy; for (int i = 0; i < cnt; i++) { xx = Floor(i % meshSegments) / meshSegments; yy = Floor(i / meshSegments) / meshSegments; } return(MeshHelpers.MakeMesh(vertices, uv, triangles, normals)); }
private void ReadFaceMeshes(MDLParser mdl, VVDParser vvd, VTXParser vtx, BSPParser bspParser, VPKParser vpkParser) { int textureIndex = 0; if (mdl.bodyParts.Length == vtx.bodyParts.Length) { for (int bodyPartIndex = 0; bodyPartIndex < mdl.bodyParts.Length; bodyPartIndex++) { for (int modelIndex = 0; modelIndex < mdl.bodyParts[bodyPartIndex].models.Length; modelIndex++) { //int currentPosition = 0; for (int meshIndex = 0; meshIndex < mdl.bodyParts[bodyPartIndex].models[modelIndex].theMeshes.Length; meshIndex++) { int rootLodIndex = mdl.header1.rootLod; //int rootLodIndex = 0; //int rootLodCount = 1; //if (mdl.header1.numAllowedRootLods == 0) // rootLodCount = vtx.header.numLODs; FaceMesh currentFace = new FaceMesh(); int verticesStartIndex = mdl.bodyParts[bodyPartIndex].models[modelIndex].theMeshes[meshIndex].vertexIndexStart; int vertexCount = 0; //int vertexCount = mdl.bodyParts[bodyPartIndex].models[modelIndex].theMeshes[meshIndex].vertexCount; //int vertexCount = mdl.bodyParts[bodyPartIndex].models[modelIndex].theMeshes[meshIndex].vertexData.lodVertexCount[rootLodIndex]; //int vertexCount = 0; //for (int i = 0; i <= rootLodIndex; i++) // vertexCount += mdl.bodyParts[bodyPartIndex].models[modelIndex].theMeshes[meshIndex].vertexData.lodVertexCount[i]; int trianglesCount = 0; for (int stripGroupIndex = 0; stripGroupIndex < vtx.bodyParts[bodyPartIndex].theVtxModels[modelIndex].theVtxModelLods[rootLodIndex].theVtxMeshes[meshIndex].stripGroupCount; stripGroupIndex++) { var currentStripGroup = vtx.bodyParts[bodyPartIndex].theVtxModels[modelIndex].theVtxModelLods[rootLodIndex].theVtxMeshes[meshIndex].theVtxStripGroups[stripGroupIndex]; for (int stripIndex = 0; stripIndex < currentStripGroup.theVtxStrips.Length; stripIndex++) { var currentStrip = currentStripGroup.theVtxStrips[stripIndex]; if (((StripHeaderFlags_t)currentStrip.flags & StripHeaderFlags_t.STRIP_IS_TRILIST) > 0) { trianglesCount += currentStrip.indexCount; } } } //List<int> triangles = new List<int>(); int[] triangles = new int[trianglesCount]; int trianglesIndex = 0; //for (int countUpLodIndex = 0; countUpLodIndex <= rootLodIndex; countUpLodIndex++) //{ for (int stripGroupIndex = 0; stripGroupIndex < vtx.bodyParts[bodyPartIndex].theVtxModels[modelIndex].theVtxModelLods[rootLodIndex].theVtxMeshes[meshIndex].stripGroupCount; stripGroupIndex++) { //var currentStripGroup = vtx.bodyParts[bodyPartIndex].theVtxModels[modelIndex].theVtxModelLods[rootLodIndex].theVtxMeshes[meshIndex].theVtxStripGroups[0]; var currentStripGroup = vtx.bodyParts[bodyPartIndex].theVtxModels[modelIndex].theVtxModelLods[rootLodIndex].theVtxMeshes[meshIndex].theVtxStripGroups[stripGroupIndex]; //int trianglesCount = currentStripGroup.theVtxIndices.Length; //int[] triangles = new int[trianglesCount]; for (int stripIndex = 0; stripIndex < currentStripGroup.theVtxStrips.Length; stripIndex++) { var currentStrip = currentStripGroup.theVtxStrips[stripIndex]; if (((StripHeaderFlags_t)currentStrip.flags & StripHeaderFlags_t.STRIP_IS_TRILIST) > 0) { for (int indexIndex = 0; indexIndex < currentStrip.indexCount; indexIndex += 3) { int vertexIndexA = verticesStartIndex + currentStripGroup.theVtxVertices[currentStripGroup.theVtxIndices[indexIndex + currentStrip.indexMeshIndex]].originalMeshVertexIndex; int vertexIndexB = verticesStartIndex + currentStripGroup.theVtxVertices[currentStripGroup.theVtxIndices[indexIndex + currentStrip.indexMeshIndex + 2]].originalMeshVertexIndex; int vertexIndexC = verticesStartIndex + currentStripGroup.theVtxVertices[currentStripGroup.theVtxIndices[indexIndex + currentStrip.indexMeshIndex + 1]].originalMeshVertexIndex; vertexCount = Mathf.Max(vertexCount, vertexIndexA, vertexIndexB, vertexIndexC); //if (vertexIndexA < vertices.Length && vertexIndexB < vertices.Length && vertexIndexC < vertices.Length) //{ triangles[trianglesIndex++] = vertexIndexA; triangles[trianglesIndex++] = vertexIndexB; triangles[trianglesIndex++] = vertexIndexC; //triangles.Add(vertexIndexA); //triangles.Add(vertexIndexB); //triangles.Add(vertexIndexC); //} } } } } //} vertexCount += 1; //vertexCount = vvd.vertices.Length; //vertexCount = vvd.header.numLODVertices[rootLodIndex]; Vector3[] vertices = new Vector3[vertexCount]; Vector3[] normals = new Vector3[vertexCount]; Vector2[] uv = new Vector2[vertexCount]; for (int verticesIndex = 0; verticesIndex < vertices.Length; verticesIndex++) { vertices[verticesIndex] = vvd.vertices[verticesIndex].m_vecPosition; normals[verticesIndex] = vvd.vertices[verticesIndex].m_vecNormal; uv[verticesIndex] = vvd.vertices[verticesIndex].m_vecTexCoord; } Debug.Assert(triangles.Length % 3 == 0, "SourceModel: Triangles not a multiple of three for " + modelPath); if (mdl.header1.includemodel_count > 0) { Debug.LogWarning("SourceModel: Include model count greater than zero (" + mdl.header1.includemodel_count + ", " + mdl.header1.includemodel_index + ") for " + modelPath); } if (vvd.header.numFixups > 0) { Debug.LogWarning("SourceModel: " + vvd.header.numFixups + " fixups found for " + modelPath); } MeshHelpers.MeshData meshData; //var outcome = MeshHelpers.GenerateConvexHull(vertices, out meshData, 0.2); //if (outcome != MIConvexHull.ConvexHullCreationResultOutcome.Success) // Debug.LogError("SourceModel: Convex hull error " + outcome + " for " + modelPath); if (decimationPercent > 0) { meshData = MeshHelpers.DecimateByTriangleCount(vertices, triangles, normals, 1 - decimationPercent); meshData.uv = new Vector2[meshData.vertices.Length]; System.Array.Copy(uv, meshData.uv, meshData.vertices.Length); } else { meshData = new MeshHelpers.MeshData(); meshData.vertices = vertices; meshData.triangles = triangles; meshData.normals = normals; meshData.uv = uv; } currentFace.meshData = meshData; string textureName = ""; string texturePath = mdl.texturePaths[0].Replace("\\", "/").ToLower(); if (textureIndex < mdl.textures.Length) { textureName = mdl.textures[textureIndex].name.Replace("\\", "/").ToLower(); } //textureName = mdl.textures[textureIndex].name.Replace("\\", "/").ToLower(); if (textureName.IndexOf(texturePath) > -1) { texturePath = ""; } string textureLocation = texturePath + textureName; //if (modelTextures != null && textureIndex < modelTextures.Length) //Should not have this line // textureLocation = modelTextures[textureIndex]?.location; currentFace.faceName = textureLocation; currentFace.material = VMTData.GrabVMT(bspParser, vpkParser, textureLocation); //if (!excludeTextures) // currentFace.texture = SourceTexture.GrabTexture(bspParser, vpkParser, textureLocation); faces.Add(currentFace); textureIndex++; } } } } else { Debug.LogError("SourceModel: MDL and VTX body part count doesn't match (" + modelPath + ")"); } }