Пример #1
0
	// Use this for initialization
	void Start()
	{
		CreateLineMaterial();

		mesh = new Mesh();
		Vertex3[] vertices = new Vertex3[NumberOfVertices];
		Vector3[] meshVerts = new Vector3[NumberOfVertices];
		int[] indices = new int[NumberOfVertices];

		Random.seed = 1;
		for (var i = 0; i < NumberOfVertices; i++)
		{
			vertices[i] = new Vertex3(size * Random.Range(-1.0f, 1.0f), size * Random.Range(-1.0f, 1.0f), size * Random.Range(-1.0f, 1.0f));
			meshVerts[i] = vertices[i].ToVector3();
			indices[i] = i;
		}

		mesh.vertices = meshVerts;
		mesh.SetIndices(indices, MeshTopology.Points, 0);
		mesh.bounds = new Bounds(Vector3.zero, new Vector3(size, size, size));

		float now = Time.realtimeSinceStartup;
		voronoiMesh = VoronoiMesh.Create<Vertex3, Cell3>(vertices);
		float interval = Time.realtimeSinceStartup - now;

		Debug.Log("time = " + interval * 1000.0f);

	}
Пример #2
0
        private void CreateTriangles()
        {
            // face 1
            var a = new Vertex3(new Vector3(Min.X, Min.Y, Min.Z));
            var b = new Vertex3(new Vector3(Max.X, Min.Y, Min.Z));
            var c = new Vertex3(new Vector3(Min.X, Max.Y, Min.Z));
            var d = new Vertex3(new Vector3(Max.X, Max.Y, Min.Z));
            TriangleList.Add(new Triangle(this, a, b, d));
            TriangleList.Add(new Triangle(this, a, d, c));

            // lateral 1
            a = new Vertex3(new Vector3(Min.X, Min.Y, Min.Z));
            b = new Vertex3(new Vector3(Min.X, Min.Y, Max.Z));
            c = new Vertex3(new Vector3(Min.X, Max.Y, Min.Z));
            d = new Vertex3(new Vector3(Min.X, Max.Y, Max.Z));
            TriangleList.Add(new Triangle(this, a, b, d));
            TriangleList.Add(new Triangle(this, a, d, c));

            // lateral 2
            a = new Vertex3(new Vector3(Max.X, Min.Y, Min.Z));
            b = new Vertex3(new Vector3(Max.X, Min.Y, Max.Z));
            c = new Vertex3(new Vector3(Max.X, Max.Y, Min.Z));
            d = new Vertex3(new Vector3(Max.X, Max.Y, Max.Z));
            TriangleList.Add(new Triangle(this, a, b, d));
            TriangleList.Add(new Triangle(this, a, d, c));

            // face 2
            a = new Vertex3(new Vector3(Min.X, Min.Y, Max.Z));
            b = new Vertex3(new Vector3(Max.X, Min.Y, Max.Z));
            c = new Vertex3(new Vector3(Min.X, Max.Y, Max.Z));
            d = new Vertex3(new Vector3(Max.X, Max.Y, Max.Z));
            TriangleList.Add(new Triangle(this, a, b, d));
            TriangleList.Add(new Triangle(this, a, d, c));
        }
Пример #3
0
 public MovingJoint(JointType jointType, Vertex3 position, double bendAngle)
     : base(jointType, position)
 {
     this.bendAngle = bendAngle;
 }
Пример #4
0
 public Joint(JointType jointType, Vertex3 position)
 {
     this.jointType = jointType;
     this.position = position;
 }
