Example #1
0
    private void CreatePrefab(bool _delete, bool _inst)
    {
        ProceduralGear g      = (ProceduralGear)target;
        MeshFilter     filter = (MeshFilter)g.GetComponent(typeof(MeshFilter));
        GameObject     newGO  = new GameObject();

        newGO.name = g.name;
        newGO.AddComponent(typeof(MeshFilter));
        newGO.AddComponent(typeof(MeshRenderer));
        SaveMesh();
        MeshFilter f = (MeshFilter)newGO.GetComponent(typeof(MeshFilter));

        f.sharedMesh = Mesh.Instantiate(filter.sharedMesh) as Mesh;
        MeshRenderer r  = (MeshRenderer)newGO.GetComponent(typeof(MeshRenderer));
        MeshRenderer r2 = (MeshRenderer)g.GetComponent(typeof(MeshRenderer));

        r.sharedMaterials = r2.sharedMaterials;
                #pragma warning disable 618
        Object prefab = EditorUtility.CreateEmptyPrefab(path + "/" + g.name + ".prefab");
        EditorUtility.ReplacePrefab(g.gameObject, prefab, ReplacePrefabOptions.ConnectToPrefab);
                #pragma warning restore 618
        if (!_delete)
        {
            if (_inst)
            {
                GameObject go = (GameObject)Instantiate(g.gameObject, g.gameObject.transform.position, g.gameObject.transform.rotation);
                go.name = go.name.Replace("(Clone)", "");
            }
            DestroyImmediate(newGO, true);
        }
    }
Example #2
0
    private void DrawHandleCircles(GameObject _selected)
    {
        Transform t = _selected.transform;

        Handles.color = Color.magenta;
        float handelScale = t.localScale.x;

        while (t.parent != null)
        {
            t            = t.parent;
            handelScale *= t.localScale.x;
        }
        t = _selected.transform;
        ProceduralGear gear = _selected.GetComponent <ProceduralGear> ();

        if (!twoSelected)
        {
            if (gear == null)
            {
                Handles.Disc(Quaternion.identity, t.position, t.up, handelScale * 0.5f, true, 0f);
                return;
            }
            Handles.Disc(Quaternion.identity, t.position, t.up, gear.prefs.dk * handelScale, true, 0f);
        }
    }
Example #3
0
    void OnSceneGUI()
    {
        ProceduralGear gear = (ProceduralGear)target;

        if (gear.points == null)
        {
            return;
        }

        Transform gearTransform = gear.transform;
        Vector3   baseRadius    = new Vector3(gear.df * gearTransform.localScale.x, 0f, 0f);

        Undo.RecordObject(gear, "TM PointMove");

        Handles.Disc(Quaternion.identity, gearTransform.position, gearTransform.up, baseRadius.x, true, 0f);
        Handles.Disc(Quaternion.identity, gearTransform.position, gearTransform.up, gear.prefs.dk * gearTransform.localScale.x, true, 0f);
        int partCount = gear.prefs.bodyParts + gear.prefs.teethParts;

        for (int i = 0; i < (symmetric ? partCount + 1 : gear.points.Length - 3); i++)
        {
            if (i != gear.prefs.bodyParts)
            {
                string  str      = "";
                Vector3 oldPoint = gearTransform.TransformPoint(baseRadius + gear.points[i].offset);
                if (i > partCount)
                {
                    Handles.color = colors[i - partCount + 1];
                    str           = (i - partCount).ToString();
                }
                else if (i > gear.prefs.bodyParts)
                {
                    Handles.color = colors[i - gear.prefs.bodyParts + 1];
                    str           = (i - gear.prefs.bodyParts).ToString();
                }
                else
                {
                    Handles.color = colors[i + 2];
                    str           = (i + 1).ToString();
                }

                Vector3 newPoint = Handles.FreeMoveHandle(oldPoint, Quaternion.identity, gear.prefs.modul * 0.3f, pointSnap, Handles.DotCap);
                if (oldPoint != newPoint)
                {
                    gear.points[i].offset = gearTransform.InverseTransformPoint(newPoint - baseRadius);
                    if (GUI.changed)
                    {
                        gear.UpdateGear();
                    }
                }
                Handles.Label(oldPoint, new GUIContent(str), EditorStyles.boldLabel);
            }
        }
    }
Example #4
0
    private void DrawHandleCircles(GameObject _selected)
    {
        Transform      t    = _selected.transform;
        ProceduralGear gear = _selected.GetComponent <ProceduralGear> ();

        if (!twoSelected)
        {
            if (gear == null)
            {
                Handles.Disc(Quaternion.identity, t.position, t.up, t.localScale.x * 0.5f, true, 0f);
                return;
            }
            Handles.Disc(Quaternion.identity, t.position, t.up, gear.prefs.dk * t.localScale.x, true, 0f);
        }
    }
Example #5
0
 void OnEnable()
 {
     if (Selection.activeGameObject != null)
     {
         gears  = new SerializedObject(targets);
         points = gears.FindProperty("points");
         prefs  = gears.FindProperty("prefs");
         ProceduralGear g = (ProceduralGear)target;
         if (target != null)
         {
             switchInnerGearing = g.prefs.inner;
             if (g.points != null)
             {
                 colorPalette = g.points[g.prefs.bodyParts].uvOffX;
             }
         }
     }
     this.currentMenu = MenuPrefs;
     SetColorPalette();
 }
Example #6
0
    private void SaveMesh()
    {
        ProceduralGear g = (ProceduralGear)target;
        string         meshPrefabPath = "";

        meshPrefabPath = path + bSlash + g.name + ".asset";
        MeshFilter filter = (MeshFilter)g.GetComponent(typeof(MeshFilter));

        filter.sharedMesh.hideFlags = HideFlags.None;
        Unwrapping.GenerateSecondaryUVSet(filter.sharedMesh);
        Mesh mesh = Mesh.Instantiate(filter.sharedMesh) as Mesh;

        if (!System.IO.File.Exists(Application.dataPath.Replace("Assets", path) + bSlash + g.name + ".asset"))
        {
            AssetDatabase.CreateAsset(mesh, meshPrefabPath);
        }
        else
        {
            AssetDatabase.SaveAssets();
        }
        filter.sharedMesh.hideFlags = HideFlags.HideAndDontSave;
    }
Example #7
0
    private void Tool_MoveTo(GameObject[] _selectedObjects, float _degree, bool _useDegree)
    {
        ProceduralGear gear1          = _selectedObjects [0].GetComponent <ProceduralGear> ();
        ProceduralGear gear2          = _selectedObjects [1].GetComponent <ProceduralGear> ();
        Transform      gear1Transform = gear1.transform;
        Transform      gear2Transform = gear2.transform;

        float     neededDistance        = (gear1.prefs.d) + (gear2.prefs.d);
        Transform originalParentObjectA = gear1Transform.parent;

        gear1Transform.parent = gear2Transform;
        Quaternion originalObjectBRotation = gear2Transform.rotation;

        gear2Transform.rotation = Quaternion.identity;

        if (!_useDegree)
        {
            gear1.gameObject.transform.Translate(0.000f, 0.000f, 0.00001f);
            Quaternion relativRotation = Quaternion.LookRotation(gear1Transform.position - gear2Transform.position);
            _degree = relativRotation.eulerAngles.y - gear2Transform.rotation.eulerAngles.y;
        }

        Tool_CenterObjects(_selectedObjects);
        gear1Transform.Rotate(0.0f, _degree, 0.0f);
        gear1Transform.Translate(0.0f, 0.0f, neededDistance);
        gear2Transform.rotation = originalObjectBRotation;
        gear1Transform.parent   = originalParentObjectA;

        float angleBetweenTwoTeethOnSource = (360.0f / gear1.prefs.teethCount);
        float angleBetweenTwoTeethOnTarget = (360.0f / gear2.prefs.teethCount);
        float ratio = angleBetweenTwoTeethOnSource / angleBetweenTwoTeethOnTarget;

        gear1Transform.rotation = gear2Transform.rotation;
        gear1Transform.Rotate(0.0f, _degree, 0.0f);
        gear1Transform.Rotate(0.0f, tool_RotaDegree += 90 + (angleBetweenTwoTeethOnSource * 0.5f) + (_degree * ratio), 0.0f);
    }
