Beispiel #1
0
    static bool TestAttributes()
    {
        var mesh = new BMesh();

        mesh.AddVertexAttribute(new AttributeDefinition("test", AttributeBaseType.Float, 3));
        mesh.AddEdgeAttribute("edgetest", AttributeBaseType.Float, 2);
        mesh.AddFaceAttribute("facetest", AttributeBaseType.Int, 1);

        Vertex v0 = mesh.AddVertex(new Vector3(-1, 0, -1));
        Vertex v1 = mesh.AddVertex(new Vector3(-1, 0, 1));
        Vertex v2 = mesh.AddVertex(new Vector3(1, 0, 1));
        Vertex v3 = mesh.AddVertex(new Vector3(1, 0, -1));
        Face   f0 = mesh.AddFace(v0, v1, v2);
        Face   f1 = mesh.AddFace(v2, v1, v3);

        var otherAttr = new AttributeDefinition("other", AttributeBaseType.Int, 1);
        var def       = otherAttr.defaultValue as IntAttributeValue;

        def.data[0] = 42;
        mesh.AddVertexAttribute(otherAttr);
        foreach (var v in mesh.vertices)
        {
            Debug.Assert(v.attributes.ContainsKey("test"), "vertex has test attribute");
            var test = v.attributes["test"] as FloatAttributeValue;
            Debug.Assert(test != null, "vertex test attribute has float value");
            var testAsInt = v.attributes["test"] as IntAttributeValue;
            Debug.Assert(testAsInt == null, "vertex test attribute has no int value");
            Debug.Assert(test.data.Length == 3, "vertex test attribute has 3 dimensions");
            Debug.Assert(test.data[0] == 0 && test.data[1] == 0 && test.data[2] == 0, "vertex test attribute has value (0, 0, 0)");

            Debug.Assert(v.attributes.ContainsKey("other"), "vertex has other attribute");
            var other = v.attributes["other"] as IntAttributeValue;
            Debug.Assert(other.data.Length == 1, "vertex other attribute has 1 dimension");
            Debug.Assert(other.data[0] == 42, "vertex other attribute has value 42");
        }

        foreach (var e in mesh.edges)
        {
            Debug.Assert(e.attributes.ContainsKey("edgetest"), "edge has test attribute");
            var edgetest = e.attributes["edgetest"] as FloatAttributeValue;
            Debug.Assert(edgetest != null, "edge test attribute has float value");
            var edgetestAsInt = e.attributes["edgetest"] as IntAttributeValue;
            Debug.Assert(edgetestAsInt == null, "edge test attribute has no int value");
            Debug.Assert(edgetest.data.Length == 2, "edge test attribute has 2 dimensions");
            Debug.Assert(edgetest.data[0] == 0 && edgetest.data[1] == 0, "edge test attribute has value (0, 0)");
        }

        foreach (var f in mesh.faces)
        {
            Debug.Assert(f.attributes.ContainsKey("facetest"), "face has test attribute");
            var facetest = f.attributes["facetest"] as IntAttributeValue;
            Debug.Assert(facetest != null, "face test attribute has int value");
            var facetestAsFloat = f.attributes["facetest"] as FloatAttributeValue;
            Debug.Assert(facetestAsFloat == null, "face test attribute has no float value");
            Debug.Assert(facetest.data.Length == 1, "face test attribute has 1 dimensions");
            Debug.Assert(facetest.data[0] == 0, "face test attribute has value (0)");
        }

        {
            var other1 = v1.attributes["other"] as IntAttributeValue;
            var other2 = v2.attributes["other"] as IntAttributeValue;
            other1.data[0] = 43;
            Debug.Assert(other2.data[0] == 42, "default vertex attribute values are independent");
        }

        {
            var v4 = new Vertex(new Vector3(0, 0, 0))
            {
                attributes = new Dictionary <string, AttributeValue>
                {
                    ["other"] = new FloatAttributeValue {
                        data = new float[] { 1, 2, 3 }
                    }
                }
            };
            Debug.Log("Test expects warning:");
            Debug.Assert(mesh.AddVertex(v4) == v4, "add point is conservative");
            Debug.Assert(v4.attributes.ContainsKey("test"), "new vertex has test attribute");
            var test = v4.attributes["test"] as FloatAttributeValue;
            Debug.Assert(test.data[0] == 0 && test.data[1] == 0 && test.data[2] == 0, "new vertex test attribute has value (0, 0, 0)");
            var other = v4.attributes["other"] as IntAttributeValue;
            Debug.Assert(other != null, "new vertex other attribute is an int");
            Debug.Assert(other.data[0] == 42, "new vertex other attribute ignored non int provided value");
        }

        Debug.Log("TestBMesh TestAttributes passed.");

        return(true);
    }