Пример #5
0
    public void Create()
    {
        foreach (Transform child in transform)
        {
            Destroy(child.gameObject);
        }

        //InputField[] fields = FindObjectsOfType<InputField>();
        //InputField numseed = fields[0];
        //InputField numplates = fields[1];
        //InputField numtiles = fields[2];
        //numberOfVertices = Convert.ToInt32(numtiles.text);
        //plates = Convert.ToInt32(numplates.text);
        //seed = Convert.ToInt32(numseed.text);

        // INITIALIZATION
        Vertex3[] vertices = new Vertex3[numberOfVertices];
        Vector3[] meshVerts = new Vector3[numberOfVertices];
        allVerts = new List<Vector3>();
        vertsForVoronoiHull = new List<Vector3>();

        // VORONOI VERTICES NEED ONE EXTRA ONE IN CENTER
        Vertex3[] voronoiVertices = new Vertex3[numberOfVertices + 1];

        // RANDOM SEED
        Random.seed = seed;

        // GENERATE UNIFORM POINTS
        allVerts = GeneratePointsUniformly(); 
        allVerts.Sort((v1, v2) => v1.y.CompareTo(v2.y));

        // SET INDICES FOR VORONOI
        int i = 0;
        while (i < numberOfVertices)
        {
            vertices[i] = new Vertex3(allVerts[i].x, allVerts[i].y, allVerts[i].z);
            voronoiVertices[i] = vertices[i];
            meshVerts[i] = vertices[i].ToVector3(); ;
            i++;
        }
        // SET LAST EXTRA VERTEX
        voronoiVertices[numberOfVertices] = new Vertex3(0, 0, 0);

        
        // VORONOI
        voronoiMesh = VoronoiMesh.Create<Vertex3, Cell3>(voronoiVertices);
        

        
        // VORONOI HULL GENERATION
        int index = 0;
        foreach (var edge in voronoiMesh.Edges)
        {
            Vector3 source = new Vector3(edge.Source.Circumcenter.x, edge.Source.Circumcenter.y, edge.Source.Circumcenter.z);
            Vector3 target = new Vector3(edge.Target.Circumcenter.x, edge.Target.Circumcenter.y, edge.Target.Circumcenter.z);
            source *= ((float)size / 2.5f);
            target *= ((float)size / 2.5f);
            vertsForVoronoiHull.Add(source);
            vertsForVoronoiHull.Add(target);
            index++;
        }

        // REMOVE DUPLICATE POINTS
        vertsForVoronoiHull = vertsForVoronoiHull.Distinct().ToList();

        // CONVERT FROM VECTOR3 LIST TO VERTEX3 LIST FOR FINAL HULL
        Vertex3[] verticesDelaunay = new Vertex3[vertsForVoronoiHull.Count];

        int g = 0;
        while (g < vertsForVoronoiHull.Count)
        {
            verticesDelaunay[g] = new Vertex3(vertsForVoronoiHull[g].x, vertsForVoronoiHull[g].y, vertsForVoronoiHull[g].z);
            g++;
        }

        // GENERATE VORONOI HULL
        ConvexHull<Vertex3, Face3> convexHull = ConvexHull.Create<Vertex3, Face3>(verticesDelaunay);
        convexHullVertices = new List<Vertex3>(convexHull.Points);
        convexHullFaces = new List<Face3>(convexHull.Faces);
        convexHullIndices = new List<int>();

        foreach (Face3 f in convexHullFaces)
        {
            convexHullIndices.Add(convexHullVertices.IndexOf(f.Vertices[0]));
            convexHullIndices.Add(convexHullVertices.IndexOf(f.Vertices[1]));
            convexHullIndices.Add(convexHullVertices.IndexOf(f.Vertices[2]));
        }

        Dictionary<Vector3, List<Vector3>> normals = new Dictionary<Vector3, List<Vector3>>();

        // CREATE TRIANGLES FOR MESH
        for (int j = 0; j < convexHullIndices.Count; j += 3)
        {
            int v0 = convexHullIndices[j + 0];
            int v1 = convexHullIndices[j + 1];
            int v2 = convexHullIndices[j + 2];

            Vector3 a = new Vector3((float)convexHullVertices[v0].x, (float)convexHullVertices[v0].y, (float)convexHullVertices[v0].z);
            Vector3 b = new Vector3((float)convexHullVertices[v1].x, (float)convexHullVertices[v1].y, (float)convexHullVertices[v1].z);
            Vector3 c = new Vector3((float)convexHullVertices[v2].x, (float)convexHullVertices[v2].y, (float)convexHullVertices[v2].z);

            Vector3 normal = Vector3.Cross(a - b, a - c);

            // DECLARE KEY AND ROUND IT TO AVOID FLOATING POINT ISSUES
            Vector3 key = normal.normalized;
            float roundX = Mathf.Round(key.x * 100) / 100;
            float roundY = Mathf.Round(key.y * 100) / 100;
            float roundZ = Mathf.Round(key.z * 100) / 100;
            Vector3 roundedKey = new Vector3(roundX, roundY, roundZ);

            // POPULATE DICTIONARY
            if (!normals.ContainsKey(roundedKey))
            {
                normals.Add(roundedKey, new List<Vector3>());
            }
            normals[roundedKey].Add(a);
            normals[roundedKey].Add(b);
            normals[roundedKey].Add(c);
        }
 
        // CREATE VORONOI TILES
        List<VoronoiTile> tiles = new List<VoronoiTile>();
        foreach (var pair in normals)
        {
            List<Vector3> tileVerts = new List<Vector3>();
            for (int p = 0; p < pair.Value.Count; ++p)
            {
                tileVerts.Add(pair.Value[p]);
            }
            GameObject tile = new GameObject("Tile", typeof(VoronoiTile), typeof(MeshFilter), typeof(MeshRenderer), typeof(MeshCollider));
            var thisTile = tile.GetComponent<VoronoiTile>() as VoronoiTile;
            
            thisTile.Initialize(tileVerts, false); // OPTIMIZE HERE
            
            tile.GetComponent<MeshFilter>().mesh = thisTile.tileMesh;
            tile.GetComponent<MeshCollider>().sharedMesh = thisTile.tileMesh;

            thisTile.Normal = pair.Key;
            tiles.Add(thisTile);
            ++numberOfTiles;
        }

        foreach (var tile in tiles)
        {
            tile.FindNeighbors(tiles); 
        }

        List<VoronoiTile> waterTiles = new List<VoronoiTile>();
        Material oceanMat = (Material)Resources.Load("Ocean", typeof(Material));
        foreach (var pair in normals)
        {
            List<Vector3> tileVerts = new List<Vector3>();
            for (int p = 0; p < pair.Value.Count; ++p)
            {
                tileVerts.Add(pair.Value[p]);
            }
            GameObject tile = new GameObject("WaterTile", typeof(VoronoiTile), typeof(MeshFilter), typeof(MeshRenderer));
            var thisTile = tile.GetComponent<VoronoiTile>() as VoronoiTile;
            thisTile.Initialize(tileVerts, true);
            tile.GetComponent<MeshFilter>().mesh = thisTile.tileMesh;
            tile.GetComponent<MeshRenderer>().material = oceanMat;
            waterTiles.Add(thisTile);
        }

        ocean = new GameObject("Ocean");
        ocean.transform.parent = transform;
        foreach (var tile in waterTiles)
        {
            tile.transform.parent = ocean.transform;
        }

        // FLOOD FILLS
        // GENERATE PLATES
        List<VoronoiTile> plateStartNodes = GeneratePlateStartNodes(plates, ref tiles);

        // GENERATE PLATE MATERIALS
        List<Material> plateMaterials = GenerateMaterials(1); // changed here

        // GENERATE START LISTS
        List<List<VoronoiTile>> colors = new List<List<VoronoiTile>>();
        for (int b = 0; b < plates; ++b)
        {
            colors.Add(new List<VoronoiTile>() { plateStartNodes[b] });
        }

        
        // FILL
        FloodFillSimultaneous(ref colors, plateMaterials, ref tiles);
        
        
        // GROUP PLATES
        plateList = new List<TectonicPlate>();
        for (int q = 0; q < plates; ++q)
        {
            GameObject plateTest = new GameObject("Plate" + q, typeof(TectonicPlate));
            List<VoronoiTile> testPlateTiles = new List<VoronoiTile>();
            foreach (var voronoiTile in tiles)
            {
                if (voronoiTile.plate == q) testPlateTiles.Add(voronoiTile);
            }
            var thisTecPlate = plateTest.GetComponent<TectonicPlate>();
            thisTecPlate.Initialize(ref testPlateTiles);
            int land = Random.Range(0, 10);
            if (land < landAmount) thisTecPlate.isLand = true;
            plateTest.transform.parent = transform;
            plateList.Add(thisTecPlate);
        }

        LoadMaterials();
        
        // CREATE WATER AND LAND AREAS FOR HEIGHT
        FindWaterAndLandPoints();

        // DETERMINE BIOMES
        AssignPlateProperties();
        AssignTileProperties();
        DetermineBiomes(true);
        GenerateHeight();
        DetermineHeightBiomes();
    }
Пример #6
0
	// Use this for initialization
	void Start()
	{
		CreateLineMaterial();

		mesh = new Mesh();
		Vertex3[] vertices = new Vertex3[NumberOfVertices];
		meshVerts = new Vector3[NumberOfVertices];
		int[] meshIndices = new int[NumberOfVertices];

		Random.seed = 0;
		int i = 0;
		while (i < NumberOfVertices)
		{
			Vector3 v = new Vector3(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f));

			if (mode == MODE.CUBE_VOLUME)
			{
				vertices[i] = new Vertex3(size * v.x, size * v.y, size * v.z);
				meshVerts[i] = vertices[i].ToVector3();
				meshIndices[i] = i;

				i++;
			}
			else if (mode == MODE.SPHERE_VOLUME)
			{
				if (v.magnitude < 1.0f)
				{
					vertices[i] = new Vertex3(size * v.x, size * v.y, size * v.z);
					meshVerts[i] = vertices[i].ToVector3();
					meshIndices[i] = i;

					i++;
				}

			}
			else if (mode == MODE.SPHERE_SURFACE)
			{
				v.Normalize();

				vertices[i] = new Vertex3(size * v.x, size * v.y, size * v.z);
				meshVerts[i] = vertices[i].ToVector3();
				meshIndices[i] = i;

				i++;
			}
		}

		mesh.vertices = meshVerts;
		mesh.SetIndices(meshIndices, MeshTopology.Points, 0);
		//mesh.bounds = new Bounds(Vector3.zero, new Vector3((float)size,(float)size,(float)size));

		float now = Time.realtimeSinceStartup;
		ConvexHull<Vertex3, Face3> convexHull = ConvexHull.Create<Vertex3, Face3>(vertices);
		float interval = Time.realtimeSinceStartup - now;

		convexHullVertices = new List<Vertex3>(convexHull.Points);
		convexHullFaces = new List<Face3>(convexHull.Faces);
		convexHullIndices = new List<int>();

		Debug.Log("#test# " + vertices.Length);
		Debug.Log("#test# " + convexHullVertices.Count);
		Vector3[] newVertices = new Vector3[vertices.Length];
		triangles = new int[newVertices.Length * 3];
		var triIndex = 0;
		foreach (Face3 face in convexHullFaces)
		{
			for (int vIndex = 0; vIndex < face.Vertices.Length; vIndex++)
			{
				var indexOf = convexHullVertices.IndexOf(face.Vertices[vIndex]);
				convexHullIndices.Add(indexOf);
				newVertices[indexOf] = face.Vertices[vIndex].ToVector3();
				triangles[triIndex + vIndex] = indexOf;
			}
			triIndex += 3;
			//convexHullIndices.Add(convexHullVertices.IndexOf(face.Vertices[0]));
			//convexHullIndices.Add(convexHullVertices.IndexOf(face.Vertices[1]));
			//convexHullIndices.Add(convexHullVertices.IndexOf(face.Vertices[2]));

			Debug.DrawLine(face.Vertices[0].ToVector3(), face.Vertices[1].ToVector3(), new Color(0.1f, 0.1f, 0.1f), 100f);
			Debug.DrawLine(face.Vertices[1].ToVector3(), face.Vertices[2].ToVector3(), new Color(0.1f, 0.1f, 0.1f), 100f);
			Debug.DrawLine(face.Vertices[2].ToVector3(), face.Vertices[0].ToVector3(), new Color(0.1f, 0.1f, 0.1f), 100f);
		}
		mesh.vertices = newVertices;
		mesh.triangles = triangles;

		GameObject.Find("Test").GetComponent<MeshFilter>().mesh = mesh;
		GameObject.Find("Test").GetComponent<MeshCollider>().sharedMesh = mesh;

		Debug.Log("Out of the " + NumberOfVertices + " vertices, there are " + convexHullVertices.Count + " verts on the convex hull.");
		Debug.Log("time = " + interval * 1000.0f + " ms");
	}
		/// <summary>
		/// Set uniform state variable (variant type variable)
		/// </summary>
		/// <param name="ctx">
		/// A <see cref="GraphicsContext"/> used for operations.
		/// </param>
		/// <param name="uniformName">
		/// A <see cref="String"/> that specify the variable name in the shader source.
		/// </param>
		/// <param name="v">
		/// A <see cref="Vertex3"/> holding the uniform variabile data.
		/// </param>
		public void SetVariantUniform(GraphicsContext ctx, string uniformName, Vertex3 v)
		{
			if (v == null)
				throw new ArgumentNullException("v");

			SetUniform(ctx, uniformName, v.X, v.Y, v.Z);
		}