public GameObject DrawCeiling3D() { GameObject polyExtruderGO = new GameObject(); PolyExtruder polyExtruder = polyExtruderGO.AddComponent <PolyExtruder>(); polyExtruderGO.name = "TEST EXTRUDER"; var vertices2D = Points.Select(v => new Vector2(v.x, v.y)).ToArray(); polyExtruder.createPrism(polyExtruderGO.name, 0.2f, vertices2D, Color.grey, true); polyExtruderGO.transform.Translate(new Vector3(2000, LevelBottom, 0), Space.World); polyExtruder.prismColor = Color.blue; // Combine meshes MeshFilter[] allMesheFilters = polyExtruderGO.GetComponentsInChildren <MeshFilter>(); var mesh = CombineMeshes(allMesheFilters.ToList()); if (Vertices3D != null && Vertices3D.Length > 0) { mesh.uv = UvCalculator.CalculateUVs(Vertices3D, 1); } mesh.RecalculateNormals(); GameObject newObj = new GameObject(); newObj.name = Name; newObj.transform.position = polyExtruderGO.transform.position; newObj.transform.rotation = polyExtruderGO.transform.rotation; newObj.transform.localScale = polyExtruderGO.transform.localScale; var newObjMeshFilter = newObj.AddComponent <MeshFilter>(); var newObjMeshRenderer = newObj.AddComponent <MeshRenderer>(); newObjMeshFilter.mesh = mesh; newObjMeshRenderer.material = Material; newObj.AddComponent <MeshCollider>(); newObj.tag = "Ceiling"; DestroyImmediate(polyExtruderGO); var refInstance3D = Instance3D; if (refInstance3D != null) { DestroyImmediate(refInstance3D); } newObj.layer = LayerMask.NameToLayer("3DArea"); newObj.transform.parent = GameObject.Find("3DArea").transform; Instance3D = newObj; return(newObj); }
public void Show() { if (rangeMesh == null) { rangeMesh = new Mesh(); } if (meshFilter == null) { meshFilter = GetComponent <MeshFilter>(); } float arcStart = 0; float arcSize = 360; if (objectDetector.filterByArc) { arcStart = objectDetector.CurrentArcStartAngle; arcSize = objectDetector.ArcSize; } if (objectDetector.MinRange == 0) { circleMeshGenerator.GenerateMesh(rangeMesh, arcStart, arcSize, objectDetector.MaxRange, resolution); } else { donutMeshGenerator.GenerateMesh(rangeMesh, arcStart, arcSize, objectDetector.MinRange, objectDetector.MaxRange, resolution); } rangeMesh.uv = UvCalculator.CalculateUVs(rangeMesh.vertices, 1); meshFilter.sharedMesh = rangeMesh; }
void CreateMaskMesh() { var points2d = new Vector2[selectionPoints.Count]; for (int i = 0; i < points2d.Length; i++) { points2d[i] = new Vector2(selectionPoints[i].x, selectionPoints[i].z); } Triangulator tr = new Triangulator(points2d); int[] indices = tr.Triangulate(); // Create the Vector3 vertices Vector3[] vertices = new Vector3[selectionPoints.Count]; for (int i = 0; i < vertices.Length; i++) { vertices[i] = new Vector3(selectionPoints[i].x, 0, selectionPoints[i].z); } var uv = UvCalculator.CalculateUVs(vertices, 1f); // Create the mesh Mesh msh = new Mesh(); msh.vertices = vertices; msh.triangles = indices; msh.uv = uv; msh.RecalculateNormals(); msh.RecalculateBounds(); shapeSelection = msh; selectionPoints.Add(selectionPoints[0]); EditorWindow.GetWindow <Prefabshop>().maskShape = msh; EditorWindow.GetWindow <Prefabshop>().maskOutline = selectionPoints.ToArray(); }
public void BuildObject() { if (builtRamp == null || myFilter == null || myRenderer == null || myCollider == null) { InitializeRampObject(); } Vector3[] vertices = GenerateVertices(); int[] indices = GenerateIndices(); Color[] colors = GenerateColors(vertices.Length); Vector2[] uvs = UvCalculator.CalculateUVs(vertices, 1); Vector3[] normals = GenerateNormals(); Mesh newMesh = new Mesh(); newMesh.vertices = vertices; newMesh.triangles = indices; newMesh.colors = colors; newMesh.uv = uvs; newMesh.normals = normals; newMesh.name = "generated mesh"; //newMesh.RecalculateNormals(); myFilter.sharedMesh = newMesh; myCollider.sharedMesh = newMesh; myRenderer.sharedMaterial = material; }
public GameObject DrawRoof3D() { GameObject roofObject = new GameObject(); roofObject.name = Name; roofObject.tag = Tag; MeshFilter mf = roofObject.AddComponent <MeshFilter>(); Mesh tmesh = new Mesh(); tmesh.vertices = Vertices3D; tmesh.triangles = Triangles3D; if (Vertices3D != null && Vertices3D.Length > 0) { tmesh.uv = UvCalculator.CalculateUVs(Vertices3D, 1); } tmesh.RecalculateNormals(); mf.mesh = tmesh; var mr = roofObject.AddComponent <MeshRenderer>(); mr.material = Material; roofObject.transform.Translate(new Vector3(2000, 0, 0), Space.World); roofObject.transform.parent = GameObject.Find("3DArea").transform; roofObject.AddComponent <MeshCollider>(); roofObject.layer = LayerMask.NameToLayer("3DArea"); return(roofObject); }
public GameObject DrawWallWithHole(GameObject holeObject) { GameObject wall = new GameObject(); wall.name = Name; var solidWall = SolidFromGameObject(Instance3D); var solidHole = SolidFromGameObject(holeObject); // Referencja do starej œciany (aby mo¿na by³o usun¹c obiekt) var oldWall = Instance3D; var modeller = new Net3dBool.BooleanModeller(solidWall, solidHole); Solid tmp = modeller.getDifference(); MeshFilter mf = wall.AddComponent <MeshFilter>(); Mesh tmesh = new Mesh(); tmesh.vertices = GetVertices(tmp); tmesh.triangles = tmp.getIndices(); if (tmesh.vertices != null && tmesh.vertices.Length > 0) { tmesh.uv = UvCalculator.CalculateUVs(tmesh.vertices, 1); } tmesh.colors = GetColorsMesh(tmp); tmesh.RecalculateNormals(); mf.mesh = tmesh; mf.mesh.RecalculateTangents(0); mf.mesh.RecalculateNormals(); mf.mesh.RecalculateBounds(); mf.mesh.OptimizeReorderVertexBuffer(); mf.mesh.OptimizeIndexBuffers(); mf.mesh.Optimize(); var mr = wall.AddComponent <MeshRenderer>(); mr.material = Material; wall.AddComponent <MeshCollider>(); wall.tag = "Wall"; wall.transform.parent = GameObject.Find("3DArea").transform; wall.transform.Translate(new Vector3(2000, 0, 0), Space.World); Instance3D = wall; DestroyImmediate(oldWall); DestroyImmediate(holeObject); wall.layer = LayerMask.NameToLayer("3DArea"); return(wall); }
private void GenereateMeshUVS() { Vector2[] UV = { }; if (_mesh != null) { if (_mesh.vertexCount > 3) { UV = UvCalculator.CalculateUVs(_mesh.vertices, 1); foreach (Vector2 textureUV in UV) { textureUV.Normalize(); } } _mesh.uv = UV; } }
public GameObject DrawWall3D() { GameObject wallobject = new GameObject(); wallobject.name = Name; wallobject.tag = Tag; MeshFilter mf = wallobject.AddComponent <MeshFilter>(); Mesh tmesh = new Mesh(); tmesh.vertices = Vertices3D; tmesh.triangles = Triangles3D; if (Vertices3D != null && Vertices3D.Length > 0) { tmesh.uv = UvCalculator.CalculateUVs(Vertices3D, 1); } tmesh.RecalculateNormals(); mf.mesh = tmesh; var mr = wallobject.AddComponent <MeshRenderer>(); mr.material = Material; wallobject.transform.Translate(new Vector3(2000, 0, 0), Space.World); wallobject.transform.parent = GameObject.Find("3DArea").transform; // Doda Collider gdy œciana ma d³ugoœæ min. 20 cm if (Vector3.Distance(StartPoint, EndPoint) > 0.2f) { wallobject.AddComponent <MeshCollider>(); } wallobject.layer = LayerMask.NameToLayer("3DArea"); return(wallobject); }
public GameObject GableWall3D(float kneeHeight) { GameObject gableWall = new GameObject(); gableWall.name = Name; var sp = GetPosition3dView(StartPoint, 0f); // StartPoint var ep = GetPosition3dView(EndPoint, 0f); // EndPoint // Referencja do starej œciany (aby mo¿na by³o usun¹c obiekt) var oldWall = Instance3D; List <Vector3> points = new List <Vector3>(); Vector3 centralVector = (sp + ep) / 2; // Front points.Add(new Vector3(sp.x, BottomLevel, sp.z)); points.Add(new Vector3(sp.x, BottomLevel + kneeHeight, sp.z)); points.Add(new Vector3(centralVector.x, BottomLevel + Height, centralVector.z)); points.Add(new Vector3(ep.x, BottomLevel + kneeHeight, ep.z)); points.Add(new Vector3(ep.x, BottomLevel, ep.z)); // Back var l = ep - sp; var s = Vector3.Normalize(Vector3.Cross(l, Vector3.up)); points[0] = points[0] - s * Width / 2; points[1] = points[1] - s * Width / 2; points[2] = points[2] - s * Width / 2; points[3] = points[3] - s * Width / 2; points[4] = points[4] - s * Width / 2; points.Add(points[0] + s * Width); points.Add(points[1] + s * Width); points.Add(points[2] + s * Width); points.Add(points[3] + s * Width); points.Add(points[4] + s * Width); List <int> triangles = new List <int>(); // Front triangles triangles.AddRange(new int[] { 0, 1, 4 }); triangles.AddRange(new int[] { 4, 1, 3 }); triangles.AddRange(new int[] { 1, 2, 3 }); // Back triangles triangles.AddRange(new int[] { 9, 6, 5 }); triangles.AddRange(new int[] { 6, 9, 8 }); triangles.AddRange(new int[] { 7, 6, 8 }); // Bottom triangles triangles.AddRange(new int[] { 0, 9, 5 }); triangles.AddRange(new int[] { 0, 4, 9 }); // LeftTop triangles.AddRange(new int[] { 6, 7, 1 }); triangles.AddRange(new int[] { 1, 7, 2 }); // LeftBottom triangles.AddRange(new int[] { 5, 6, 0 }); triangles.AddRange(new int[] { 0, 6, 1 }); // RightBottom triangles.AddRange(new int[] { 4, 8, 9 }); triangles.AddRange(new int[] { 4, 3, 8 }); // RightTop triangles.AddRange(new int[] { 3, 2, 8 }); triangles.AddRange(new int[] { 8, 2, 7 }); var mesh = new Mesh { vertices = points.ToArray(), triangles = triangles.ToArray(), }; if (points.ToArray() != null && points.ToArray().Length > 0) { mesh.uv = UvCalculator.CalculateUVs(points.ToArray(), 1); } mesh.RecalculateNormals(); mesh.RecalculateBounds(); var meshRenderer = gableWall.AddComponent <MeshRenderer>(); var filter = gableWall.AddComponent <MeshFilter>(); meshRenderer.material = Material; filter.mesh = mesh; gableWall.AddComponent <MeshCollider>(); gableWall.tag = "Wall"; gableWall.transform.Translate(new Vector3(2000, 0, 0), Space.World); gableWall.transform.parent = GameObject.Find("3DArea").transform; gableWall.layer = LayerMask.NameToLayer("3DArea"); Instance3D = gableWall; DestroyImmediate(oldWall); return(gableWall); }
public void DrawNewAgents() { RegionName = "GamaMap"; bool isNewAgentCreated = false; List <FeatureMesh> meshList = new List <FeatureMesh>(); foreach (var agent in GamaManager.gamaAgentList) { if (!agent.isDrawed) { isNewAgentCreated = true; agent.isDrawed = true; //FeatureMesh featureMesh = new FeatureMesh("NameGama1", agent.geometry, "NameGama13", agent.agentName); FeatureMesh featureMesh = new FeatureMesh(agent.agentName, agent.getCollection(), agent.getLayer(), agent.agentName); List <Vector3> Vertices = new List <Vector3>(); List <Vector2> UVs = new List <Vector2>(); List <int> Indices = new List <int>(); MeshData meshData = featureMesh.Mesh; List <MeshData.Submesh> Submeshes = new List <MeshData.Submesh>(); MeshData.Submesh submesh = new MeshData.Submesh(); Vector2[] vertices2D = agent.agentCoordinate.getVector2Coordinates(); /* * // To delete. This is dealt in the UtilXml Class * List<Vector2> vect = new List<Vector2>(); * vect = vertices2D.ToList(); * if (agent.geometry.Equals("Polygon")) * { * // vect.RemoveAt(vect.Count - 1); * } * vertices2D = vect.ToArray(); */ Triangulator triangulator = new Triangulator(vertices2D); triangulator.setAllPoints(triangulator.get2dVertices()); float elevation = this.elevation; if (agent.geometry.Equals("LineString")) { elevation = 0.0f; } //Vertices = triangulator.get3dVerticesList(elevation); Vertices = triangulator.get3dVerticesList(agent.height); Debug.Log("-------------->>>> " + agent.agentName + " hight is ---------------> " + agent.height); Indices = triangulator.getTriangulesList(); Vector3[] VerticesArray = Vertices.ToArray(); Vector2[] UvArray = UvCalculator.CalculateUVs(VerticesArray, 100); UVs = new List <Vector2>(); UVs = UvArray.ToList(); /* * if (agent.geometry.Equals("Point")) * { * Vertices = agent.agentCoordinate.getVector3Coordinates().ToList(); * Indices = new List<int>(); * Indices.Add(1);Indices.Add(1);Indices.Add(1); * UVs = new List<Vector2>(); * } */ submesh.Indices = Indices; submesh.Material = buildingMaterial; Submeshes.Add(submesh); Debug.Log("addGamaMeshData ------> " + agent.geometry + " Agent name -> " + agent.agentName); meshData.addGamaMeshData(Vertices, UVs, Submeshes, agent); featureMesh.Mesh = meshData; meshList.Add(featureMesh); } } if (isNewAgentCreated) { if (regionMap != null) { //DestroyImmediate(regionMap); Debug.Log("regionMap is Null"); } // Merge all feature meshes List <FeatureMesh> features = new List <FeatureMesh>(); features.AddRange(meshList); regionMap = GameObject.Find(RegionName); if (regionMap == null) { regionMap = new GameObject(RegionName); } Debug.Log("The rgion Name is : " + RegionName); //regionMap = new GameObject(RegionName); var sceneGraph = new SceneGraph(regionMap, GroupOptions, GameObjectOptions, features); //sceneGraph sceneGraph.DrawFromGama(); } }
public static Mesh FourWayIntersection(List <Pair <Vector3> > outerPoints, List <Vector3> innerPoints, string name) { Mesh mesh = new Mesh(); List <int> triangles = new List <int>(); List <Vector3> verts = new List <Vector3>(); List <Vector3> norms = new List <Vector3>(); List <Vector2> uvs = new List <Vector2>(); for (int i = 0; i < 4; i++) { verts.Add(innerPoints[i]); norms.Add(Vector3.up); } triangles.Add(0); triangles.Add(2); triangles.Add(3); triangles.Add(0); triangles.Add(1); triangles.Add(2); int innerVertsIndex = 4; int outerVertsIndex = 0; for (int i = 0; i < 4; i++) { //First vertex + uvs and norms verts.Add(outerPoints[i].First); norms.Add(Vector3.up); //Second vertex + uvs and norms verts.Add(outerPoints[i].Second); norms.Add(Vector3.up); triangles.Add(innerVertsIndex + outerVertsIndex); triangles.Add((i + 1) % 4); triangles.Add(i); triangles.Add(innerVertsIndex + outerVertsIndex); triangles.Add(innerVertsIndex + outerVertsIndex + 1); triangles.Add((i + 1) % 4); triangles.Add(innerVertsIndex + outerVertsIndex + 1); triangles.Add(innerVertsIndex + ((outerVertsIndex + 2) % 8)); triangles.Add((i + 1) % 4); outerVertsIndex += 2; } mesh.vertices = verts.ToArray(); mesh.normals = norms.ToArray(); mesh.triangles = triangles.ToArray(); mesh.uv = UvCalculator.CalculateUVs(verts.ToArray(), 100); mesh.name = name; mesh.RecalculateBounds(); mesh.RecalculateNormals(); return(mesh); }
public void DrawNewAgents() { List <FeatureMesh> meshList = new List <FeatureMesh>(); foreach (var agent in GamaManager.gamaAgentList) { if (!agent.isDrawed) { agent.isDrawed = true; FeatureMesh featureMesh = new FeatureMesh("NameGama1", "GamaRoads", "NameGama13", agent.agentName); List <Vector3> Vertices = new List <Vector3>(); List <Vector2> UVs = new List <Vector2>(); List <int> Indices = new List <int>(); MeshData meshData = featureMesh.Mesh; List <MeshData.Submesh> Submeshes = new List <MeshData.Submesh>(); MeshData.Submesh submesh = new MeshData.Submesh(); Vector2[] vertices2D = agent.agentCoordinate.getVector2Coordinates(); Triangulator triangulator = new Triangulator(vertices2D); Vertices = triangulator.get3dVerticesList(vertices2D, 2); Indices = triangulator.getTriangulesList(); UVs = new List <Vector2>(); Vector3[] VerticesArray = Vertices.ToArray(); Vector2[] UvArray = UvCalculator.CalculateUVs(VerticesArray, 1); UVs = UvArray.ToList(); submesh.Indices = Indices; Submeshes.Add(submesh); meshData.addGamaMeshData(Vertices, UVs, Submeshes); featureMesh.Mesh = meshData; meshList.Add(featureMesh); meshList.Add(featureMesh); } } if (regionMap != null) { // DestroyImmediate(regionMap); Debug.Log("regionMap is Null"); } // Merge all feature meshes List <FeatureMesh> features = new List <FeatureMesh>(); /* * foreach (var task in tasks) * { * if (task.Generation == generation) * { * features.AddRange(task.Data); * } * } * * tasks.Clear(); * nTasksForArea = 0; */ features.AddRange(meshList); regionMap = new GameObject(RegionName); var sceneGraph = new SceneGraph(regionMap, GroupOptions, GameObjectOptions, features); sceneGraph.Generate(); }
List <Mesh> GenerateMeshInput(Dictionary <Vector3Int, Chunk> world, Vector3Int currChunkCoord, Vector3 offset) //generates mesh from input data { List <Mesh> chunkMesh = new List <Mesh>(); VoxelData data = world[currChunkCoord].data; float[] TopGrid = new float[_XCount * _YCount]; float[] BottomGrid = new float[_XCount * _YCount]; int z = 0; while (z < _ZCount) { vertices.Clear(); tris.Clear(); FillGrid(TopGrid, z, world, currChunkCoord); z++; int vertexCount = 0; while (z < _ZCount && vertexCount < MAX_VERTS - 12000)//max verts minus the most verts that could be returned (12 verts per cube, 1000 cubes in a grid pair) { FillGrid(BottomGrid, z, world, currChunkCoord); int newVerts = PolygonizeGrids(TopGrid, BottomGrid, z, Vector3.zero); Debug.Log("New verts from polygonization: " + newVerts); vertexCount += newVerts; var temp = TopGrid; TopGrid = BottomGrid; BottomGrid = temp; z++; } if (z < _ZCount) { Debug.LogWarning("Exceeded Max Vert Count, splitting mesh"); } Vector3Int topZ = currChunkCoord + new Vector3Int(0, 0, 1); //if we go out the top of a chunk, add a bridge between data sets if (z >= _ZCount && world.ContainsKey(topZ)) { FillGrid(BottomGrid, 0, world, topZ); vertexCount += PolygonizeGrids(TopGrid, BottomGrid, 0, new Vector3Int(0, 0, data.dataWidth)); var temp = TopGrid; TopGrid = BottomGrid; BottomGrid = temp; FillGrid(BottomGrid, 1, world, topZ); vertexCount += PolygonizeGrids(TopGrid, BottomGrid, 1, new Vector3Int(0, 0, data.dataWidth)); } Debug.Log("Verts: " + vertices.Count); Debug.Log("Tris: " + tris.Count); //mesh generation from calculated vertices generatedMesh = new Mesh(); Vector3[] genVerts = new Vector3[vertices.Count]; Vector3[] meshNormals = new Vector3[vertices.Count]; Color[] meshColors = new Color[vertices.Count]; //colors for debug only //mesh vertices for (int i = 0; i < genVerts.Length; i++) { genVerts[i] = vertices[i] + offset; Vector3 vertValue = (vertices[i]); //meshColors[i] = new Color(vertValue.y/100f, vertValue.y/100f, vertValue.y/100f); } generatedMesh.vertices = genVerts; //triangle vertices int[] triIndices = new int[tris.Count * 3]; for (int i = 0; i < (triIndices.Length / 3) - 2; i++) //forwards triangles { triIndices[(i * 3)] = (int)tris[i].z; triIndices[(i * 3) + 1] = (int)tris[i].y; triIndices[(i * 3) + 2] = (int)tris[i].x; Vector3 cross = Vector3.Cross(genVerts[triIndices[(i * 3) + 1]] - genVerts[triIndices[(i * 3)]], genVerts[triIndices[(i * 3) + 2]] - genVerts[triIndices[(i * 3)]]); meshNormals[triIndices[(i * 3)]] += cross; meshNormals[triIndices[(i * 3) + 1]] += cross; meshNormals[triIndices[(i * 3) + 2]] += cross; meshColors[triIndices[(i * 3)]] = new Color((float)i / ((triIndices.Length / 3) - 2), (float)i / ((triIndices.Length / 3) - 2), (float)i / ((triIndices.Length / 3) - 2)); meshColors[triIndices[(i * 3) + 1]] = new Color((float)i / ((triIndices.Length / 3) - 2), (float)i / ((triIndices.Length / 3) - 2), (float)i / ((triIndices.Length / 3) - 2)); meshColors[triIndices[(i * 3) + 1]] = new Color((float)i / ((triIndices.Length / 3) - 2), (float)i / ((triIndices.Length / 3) - 2), (float)i / ((triIndices.Length / 3) - 2)); } generatedMesh.triangles = triIndices; //smooth mesh normals or flat mesh normals //toggle-able because it increases load time a lot Vector3[] smoothMeshNormals = null; if (smoothShade) { smoothMeshNormals = new Vector3[meshNormals.Length]; /*for(int i = 0; i<smoothMeshNormals.Length; i++) * { * smoothMeshNormals[i] = ComputeVertexNormal(genVerts[i] - offset, meshNormals); * if(rand.NextDouble() < .0001) * { * Quaternion cRot = new Quaternion(0, 0, 0, 0); * Vector3 arbitraryForwards = new Vector3(1, 2, ((-1 * smoothMeshNormals[i].x) + (-2 * smoothMeshNormals[i].y))/smoothMeshNormals[i].z); * //cRot.SetLookRotation(arbitraryForwards, smoothMeshNormals[i]); * Instantiate(crystal, genVerts[i], cRot); * } * }*/ for (int i = 0; i < tris.Count; i++) { smoothMeshNormals[(int)tris[i].z] = ComputeVertexNormal(genVerts[(int)tris[i].z] - offset, i, (int)tris[i].z, meshNormals); smoothMeshNormals[(int)tris[i].y] = ComputeVertexNormal(genVerts[(int)tris[i].y] - offset, i, (int)tris[i].y, meshNormals); smoothMeshNormals[(int)tris[i].x] = ComputeVertexNormal(genVerts[(int)tris[i].x] - offset, i, (int)tris[i].x, meshNormals); } } else { for (int i = 0; i < meshNormals.Length; i++) { meshNormals[i] = Vector3.Normalize(meshNormals[i]); if (rand.NextDouble() < .0001) { Quaternion cRot = new Quaternion(0, 0, 0, 0); Vector3 arbitraryForwards = new Vector3(1, 2, ((-1 * meshNormals[i].x) + (-2 * meshNormals[i].y)) / meshNormals[i].z); //cRot.SetLookRotation(arbitraryForwards, meshNormals[i]); Instantiate(crystal, genVerts[i], Quaternion.identity); } } } if (smoothShade) { generatedMesh.normals = smoothMeshNormals; } else { generatedMesh.normals = meshNormals; } generatedMesh.uv = UvCalculator.CalculateUVs(genVerts, triIndices, 1.0f); generatedMesh.colors = meshColors; //meshes stored in a list because one chunk tends to go over the maximum vertex count chunkMesh.Add(generatedMesh); //if mesh splits mid gen, go over and do a bridging section between meshes if (z < _ZCount) { z--; } } //clear lists vertices.Clear(); tris.Clear(); return(chunkMesh); }
// Update is called once per frame void Update() { //ROTATION //rotate entire mesh (including free vertices) when L index trigger is pushed if (OVRInput.Get(OVRInput.RawButton.LIndexTrigger)) { //Archived //Quaternion offset = Quaternion.Inverse(controllerOrientation) * OVRInput.GetLocalControllerRotation(OVRInput.Controller.LTouch); // controllerOrientation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.LTouch); //meshSpace.transform.rotation = meshSpace.transform.rotation * offset; meshSpace.transform.SetParent(this.transform, true); } //OPEN MENU if thumbstick is moved if (OVRInput.Get(OVRInput.RawAxis2D.LThumbstick) != Vector2.zero) { if (!pieMenu.isOpen()) { pieMenu.Open(); } pieMenu.CustomInputVector = OVRInput.Get(OVRInput.RawAxis2D.LThumbstick); } else //SELECT and CLOSE MENU when thumbstick is released { if (pieMenu.isOpen()) { //select menu item by pressing thumbstick pieMenu.confirmSelect(); pieMenu.Close(); } } //Export mesh when X is pushed if (OVRInput.GetDown(OVRInput.RawButton.X)) { string savePath = "C:/Users/Davide/Desktop/export.obj"; MeshFilter mf = targetMeshObj.GetComponent <MeshFilter>(); //add UVs Vector2[] uvs = UvCalculator.CalculateUVs(mf.mesh.vertices, 1.0f); mf.mesh.uv = uvs; ObjExporter.MeshToFile(mf, savePath); } //Import mesh when Y is pushed //This should only be called at the beginning of the game (no custom mesh created) for now if (OVRInput.GetDown(OVRInput.RawButton.Y)) { string loadPath = "C:/Users/Davide/Desktop/export.obj"; Mesh m = new Mesh(); ObjImporter importer = new ObjImporter(); m = importer.ImportFile(loadPath); //DEBUG //print("\nLENGTH 1"); //print((m.vertices.Length).ToString()); //print((m.triangles.Length).ToString()); //a hack to correct the doubling of triangles caused by import script int[] correctTriangles = new int[m.vertices.Length]; for (int i = 0; i < m.vertices.Length; i++) { correctTriangles[i] = i; } m.triangles = correctTriangles; Material mat = new Material(Shader.Find("Custom/VertexColored")); if (targetMeshObj.GetComponent <MeshRenderer>() == null) { targetMeshObj.AddComponent <MeshRenderer>(); } targetMeshObj.GetComponent <MeshFilter>().mesh = m; targetMeshObj.GetComponent <MeshRenderer>().material = mat; //scale //targetMeshObj.transform.localScale = new Vector3(0.005f, 0.005f, 0.005f); //targetMeshObj.transform.Translate(0, 0, 2f); //DEBUG //print("\nLENGTH 2"); //print((m.vertices.Length).ToString()); //print((m.triangles.Length).ToString()); //create vertices and related behind-the-scenes data for editing rightHandAnchor.GetComponent <MeshHandler>().PrepareImportedMesh(); } }
public GameObject DrawRoofType1() { // Zaczynamy rysowaæ od Lewy Górny Naro¿nik GameObject roof = new GameObject(); roof.name = Name; var oldRoof = Instance3D; var vertices2D = Points.Select(v => new Vector2(v.x, v.y)).ToArray(); List <Vector3> points = new List <Vector3>(); Vector2 cV12 = (vertices2D[1] + vertices2D[2]) / 2; Vector2 cV30 = (vertices2D[3] + vertices2D[0]) / 2; points.Add(new Vector3(vertices2D[0].x, LevelBottom, vertices2D[0].y)); // 0 points.Add(new Vector3(vertices2D[0].x, LevelBottom + Thickness, vertices2D[0].y)); // 1 points.Add(new Vector3(cV30.x, LevelBottom + Height - Thickness, cV30.y)); // 2 points.Add(new Vector3(cV30.x, LevelBottom + Height, cV30.y)); // 3 points.Add(new Vector3(vertices2D[3].x, LevelBottom, vertices2D[3].y)); // 4 points.Add(new Vector3(vertices2D[3].x, LevelBottom + Thickness, vertices2D[3].y)); // 5 points.Add(new Vector3(vertices2D[1].x, LevelBottom, vertices2D[1].y)); // 6 points.Add(new Vector3(vertices2D[1].x, LevelBottom + Thickness, vertices2D[1].y)); // 7 points.Add(new Vector3(cV12.x, LevelBottom + Height - Thickness, cV12.y)); // 8 points.Add(new Vector3(cV12.x, LevelBottom + Height, cV12.y)); // 9 points.Add(new Vector3(vertices2D[2].x, LevelBottom, vertices2D[2].y)); // 10 points.Add(new Vector3(vertices2D[2].x, LevelBottom + Thickness, vertices2D[2].y)); // 11 List <int> triangles = new List <int>(); // Front triangles.AddRange(new int[] { 0, 1, 3 }); triangles.AddRange(new int[] { 0, 3, 2 }); triangles.AddRange(new int[] { 2, 3, 4 }); triangles.AddRange(new int[] { 4, 3, 5 }); // Back triangles.AddRange(new int[] { 10, 11, 9 }); triangles.AddRange(new int[] { 10, 9, 8 }); triangles.AddRange(new int[] { 9, 7, 6 }); triangles.AddRange(new int[] { 9, 6, 8 }); // Rest triangles.AddRange(new int[] { 4, 5, 11 }); triangles.AddRange(new int[] { 4, 11, 10 }); triangles.AddRange(new int[] { 5, 9, 11 }); triangles.AddRange(new int[] { 5, 3, 9 }); triangles.AddRange(new int[] { 6, 7, 1 }); triangles.AddRange(new int[] { 6, 1, 0 }); triangles.AddRange(new int[] { 7, 3, 1 }); triangles.AddRange(new int[] { 7, 9, 3 }); var mesh = new Mesh { vertices = points.ToArray(), triangles = triangles.ToArray() }; mesh.uv = UvCalculator.CalculateUVs(mesh.vertices, 1); mesh.RecalculateNormals(); mesh.RecalculateBounds(); var meshRenderer = roof.AddComponent <MeshRenderer>(); var filter = roof.AddComponent <MeshFilter>(); meshRenderer.material = Material; filter.mesh = mesh; roof.AddComponent <MeshCollider>(); roof.tag = "Roof"; roof.transform.parent = GameObject.Find("3DArea").transform; roof.layer = LayerMask.NameToLayer("3DArea"); roof.transform.Translate(new Vector3(2000, 0, 0), Space.World); Instance3D = roof; if (oldRoof != null) { DestroyImmediate(oldRoof); } return(roof); }
public GameObject DrawRoofType1_v2() { // Zaczynamy rysowaæ od Lewy Górny Naro¿nik GameObject roof = new GameObject(); roof.name = Name; var oldRoof = Instance3D; var vertices2D = Points.Select(v => new Vector2(v.x, v.y)).ToArray(); List <Vector3> points = new List <Vector3>(); //var y1 = 0f; // wysokoœæ //var l1 = 0.1f; // okap z boku //var l2 = 0.1f; // okap na szczycie //Height = 2.8f; var y1 = 0.5f; // wysokoœæ var l1 = 0.7f; // okap z boku var l2 = 0.7f; // okap na szczycie // P0' var v03n = (vertices2D[0] - vertices2D[3]).normalized; var v01n = (vertices2D[0] - vertices2D[1]).normalized; var pl01 = v03n * l1; var pl02 = v01n * l2; var P0prim = vertices2D[0] + pl01 + pl02; // P6' var v12n = (vertices2D[1] - vertices2D[2]).normalized; var v10n = (vertices2D[1] - vertices2D[0]).normalized; var pl61 = v12n * l1; var pl62 = v10n * l2; var P6prim = vertices2D[1] + pl61 + pl62; // P10' var v23n = (vertices2D[2] - vertices2D[3]).normalized; var v21n = (vertices2D[2] - vertices2D[1]).normalized; var pl101 = v21n * l1; var pl102 = v23n * l2; var P10prim = vertices2D[2] + pl101 + pl102; // P4' var v30n = (vertices2D[3] - vertices2D[0]).normalized; var v32n = (vertices2D[3] - vertices2D[2]).normalized; var pl41 = v30n * l1; var pl42 = v32n * l2; var P4prim = vertices2D[3] + pl41 + pl42; Vector2 cV12 = (P6prim + P10prim) / 2; Vector2 cV30 = (P4prim + P0prim) / 2; points.Add(new Vector3(P0prim.x, LevelBottom - y1, P0prim.y)); // 0 points.Add(new Vector3(P0prim.x, LevelBottom + Thickness - y1, P0prim.y)); // 1 points.Add(new Vector3(cV30.x, LevelBottom + Height - Thickness, cV30.y)); // 2 points.Add(new Vector3(cV30.x, LevelBottom + Height, cV30.y)); // 3 points.Add(new Vector3(P4prim.x, LevelBottom - y1, P4prim.y)); // 4 points.Add(new Vector3(P4prim.x, LevelBottom + Thickness - y1, P4prim.y)); // 5 points.Add(new Vector3(P6prim.x, LevelBottom - y1, P6prim.y)); // 6 points.Add(new Vector3(P6prim.x, LevelBottom + Thickness - y1, P6prim.y)); // 7 points.Add(new Vector3(cV12.x, LevelBottom + Height - Thickness, cV12.y)); // 8 points.Add(new Vector3(cV12.x, LevelBottom + Height, cV12.y)); // 9 points.Add(new Vector3(P10prim.x, LevelBottom - y1, P10prim.y)); // 10 points.Add(new Vector3(P10prim.x, LevelBottom + Thickness - y1, P10prim.y)); // 11 List <int> triangles = new List <int>(); // Front triangles.AddRange(new int[] { 0, 1, 3 }); triangles.AddRange(new int[] { 0, 3, 2 }); triangles.AddRange(new int[] { 2, 3, 4 }); triangles.AddRange(new int[] { 4, 3, 5 }); // Back triangles.AddRange(new int[] { 10, 11, 9 }); triangles.AddRange(new int[] { 10, 9, 8 }); triangles.AddRange(new int[] { 9, 7, 6 }); triangles.AddRange(new int[] { 9, 6, 8 }); // Rest triangles.AddRange(new int[] { 4, 5, 11 }); triangles.AddRange(new int[] { 4, 11, 10 }); triangles.AddRange(new int[] { 5, 9, 11 }); triangles.AddRange(new int[] { 5, 3, 9 }); triangles.AddRange(new int[] { 6, 7, 1 }); triangles.AddRange(new int[] { 6, 1, 0 }); triangles.AddRange(new int[] { 7, 3, 1 }); triangles.AddRange(new int[] { 7, 9, 3 }); // Inside triangles.AddRange(new int[] { 0, 2, 6 }); triangles.AddRange(new int[] { 2, 8, 6 }); triangles.AddRange(new int[] { 8, 2, 4 }); triangles.AddRange(new int[] { 8, 4, 10 }); var mesh = new Mesh { vertices = points.ToArray(), triangles = triangles.ToArray() }; mesh.uv = UvCalculator.CalculateUVs(mesh.vertices, 1); mesh.RecalculateNormals(); mesh.RecalculateBounds(); var meshRenderer = roof.AddComponent <MeshRenderer>(); var filter = roof.AddComponent <MeshFilter>(); meshRenderer.material = Material; filter.mesh = mesh; roof.AddComponent <MeshCollider>(); roof.tag = "Roof"; roof.transform.parent = GameObject.Find("3DArea").transform; roof.layer = LayerMask.NameToLayer("3DArea"); roof.transform.Translate(new Vector3(2000, 0, 0), Space.World); Instance3D = roof; if (oldRoof != null) { DestroyImmediate(oldRoof); } return(roof); }
public void UpdateChunkMesh(Chunk chunk, bool?blocking = null) { int numVoxelsPerAxis = numPointsPerAxis - 1; int numThreadsPerAxis = Mathf.CeilToInt(numVoxelsPerAxis / (float)threadGroupSize); float pointSpacing = boundsSize / (numPointsPerAxis - 1); bool blockGpu = blocking ?? blockingGpu; Vector3Int coord = chunk.coord; Vector3 centre = CentreFromCoord(coord); Vector3 worldBounds = new Vector3(numChunks.x, numChunks.y, numChunks.z) * boundsSize; densityGenerator.Generate(pointsBuffer, numPointsPerAxis, boundsSize, worldBounds, centre, offset, pointSpacing); void SetupShader() { triangleBuffer.SetCounterValue(0); shader.SetBuffer(0, "points", pointsBuffer); shader.SetBuffer(0, "triangles", triangleBuffer); shader.SetInt("numPointsPerAxis", numPointsPerAxis); shader.SetFloat("isoLevel", isoLevel); shader.Dispatch(0, numThreadsPerAxis, numThreadsPerAxis, numThreadsPerAxis); } ComputeBuffer.CopyCount(triangleBuffer, triCountBuffer, 0); void HandleReadBack() { int[] triCountArray = { 0 }; triCountBuffer.GetData(triCountArray); int numTris = triCountArray[0]; if (numTris > maxTris || tris == null || vertices == null || meshTriangles == null) { maxTris = numTris; tris = new Triangle[numTris]; vertices = new Vector3[numTris * 3]; meshTriangles = new int[numTris * 3]; } // Get triangle data from shader triangleBuffer.GetData(tris, 0, 0, numTris); Mesh mesh = chunk.mesh; mesh.Clear(); for (int i = 0; i < numTris; i++) { for (int j = 0; j < 3; j++) { meshTriangles[i * 3 + j] = i * 3 + j; vertices[i * 3 + j] = tris[i][j]; } } mesh.vertices = vertices; mesh.triangles = meshTriangles; var scale = chunk.GetComponent <Transform>().localScale; mesh.SetUVs(0, UvCalculator.CalculateUVs(vertices, scale.magnitude)); NormalSolver.RecalculateNormals(mesh, normalDegrees); chunk.UpdateColliders(); } if (!blockGpu) { chunk.asyncOp0 = null; chunk.asyncOp1 = null; chunk.asyncOp2 = null; var async0 = AsyncGPUReadback.Request(pointsBuffer, delegate(AsyncGPUReadbackRequest a0) { SetupShader(); var async1 = AsyncGPUReadback.Request(triangleBuffer, delegate(AsyncGPUReadbackRequest a1) { // Get number of triangles in the triangle buffer ComputeBuffer.CopyCount(triangleBuffer, triCountBuffer, 0); var async2 = AsyncGPUReadback.Request(triCountBuffer, delegate(AsyncGPUReadbackRequest a2) { HandleReadBack(); }); chunk.asyncOp2 = async2; }); chunk.asyncOp1 = async1; }); chunk.asyncOp0 = async0; } else { SetupShader(); ComputeBuffer.CopyCount(triangleBuffer, triCountBuffer, 0); HandleReadBack(); } }