示例#1
0
    private void Initialize()
    {
        //Setup global state
        material.color = color;
        int thePartsCount = (int)(width / partSize);

        parts = new WaterLinePart[thePartsCount];

        //Generate parts
        for (int i = 0; i < thePartsCount; i++)
        {
            //Object & geometry
            GameObject theGameObject = new GameObject("WavePart_" + i);
            theGameObject.tag = "waterPart";
            theGameObject.transform.parent        = this.transform;
            theGameObject.transform.localPosition = new Vector3(i * partSize, 0, -1);

            //Physics
            PolygonCollider2D polygonCollider2D = theGameObject.AddComponent <PolygonCollider2D>();
            polygonCollider2D.isTrigger      = true;
            polygonCollider2D.usedByEffector = true;

            BuoyancyEffector2D theEffector = theGameObject.AddComponent <BuoyancyEffector2D>();

            //Graphics
            Mesh mesh = new Mesh();
            mesh.MarkDynamic();

            theGameObject.AddComponent <MeshFilter>().mesh       = mesh;
            theGameObject.AddComponent <MeshRenderer>().material = material;

            //Setup part
            parts[i] = new WaterLinePart();

            parts[i]._heightOld = 0.0f;
            parts[i]._heightNew = 0.0f;
            parts[i]._speed     = 0.0f;
            parts[i]._effector  = theEffector;

            parts[i].mesh       = mesh;
            parts[i].gameObject = theGameObject;
        }

        for (int i = 0; i < thePartsCount; i++)
        {
            UpdateMeshVertices(i);
            InitializeTrianglesAndNormalsForMesh(i);
        }
    }
示例#2
0
    void Update()
    {
        //Debug
        //if(Input.GetButtonDown ("Fire1")) Splash(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, 3.0f, 100.0f);

        //Update model state
        for (int i = 1, size = parts.Length; i < size - 1; i++)
        {
            WaterLinePart theBeforePart = parts[i - 1];
            WaterLinePart thePart       = parts[i];
            WaterLinePart theAfterPart  = parts[i + 1];

            //Get force
            float theBeforeDelta = theBeforePart._heightOld - thePart._heightOld;
            float theAfterDelta  = theAfterPart._heightOld - thePart._heightOld;
            float theForce       = theBeforeDelta + theAfterDelta;

            //Update speed
            thePart._speed *= (1.0f - disasiasion);
            thePart._speed += theForce / partMass;
            //thePart._speed = (thePart._speed<0.1f && thePart._speed>-0.1f ? 0.0f : thePart._speed);

            //Update next position
            thePart._heightNew = thePart._heightOld + thePart._speed;

            //Update flow
            thePart._flow = (theBeforePart._flowOld + thePart._flowOld + theAfterPart._flowOld) / 3.0f;
            //thePart._flow = (thePart._flow<0.1f && thePart._flow>-0.1f ? 0.0f : thePart._flow);
        }

        //Update view
        for (int i = 0, size = parts.Length; i < size; i++)
        {
            //parts[i]._heightNew = parts[i]._heightOld;

            // Update the dot position
            Vector3 newPosition = new Vector3(
                parts[i].gameObject.transform.localPosition.x,
                parts[i]._heightNew,
                parts[i].gameObject.transform.localPosition.z);
            parts[i].gameObject.transform.localPosition = newPosition;
        }

        //Prepare next model state
        for (int i = 0, size = parts.Length; i < size; i++)
        {
            parts[i]._heightOld = parts[i]._heightNew;
            parts[i]._effector.flowMagnitude = defaultFlow + parts[i]._flow;
            parts[i]._flowOld = parts[i]._flow;
        }

        // Update meshes
        for (int i = 0, size = parts.Length; i < size; i++)
        {
            UpdateMeshVertices(i);
        }

        //Add idle waves

        if (Random.value < idleWavesIntensity)
        {
            Splash(Random.value * width, idleWavesPower * Random.value, 0.0f);
        }
    }