Esempio n. 1
0
        public static float NormalizeRange(float _value, float _min, float _max, float _fade)
        {
            float _peak_min = _min + _fade;
            float _peak_max = _max - _fade;

            return(Mathf.Clamp01((_value >= _min && _value < _peak_min ? MathTools.Normalize(_value - _min, 0, _peak_min - _min) : (_value <= _max && _value > _peak_max ? MathTools.Normalize(_max - _value, 0, _peak_max - _min) : (_value >= _peak_min && _value <= _peak_max ? 1f : 0f)))));
        }
Esempio n. 2
0
        public static float CalculateAngularVelocity(Vector3 _velocity, Vector3 _velocity_max, Vector3 _velocity_default)
        {
            if (_velocity.z == 0)
            {
                return(_velocity_default.y);
            }

            float _multiplier = Mathf.Clamp(1.0f - MathTools.Normalize(Mathf.Abs(_velocity.z), 0, _velocity_max.z), 0.1f, 1f);

            return(_velocity_default.y * Mathf.Pow(_multiplier, 2));
        }
        public static void UpdateTrees(TerrainData _terrain_data)
        {
            GameObject[] _tree_objects = new GameObject[3];
            _tree_objects[0] = (GameObject)Resources.Load("Trees/tree1");
            _tree_objects[1] = (GameObject)Resources.Load("Trees/tree2");
            _tree_objects[2] = (GameObject)Resources.Load("Trees/tree3");

            TreePrototype[] _trees = new TreePrototype[3];
            _trees[0]        = new TreePrototype();
            _trees[0].prefab = _tree_objects[0];

            _trees[1]        = new TreePrototype();
            _trees[1].prefab = _tree_objects[1];

            _trees[2]        = new TreePrototype();
            _trees[2].prefab = _tree_objects[2];
            //_trees[0].bendFactor =

            _terrain_data.treePrototypes = _trees;

            List <TreeInstance> _tree_instances = new List <TreeInstance>();

            for (int i = 0; i < TerrainTreesMax; i++)
            {
                Vector3 _pos = PositionTools.GetRandomRectPosition(1, 1);
                _pos.y = MathTools.Normalize(_terrain_data.GetInterpolatedHeight(_pos.x, _pos.z), 0, _terrain_data.size.y);

                Vector3 _ground = _terrain_data.GetInterpolatedNormal(_pos.x, _pos.z);
                float   _angle  = Mathf.Abs(Vector3.Angle(_ground, Vector3.up));

                //Debug.Log( _pos + " - " + _ground + " - " + Vector3.Angle( _ground, Vector3.up ) );

                if (_angle > 0 && _angle < TerrainTreesMaxAngle)
                {
                    TreeInstance _tree = new TreeInstance();
                    _tree.prototypeIndex = UnityEngine.Random.Range(0, 3);
                    _tree.position       = _pos;
                    _tree.rotation       = UnityEngine.Random.Range(0, 360);
                    _tree.heightScale    = UnityEngine.Random.Range(0.75f, 1.5f);
                    _tree.widthScale     = _tree.heightScale + UnityEngine.Random.Range(-0.15f, 0.15f);
                    _tree.color          = new Color(1, 1, 1);
                    _tree.lightmapColor  = new Color(1, 1, 1);


                    _tree_instances.Add(_tree);
                }
            }

            _terrain_data.treeInstances = _tree_instances.ToArray();
        }
        public static void CreatePlateau(Terrain _terrain, Vector3 _position, float _radius)
        {
            if (_terrain == null)
            {
                return;
            }

            _position = _terrain.gameObject.transform.InverseTransformPoint(_position);

            TerrainData _terrain_data = _terrain.terrainData;

            float _height = MathTools.Normalize(_position.y, 0, _terrain_data.size.y);

            float _xm = _terrain_data.heightmapWidth / _terrain_data.size.x;
            float _ym = _terrain_data.heightmapHeight / _terrain_data.size.z;

            int _xr = Mathf.RoundToInt(_radius * _xm);
            int _yr = Mathf.RoundToInt(_radius * _ym);
            int _xd = _xr * 2;
            int _yd = _yr * 2;

            int _base_x = Mathf.RoundToInt(_position.x * _xm) - _xr;
            int _base_y = Mathf.RoundToInt(_position.z * _ym) - _yr;


            _base_x = Mathf.Clamp(_base_x, 0, _terrain_data.heightmapWidth - _xd);
            _base_y = Mathf.Clamp(_base_y, 0, _terrain_data.heightmapHeight - _yd);

            float _max_distance = Mathf.Sqrt(Mathf.Pow(_yr, 2) + Mathf.Pow(_xr, 2));

            float[,] _heights = _terrain_data.GetHeights(_base_x, _base_y, _xd, _yd);

            for (int a = 0; a < _xd; a++)
            {
                for (int b = 0; b < _yd; b++)
                {
                    float _dist      = Mathf.Sqrt(Mathf.Pow((_yr - b), 2) + Mathf.Pow((_xr - a), 2));
                    float _variation = (_dist < _max_distance * 0.5f ? 1f : Mathf.Clamp01(1 - MathTools.Normalize(_dist, 0, _max_distance)));

                    _heights[a, b] = Mathf.Lerp(_heights[a, b], _height, _variation);
                }
            }

            _terrain_data.SetHeights(_base_x, _base_y, _heights);
        }
        public static void UpdateTerrainBumps(TerrainData _terrain_data, int _res, float _variance_min, float _variance_max)
        {
            _variance_max = _variance_max / Mathf.Max(DesiredTerrainResolutionMultiplier, 1);
            _variance_min = _variance_min / Mathf.Max(DesiredTerrainResolutionMultiplier, 1);

            if (_variance_max == 0)
            {
                return;
            }

            float[,] _heights = _terrain_data.GetHeights(0, 0, _res, _res);

            for (int _i = 0; _i < _res * _res; _i++)
            {
                int _x = _i % _res;
                int _y = _i / _res;

                float _height = _heights[_x, _y];

                float   _xn               = MathTools.Normalize(_x, 0, _res);
                float   _yn               = MathTools.Normalize(_y, 0, _res);
                Vector3 _ground           = _terrain_data.GetInterpolatedNormal(_yn, _xn);
                float   _angle            = Mathf.Abs(Vector3.Angle(_ground, Vector3.up));
                float   _angle_multiplier = (_angle / 90);

                if (_angle_multiplier > 0)
                {
                    _angle_multiplier += _height;
                }


                float _variance = UnityEngine.Random.Range(_variance_min, _variance_max) * _angle_multiplier;

                float _point_height = Mathf.Lerp(_height, _height + _variance, 0.5f);

                _heights[_x, _y] = _point_height;
            }



            _terrain_data.SetHeights(0, 0, _heights);
        }
        public static void Text(string _text, Vector3 _world_position, Color _color, float _font_size = 14, FontStyle _font_style = FontStyle.Normal)
        {
                        #if UNITY_EDITOR
            UnityEditor.Handles.BeginGUI();

            UnityEditor.SceneView view = UnityEditor.SceneView.currentDrawingSceneView;
            Vector3 _screen_position   = view.camera.WorldToScreenPoint(_world_position);
            Vector2 _size = GUI.skin.label.CalcSize(new GUIContent(_text));

            GUIStyle _style = new GUIStyle();
            _style.normal.textColor = _color;
            float _distance   = (int)PositionTools.Distance(view.camera.transform.position, _world_position);
            float _normalized = 1 - MathTools.Normalize(_distance, 0, 250);
            if (_normalized > 0.1f && _normalized < 1)
            {
                _style.fontSize  = (int)(_font_size * _normalized);
                _style.fontStyle = _font_style;
                GUI.Label(new Rect(_screen_position.x - (_size.x / 2), -_screen_position.y + view.position.height + 4, _size.x, _size.y), _text, _style);
            }
            UnityEditor.Handles.EndGUI();
                        #endif
        }
        public static void UpdateDetailLayer(TerrainData _terrain_data)
        {
            //_terrain_data.SetDetailResolution( TerrainDetailResolution, TerrainPatchDetail );

            _terrain_data.wavingGrassStrength = 0.25f;
            _terrain_data.wavingGrassAmount   = 0.25f;

            DetailPrototype[] _details = new DetailPrototype[10];

            int _index = 0;

            _details[_index] = new DetailPrototype();
            _details[_index].prototypeTexture = (Texture2D)Resources.Load("Grass/Grass01");
            _details[_index].minWidth         = 0.25f;
            _details[_index].maxWidth         = 1f;
            _details[_index].minHeight        = 0.25f;
            _details[_index].maxHeight        = 1.5f;
            _details[_index].renderMode       = DetailRenderMode.Grass;

            _index           = 1;
            _details[_index] = new DetailPrototype();
            _details[_index].prototypeTexture = (Texture2D)Resources.Load("Grass/Grass02");
            _details[_index].minWidth         = 0.25f;
            _details[_index].maxWidth         = 1f;
            _details[_index].minHeight        = 0.25f;
            _details[_index].maxHeight        = 1.5f;
            _details[_index].renderMode       = DetailRenderMode.Grass;

            _index           = 2;
            _details[_index] = new DetailPrototype();
            _details[_index].prototypeTexture = (Texture2D)Resources.Load("Grass/Grass03");
            _details[_index].minWidth         = 0.25f;
            _details[_index].maxWidth         = 1f;
            _details[_index].minHeight        = 0.25f;
            _details[_index].maxHeight        = 1.5f;
            _details[_index].renderMode       = DetailRenderMode.Grass;

            _index           = 3;
            _details[_index] = new DetailPrototype();
            _details[_index].prototypeTexture = (Texture2D)Resources.Load("Grass/Flower01");
            _details[_index].minWidth         = 0.25f;
            _details[_index].maxWidth         = 1f;
            _details[_index].minHeight        = 0.25f;
            _details[_index].maxHeight        = 1f;
            _details[_index].healthyColor     = Color.white;
            _details[_index].renderMode       = DetailRenderMode.Grass;

            _index           = 4;
            _details[_index] = new DetailPrototype();
            _details[_index].prototypeTexture = (Texture2D)Resources.Load("Grass/Flower02");
            _details[_index].minWidth         = 0.25f;
            _details[_index].maxWidth         = 1f;
            _details[_index].minHeight        = 0.25f;
            _details[_index].maxHeight        = 1f;
            _details[_index].healthyColor     = Color.yellow;
            _details[_index].renderMode       = DetailRenderMode.Grass;

            _index           = 5;
            _details[_index] = new DetailPrototype();
            _details[_index].usePrototypeMesh = true;
            _details[_index].prototype        = (GameObject)Resources.Load("Bushes/Fern");
            _details[_index].minWidth         = 0.25f;
            _details[_index].maxWidth         = 1f;
            _details[_index].minHeight        = 0.25f;
            _details[_index].maxHeight        = 1f;
            _details[_index].healthyColor     = Color.white;
            _details[_index].renderMode       = DetailRenderMode.Grass;

            _index           = 6;
            _details[_index] = new DetailPrototype();
            _details[_index].usePrototypeMesh = true;
            _details[_index].prototype        = (GameObject)Resources.Load("Bushes/Bush1");
            _details[_index].minWidth         = 0.25f;
            _details[_index].maxWidth         = 1f;
            _details[_index].minHeight        = 0.25f;
            _details[_index].maxHeight        = 1f;
            _details[_index].healthyColor     = Color.white;
            _details[_index].renderMode       = DetailRenderMode.Grass;

            _index           = 7;
            _details[_index] = new DetailPrototype();
            _details[_index].usePrototypeMesh = true;
            _details[_index].prototype        = (GameObject)Resources.Load("Bushes/Bush2");
            _details[_index].minWidth         = 0.25f;
            _details[_index].maxWidth         = 1f;
            _details[_index].minHeight        = 0.25f;
            _details[_index].maxHeight        = 1f;
            _details[_index].healthyColor     = Color.white;
            _details[_index].renderMode       = DetailRenderMode.Grass;

            _index           = 8;
            _details[_index] = new DetailPrototype();
            _details[_index].usePrototypeMesh = true;
            _details[_index].prototype        = (GameObject)Resources.Load("Bushes/Bush3");
            _details[_index].minWidth         = 0.25f;
            _details[_index].maxWidth         = 1f;
            _details[_index].minHeight        = 0.25f;
            _details[_index].maxHeight        = 1f;
            _details[_index].healthyColor     = Color.white;
            _details[_index].renderMode       = DetailRenderMode.Grass;

            _index           = 9;
            _details[_index] = new DetailPrototype();
            _details[_index].usePrototypeMesh = true;
            _details[_index].prototype        = (GameObject)Resources.Load("Bushes/Bush4");
            _details[_index].minWidth         = 0.25f;
            _details[_index].maxWidth         = 1f;
            _details[_index].minHeight        = 0.25f;
            _details[_index].maxHeight        = 1f;
            _details[_index].healthyColor     = Color.white;
            _details[_index].renderMode       = DetailRenderMode.Grass;


            _terrain_data.detailPrototypes = _details;

            TerrainDetailResolution = _terrain_data.detailResolution;

            int[,] _density_map_00 = new int[TerrainDetailResolution, TerrainDetailResolution];
            int[,] _density_map_01 = new int[TerrainDetailResolution, TerrainDetailResolution];
            int[,] _density_map_02 = new int[TerrainDetailResolution, TerrainDetailResolution];
            int[,] _density_map_03 = new int[TerrainDetailResolution, TerrainDetailResolution];
            int[,] _density_map_04 = new int[TerrainDetailResolution, TerrainDetailResolution];

            int[,] _density_map_bush_00 = new int[TerrainDetailResolution, TerrainDetailResolution];
            int[,] _density_map_bush_01 = new int[TerrainDetailResolution, TerrainDetailResolution];
            int[,] _density_map_bush_02 = new int[TerrainDetailResolution, TerrainDetailResolution];
            int[,] _density_map_bush_03 = new int[TerrainDetailResolution, TerrainDetailResolution];
            int[,] _density_map_bush_04 = new int[TerrainDetailResolution, TerrainDetailResolution];

            float _x_pos_m = (float)_terrain_data.size.x / (float)TerrainDetailResolution;
            float _y_pos_m = (float)_terrain_data.size.z / (float)TerrainDetailResolution;

            if (TerrainGrassDensity > 0 || TerrainMeshDensity > 0)
            {
                for (int a = 0; a < TerrainDetailResolution; a++)
                {
                    for (int b = 0; b < TerrainDetailResolution; b++)
                    {
                        float   _x      = MathTools.Normalize(a * _x_pos_m, 0, _terrain_data.size.x);
                        float   _y      = MathTools.Normalize(b * _y_pos_m, 0, _terrain_data.size.z);
                        Vector3 _ground = _terrain_data.GetInterpolatedNormal(_y, _x);
                        float   _angle  = Mathf.Abs(Vector3.Angle(_ground, Vector3.up));

                        float _f = 5;
                        //float _m = Mathf.Clamp01( 1 - ( _angle / TerrainGrassAngle ) );

                        if (TerrainGrassDensity > 0)
                        {
                            _density_map_00[a, b] = Mathf.RoundToInt(UnityEngine.Random.Range(TerrainGrassDensity * 0.5f, TerrainGrassDensity) * MathTools.NormalizeRange(_angle, -_f, TerrainGrassAngle + _f, _f));

                            _f = 5;
                            _density_map_01[a, b] = Mathf.RoundToInt(UnityEngine.Random.Range(TerrainGrassDensity * 0.25f, TerrainGrassDensity * 0.3f) * MathTools.NormalizeRange(_angle, 0, TerrainGrassAngle + _f, _f));

                            _f = 3;
                            _density_map_02[a, b] = Mathf.RoundToInt(UnityEngine.Random.Range(TerrainGrassDensity * 0.15f, TerrainGrassDensity * 0.25f) * MathTools.NormalizeRange(_angle, -_f * 0.5f, TerrainGrassAngle + _f, _f));

                            _f = 2;
                            _density_map_03[a, b] = Mathf.RoundToInt(UnityEngine.Random.Range(TerrainGrassDensity * 0.1f, TerrainGrassDensity * 0.5f) * MathTools.NormalizeRange(_angle, -_f, (TerrainGrassAngle * 0.25f) + _f, _f));

                            _f = 2;
                            _density_map_04[a, b] = Mathf.RoundToInt(UnityEngine.Random.Range(TerrainGrassDensity * 0.1f, TerrainGrassDensity * 0.5f) * MathTools.NormalizeRange(_angle, -_f, (TerrainGrassAngle * 0.25f) + _f, _f));
                        }

                        if (TerrainMeshDensity > 0)
                        {
                            _f = 5;
                            _density_map_bush_00[a, b] = Mathf.RoundToInt((UnityEngine.Random.Range(0, Mathf.RoundToInt(100 / TerrainMeshDensity)) == 0 ? 1:0) * MathTools.NormalizeRange(_angle, (TerrainGrassAngle * 0.5f) - _f, (TerrainGrassAngle * 2f) + _f, _f));

                            _f = 2;
                            _density_map_bush_01[a, b] = Mathf.RoundToInt((UnityEngine.Random.Range(0, Mathf.RoundToInt(500 / TerrainMeshDensity)) == 0 ? 1:0) * MathTools.NormalizeRange(_angle, _f, (TerrainMeshAngle * 1.5f) + _f, _f));

                            _f = 2;
                            _density_map_bush_03[a, b] = Mathf.RoundToInt((UnityEngine.Random.Range(0, Mathf.RoundToInt(500 / TerrainMeshDensity)) == 0 ? 1:0) * MathTools.NormalizeRange(_angle, _f, (TerrainMeshAngle * 1.5f) + _f, _f));

                            _f = 2;
                            _density_map_bush_03[a, b] = Mathf.RoundToInt((UnityEngine.Random.Range(0, Mathf.RoundToInt(500 / TerrainMeshDensity)) == 0 ? 1:0) * MathTools.NormalizeRange(_angle, _f, (TerrainMeshAngle * 1.5f) + _f, _f));

                            _f = 2;
                            _density_map_bush_04[a, b] = Mathf.RoundToInt((UnityEngine.Random.Range(0, Mathf.RoundToInt(500 / TerrainMeshDensity)) == 0 ? 1:0) * MathTools.NormalizeRange(_angle, _f, (TerrainMeshAngle * 1.5f) + _f, _f));
                        }
                    }
                }
            }

            _terrain_data.SetDetailLayer(0, 0, 0, _density_map_00);
            _terrain_data.SetDetailLayer(0, 0, 1, _density_map_01);
            _terrain_data.SetDetailLayer(0, 0, 2, _density_map_02);
            _terrain_data.SetDetailLayer(0, 0, 3, _density_map_03);
            _terrain_data.SetDetailLayer(0, 0, 4, _density_map_04);

            _terrain_data.SetDetailLayer(0, 0, 5, _density_map_bush_00);
            _terrain_data.SetDetailLayer(0, 0, 6, _density_map_bush_01);
            _terrain_data.SetDetailLayer(0, 0, 7, _density_map_bush_02);
            _terrain_data.SetDetailLayer(0, 0, 8, _density_map_bush_03);
            _terrain_data.SetDetailLayer(0, 0, 9, _density_map_bush_04);
        }
Esempio n. 8
0
        public static float NormalizePeak(float _value, float _min, float _max)
        {
            float _peak = (_max + _min) * 0.5f;

            return(Mathf.Clamp01((_value >= _min && _value < _peak ? MathTools.Normalize(_value - _min, 0, _peak - _min) : (_value <= _max && _value > _peak ? MathTools.Normalize(_max - _value, 0, _peak - _min) : (_value == _peak ? 1f : 0f)))));
        }