Changing Textures and Creating Surfaces at Runtime

Hi, I’m completely new to Unity so I apologize if the following is trivial or uses poor jargon.

I’m working on a research project for which I would like to take a cucumber prefab and, at runtime, be able to replace portions of it’s skin texture with a cucumber-flesh color/texture to simulate peeling. In addition, on these peeled sections I would like to generate surfaces that will have a transparent material (needed for labelling in the perception module).

Does anyone have advice or resources on how I could do this? I really appreciate any help/tips, thanks!

Do you know how to make a simple 2 triangle mesh already and are looking for the mechanisms to create mesh through unity?

You will need to -

Create a Mesh, a small square or a single triangle maybe would suffice.
MYMESH = new Mesh();

MYMESH.vertices, MYMESH.triangles. MYMESH.normals, MYMESH.uvs
for all of your mesh required data.

you could of course instantiate prefabs of planes or triangles Unity - Scripting API: Object.Instantiate (unity3d.com) and then use GetComponent().material to change the color, and texture and what not.

Thanks for the quick reply! So I didn’t know how to make a mesh but if I’m following correctly I can use Object.Instantiate to make a duplicate of an object (the cucumber) and then change properties of this clone to create regions of peeled areas.

Do you have any advice on how to generate where the peeled regions would be? I’d like to randomly generate these areas. I assume I could create a mask and then adjust the color of the cloned object accordingly. I don’t know how to do that though, is there a resource you could point me to?

If knife contacted, you could begin to create a mesh, and attach two of those vertices to the knife. as the knife drags it stretches the vertices make a long peel. A super peel out if you will. But that would be down to your preferences. And the quality level you are aiming for. If the project is only about peeling a cucumber, go all the way, make the mesh from vert, assemble the triangles, drag the knife and slice the cumber.

i suppose another way is to have a 3D model prepared, and delete or release its triangles when knife is contacted, turn the triangle into a mesh of itself, and release. This would be flaking. But it could reveal a 3D modelled inner core. By deleting layers like that of an onion. TO reveal more layer and structure. While the peeling itself to leave a curled peel behind as the most advanced i could possibly theorize for you on the subject.

So actually I’m not using a knife to peel, I’m trying to generate a synthetic dataset to train ML models. Any solution that doesn’t involve the action of peeling but produces a peeled result it what I’m looking for. Any ideas? Again, I appreciate the help!

The idea of flaking is interesting. Could I modify a prefab I got on the unity asset store to do this? Like could I add a core to an existing asset and program the the removal of the top surface?

I think this problem is more about editing the pixels of a texture (or laying another texture over another) than editing a mesh. I’m going to page @Kurt-Dekker because I know he has a project he can share that would demonstrate how this is done.

You’re absolutely correct, in the end I only care about the final pixels that are rendered. Thank you!

I suppose one could create a web of triangles inside an existing prefab, I have done this before to create a crystal ball effect for a transparent material. this web of triangles can give voxel-like structure to the interior if bashed around or peeled. Normally people would use a particle effect, and have it all happen so quickly you don’t need to physically remove a vertice to create a mesh. But it is possible.

Making a triangle is relatively simple
Three points 0and1, 0and2, 1and2
Three vertices
X0Y0, X0,Y1, X1,Y1

This can be colored already. But for texturing it requires UV’s so you’ll have to approximate a bounds for each surface created and produce UVs coordinates for an image.

Thanks Spiney… I actually don’t have a ready-made solution to this. :slight_smile:

Hey OP you might benefit from hand-animating what you want to happen in Blender, eg., peel up and make fresh geometry, change the texture of the interior, peel it up, etc.

The process of doing this would serve to help you understand the problem and break it into geometry synthesis and/or texture pixel painting.

Oh, I figured your scratch-ticket project was similar in principle to this, ergo, editing a texture at run time.

1 Like

That does sound very similar to what I’m looking for :slight_smile:

Oh! Well then, please enjoy. Links to full project in video comments…

https://www.youtube.com/watch?v=N_Ia9_LpV2M

here:

https://discussions.unity.com/t/845410/4

1 Like