Setting Vertex Color Alpha By UV.Y Position

Setting Vertex Color Alpha By UV.Y Position

wyomingstateflag
Contributor Contributor
344 Views
6 Replies
Message 1 of 7

Setting Vertex Color Alpha By UV.Y Position

wyomingstateflag
Contributor
Contributor

Hi, I have a pseudo-code version of the function I'm trying to write but I can't quite figure out how to get both the UV and vertex colour from the same vertex. Can anyone help me out with this?

 
fn UVtoVertColour obj mapChannel = (
    for i = 1 to (polyop.getNumMapVerts obj 0) do 
    (
        vCol = polyop.getMapVert obj 0 i
        uv = polyop.getUV obj mapChannel i --NOT A REAL FUNCTION
        vColAlpha = uv.y
        if vColAlpha < 0 then vColAlpha = 0
        if vColAlpha > 1 then vColAlpha = 1
        polyop.setVertColor obj -2 #{i} (color vColAlpha 0 0) --https://forums.autodesk.com/t5/3ds-max-programming-forum/get-amp-set-vertex-alpha/td-p/4020406
    )
)
0 Likes
Accepted solutions (2)
345 Views
6 Replies
Replies (6)
Message 2 of 7

MartinBeh
Advisor
Advisor
Accepted solution

This page might help, especially the section "Finding the corresponding vertices".

Generally: UVs, vertex colors and other channels are linked to the 3D mesh through their faces only, so you will need to do a bit of index shuffling in order to get UV/VC given a mesh vertex number.

Martin B   EESignature
→ please 'Like' posts that are helpful; if a post answers your question please click the "Accept Solution" button.
Message 3 of 7

dmitriy.shpilevoy
Collaborator
Collaborator

Vertex color IS UV.

UV channel 0 is used to store vertex color.

Message 4 of 7

wyomingstateflag
Contributor
Contributor

Thanks for the info am I understanding correctly that UV channel -2 is the Alpha of the vertex color? This is honestly very confusing, it feel's like a spiderweb weaving in and out every which way and back onto itself

https://forums.autodesk.com/t5/3ds-max-programming-forum/get-amp-set-vertex-alpha/td-p/4020406

 

0 Likes
Message 5 of 7

dmitriy.shpilevoy
Collaborator
Collaborator

Yep, -2 is alpha.  

Also keep an eye for gamma correction if you work with vertex color as with color instead of manipulating UVs directly. It gave me a lot of headache.

Message 6 of 7

wyomingstateflag
Contributor
Contributor
Accepted solution

Thanks for the info shared in this thread 🙂
For anyone reading this in future, this was my solution which was mostly taken and adapted from this thread: https://forums.autodesk.com/t5/3ds-max-programming-forum/error-effect-from-uv-to-vertexcolor/td-p/11... 

fn UVtoVertColourAlpha obj mapchannel = (
    polyop.setMapSupport obj -2 true

    visited = #{}
    visited.count = obj.numVerts
    
    for f = 1 to obj.numFaces do (
        uvVertsFace = polyop.getMapFace obj mapchannel f
        colVertsFace = polyop.getMapFace obj -2 f

        --Loop through all vertices in this face
        for v = 1 to colVertsFace.count where not visited[colVertsFace[v]] do (
            uv = polyop.getMapVert obj mapchannel uvVertsFace[v] --Get UV coord of our vert
            vColAlpha = uv.y

            --Clamp value between 0 - 1
            if vColAlpha < 0 then vColAlpha = 0
            if vColAlpha > 1 then vColAlpha = 1

            polyop.setMapVert obj -2 colVertsFace[v] (color vColAlpha vColAlpha vColAlpha)
            visited[colVertsFace[v]] = true
        )
    )
)

 

Message 7 of 7

wyomingstateflag
Contributor
Contributor

To add to this, I've now put together a full rollout menu where you can selectively edit any of the RGBA channels, select UV channel, UV reference axis. Should be pretty useful for my use case so if anyone might find it useful here it is:
https://github.com/sam563/MaxScripts/blob/main/UVtoVertexColour.mcr 

wyomingstateflag_0-1748474622569.png