void ChangePivot(Vector3 center)
    {
        Vector3 position = spline.transform.position;

        spline.transform.position += center;
        for (int i = 0; i < spline.controlPoints.Count; i++)
        {
            Vector4 vec = spline.controlPoints [i];
            vec.x -= center.x;
            vec.y -= center.y;
            vec.z -= center.z;
            spline.controlPoints [i] = vec;
        }
        spline.GenerateSpline();
    }
    public override void OnInspectorGUI()
    {
        EditorGUILayout.Space();
        logo = (Texture2D)Resources.Load("logoRAM");



        Color baseCol = GUI.color;

        spline = (RamSpline)target;


        scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
        {
            GUIContent btnTxt = new GUIContent(logo);

            var rt = GUILayoutUtility.GetRect(btnTxt, GUI.skin.label, GUILayout.ExpandWidth(false));
            rt.center = new Vector2(EditorGUIUtility.currentViewWidth / 2, rt.center.y);

            GUI.Button(rt, btnTxt, GUI.skin.label);



            EditorGUILayout.HelpBox("Add Point  - CTRL + Left Mouse Button Click", MessageType.Info);
            EditorGUI.indentLevel++;

            EditorGUILayout.BeginHorizontal();
            showPositions = EditorGUILayout.Foldout(showPositions, "Points");

            if (GUILayout.Button("Add point at end"))
            {
                Undo.RecordObject(spline, "Add point");

                int     i        = spline.controlPoints.Count - 1;
                Vector4 position = Vector3.zero;
                position.w = spline.width;

                if (i < spline.controlPoints.Count - 1 && spline.controlPoints.Count > i + 1)
                {
                    position = spline.controlPoints [i];
                    Vector4 positionSecond = spline.controlPoints [i + 1];
                    if (Vector3.Distance((Vector3)positionSecond, (Vector3)position) > 0)
                    {
                        position = (position + positionSecond) * 0.5f;
                    }
                    else
                    {
                        position.x += 1;
                    }
                }
                else if (spline.controlPoints.Count > 1 && i == spline.controlPoints.Count - 1)
                {
                    position = spline.controlPoints [i];
                    Vector4 positionSecond = spline.controlPoints [i - 1];
                    if (Vector3.Distance((Vector3)positionSecond, (Vector3)position) > 0)
                    {
                        position = position + (position - positionSecond);
                    }
                    else
                    {
                        position.x += 1;
                    }
                }
                else if (spline.controlPoints.Count > 0)
                {
                    position    = spline.controlPoints [i];
                    position.x += 1;
                }

                spline.controlPoints.Add(position);

                spline.GenerateSpline();
            }
            EditorGUILayout.EndHorizontal();

            EditorGUI.BeginChangeCheck();
            if (showPositions)
            {
                EditorGUI.indentLevel++;
                for (int i = 0; i < spline.controlPoints.Count; i++)
                {
                    EditorGUILayout.BeginHorizontal();

                    spline.controlPoints [i] = EditorGUILayout.Vector4Field("", spline.controlPoints [i]);

                    if (spline.controlPoints [i].w <= 0)
                    {
                        Vector4 vec4 = spline.controlPoints [i];
                        vec4.w = 1;
                        spline.controlPoints [i] = vec4;
                    }

                    if (GUILayout.Button(new GUIContent("A", "Add point after this point")))
                    {
                        Undo.RecordObject(spline, "Add point");


                        Vector4 position = spline.controlPoints [i];

                        if (i < spline.controlPoints.Count - 1 && spline.controlPoints.Count > i + 1)
                        {
                            Vector4 positionSecond = spline.controlPoints [i + 1];
                            if (Vector3.Distance((Vector3)positionSecond, (Vector3)position) > 0)
                            {
                                position = (position + positionSecond) * 0.5f;
                            }
                            else
                            {
                                position.x += 1;
                            }
                        }
                        else if (spline.controlPoints.Count > 1 && i == spline.controlPoints.Count - 1)
                        {
                            Vector4 positionSecond = spline.controlPoints [i - 1];
                            if (Vector3.Distance((Vector3)positionSecond, (Vector3)position) > 0)
                            {
                                position = position + (position - positionSecond);
                            }
                            else
                            {
                                position.x += 1;
                            }
                        }
                        else
                        {
                            position.x += 1;
                        }

                        spline.controlPoints.Insert(i + 1, position);

                        spline.GenerateSpline();
                    }

                    if (GUILayout.Button(new GUIContent("R", "Remove this Point")))
                    {
                        Undo.RecordObject(spline, "Remove point");
                        spline.controlPoints.RemoveAt(i);
                        spline.GenerateSpline();
                    }

                    if (GUILayout.Toggle(selectedPosition == i, new GUIContent("S", "Select point"), "Button"))
                    {
                        selectedPosition = i;
                    }
                    else if (selectedPosition == i)
                    {
                        selectedPosition = -1;
                    }

                    EditorGUILayout.EndHorizontal();
                }

                EditorGUI.indentLevel--;
                EditorGUILayout.Space();
            }

            EditorGUI.indentLevel--;

            string meshResolution = "Triangles density";
            if (spline.meshfilter != null && spline.meshfilter.sharedMesh != null)
            {
                float tris = spline.meshfilter.sharedMesh.triangles.Length;
                meshResolution += " (" + tris + " tris)";
            }
            else if (spline.meshfilter != null && spline.meshfilter.sharedMesh == null)
            {
                spline.GenerateSpline();
            }
            EditorGUILayout.LabelField(meshResolution);

            EditorGUI.indentLevel++;
            spline.traingleDensity = 1 / (float)EditorGUILayout.IntSlider("U", (int)(1 / (float)spline.traingleDensity), 1, 100);

            spline.vertsInShape = EditorGUILayout.IntSlider("V", spline.vertsInShape - 1, 1, 20) + 1;
            EditorGUI.indentLevel--;


            spline.uvScale = EditorGUILayout.FloatField("UV scale (texture tiling)", spline.uvScale);

            if (EditorGUI.EndChangeCheck())
            {
                spline.GenerateSpline();
            }


            EditorGUILayout.BeginHorizontal();
            {
                spline.width = EditorGUILayout.FloatField("River width", spline.width);
                if (GUILayout.Button("Change width for whole river"))
                {
                    if (spline.width > 0)
                    {
                        Undo.RecordObject(spline, "Add point");
                        for (int i = 0; i < spline.controlPoints.Count; i++)
                        {
                            Vector4 point = spline.controlPoints [i];
                            point.w = spline.width;
                            spline.controlPoints [i] = point;
                        }

                        spline.GenerateSpline();
                    }
                }
            }
            EditorGUILayout.EndHorizontal();


            EditorGUILayout.Space();



            if (GUILayout.Button("Set basic river material"))
            {
                try {
                    string materialName = "RAM_River_Material_Gamma";
                    if (PlayerSettings.colorSpace == ColorSpace.Linear)
                    {
                        materialName = "RAM_River_Material_Linear";
                    }

                    Material riverMat = (Material)Resources.Load(materialName);

                    if (riverMat != null)
                    {
                        spline.GetComponent <MeshRenderer> ().sharedMaterial = riverMat;
                    }
                } catch {
                }
            }

            if (GUILayout.Button("Set basic river material with tesselation"))
            {
                try {
                    string materialName = "RAM_River_Material_Gamma_Tess";
                    if (PlayerSettings.colorSpace == ColorSpace.Linear)
                    {
                        materialName = "RAM_River_Material_Linear_Tess";
                    }

                    Material riverMat = (Material)Resources.Load(materialName);

                    if (riverMat != null)
                    {
                        spline.GetComponent <MeshRenderer> ().sharedMaterial = riverMat;
                    }
                } catch {
                }
            }
            if (GUILayout.Button("Set basic river material - vertex color"))
            {
                try {
                    string materialName = "RAM_River_Material_Gamma_Vertex_Color";
                    if (PlayerSettings.colorSpace == ColorSpace.Linear)
                    {
                        materialName = "RAM_River_Material_Linear_Vertex_Color";
                    }

                    Material riverMat = (Material)Resources.Load(materialName);

                    if (riverMat != null)
                    {
                        spline.GetComponent <MeshRenderer> ().sharedMaterial = riverMat;
                    }
                } catch {
                }
            }

            if (GUILayout.Button("Set basic river material with tesselation - vertex color"))
            {
                try {
                    string materialName = "RAM_River_Material_Gamma_Tess_Vertex_Color";
                    if (PlayerSettings.colorSpace == ColorSpace.Linear)
                    {
                        materialName = "RAM_River_Material_Linear_Tess_Vertex_Color";
                    }

                    Material riverMat = (Material)Resources.Load(materialName);

                    if (riverMat != null)
                    {
                        spline.GetComponent <MeshRenderer> ().sharedMaterial = riverMat;
                    }
                } catch {
                }
            }



            EditorGUILayout.Space();
            EditorGUILayout.Space();

            EditorGUI.indentLevel++;
            spline.showAdditionalOptions = EditorGUILayout.Foldout(spline.showAdditionalOptions, "Additional options");

            if (spline.showAdditionalOptions)
            {
                GUIStyle buttonEditingStyle = new GUIStyle(GUI.skin.GetStyle("Button"));
                buttonEditingStyle.fontStyle = FontStyle.Bold;
                buttonEditingStyle.fontSize  = 15;

                Color guiBaseColor = GUI.color;

                GUI.color = Color.green;
                if (!spline.drawOnMesh && GUILayout.Button("Start drawing vertex color", buttonEditingStyle))
                {
                    spline.drawOnMesh = true;
                }
                GUI.color = Color.red;
                if (spline.drawOnMesh && GUILayout.Button("Stop drawing vertex color", buttonEditingStyle))
                {
                    spline.drawOnMesh = false;
                }
                GUI.color = guiBaseColor;

                if (spline.drawOnMesh)
                {
                    EditorGUILayout.HelpBox("R - Slow Water G - Small Cascade B - Big Cascade A - Opacity", MessageType.Info);
                    spline.drawColor = EditorGUILayout.ColorField("Draw color", spline.drawColor);
                    spline.opacity   = EditorGUILayout.FloatField("Opacity", spline.opacity);
                    spline.drawSize  = EditorGUILayout.FloatField("Size", spline.drawSize);

                    if (spline.drawSize < 0)
                    {
                        spline.drawSize = 0;
                    }
                }

                if (!spline.showVertexColors)
                {
                    if (GUILayout.Button("Show vertex colors"))
                    {
                        spline.oldMaterial = spline.GetComponent <MeshRenderer> ().sharedMaterial;
                        spline.GetComponent <MeshRenderer> ().sharedMaterial = new Material(Shader.Find("Debug/Vertex color"));
                        spline.showVertexColors = true;
                    }
                }
                else
                {
                    if (GUILayout.Button("Hide vertex colors"))
                    {
                        spline.GetComponent <MeshRenderer> ().sharedMaterial = spline.oldMaterial;
                        spline.showVertexColors = false;
                    }
                }


                EditorGUILayout.Space();
                EditorGUILayout.Space();

                if (GUILayout.Button("Set object pivot to center"))
                {
                    Vector3 center = spline.meshfilter.sharedMesh.bounds.center;

                    ChangePivot(center);
                }
                EditorGUILayout.BeginHorizontal();
                {
                    if (GUILayout.Button("Set object pivot position"))
                    {
                        ChangePivot(pivotChange - spline.transform.position);
                    }
                    pivotChange = EditorGUILayout.Vector3Field("", pivotChange);
                }
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.Space();

                if (GUILayout.Button(new GUIContent("Regenerate spline", "Racalculates whole mesh")))
                {
                    spline.GenerateSpline();
                }

                EditorGUILayout.Space();
                if (GUILayout.Button("Export as mesh"))
                {
                    string path = EditorUtility.SaveFilePanelInProject("Save river mesh", "", "asset", "Save river mesh");


                    if (path.Length != 0 && spline.meshfilter.sharedMesh != null)
                    {
                        AssetDatabase.CreateAsset(spline.meshfilter.sharedMesh, path);

                        AssetDatabase.Refresh();
                        spline.GenerateSpline();
                    }
                }
            }
        }
        EditorGUILayout.EndScrollView();
    }