Ambiera Forum

Discussions, Help and Support.

Create a new account, Search, or Login:
Name: Password:
Ambiera Forum > CopperCube > Help with CopperCube
Vertex color by material slot
Author Text

Guest
Guest
Quote
2022-07-01 19:13:22

So I'm interested in vertex color and procedural textures for my future CC project. Graphics like the original Playstation. That said, I've been making some tools to work with various free CC0 low poly models I've found. I'm really a big fan of these modelers who give out these great sets for free, and I want to make using them in CC more enjoyable. I have quite a few ideas to chase at the moment, but recently Just_In_Case put my "Random Vertex Color" editor plugin on his great website. This new editor plugin takes in the number of materials assigned to a model and allows the user to set a vertex color per material slot. Here is the code:


// CopperCube editor plugin that sets a vertex color per material

function vertexColorByMaterial() {
var meshnode = editorGetSelectedSceneNode();
var bufferCount = ccbGetSceneNodeMeshBufferCount(meshnode);

alert("Selected node has " + bufferCount + " material(s).");

if (bufferCount == 0) alert("The selected node has no 3D geometry.");
else {
for (var i = 0; i < bufferCount; ++i) {
var vertexcount = ccbGetMeshBufferVertexCount(meshnode, i);

var rgb = prompt(
"RGB" + "[" + (i + 1) + "/" + bufferCount + "]: ",
"64,64,64"
);

var rgbStr = rgb.replace(" ", "");
var rgbArr = rgbStr.split(",");

var r = parseInt(rgbArr[0]);

if (r < 0) {
r = 0;
}
if (r > 255) {
r = 255;
}

var g = parseInt(rgbArr[1]);

if (g < 0) {
g = 0;
}
if (g > 255) {
g = 255;
}

var b = parseInt(rgbArr[2]);

if (b < 0) {
b = 0;
}
if (b > 255) {
b = 255;
}

for (var v = 0; v < vertexcount; ++v) {
function rgbToHex(r, g, b) {
return (
"0x" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1)
);
}

var color = rgbToHex(parseInt(r), parseInt(g), parseInt(b));

ccbSetMeshBufferVertexColor(
meshnode,
i,
v,
parseInt(Number(color), 10)
);
}
}
}
}

editorRegisterMenuEntry(
"vertexColorByMaterial()",
"Apply vertex color per material\tCtrl+Shift+L"
);


I give this code to the CC community to do with what they want, free of charge, for both non-commercial and commercial purposes. Consider it CC0. No credit needed.

Simply copy and paste the code into a text file, save it as "vertexColorByMaterial.js", then place it in your CC plugins folder in your CC documents folder.

The key combo for this plugin is "Ctrl+Shift+L" but can be changed to anything you want by simply changing this line of code:


editorRegisterMenuEntry(
"vertexColorByMaterial()",
"Apply vertex color per material\tCtrl+Shift+B"
);


Note: In the example code above, I have changed "Ctrl+Shift+L" to "Ctrl+Shift+B".

I hope someone finds this code useful. Cheers.


Guest
Guest
Quote
2022-07-01 20:01:04

Oops! I meant to put this in the Programming and Scripting section of the forums. My apologies.


Aiming_bullets
Guest
Quote
2022-07-01 22:11:18

I am already doing something like that with Justin's case shader, that make use of vertex color for specific or all material / textures.


Guest
Guest
Quote
2022-07-01 22:15:13

That's great, @Aiming_bullets. I've not gotten around to messing with shader code yet, but will eventually. Good luck with your code.


Guest
Guest
Quote
2022-07-02 02:20:15

Noticed a minor error. Here's the updated code:


// CopperCube editor plugin that sets a vertex color per material

