Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
WebGL + ccbGetSceneNodeMeshBufferCount(meshnode);

gree
Registered User
Quote
2022-11-11 21:04:10

Based on some example scripts from this forum I've created the following "on-click-random-color" JS code.

On a "click behaviour" it changes the vertex color to random RGB. This works fine as long its an *.exe publish.

Does anyone know how to convert it that it works with WebGL too? I know the "ccbGetSceneNodeMeshBufferCount();" does not work on WebGL; any workaround idea? Thanks.





var rndIntR = Math.floor(Math.random() * 255) + 1;
var rndIntG = Math.floor(Math.random() * 255) + 1;
var rndIntB = Math.floor(Math.random() * 255) + 1;
//print(rndInt);

var meshnode = ccbGetCurrentNode();
var bufferCount = ccbGetSceneNodeMeshBufferCount(meshnode);


for (var i = 0; i < bufferCount; ++i) {
var vertexcount = ccbGetMeshBufferVertexCount(meshnode, i);

var rgb = "0x" + Math.random().toString(16).slice(2, 8);

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

var r = rndIntR;

var g = rndIntG;

var b = rndIntB;

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
}
}

//var rndIntR = Math.floor(Math.random() * 255) + 1;
//var rndIntG = Math.floor(Math.random() * 255) + 1;
//var rndIntB = Math.floor(Math.random() * 255) + 1;
//print(rndInt);

//print("END");




just_in_case
Moderator
Quote
2022-11-12 07:41:07

Why don't you use a shader to do that, instead of coloring it through the geometry editing tools.

Shaders work for webGL, it is not hard to create an RGB color multiplying shader for WebGL.


gree
Registered User
Quote
2022-11-12 12:11:41

Great Tipp - Thanks just_in_case. I will look into WebGL shaders.
Is there a place for ccb shaders where i can get some examples to play with?


just_in_case
Moderator
Quote
2022-11-12 16:06:20

There is example in the CC documentation, also some of my shaders has webGL support. Like blend two textures together has support for webGL. Animate Texture Coord shader has support for webGL as well.
If you want you can go through them.
hope that helps :)


gree
Registered User
Quote
2022-11-12 17:29:50

Awesome - will do that later. thanks


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Internatio?al" (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© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact