How to Randomly assign specified colors from Styles to faces & edges ?

Next
 From:  ed (EDDYF)
10222.1 
How to Randomly assign specified colors from Styles to faces & edges?

I found the following script that comes close to what I need.

Randomly assign colors from Styles to faces & edges:

http://moi3d.com/forum/lmessages.php?webtag=MOI&msg=3876.10

script: /* Assign random styles to faces and edges */ var styles = moi.geometryDatabase.getObjectStyles(); var breps = moi.geometryDatabase.getObjects().getBReps(); for ( var i = 0; i < breps.length; ++i ) { var brep = breps.item(i); var edges = brep.getEdges(); for ( var j = 0; j < edges.length; ++j ) { edges.item(j).styleIndex = Math.floor(Math.random() * styles.length); } var faces = brep.getFaces(); for ( var j = 0; j < faces.length; ++j ) { faces.item(j).styleIndex = Math.floor(Math.random() * styles.length); } }

But is there a way to specify just a subset of colors from the Styles?

This is because some Styles may already be assigned to other objects in the scene and need to remain unique.

A use case, for example, would be a wooden ship where perhaps a hundred wood planks are used. To give a more natural look in the render program, say 5 unique wood shades & textures are pre-defined in the render program. These 5 wood variations get assigned based on 5 pre-defined colors assigned randomly throughout all the planks in MoI.

Taking it one step further, making a more universal script, how about if the user “seeds” the model with perhaps up to 20 unique Style colors before running the script? So you assign Style Red to one plank, Blue to another plank, and Green to a third plank. The remaining 97 planks are the default Style Black. The script sees 3 unique colors in the Selected Faces, and then randomly assigns those 3 colors to the remaining selected 97 faces (planks).

Other use cases come to mind for adding random pre-defined color / texture variations (subtle or not subtle) in the render program: Concrete blocks. Roof tiles. Wood floors. Windows on a skyscraper (yellow = lights on, blue=dark).

Ed Ferguson
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Frenchy Pilou (PILOU)
10222.2 
This one make a gradient with defined colors!

Just change in the script's text values that you want...

script: /* Gradient Styles */ var red = 12, green = 15, blue = 20; for ( var i = 1; i <= 100; ++i ) { var style = moi.geometryDatabase.addStyle(); style.name = 'Gradient ' + i; style.color = (Math.abs(red%256)<<16) | (Math.abs(green%256)<<8) | Math.abs(blue%256); red += 2; green += 3; blue -= 7; }





---
Pilou
Is beautiful that please without concept!
My Moi French Site My Gallery My MagicaVoxel Gallery
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  ed (EDDYF)
10222.3 In reply to 10222.2 
Pilou -

I'm not looking for a gradient. I'm looking for a specified, limited number of colors to be randomly distributed among a group of selected faces.

Ed Ferguson
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Frenchy Pilou (PILOU)
10222.4 In reply to 10222.3 
Sure but inside this one you can see how select your colors...
so maybe a mix is possible...
---
Pilou
Is beautiful that please without concept!
My Moi French Site My Gallery My MagicaVoxel Gallery
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  bemfarmer
10222.5 In reply to 10222.1 
Hi Ed,
It should be possible to select a random style from a more or less sequential set of styleIndex values.
In the ModulationTest2 script that Michael helped me with several years ago, the function AssignColors was used. It would have to be randomized over the desired range of style indexes. Generate a random number between 0 and 6, and add it to the beginning of your selected range of styles....
Apply and repeat...

- Brian
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Michael Gibson
10222.6 In reply to 10222.1 
Hi Ed,

re:
> But is there a way to specify just a subset of colors from the Styles?

So the code that generates the style index is this part here (it's in 2 places in the script one for where it sets up edges and another faces):

styleIndex = Math.floor(Math.random() * styles.length);

If you wanted it to choose only from the first 10 styles, you could do it like this instead:

styleIndex = Math.floor(Math.random() * 10);

If you wanted it to choose from the last 10 styles, you could do it like this:

styleIndex = styles.length - 1 - Math.floor(Math.random() * 10);

- Michael
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
 From:  ed (EDDYF)
10222.7 In reply to 10222.6 
Thanks Michael!

Tested here using first 6 default colors in Styles list.

script: /* Assign random styles to faces and edges */ var styles = moi.geometryDatabase.getObjectStyles(); var breps = moi.geometryDatabase.getObjects().getBReps(); for ( var i = 0; i < breps.length; ++i )
{ var brep = breps.item(i); var edges = brep.getEdges(); for ( var j = 0; j < edges.length; ++j ) { edges.item(j).styleIndex = Math.floor(Math.random() * 6); } var faces = brep.getFaces();
for ( var j = 0; j < faces.length; ++j ) { faces.item(j).styleIndex = Math.floor(Math.random() * 6); } }




Ed Ferguson
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged
 

Reply to All Reply to All