コード例 #1
0
    /*
     *  EditorGUILayout.BeginHorizontal();
     *  scrollPos =
     *      EditorGUILayout.BeginScrollView(scrollPos, GUILayout.Width(100), GUILayout.Height(100));
     *  GUILayout.Label(t);
     *  EditorGUILayout.EndScrollView();
     */

    private void OnGUI()
    {
        // if(isInit)
        // {
        //     //lSys = new LSystem();
        //     //rules.Add("");
        //     //varsCreated = variables.Count;

        //     //serObj = new SerializedObject(this);
        //     isInit = false;
        // }

        lSys.Clear(); //Could do this first thing on clicking "Generate"? Will that keep adding production rules...

        scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
        //scrollPos = EditorGUILayout.BeginVertical(GUILayout.Height(100));

        GUILayout.Label("L-Systems:", EditorStyles.boldLabel);
        //SerializedObject obj = new SerializedObject(this);

        // SerializedProperty keyProp = obj.FindProperty("keyList");
        // EditorGUILayout.PropertyField(keyProp, new GUIContent("Names: "), true);

        // SerializedProperty valProp = obj.FindProperty("valList");
        // EditorGUILayout.PropertyField(valProp, new GUIContent("Values: "), true);

        DrawVariableMap();
        //GUILayout.Space(VariableDrawer.propHeight);
        // int lowCount = keyList.Count;
        // if (valList.Count < lowCount)
        // {
        //     lowCount = valList.Count;

        // }

        //change to just copy dict?
        foreach (KeyValuePair <string, float> kv in variables)
        {
            //Debug.Log("Key: " + kv.Key + " = " + kv.Value);
            lSys.SetVar(kv.Key, kv.Value);
            // lSys.SetVar(variables[i].key, variables[i].value);
        }

        axiom = EditorGUILayout.DelayedTextField("Axiom: ", lSys.GetAxiom());
        lSys.SetAxiom(axiom);

        DrawProductionRules();

        serObj.ApplyModifiedProperties();


        foreach (ProductionRule pr in pRules)
        {
            pr.suc.Clear();
            if (pr.sucRep.Length > 0)
            {
                List <Module> mods = ModuleParser.StringToModuleList(pr.sucRep, lSys);
                pr.suc.InsertRange(0, mods);
            }
        }

        stepSize = EditorGUILayout.FloatField("Segment Length: ", stepSize);
        width    = EditorGUILayout.FloatField("Segment Width: ", width);
        angle    = EditorGUILayout.FloatField("Angle: ", angle);
        num      = EditorGUILayout.IntField("Iterations: ", num);
        numOut   = EditorGUILayout.IntField("Output Count: ", numOut);

        turtlePos = (Interpreter.TurtlePos)EditorGUILayout.EnumPopup("Turtle Position: ", turtlePos);

        GUILayout.FlexibleSpace();

        if (GUILayout.Button("Generate"))
        {
            //Debug.Log("Generating...");

            //Debug.Log(pRules.Count);
            foreach (ProductionRule r in pRules)
            {
                lSys.AddRule(r);
            }


            float xOffset = 10f;

            Material mat = new Material(Shader.Find("Sprites/Default"));
            mat.color = new Color(0.0f, 0.0f, 0.0f, 1.0f);
            AssetDatabase.CreateAsset(mat, "Assets/GeometryMaterial.mat");

            List <Mesh> meshes = new List <Mesh>();
            dupCount = 0;
            dupMap.Clear();

            //string symbols = "Ff+-[]|";
            for (int i = 0; i < numOut; ++i)
            {
                EditorUtility.DisplayProgressBar("Generating Geometry...", "Generating Geometry...", ((float)i / numOut));
                List <Module> mods    = lSys.RunSystem(num);
                string        str_out = "";
                foreach (Module m in mods)
                {
                    str_out += m;
                    //if (symbols.IndexOf(m.sym) != -1)
                }


                //str_out = Regex.Replace(str_out, "[^Ff+*/ ()0-9|,-]", "", RegexOptions.Compiled);
                //str_out.Replace("^[Ff+-()[][0-9]", "");
                //Debug.Log(str_out);

                if (dupMap.ContainsKey(str_out))
                {
                    dupMap[str_out] += 1;
                }
                else
                {
                    dupMap.Add(str_out, 1);
                }
                //Debug.Log(str_out);



                Interpreter intptr = new Interpreter();
                GameObject  go     = new GameObject("Geometry" + i);
                //go.transform.position = Vector3.zero;
                go.transform.position = new Vector3(0f, 0f, 0f);
                MeshFilter   mf = go.AddComponent <MeshFilter>();
                MeshRenderer mr = go.AddComponent <MeshRenderer>();

                //Material mat = new Material(Shader.Find("Sprites/Default"));
                mr.sharedMaterial = mat;
                //mr.sharedMaterial.color = Color.black;
                Mesh newMesh = intptr.InterpretSystem(mods, turtlePos, stepSize, width, angle);
                mf.mesh = newMesh;

                //Dictionary<Vector3, int> vertexCount = new Dictionary<Vector3, int>();
                //foreach (Vector3 v in newMesh.vertices)
                //{
                //    if (vertexCount.ContainsKey(v))
                //    {
                //        vertexCount[v] += 1;
                //    }
                //    else
                //    {
                //        vertexCount.Add(v, 1);
                //    }
                //}

                //bool isDuplicate = false;
                //foreach (Mesh m in meshes)
                //{
                //    Dictionary<Vector3, int> vCount = new Dictionary<Vector3, int>();
                //    foreach (Vector3 v in m.vertices)
                //    {
                //        if (vCount.ContainsKey(v))
                //        {
                //            vCount[v] += 1;
                //        }
                //        else
                //        {
                //            vCount.Add(v, 1);
                //        }
                //    }

                //    bool isDup = true;
                //    if (vertexCount.Keys.Count == vCount.Keys.Count)
                //    {
                //        foreach (KeyValuePair<Vector3, int> kv in vertexCount)
                //        {
                //            if (!vCount.ContainsKey(kv.Key))
                //            {
                //                isDup = false;
                //                break;
                //            }
                //            else if (vCount[kv.Key] != kv.Value)
                //            {
                //                isDup = false;
                //                break;
                //            }
                //        }
                //        if (isDup)
                //        {
                //            isDuplicate = true;
                //            ++dupCount;
                //        }
                //    }
                //}
                //if (!isDuplicate)
                //    meshes.Add(newMesh);

                ++createdCount;

                AssetDatabase.CreateAsset(mf.sharedMesh, "Assets/NewGO" + createdCount + ".mesh");
                //AssetDatabase.CreateAsset(mr.sharedMaterial, "Assets/NewGO" + createdCount + ".mat");
                PrefabUtility.SaveAsPrefabAssetAndConnect(go, "Assets/NewGO" + createdCount + ".prefab", InteractionMode.AutomatedAction);
            }
            EditorUtility.ClearProgressBar();

            foreach (KeyValuePair <string, int> kv in dupMap)
            {
                if (kv.Value > 1)
                {
                    dupCount += kv.Value - 1;
                    Debug.Log("Duplicate Reuslt: " + kv.Key + "\nTimes Generated: " + kv.Value);
                }
            }
            Debug.Log("Num Duplicates: " + dupCount);
        }
        EditorGUILayout.EndScrollView();
    }