Beispiel #1
0
    // Use this for initialization
    void Start()
    {
        /*groundTexture = new Texture2D(2048, 2048);
         * groundUVs = groundTexture.PackTextures(textures, 0, 2048);*/

        chunkSize = TerrainBrain.chunkSize;
        offset    = transform.position;
        //m_tmpdata = TerrainBrain.Instance().getTerrainData(transform.position); //new int[10, 10, 10];


        gameObject.AddComponent <MeshFilter>();
        gameObject.AddComponent <MeshRenderer>();
        gameObject.AddComponent <MeshCollider>();

        m_meshFilter = this.GetComponent <MeshFilter>();

        Mesh newMesh = new Mesh();

        m_meshFilter.mesh = newMesh;

        groundUVs = TerrainBrain.Instance().getUVs();
        //Debug.Log("UV size = " + groundUVs.Length.ToString());

        //Debug.Log("Chunk is in location: " + transform.position);
        regenerateMesh();

        //renderer.material.shader = Shader.Find("Diffuse");
        GetComponent <Renderer>().material.shader      = shader;
        GetComponent <Renderer>().material.mainTexture = TerrainBrain.Instance().getGroundTexture(); //groundTexture;

        GetComponent <Renderer>().material.SetFloat("_LightPower", 0.5f);

        this.enabled = true;
    }
Beispiel #2
0
 int getTerrainValue(Vector3 pos)
 {
     /*if (pos.z < 0 || pos.z > 9 || pos.y < 0 || pos.y > 9 || pos.x < 0 || pos.x > 9)
      *  return 0;
      * else
      * {*/
     pos += transform.position;
     return(TerrainBrain.Instance().getTerrainDensity((int)pos.x, (int)pos.y, (int)pos.z));
     //}
     //return m_tmpdata[(int)pos.x, (int)pos.y, (int)pos.z];
 }
Beispiel #3
0
    // Use this for initialization
    void Start()
    {
        m_instance = this;

        Screen.SetResolution(Screen.currentResolution.width, Screen.currentResolution.height, true);

        blockLoadDistance = Mathf.CeilToInt((viewDistance - chunkSize) / chunkSize);
        //Debug.Log("Load distance = " + blockLoadDistance.ToString());
        m_meshCache = new GameObject[blockLoadDistance * 2 + 1, blockLoadDistance *2 + 1, blockLoadDistance *2 + 1];

        currentPos   = playerStart;
        currentPos.x = Mathf.Floor(currentPos.x / chunkSize);
        currentPos.y = Mathf.Floor(currentPos.y / chunkSize);
        currentPos.z = Mathf.Floor(currentPos.z / chunkSize);
        lastPos      = currentPos;

        groundTexture            = new Texture2D(2048, 2048);
        groundTexture.anisoLevel = 9;
        groundUVs = groundTexture.PackTextures(textures, 0, 2048);
        //Debug.Log("groundUV size = " + groundUVs.Length.ToString());
        //Debug.Log("Ground UVs = " + groundUVs.ToString());

        //GameObject.Instantiate(prefab, new Vector3(0, 0, 0), Quaternion.identity);
        //GameObject.Instantiate(prefab, new Vector3(0, -10, 0), Quaternion.identity);

        Vector3 scanStart = new Vector3(0, 100, 0);
        int     d         = 0;

        while (d == 0)
        {
            scanStart.y--;
            d = getTerrainDensity(Mathf.RoundToInt(scanStart.x), Mathf.RoundToInt(scanStart.y), Mathf.RoundToInt(scanStart.z));
        }



        scanStart.y += 3;
        scanStart.x += 0.5f;
        scanStart.z += 0.5f;
        playerStart  = scanStart;
        Debug.Log("Start pos = " + scanStart.ToString());

        Camera.main.transform.position = playerStart;
        GUIText status = GameObject.Find("StatusDisplay").GetComponent <GUIText>();

        status.text = "Loading...";

        //Camera.main.GetComponent<FPSWalker>().gravity=0;



        //stepLoad();
    }
