Esempio n. 1
0
 void  SearchWaterLine()
 {
     GameObject[] waterBlockGameObjects;
     waterBlockGameObjects = GameObject.FindGameObjectsWithTag("waterBlock");
     foreach (GameObject wBlockGM in waterBlockGameObjects)
     {
         WaterLine wb = wBlockGM.GetComponent <WaterLine> ();
         waterLineList.Add(wb);
     }
 }
Esempio n. 2
0
    void OnDrawGizmos()
    {
        waterLine = gameObject.GetComponent <WaterLine>();
        float waterHeight = waterLine.Height;
        float waterWidth  = waterLine.Width;

        Vector2 drawSize     = new Vector2(waterWidth, waterHeight);
        Vector2 drawPosition = new Vector2(transform.position.x, transform.position.y - waterHeight / 2);

        Gizmos.color = Color.black;
        Gizmos.DrawWireCube(drawPosition, drawSize);
    }
Esempio n. 3
0
    void LateUpdate()
    {
        timeElapsed += Time.deltaTime;
        frameCount++;

        // Check for frame skip
        if (emit && timeElapsed >= emitTimeInterval && fo.waterLines.Count > 0f && foamTexCount > 0)
        {
            frameCount  = 0;
            timeElapsed = 0;

            int emitted     = 0;
            int triedToEmit = 0;

            if (emissionPriority == EmissionPriority.HighestVelocity || emissionPriority == EmissionPriority.RandomVelocityWeighted)
            {
                waterLineSorted = fo.waterLines.OrderByDescending(p => p.tri.velocityMagTimesDot).ToList();
            }
            else
            {
                waterLineSorted = fo.waterLines.OrderByDescending(p => p.tri.dynamicForce.magnitude * p.tri.dotNormalVelocityNormal).ToList();
            }

            WaterLine fastestLine = waterLineSorted[0];

            // Check if object should emit
            if (fastestLine.tri.velocityMagnitude > sleepTresholdVelocity && fo.IsTouchingWater && fo.enabled)
            {
                int pointCount = fo.waterLines.Count;

                // Check if emit per frame valid
                if (emitPerCycle >= pointCount)
                {
                    emitPerCycle = Mathf.Max(0, pointCount - 1);
                }

                // If random weighted, take the first quater of the descending list
                if (emissionPriority == EmissionPriority.RandomVelocityWeighted || emissionPriority == EmissionPriority.RandomForceWeighted)
                {
                    int cnt = pointCount / 4;
                    if (cnt < emitPerCycle)
                    {
                        cnt = emitPerCycle;
                    }
                    if (cnt < waterLineSorted.Count)
                    {
                        waterLineSorted = waterLineSorted.Take(cnt).ToList();
                    }
                }

                // Emit allowed number of particles
                while (emitted < emitPerCycle)
                {
                    // Controls
                    if (emitted >= pointCount)
                    {
                        break;
                    }
                    if (triedToEmit >= pointCount)
                    {
                        break;
                    }

                    // Get emit position
                    line = null;

                    // Get one point from water line
                    if (emissionPriority == EmissionPriority.Random)
                    {
                        line = fo.waterLines[Random.Range(0, fo.waterLines.Count)];
                    }
                    else if (emissionPriority == EmissionPriority.RandomVelocityWeighted)
                    {
                        line = waterLineSorted[Random.Range(0, waterLineSorted.Count)];
                    }
                    else if (emitted < waterLineSorted.Count)
                    {
                        line = waterLineSorted[emitted];
                    }

                    // Create new particle
                    if (line != null && line.tri.velocityMagnitude > sleepTresholdVelocity)
                    {
                        WaterParticle wp = new WaterParticle();

                        // Determine position
                        Vector3 p = line.p0;
                        p -= line.tri.velocity * Time.deltaTime;

                        // Setup particle
                        GameObject particleGo = GameObject.CreatePrimitive(PrimitiveType.Quad);
                        Destroy(particleGo.GetComponent <MeshCollider>());
                        float scale = maxParticleSize;
                        particleGo.transform.localScale = new Vector3(scale, scale, scale);
                        particleGo.transform.rotation   = Quaternion.Euler(90f, 0f, Random.Range(0f, 360f));
                        particleGo.transform.parent     = particleContainer.transform;
                        MeshRenderer meshRenderer = particleGo.GetComponent <MeshRenderer>();
                        p.y = surfaceElevation + fo.WaterHeightFunction(p.x, p.z);
                        particleGo.transform.position = p;

                        // Mesh
                        Material matInstance = new Material(Shader.Find("WaterFX/WaterParticle"));
                        matInstance.SetTexture("_MainTex", foamTextures[Random.Range(0, foamTextures.Count)]);
                        meshRenderer.material             = matInstance;
                        meshRenderer.material.renderQueue = renderQueue;
                        meshRenderer.enabled = true;

                        // Particle dynamics
                        vel                = line.tri.normal * line.tri.velocityMagnitude;
                        localVel           = fo.transform.InverseTransformVector(vel);
                        localVel.z         = 0f;
                        vel                = fo.transform.TransformVector(localVel);
                        vel.y              = 0f;
                        wp.initialVelocity = vel * initialVelocityModifier;
                        wp.initialAlpha    = Mathf.Clamp(line.tri.dynamicForce.magnitude * 0.033f * initialAlphaModifier, 0f, maxInitialAlpha);

                        // Init
                        wp.initialColor   = Color.white;
                        wp.initialColor.a = wp.initialAlpha;
                        wp.initialScale   = particleGo.transform.localScale;
                        wp.go             = particleGo;
                        wp.t          = particleGo.transform;
                        wp.mr         = meshRenderer;
                        wp.timeToLive = particleLifetime;
                        wp.mat        = meshRenderer.material;
                        wp.initTime   = Time.time;

                        // Start from the beginning of the array, replacing the oldest particle
                        if (pIndex >= maxParticles - 1)
                        {
                            pIndex = 0;
                        }

                        if (waterParticles[pIndex] != null)
                        {
                            Destroy(waterParticles[pIndex].go);
                        }
                        waterParticles[pIndex] = wp;
                        pIndex++;
                        emitted++;
                    }

                    triedToEmit++;
                }
            }
        }

        // Update existing particles
        for (int i = waterParticles.Length - 1; i >= 0; i--)
        {
            wp = waterParticles[i];
            if (wp != null)
            {
                float timeLived = Time.time - wp.initTime;
                if (timeLived >= wp.timeToLive)
                {
                    Destroy(wp.go);
                    waterParticles[i] = null;
                }
                else
                {
                    float livedPercent = timeLived / wp.timeToLive;

                    wp.t.position  += Time.deltaTime * velocityOverTime.Evaluate(livedPercent) * wp.initialVelocity;
                    wp.t.localScale = wp.initialScale * sizeOverTime.Evaluate(livedPercent);

                    float currentAlpha = alphaOverTime.Evaluate(livedPercent);
                    if (currentAlpha < 0.05f && livedPercent > 0.4f)
                    {
                        Destroy(wp.go);
                        waterParticles[i] = null;
                    }
                    else
                    {
                        wp.mr.material.SetColor(
                            "_TintColor",
                            new Color(wp.initialColor.r, wp.initialColor.g, wp.initialColor.b, wp.initialColor.a * currentAlpha)
                            );
                    }
                }
            }
        }
    }