function vertexColorByMaterial() {
var meshnode = editorGetSelectedSceneNode();
var bufferCount = ccbGetSceneNodeMeshBufferCount(meshnode);

alert("Selected node has " + bufferCount + " material(s).");

if (bufferCount == 0) alert("The selected node has no 3D geometry.");
else {
for (var i = 0; i < bufferCount; ++i) {
var vertexcount = ccbGetMeshBufferVertexCount(meshnode, i);

var rgb = prompt(
"RGB" + "[" + (i + 1) + "/" + bufferCount + "]: ",
"64,64,64"
);

if (rgb) {
var rgbStr = rgb.replace(" ", "");
var rgbArr = rgbStr.split(",");

var r = parseInt(rgbArr[0]);

if (r < 0) {
r = 0;
}
if (r > 255) {
r = 255;
}

var g = parseInt(rgbArr[1]);

if (g < 0) {
g = 0;
}
if (g > 255) {
g = 255;
}

var b = parseInt(rgbArr[2]);

if (b < 0) {
b = 0;
}
if (b > 255) {
b = 255;
}

for (var v = 0; v < vertexcount; ++v) {
function rgbToHex(r, g, b) {
return (
"0x" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1)
);
}

var color = rgbToHex(parseInt(r), parseInt(g), parseInt(b));

ccbSetMeshBufferVertexColor(
meshnode,
i,
v,
parseInt(Number(color), 10)
);
}

} else {
// Do nothing
}
}
}
}

editorRegisterMenuEntry(
"vertexColorByMaterial()",
"Apply vertex color per material\tCtrl+Shift+L"
);



blackwater
Registered User
Quote
2022-07-02 11:43:30

How nice, thank you!
I've wished for this to be possible - as a workaround i've instead applied single color textures for each material slot, which is totally fine, but this would be even better.

Tested the script and it works well in all it's simpleness. If it could be extended, or if @niko, could implement this somehow in the editor, i would further wish for the possibilities to:

- Visually see the vertex color in the material slots and preferably also pick a colour from a chart, rather than type in RGB-numbers.
- Also an icon/menu-item for using it, in the editor


Guest
Guest
Quote
2022-07-02 12:57:32

@blackwater

Before I wrote the code above, I wrote this code to display the RGB values to me since CC doesn't do that. This code is also CC0 and given freely to the community. I may return to this code in the future and make the RGB values display all on one menu, but it works for the time being. Thanks for your comment, and you're welcome.


// CopperCube editor plugin that displays the RGB values of a mesh's vertices

function getVertexColorRGB() {
var meshnode = editorGetSelectedSceneNode();
var bufferCount = ccbGetSceneNodeMeshBufferCount(meshnode);
var colorArray = [];

if (bufferCount == 0) alert("The selected node has no 3D geometry.");
else {
for (var i = 0; i < bufferCount; ++i) {
var vertexcount = ccbGetMeshBufferVertexCount(meshnode, i);
for (var v = 0; v < vertexcount; ++v) {
colorArray.push(ccbGetMeshBufferVertexColor(meshnode, i, v));
}
}

function onlyUnique(value, index, self) {
return self.indexOf(value) == index;
}

var unique = colorArray.filter(onlyUnique);

alert("Selected node has " + unique.length + " vertex color(s).");

function rgb(value) {
var r = (value & 0xff0000) >> 16;
var g = (value & 0x00ff00) >> 8;
var b = value & 0x0000ff;

return r + ", " + g + ", " + b;
}

for (let c = 0; c < unique.length; c++) {
var msg = "RGB[" + (c + 1) + "/" + unique.length + "]: " + rgb(unique[c]);

if (confirm(msg)) {
// Do nothing
} else {
break;
}
}
}
}

editorRegisterMenuEntry(
"getVertexColorRGB()",
"Get vertex color RGB(s)\tCtrl+Shift+P"
);



Robo
Guest
Quote
2022-07-03 07:49:37

Cheers 'Guest' - some nice code...

I didnt know you could replace text using rgb.replace(" ", ""); - good to know...


Guest
Guest
Quote
2022-07-03 23:16:16

Thanks for the comment, @Robo. Much appreciated.


just_in_case
Moderator
Quote
2022-07-04 07:08:16

Nice extension @guest, @aiming_bullets my shader applier per pixel color to the object.

@Guest I recommend you to check out Shaders as well, it is only adding one line of code to the sample shader available in the documentation. just multiply the output with the desired color and you will get the same effect.

But I am glad you are providing alternate solutions to that, as shaders don't work on all the platforms, so working with polygon editing might be a ++ in such cases.

Thanks for wonderful code of yours :)


Guest
Guest
Quote
2022-07-04 18:28:25

I'll definitely check out your shader, @just_in_case. Thanks for commenting.


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Internationa?" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© 2020 Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact