Ejemplo n.º 1
0
    public static MeshData GenerateSphereChunkMesh(NoiseMapData noiseMap, SphereMeshSettings sphereSettings, int lod)
    {
        int vertexIncrement = sphereSettings.GetVertexIncrement(lod);

        MarchingCubes mc = new MarchingCubes(sphereSettings.chunkSize / vertexIncrement + 1, sphereSettings.chunkSize / vertexIncrement + 1, sphereSettings.chunkSize / vertexIncrement + 1);

        mc.init_all();
        mc = computeSphereChunk(mc, noiseMap.noiseMap, sphereSettings.noiseHeightScale, sphereSettings.radius, noiseMap.offset, vertexIncrement, sphereSettings.chunkSize);
        mc.run();
        mc.clean_temps();

        //TerrainChunk.stopwatch = new System.Diagnostics.Stopwatch();
        //TerrainChunk.stopwatch.Start();

        MeshData meshData = new MeshData();

        meshData.vertices = VertexToVector3Vertices(mc.vertices, mc.nverts(), noiseMap.offset, vertexIncrement);
        meshData.normals  = VertexToVector3Normals(mc.vertices, mc.nverts());
        //meshData.uv = NormalsToUVs(mesh.normals);
        meshData.uv        = VerticesToUVs(meshData.vertices);
        meshData.triangles = TrianglesToInt(mc.triangles, mc.ntrigs());

        meshData.lod = lod;

        //TerrainChunk.stopwatch.Stop();
        //Debug.Log("Created MeshData in " + TerrainChunk.stopwatch.ElapsedMilliseconds / 1000f + " seconds");

        return(meshData);
    }
Ejemplo n.º 2
0
    public Branch(int nodesPerChild, Vector3Int trunkOrigin, SphereMeshSettings meshSettings)
    {
        this.nodesPerChild = nodesPerChild;
        this.meshSettings  = meshSettings;
        origin             = trunkOrigin;
        //Debug.Log("Created trunk with origin: " + origin + "nodesPerChild: " + nodesPerChild);

        int nodeDiameter = nodesPerChild * 2;

        bounds = new Bounds(trunkOrigin * meshSettings.chunkSize + (Vector3.one * meshSettings.chunkSize * nodesPerChild), (Vector3.one * meshSettings.chunkSize * nodeDiameter));

        GenerateChildren();
    }
Ejemplo n.º 3
0
    public Branch(Vector3Int nodeIndex, int nodesPerChild, Vector3Int parentOrigin, SphereMeshSettings meshSettings)
    {
        this.nodesPerChild = nodesPerChild;
        this.meshSettings  = meshSettings;
        origin             = parentOrigin + (nodeIndex * 2 * nodesPerChild);
        //origin = parentOrigin + (nodeIndex * 2 * nodesPerChild * meshSettings.chunkSize);
        //Debug.Log("Created branch " + nodeIndex + "with origin: " + origin + "nodesPerChild: " + nodesPerChild);

        int nodeDiameter = nodesPerChild * 2;

        bounds = new Bounds(origin * meshSettings.chunkSize + (Vector3.one * meshSettings.chunkSize * nodesPerChild), (Vector3.one * meshSettings.chunkSize * nodeDiameter));

        GenerateChildren();
    }
Ejemplo n.º 4
0
    public Octree(SphereMeshSettings meshSettings)
    {
        rootNodeRadius = Mathf.CeilToInt((meshSettings.radius + meshSettings.noiseHeightScale * 1.5f) / meshSettings.chunkSize);

        if (rootNodeRadius % 2 == 1)
        {
            rootNodeRadius++;
        }

        //origin = new Vector3Int(-rootNodeRadius * meshSettings.chunkSize, -rootNodeRadius * meshSettings.chunkSize, -rootNodeRadius * meshSettings.chunkSize);
        origin = new Vector3Int(-rootNodeRadius, -rootNodeRadius, -rootNodeRadius);

        trunk = new Branch(rootNodeRadius, origin, meshSettings);
    }
Ejemplo n.º 5
0
    public static MeshData GenerateSphereMesh(NoiseMapData mapData, SphereMeshSettings sphereSettings, int vertexIncrement)
    {
        int           mcExpand     = Mathf.Min(Mathf.CeilToInt(sphereSettings.noiseHeightScale * sphereSettings.radius * 3) * 2 + 2, 59);
        int           halfMCExpand = mcExpand / 2;
        Vector3       addOffset    = new Vector3(-halfMCExpand, -halfMCExpand, -halfMCExpand);
        MarchingCubes mc           = new MarchingCubes(sphereSettings.chunkSize / vertexIncrement + mcExpand, sphereSettings.chunkSize / vertexIncrement + mcExpand, sphereSettings.chunkSize / vertexIncrement + mcExpand);

        mc.init_all();
        mc = computeSphere(mc, mapData.noiseMap, sphereSettings.noiseHeightScale, sphereSettings.radius, mapData.offset, vertexIncrement, sphereSettings.chunkSize, halfMCExpand);
        mc.run();
        mc.clean_temps();

        MeshData meshData = new MeshData();

        meshData.vertices = VertexToVector3Vertices(mc.vertices, mc.nverts(), mapData.offset + addOffset, vertexIncrement);
        meshData.normals  = VertexToVector3Normals(mc.vertices, mc.nverts());
        //meshData.uv = NormalsToUVs(mesh.normals);
        meshData.uv        = VerticesToUVs(meshData.vertices);
        meshData.triangles = TrianglesToInt(mc.triangles, mc.ntrigs());

        return(meshData);
    }
Ejemplo n.º 6
0
    public TerrainChunk(Vector3 offset, Transform parent, NoiseSettings noiseSettings, SphereMeshSettings meshSettings, Material material, Transform viewer)
    {
        Vector3 size = Vector3.one * meshSettings.chunkSize;

        bounds = new Bounds(offset + size / 2, size);

        gameObject = new GameObject("Chunk: " + bounds.min);
        gameObject.transform.SetParent(parent);

        this.viewer        = viewer;
        this.noiseSettings = noiseSettings;
        this.meshSettings  = meshSettings;
        this.lodSettings   = meshSettings.lodSettings;

        gameObject.AddComponent <MeshRenderer>().sharedMaterial = material;
        meshFilter   = gameObject.AddComponent <MeshFilter>();
        meshCollider = gameObject.AddComponent <MeshCollider>();

        InitializeLOD();

        hasRequestedNoiseMap = true;
        UnityTaskManager.ScheduleTask(RequestNoiseMap, OnNoiseMapReceived);
    }