- Home /
I want to add stripes at varying lengths on my object. How do I do that?
So let's say I have an object and I want to add stripes at varying lengths on my object. How can I do that?
I assume it has something to do with adding an extra layer of material or texture. How do I add another material/texture at a certain length on my object?
See picture below for what I'm trying to achieve.
Also I want to make the stripes I added selectable.
Answer by Owen-Reynolds · Dec 16, 2013 at 06:54 PM
A "95%" method might be to add colored cylinders (with capsule colliders?) sticking out just a little. (The idea being, 95% of what you thought you wanted is often good enough, and the last 5% will be a huge pain.)
Otherwise, remodel it with submeshes where the stripes should be. Each will give a material slot in Unity (once you have it, can easily test by hand-coloring in the Inspector.) For selection, I'd still go with an invisible sphere/capsule collider. Give it a tiny script with just int stripeNum;
.
Agreed, I gave the 5% answer below. Bit more work, but will avoid many $$anonymous$$or issues. Specifically, no misaligned cylinders intercolliding with mesh, no zfighting if the cylinders are too close to the mesh, and no baking the number and position of the cylinders into the mesh.
But whatever suits your needs most.
I have tried Owen Reynolds solution. However, when I want to select the tree and not the stripes, it becomes hard to do because sometimes unity won't let me select anything or it will select the stripes and not the tree. I can only select the tree on the ends of the objects and not behind strips.
How large are the colliders? And, if the stripes are children of the big tree, or vice-versa, ray cast needs to use, ummm, collision.collider
ins$$anonymous$$d of collision.transform
.
I have one more question, how do instantiate the cylinders? Let's say I just have the tree. Then when the user clicks on the tree, the tree is replaced with the tree with the cylinders ins$$anonymous$$d of a plain tree?
I was thinking hand-place the cylinders, and have them always be "on." The cylinders are the stripes.
If the number and position of stripes changes as the game plays, ouch. Could mark out a bezier curve using empties, for position/width. But the hard method with hand-made UVs and 1 material/stripe starts to look better.
Answer by OP_toss · Dec 16, 2013 at 07:06 PM
Yeah you've got the right idea.
Easiest solution would be to use multiple materials, then get the uv coord from the raycasthit, and thus which line was selected.
Now in more detail:
So to add a single stripe, make a texture with one horizontal line in it. Set the repeat to Clamp, and make sure the top and bottom edges are transparent.
To add a line to the mesh:
Find the Mesh Renderer and set the Materials size to 2.
Make a new transparent material and set the texture to your line. Add it below the default material. It should make a line over your mesh.
Adjust the Tiling to get the line how you like.
Repeat per line you need added.
Note: This requires proper UVs that are aligned along the lines you want. This also assumes you will need to add and remove lines often, and you want to determine exactly WHICH line was selected. If this is wrong reply and I'll adjust the answer.
In order to make them selectable:
On Mouse Down, perform a Collider.Raycast/Physics.Raycast from the mouse position along it's direction. If you hit the collider, continue.
Get the texcoord from the RaycastHit object. With a little math you should be able to get the pixel value from the uv coordinate. Remember the uv may be in relation to the first material, so it may be a bit off.
Check if the alpha of the pixel is above a certain threshold in order to determine if it is indeed a stripe. You'll know which line by which material uv offsets you're using.
I will try your method sometime this week and see if I run into any trouble. Seems interesting. Thanks!
Your answer
Follow this Question
Related Questions
Changing scrolling textures on a plane using transparency 1 Answer
A node in a childnode? 1 Answer
Textures are not tiling properly on my wall model! 3 Answers
Transparent Texture2D 2 Answers