Example #8
0
    private void MenuOpt()
    {
        Color gCol = GUI.color;

        MenuButtons(3);
        ProceduralGear g = (ProceduralGear)target;

        GUI.color = gCol;
        g.name    = EditorGUILayout.TextField("Name", g.name.Replace("(Clone)", ""), GUILayout.Width(300), GUILayout.Height(14));
        path      = EditorGUILayout.TextField("Path", path, GUILayout.Width(300), GUILayout.Height(14));
        EditorGUILayout.Separator();
        EditorGUILayout.BeginHorizontal();
        bool noPath = false;

        if (System.IO.File.Exists(Application.dataPath.Replace("Assets", path) + bSlash + g.name + ".asset"))
        {
            GUI.color = Color.red;
            InfoField("  File exist or mesh already saved.", Color.red, 205f);
            EditorGUILayout.Space();
        }
        else
        if (System.IO.Directory.Exists(Application.dataPath.Replace("Assets", path)) && path != "")
        {
            EditorGUILayout.LabelField("Save mesh to Asset folder");
        }
        else
        {
            GUI.color = Color.red;
            InfoField("  Path does not exist.", Color.red, 150f);
            EditorGUILayout.Space();
            noPath = true;
        }

        GUILayoutOption buttonWidth = GUILayout.Width(120);

        if (GUILayout.Button("Save Mesh", buttonWidth))
        {
            if (GUI.color != Color.red)
            {
                SaveMesh();
            }
        }
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.Separator();
        if (GUI.color == Color.red)
        {
            EditorGUILayout.Separator();
        }
        if (!noPath)
        {
            GUI.color = gCol;
        }
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("Create a prefab in Assets folder");
        if (GUILayout.Button("Create Prefab", buttonWidth))
        {
            if (GUI.color != Color.red)
            {
                CreatePrefab(false, false);
            }
        }
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.Separator();
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("Duplicate and make this to prefab");
        if (GUILayout.Button("Duplicate", buttonWidth))
        {
            if (GUI.color != Color.red)
            {
                CreatePrefab(false, true);
            }
        }
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.Separator();
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("Copy as object and this to prefab");
        if (GUILayout.Button("Copy Object", buttonWidth))
        {
            if (GUI.color != Color.red)
            {
                CreatePrefab(true, true);
            }
        }
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.Separator();
        EditorGUILayout.Separator();
        EditorGUILayout.LabelField("Preferences:");
        GUI.color = gCol;
        EditorGUILayout.Separator();
        if (targets.Length < 2)
        {
            GUI.color = Color.white;
            if (g.points[g.prefs.bodyParts].uvOffX == 0)
            {
                g.points[g.prefs.bodyParts].uvOffX = defaultColorPalette;
            }
            colorPalette = Mathf.Clamp(Mathf.FloorToInt(SliderFloatField("Color Palette", g.points[g.prefs.bodyParts].uvOffX, -1, 200f)), 1, 200);
            if (g.points[g.prefs.bodyParts].uvOffX != colorPalette)
            {
                SetColorPalette();
                g.points[g.prefs.bodyParts].uvOffX = colorPalette;
            }
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.Space();
            for (int i = 2; i < 20; i++)
            {
                InfoField("", newColor(i), 10f);
            }
            EditorGUILayout.Space();
            EditorGUILayout.EndHorizontal();
        }
        else
        {
            EditorGUILayout.LabelField("(Multi Object Editing not supported for preferences.)");
        }
        EditorGUILayout.BeginHorizontal();
        GUI.color = Color.gray;
        EditorGUILayout.Space();
        if (GUILayout.Button("?", GUILayout.Width(20)))
        {
            showOptInfo = !showOptInfo;
        }
        EditorGUILayout.EndHorizontal();
        if (showOptInfo)
        {
            ShowOptInfo();
        }
        EditorGUILayout.Separator();
    }
Example #9
0
    private void MenuMat()
    {
        MenuButtons(1);
        ProceduralGear g = (ProceduralGear)target;
        SerializedProperty
            bodyParts        = prefs.FindPropertyRelative("bodyParts"),
            teethParts       = prefs.FindPropertyRelative("teethParts"),
            calculateTangens = prefs.FindPropertyRelative("tangens");

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.PropertyField(calculateTangens, new GUIContent("Calculate Tangens", "Check this, only if shader needs them.\nCalculate tangens needs more time on CPU."));
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.LabelField("Textures in Renderer:");
        EditorGUILayout.BeginHorizontal();
        int matCount = 0;

        foreach (Material m in g.GetComponent <Renderer>().sharedMaterials)
        {
            GUILayout.Label(matCount.ToString(), GUILayout.MaxWidth(10f));
            GUILayout.Label(m.mainTexture, GUILayout.Width(50f), GUILayout.Height(50f));
            matCount++;
        }
        EditorGUILayout.EndHorizontal();
        MenuButtons_Parts(currenPartMenu);
        GUILayoutOption labelHeight = GUILayout.Height(40f);

        int[] indexes = GetStartEndPartIndex(bodyParts.intValue, teethParts.intValue, 0);
        for (int i = indexes[0]; i < indexes[1]; i++)
        {
            if (i == points.arraySize - 2)
            {
                EditorGUILayout.LabelField("Cap Body");
            }
            else
            if (i == points.arraySize - 1)
            {
                EditorGUILayout.LabelField("Cap Teeth");
            }
            SerializedProperty
                point = points.GetArrayElementAtIndex(i),
                mat   = point.FindPropertyRelative("mat");
            if (mat == null)
            {
                break;
            }
            if (mat.intValue > g.GetComponent <Renderer>().sharedMaterials.Length - 1)
            {
                mat.intValue = 0;
            }
            EditorGUILayout.BeginHorizontal();
            InfoField(" " + indexes[2].ToString(), colors[++indexes[2]], 15f);
            GUILayout.Label(g.GetComponent <Renderer>().sharedMaterials[mat.intValue].mainTexture, GUILayout.Width(40f), labelHeight);
            GUILayout.Label(g.GetComponent <Renderer>().sharedMaterials[mat.intValue].name, GUILayout.Width(120f), labelHeight);
            for (int j = 0; j < matCount; j++)
            {
                if (GUILayout.Button(j.ToString(), GUILayout.MaxWidth(35f)))
                {
                    mat.intValue = j;
                }
            }
            EditorGUILayout.LabelField(" ");
            EditorGUILayout.EndHorizontal();
        }
        EditorGUILayout.Separator(); EditorGUILayout.Separator();
        EditorGUILayout.BeginHorizontal();
        ShowInfoField();
        GUI.color = Color.gray;
        EditorGUILayout.LabelField(" ");
        if (GUILayout.Button("?", GUILayout.Width(20)))
        {
            showMatInfo = !showMatInfo;
        }
        EditorGUILayout.EndHorizontal();
        if (showMatInfo)
        {
            ShowMatInfo();
        }
        EditorGUILayout.Separator();
    }