Beispiel #4
0
    public int[,,] getChunk(int x, int y, int z)
    {
        IntCoords coords = new IntCoords(x, y, z);

        if (coords.Equals(m_lastCoords))
        {
            return(m_lastData);
        }

        m_lastCoords = coords;

        if (m_data.ContainsKey(coords))
        {
            m_lastData = m_data[coords];
            return(m_lastData);
        }

        int chunkSize = TerrainBrain.chunkSize;

        m_lastData = new int[chunkSize, chunkSize, chunkSize];
        TerrainBrain tb = TerrainBrain.Instance();

        for (int t = 0; t < chunkSize; t++)
        {
            for (int u = 0; u < chunkSize; u++)
            {
                for (int v = 0; v < chunkSize; v++)
                {
                    Vector3 loc = new Vector3((float)(x + t), (float)(y + u), (float)(z + v)) / TerrainBrain.noiseMultiplier;
                    //Vector3 cpos = new Vector3((float)t, (float)u, (float)v);

                    m_lastData[t, u, v] = tb.getDensity(loc);
                }
            }
        }

        //Debug.Log("Stored cache:" + coords.ToString());
        m_data[coords] = m_lastData;
        return(m_lastData);
    }
Beispiel #5
0
    public static TerrainBrain Instance()
    {
        if (m_instance == null)
        {
            Debug.LogWarning("Lost terrain brain, reaquiring.");
            GameObject ob = GameObject.Find("TerrainManager");
            if (ob == null)
            {
                Debug.LogError("Could not reaquire terrain brain.");
                return(null);
            }

            m_instance = ob.GetComponent <TerrainBrain>();
            if (m_instance == null)
            {
                Debug.LogError("Could not reaquire terrain brain component.");
                return(null);
            }
        }

        return(m_instance);
    }
Beispiel #6
0
    void doLeftClick()
    {
        //float startTime = Time.realtimeSinceStartup;

        // Grab mouse position on terrain chunk and remove the appropriate cube
        Ray        ray = Camera.main.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2)); //Input.mousePosition);
        RaycastHit hit = new RaycastHit();

        if (GetComponent <Collider>().Raycast(ray, out hit, 10.0f))
        {
            Vector3 hp = hit.point + 0.0001f * ray.direction;

            int xHit = Mathf.CeilToInt(hp.x) - 1;
            int yHit = Mathf.CeilToInt(hp.y) - 1;
            int zHit = Mathf.CeilToInt(hp.z) - 1;

            TerrainBrain.Instance().setTerrainDensity(xHit, yHit, zHit);

            int chunkX = (int)(offset.x / chunkSize);
            int chunkY = (int)(offset.y / chunkSize);
            int chunkZ = (int)(offset.z / chunkSize);

            GameObject n = null;
            if (hp.x - offset.x - 1 < 1)
            {
                n = findNeighbor(NeighborDir.X_MINUS, chunkX, chunkY, chunkZ); // findTerrainChunk(chunkX - 1, chunkY, chunkZ); // getNeighbor(NeighborDir.X_MINUS);
                if (n != null)
                {
                    n.SendMessage("regenerateMesh");            //n.GetComponent<TerrainPrefabBrain>().regenerateMesh();
                }
            }
            else if (hp.x - offset.x - 1 > chunkSize - 2)
            {
                n = findNeighbor(NeighborDir.X_PLUS, chunkX, chunkY, chunkZ);  //findTerrainChunk(chunkX + 1, chunkY, chunkZ);
                if (n != null)
                {
                    n.SendMessage("regenerateMesh");
                }
            }
            if (hp.y - offset.y - 1 < 1)
            {
                n = findNeighbor(NeighborDir.Y_MINUS, chunkX, chunkY, chunkZ);  //findTerrainChunk(chunkX, chunkY - 1, chunkZ);
                if (n != null)
                {
                    n.SendMessage("regenerateMesh");
                }
            }
            else if (hp.y - offset.y - 1 > chunkSize - 2)
            {
                n = findNeighbor(NeighborDir.Y_PLUS, chunkX, chunkY, chunkZ); //findTerrainChunk(chunkX, chunkY + 1, chunkZ);
                if (n != null)
                {
                    n.SendMessage("regenerateMesh");
                }
            }
            if (hp.z - offset.z - 1 < 1)
            {
                n = findNeighbor(NeighborDir.Z_MINUS, chunkX, chunkY, chunkZ);  //findTerrainChunk(chunkX, chunkY, chunkZ - 1);
                if (n != null)
                {
                    n.SendMessage("regenerateMesh");
                }
            }
            else if (hp.z - offset.z - 1 > chunkSize - 2)
            {
                n = findNeighbor(NeighborDir.Z_PLUS, chunkX, chunkY, chunkZ);  //findTerrainChunk(chunkX, chunkY, chunkZ + 1);
                if (n != null)
                {
                    n.SendMessage("regenerateMesh");
                }
            }

            regenerateMesh();
        }

        //Debug.Log("lc time = " + (Time.realtimeSinceStartup - startTime).ToString());
    }