Exemple #1
0
    private void DrawRandomTest(SerializedProperty vertexesProp, SerializedProperty trianglesProp,
                                SerializedProperty triangleAreasProp, SerializedProperty areaProp, SerializedProperty pointSizeProp)
    {
        if (areaProp.floatValue <= 0 || Mathf.Approximately(areaProp.floatValue, 0))
        {
            return;
        }

        EditorGUILayout.BeginHorizontal();

        bool isTest  = GUILayout.Button("测试");
        bool isClear = GUILayout.Button("清除");


        GUILayout.FlexibleSpace();
        GUILayout.Label("随机点大小");

        pointSizeProp.floatValue = EditorGUILayout.FloatField(pointSizeProp.floatValue, GUILayout.Width(100));
        pointSizeProp.floatValue = Mathf.Clamp(pointSizeProp.floatValue, 0.01f, 1.0f);

        bool isRefresh = GUILayout.Button("刷新");

        GUILayout.FlexibleSpace();
        EditorGUILayout.EndHorizontal();


        if (isTest)
        {
            if (vertexesProp.arraySize > 3)
            {
                PolygonArea area = new PolygonArea()
                {
                    Vertexes      = new List <Vector3>(),
                    Triangles     = new List <ThreeVector3>(),
                    TriangleAreas = new List <float>(),
                    Area          = areaProp.floatValue,
                };
                for (int i = 0; i < vertexesProp.arraySize; ++i)
                {
                    area.Vertexes.Add(vertexesProp.GetArrayElementAtIndex(i).vector3Value);
                }

                for (int i = 0; i < trianglesProp.arraySize; ++i)
                {
                    var triProp = trianglesProp.GetArrayElementAtIndex(i);
                    var x       = triProp.FindPropertyRelative("x").vector3Value;
                    var y       = triProp.FindPropertyRelative("y").vector3Value;
                    var z       = triProp.FindPropertyRelative("z").vector3Value;
                    area.Triangles.Add(new ThreeVector3(x, y, z));

                    area.TriangleAreas.Add(triangleAreasProp.GetArrayElementAtIndex(i).floatValue);
                }

                testPoints.Clear();
                for (int i = 0; i < TestPointCount; ++i)
                {
                    Vector3 p = area.RandomPoint();
                    testPoints.Add(p);
                }
                testPointsSize = pointSizeProp.floatValue;
                SceneView.RepaintAll();
            }
        }

        if (isClear)
        {
            testPoints.Clear();
            SceneView.RepaintAll();
        }


        if (isRefresh)
        {
            testPointsSize = pointSizeProp.floatValue;
            SceneView.RepaintAll();
        }
    }