Modifying tessellation within a Zero Touch node

I’m working on a ZeroTouch node library which generates custom objects. Those objects have their own Tessellate method implemented, where I usually draw the polygons and line previews myself. So far so good.

In a recent example I wanted to reuse the existing .Tessellate() method which any Dynamo geometry type supports (in this case it’s a Surface). That also works, the preview shows fine. What I would like to do however is to change the color of the output to a custom one.

This is the method at the moment:

    [R.IsVisibleInDynamoLibrary(false)]
    public void Tessellate(I.IRenderPackage package, I.TessellationParameters parameters)
    {
        Color col = new Color();
        parameters.Tolerance = 0.1;
        parameters.MaxTessellationDivisions = 8;
        this.Patch().Tessellate(package, parameters);

        for (int i = 0; i < package.MeshVertexCount; i++)
        {
            package.AddTriangleVertexColor(col.R, col.G, col.B, col.A);
        }

        if (parameters.ShowEdges)
        {
            this.PerimeterCurve().Tessellate(package, parameters);
        }
    }

However, it provides random results, as by calling package.AddTriangleVertexColor() I indeed only append color definitions instead of replacing the ones already in there, which were created by calling .Tessellate(package, parameters) earlier.

How can I completely replace the color array?

(note that in the code example, Color is a custom class, and PerimeterCurve() and Patch() are custom functions. They should be irrelevant here, they just provide a Surface and a Curve, and the Color is a dictionary providing the bytes)

Here is a screenshot of the current result. The red ones are what I want to achieve. A shows a grey one (which is still the original color I tried to replace) and B shows sub-elements where both previews are still rendered.

debugging the node shows that there are exactly twice as many vertex colors than vertices:

vertexCount

If you then look into the Mesh field, you can see the colors have “just” been appended:

How can I replace the other colors?

never mind, just found out by looking at Dynamo/GeometryColor.cs at master · DynamoDS/Dynamo · GitHub

Also figured that I have to use a new instance of parameters, otherwise I unintentionally modify the Defaults of Dynamo. Woking code now looks like this:

    [R.IsVisibleInDynamoLibrary(false)]
    public void Tessellate(I.IRenderPackage package, I.TessellationParameters parameters)
    {
        I.TessellationParameters tparams = new I.TessellationParameters
        {
            MaxTessellationDivisions = 12,
            Tolerance = 0.05,
            ShowEdges = parameters.ShowEdges
        };

        Color col = new Color();
        this.Patch().Tessellate(package, tparams);
        package.ApplyMeshVertexColors(CreateColorByteArrayOfSize(package.MeshVertexCount, col.R, col.G, col.B, col.A));

        if (parameters.ShowEdges)
        {
            this.PerimeterCurve().Tessellate(package, tparams);
        }
    }

    // copied from https://github.com/DynamoDS/Dynamo/blob/master/src/Libraries/CoreNodes/GeometryColor.cs
    private static byte[] CreateColorByteArrayOfSize(int size, byte red, byte green, byte blue, byte alpha)
    {
        var arr = new byte[size * 4];
        for (var i = 0; i < arr.Count(); i += 4)
        {
            arr[i] = red;
            arr[i + 1] = green;
            arr[i + 2] = blue;
            arr[i + 3] = alpha;
        }
        return arr;
    }
2 Likes