Esempio n. 1
0
        public int CostlyUpdate(WaveParticlesQuadtree quadtree, float deltaTime)
        {
            float depth;

            if (frequency < 0.025f)                     // in case of big waves, sample center and front of the particle to get a better result
            {
                float posx = position.x + direction.x / frequency;
                float posy = position.y + direction.y / frequency;
                depth = Mathf.Max(StaticWaterInteraction.GetTotalDepthAt(position.x, position.y), StaticWaterInteraction.GetTotalDepthAt(posx, posy));
            }
            else
            {
                depth = StaticWaterInteraction.GetTotalDepthAt(position.x, position.y);
            }

            if (depth <= 0.001f)
            {
                Destroy();
                return(0);
            }

            UpdateWaveParameters(deltaTime, depth);

            int numSubdivisions = 0;

            if (quadtree != null && !disallowSubdivision)
            {
                if (leftNeighbour != null)
                {
                    Subdivide(quadtree, leftNeighbour, this, ref numSubdivisions);
                }

                if (rightNeighbour != null)
                {
                    Subdivide(quadtree, this, rightNeighbour, ref numSubdivisions);
                }
            }

            return(numSubdivisions);
        }
Esempio n. 2
0
        private void CreateShoalingSpawnPoints()
        {
            var bounds = new Bounds(transform.position, new Vector3(boundsSize.x, 0.0f, boundsSize.y));

            float minX = bounds.min.x;
            float minZ = bounds.min.z;
            float maxX = bounds.max.x;
            float maxZ = bounds.max.z;
            float spawnPointsDensitySqrt = Mathf.Sqrt(spawnPointsDensity);
            float stepX = Mathf.Max(35.0f, bounds.size.x / 256.0f) / spawnPointsDensitySqrt;
            float stepZ = Mathf.Max(35.0f, bounds.size.z / 256.0f) / spawnPointsDensitySqrt;

            bool[,] tiles = new bool[32, 32];
            var spawnPoints = new List <SpawnPoint>();

            var waves = windWaves.SpectrumResolver.SelectShorelineWaves(50, 0, 360);

            if (waves.Length == 0)
            {
                this.spawnPoints = new SpawnPoint[0];
                return;
            }

            float minSpawnDepth = spawnDepth * 0.85f;
            float maxSpawnDepth = spawnDepth * 1.18f;

            for (float z = minZ; z < maxZ; z += stepZ)
            {
                for (float x = minX; x < maxX; x += stepX)
                {
                    int tileX = Mathf.FloorToInt(32.0f * (x - minX) / (maxX - minX));
                    int tileZ = Mathf.FloorToInt(32.0f * (z - minZ) / (maxZ - minZ));

                    if (!tiles[tileX, tileZ])
                    {
                        float depth = StaticWaterInteraction.GetTotalDepthAt(x, z);

                        if (depth > minSpawnDepth && depth < maxSpawnDepth && Random.value < 0.06f)
                        {
                            tiles[tileX, tileZ] = true;

                            Vector2 dir;
                            dir.x = StaticWaterInteraction.GetTotalDepthAt(x - 3.0f, z) - StaticWaterInteraction.GetTotalDepthAt(x + 3.0f, z);
                            dir.y = StaticWaterInteraction.GetTotalDepthAt(x, z - 3.0f) - StaticWaterInteraction.GetTotalDepthAt(x, z + 3.0f);
                            dir.Normalize();

                            var   bestWave    = waves[0];
                            float bestWaveDot = Vector2.Dot(dir, waves[0].direction);

                            for (int i = 1; i < waves.Length; ++i)
                            {
                                float dot = Vector2.Dot(dir, waves[i].direction);

                                if (dot > bestWaveDot)
                                {
                                    bestWaveDot = dot;
                                    bestWave    = waves[i];
                                }
                            }

                            spawnPoints.Add(new SpawnPoint(new Vector2(x, z), dir, bestWave.frequency, Mathf.Abs(bestWave.amplitude), bestWave.speed));
                        }
                    }
                }
            }

            this.spawnPoints = spawnPoints.ToArray();
        }