Пример #1
0
    void Start()
    {
        // Generamos malla
        CreateWaterMesh();

        // Generamos cada uno de los springs
        for (int i = 0; i < SurfaceVertices; ++i)
        {
            Transform TransformHolder;
            TransformHolder           = Instantiate(SpringPrefab, vertices[i], Quaternion.identity) as Transform;
            SpringList[i]             = TransformHolder.GetComponent <Comp_Spring>();
            SpringList[i].MaxIncrease = MaxIncrease;
            SpringList[i].MaxDecrease = MaxDecrease;
            SpringList[i].TargetY     = YSurface;
            SpringList[i].Damping     = Damping;
            SpringList[i].Tension     = Tension;
            SpringList[i].ID          = i;
            SpringList[i].Water       = this;

            if (i > 0)
            {
                SpringList[i].comp_connected_spring = SpringList[i - 1];
            }

            var boxCollider = TransformHolder.GetComponent <BoxCollider>() as BoxCollider;
            //boxCollider.size = new Vector3(VertexSpacing,0,2);
            SpringList[i].transform.parent = this.transform;
        }
        //Wave Simulator
        WaveSimulatorPrefab      = Instantiate(WaveSimulatorPrefab, new Vector3(0, 0, 0), Quaternion.identity) as Transform;
        WaveSimulator            = WaveSimulatorPrefab.GetComponent <Comp_WaveSimulator>();
        WaveSimulator.WaveHeight = WaveSimHeight;
        WaveSimulator.WaveSpeed  = WaveSimSpeed;

        //StartCoroutine(ChangeWaterHeight(5,2));
    }
Пример #2
0
    void Start()
    {
        //Generating Mesh
        mf      = GetComponent <MeshFilter>();
        mesh    = new Mesh();
        mf.mesh = mesh;

        //Generates the the surface using var i, then generates the bottom using var oo
        int oo = SurfaceVertices - 1;

        for (int i = 0; i <= VertexCount - 1; i++)
        {
            if (i < SurfaceVertices)
            {
                vertices[i] = new Vector3(StartX + (VertexSpacing * i), YSurface, 0);
            }
            else if (i >= SurfaceVertices)
            {
                vertices[i] = new Vector3(vertices[oo].x, YBottom, 0);
                oo         += -1;
            }
        }
        mesh.vertices = vertices;

        //Connecting the dots. :)
        //Setting the Triangles. I got this part working by lots of trial and error. I am sure there could be a better solution but anyways for now this doesnt affect the gameplay peformance and it works.
        int tt;

        tt = SurfaceVertices;
        for (int t = SurfaceVertices - 1; t > 0 / 2; t += -1)
        {
            TriangulateRectangle(t, t - 1, tt, tt + 1);
            tt++;
        }

        mesh.triangles = triangle;


        //Setting the Normals
        Vector3[] normals = new Vector3[VertexCount];

        for (int n = 0; n <= VertexCount - 1; n++)
        {
            normals[n] = -Vector3.forward;
        }
        mesh.normals = normals;


        //Setting the UVS
        int nVertices = mesh.vertices.Length;
        var uvs       = new Vector2[nVertices];

        for (int r = 0; r < nVertices; r++)
        {
            uvs[r] = mesh.vertices[r];
        }
        mesh.uv = uvs;
        //Mesh Generation Done

        //Generating Springs and saving each of Spring's Scripts into the array for refrence later. Also setting the properties of it.
        for (int sprngs = 0; sprngs < SurfaceVertices; sprngs++)
        {
            Transform TransformHolder;
            TransformHolder                = Instantiate(SpringPrefab, vertices[sprngs], Quaternion.identity) as Transform;
            SpringList[sprngs]             = TransformHolder.GetComponent <SpringScript>();
            SpringList[sprngs].MaxIncrease = MaxIncrease;
            SpringList[sprngs].MaxDecrease = MaxDecrease;
            SpringList[sprngs].TargetY     = YSurface;
            SpringList[sprngs].Damping     = Damping;
            SpringList[sprngs].Tension     = Tension;
            SpringList[sprngs].ID          = sprngs;
            SpringList[sprngs].Water       = this;
            var boxCollider = TransformHolder.GetComponent <BoxCollider>() as BoxCollider;
            boxCollider.size = new Vector3(VertexSpacing, 0, 2);
            SpringList[sprngs].transform.parent = this.transform;
        }
        //Wave Simulator
        WaveSimulatorPrefab      = Instantiate(WaveSimulatorPrefab, new Vector3(0, 0, 0), Quaternion.identity) as Transform;
        WaveSimulator            = WaveSimulatorPrefab.GetComponent <WaveSimulator>();
        WaveSimulator.WaveHeight = WaveSimHeight;
        WaveSimulator.WaveSpeed  = WaveSimSpeed;

        //StartCoroutine(ChangeWaterHeight(5,2));
    }