ambiera logo

Ambiera Forum

Discussions, Help and Support.

folder icon Ambiera Forum > CopperCube > Help with CopperCube
forum topic indicator Random texture
person icon
eldeibit
Registered User
Quote
2021-05-18 15:36:27

Hello !
I know there is a code for a random scene. Is there a code to give random texture to an object?

person icon
Sven
Guest
Quote
2021-05-18 16:24:41

/*
<action jsname="action_RandomTexture" description="Random Texture">
<property name="TextureMe" type="scenenode" default="cubeMesh1" />
<property name="TakeTextureFromThis" type="scenenode" default="cubeMesh1" />
</action>
*/

action_RandomTexture = function()
{
};

action_RandomTexture.prototype.execute = function(currentNode)
{

var n = ccbGetSceneNodeMaterialCount(this.TakeTextureFromThis);
var rndT=Math.floor(Math.random() * n) ;
var t = ccbGetSceneNodeMaterialProperty(this.TakeTextureFromThis, rndT, "Texture1");
ccbSetSceneNodeMaterialProperty(this.TextureMe, 0, "Texture1", t);

print("the scene node has " + n + " materials");
print("random pic was " + rndT + " material");

}


I made it fast so it may not work properly but you get idea.

person icon
Newbee
Guest
Quote
2021-05-18 16:36:48

example here:
https://dbree.org/v/3d0ce9

person icon
eldeibit
Registered User
Quote
2021-05-18 19:50:32

Thanks guys, you guys are the best !! I just modified it to run every time the game starts, which was my goal. Thank you !!

person icon
veganpete
Registered User
Quote
2023-08-16 14:57:59

[SOLVED] - I made an object with 256 materials so I could use the original script, instead of trying to make it switch texture slots as well as materials.

How can the script be modified to actually pick a random texture, instead of a random material? ie: It's always selecting "Texture1" from a random material - instead of "Texture2", "Texture3" or "Texture 4".

I've been trying to get it to work but I'm stuck.

I've made an invisible "texture_node" with 64 materials and 4 textures per material slot - so I need it to randomly pick a material (1-64) and then a random texture (1-4) from the material slot.

Anyone willing to help with modifying this script please?

Thank you. emoji icon_devil

I'm trying something like this (it kinda works but it's not actually changing the texture, it says "Property Dees not exist"....

/*
<action jsname="action_RandomTexture" description="Random Texture">
<property name="TextureMe" type="scenenode" default="cubeMesh1" />
<property name="TakeTextureFromThis" type="scenenode" default="cubeMesh1" />
</action>
*/

action_RandomTexture = function()
{
};

action_RandomTexture.prototype.execute = function(currentNode)
{

var n = ccbGetSceneNodeMaterialCount(this.TakeTextureFromThis);
var rndMat=Math.floor(Math.random() * n) ;
var rndTex=Math.floor(Math.random() *4 + 1);
var m = ccbGetSceneNodeMaterialProperty(this.TakeTextureFromThis, rndMat, rndTex);
ccbSetSceneNodeMaterialProperty(this.TextureMe, 0, "Texture1", m);

print("Scene node has " + n + " materials." );
print("random texture was " + rndMat + ", " + rndTex);

}

person icon
Dieter
Guest
Quote
2023-08-21 09:44:29

Even tho it says SOLVED, still: wouldn't it be better to have 1 material and simply dynamically change the texture, rather than to have to switch materials. AFAIK material count impacts rendering speed.

Maybe something like: Do before first rendering:

texes=new Array();
for(i=0;i<10;i++)
{
texes[i] = ccbLoadTexture("mytexture"+ i +".jpg");
}

and in the action script:

ra=Math.floor(Math.random() * 9.99);
ccbSetSceneNodeMaterialProperty(node, 0, "Texture1", texes[ra]);


person icon
VP
Guest
Quote
2023-08-21 10:56:51

Thanks Dieter!

Yes, I don't want it to affect render time at all.

The node with all the materials (256 materials) is set to "not visible". I then copy the material from the invisible to the visible node.

The main reason for this is to embed the textures in the ccb/exe file, rather than have separate texture files.

Would that still impact render time, the materials node being "not visible"?

Thank you.

person icon
Dieter
Guest
Quote
2023-08-21 11:38:34

Hi Pete
No, it would not, if the visible node has only 1 material. Must be quite some clicking to get the 256 textures onto that node But if you really need the textures to be in the exe, IDK how. But you probably can read all the texture handles from the hidden node into an array preemptively, and then use them directly, without having to deal with particular materials anymore.
Another way to ease your work would be a program that makes a mesh for you that contains the 64x4 textures procedurally, saves it as B3D, so you can import it in CC.
I have some Blitz3D B3D mesh saving code, case you're interested.

person icon
Guest
Guest
Quote
2023-08-21 12:55:54

you can use Just_in_case 2D animation scripts to hold all the textures to a single object, and they will be all be embedded into the exe file.

person icon
VP
Guest
Quote
2023-08-21 12:56:47

Thank you Dieter. I'll keep that in mind for future reference(procedural mesh-building, interesting). Really glad it wont affect rendering if not visible, that would have trashed my project; I already got the material node working by making it manually - as you say, a lot of clicking to assign all the materials to individual mesh-triangles, then more clicking to apply all the textures to the correct material slots - lol.


Create reply:










 

  

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


icon_holyicon_cryicon_devilicon_lookicon_grinicon_kissicon_monkeyicon_hmpf
icon_sadicon_happyicon_smileicon_uhicon_blink   






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