Home Technical Talk

Help exporting vertex colours in MaxScript (achieving one to one correspondence)

vertex
Offline / Send Message
Raffles vertex
Hello,

I recently got vertex colour shading working in my mobile engine as a cheap and easy alternative to lightmapping etc, especially for large outdoor areas.

The only slight problem I've encountered is exporting the data from Max. The amount of vertex colours that are generated when baking vertex lighting data in max is often 3 or 4 times more than the number of vertices. To make things easier and faster, I'd like to have just one colour value per vertex.

In the MaxScript help it even says "Many game engines still support just one color vertex per mesh vertex. Using MAXScript, it is possible to develop a utility to reduce the numbers and achieve a one-to-one correspondence" but doesn't give the slightest clue as to how :)

My MaxScript knowledge isn't great and I can't figure out how to do this.

Do any of you have experience with vertex colours and happen to know how to do this? I'm kind of amazed it isn't an option in the Assign Vertex Colors utility actually, given they even acknowledge a lot of engines only support a single colour per vertex.

Cheers.

Replies

  • Swordslayer
    Options
    Offline / Send Message
    Swordslayer interpolator
    Try this:

    fn averageColor colors =<br>(<br>	local sum = Point3 0 0 0<br>	for c in colors do sum += c<br>	sum / colors.count<br>)<br><br>fn unifyVC obj =<br>(<br>	local meshCopy = copy obj.mesh<br>	local vertexColors = for v = 1 to meshCopy.numVerts collect #()<br><br>	for f = 1 to meshCopy.numFaces do<br>	(<br>		local vertIndices = getFace meshCopy f<br>		local vertColorIndices = getVCFace meshCopy f<br><br>		for v = 1 to 3 do append vertexColors[vertIndices[v]] (getVertColor meshCopy vertColorIndices[v])<br>	)<br><br>	setNumCPVVerts meshCopy meshCopy.numVerts<br>	defaultVCFaces meshCopy<br>	for v = 1 to vertexColors.count do setVertColor meshCopy v (averageColor vertexColors[v])<br><br>	local objCopy = convertTo (copy obj isSelected:true showVertexColors:on) Mesh<br>	objCopy.mesh = meshCopy<br><br>	delete meshCopy<br>	return objCopy<br>)<br><br>if selection.count > 0 and canConvertTo selection[1] Mesh do unifyVC selection[1]

    The last line applies the function to currently selected object, you can just as well loop all the geometry in scene instead or loop multiple selected objects instead.


  • Raffles
    Options
    Offline / Send Message
    Raffles vertex
    Excellent, thanks a lot for that @Swordslayer, looks perfect. Much appreciated. I was planning on doing something similar for the importer engine side (averaging) but this is a much better option.

    Cheers.
  • Raffles
    Options
    Offline / Send Message
    Raffles vertex
    Great job @Swordslayer, I tried the script out and it works perfectly, often making shadows look better actually, smoothing out triangle edges. I'll modify it to not make a mesh copy, as I make a copy anyway as I loop through everything before exporting.

    I don't suppose you have or know of a script to dynamically split/detach a mesh into smaller chunks (from a uniform grid for example) do you? I've written something that does this engine side, but it's slow, and again it would be much better if I could do it before exporting.
  • Swordslayer
    Options
    Offline / Send Message
    Swordslayer interpolator
    Not dynamically, but it could be a start: Incremental Grid Slice
    Let me know if you had something else in mind.
  • Raffles
    Options
    Offline / Send Message
    Raffles vertex
    You know what, that's pretty much exactly what I want, thanks again for the great work :)
  • cptSwing
    Options
    Offline / Send Message
    cptSwing polycounter lvl 11
    Raffles said:

    I recently got vertex colour shading working in my mobile engine as a cheap and easy alternative to lightmapping etc, especially for large outdoor areas.

    The only slight problem I've encountered is exporting the data from Max. The amount of vertex colours that are generated when baking vertex lighting data in max is often 3 or 4 times more than the number of vertices. To make things easier and faster, I'd like to have just one colour value per vertex.
    How do these extra colors show up? You bake lighting to the vertices via the VertexPaint Radiosity settings; and then the result is added to the mesh as several datasets in Channel Info? (Just interested because this seems bizarre to me and I'm not sitting in front Max this week ;) )
  • Raffles
    Options
    Offline / Send Message
    Raffles vertex
    cptSwing said:
    Raffles said:

    I recently got vertex colour shading working in my mobile engine as a cheap and easy alternative to lightmapping etc, especially for large outdoor areas.

    The only slight problem I've encountered is exporting the data from Max. The amount of vertex colours that are generated when baking vertex lighting data in max is often 3 or 4 times more than the number of vertices. To make things easier and faster, I'd like to have just one colour value per vertex.
    How do these extra colors show up? You bake lighting to the vertices via the VertexPaint Radiosity settings; and then the result is added to the mesh as several datasets in Channel Info? (Just interested because this seems bizarre to me and I'm not sitting in front Max this week ;) )
    Yep that's right, and you have to choose radiosity otherwise you only get shadows casted from other objects with light tracer, and no AO/self shadowing.

    And strangely it doesn't show up by default, you have to go back to the modify panel and the Vertex Paint panel pops up. You then have to click one of the top two buttons "Vertex color display", unshaded and shaded.

    A little fiddly like most stuff in Max, but very easy and very fast once you know, much faster than lightmapping.

    FYI, the vertex colour channel is actually just channel 0, ie the one before default UVs ;)

    Thanks again to Swordslayer, I modified my exporter with the above script, and now Vertex Colours are showing up perfectly in my engine, and look exactly as they do in Max.
Sign In or Register to comment.