- Home /
How do I project coloured tiles onto a surface? (C#)
I am working in C# I have a big cube as the ground and a quad with a transparent grid pattern to show the user where each tile should be. I want to allow the user to select a colour and draw with this colour on the grid. Essentially recording the coloured grid in my scripts, but also changing the colour on the cube.
I can code a raycast to the point on the cube and calculate where exactly it hit just fine. But how do I draw coloured tiles onto my cube? I can change it to a quad or whatever that's fine but I don't know how to show the user's tiles they have coloured. I tried this with a separate quad for each tile and then changing the colour of the quad but this is super resource heavy.
The fps goes from like 2000 to 200 with a 60x60 grid of quads as individual objects.
Create a mesh via script, with each square consisting of 4 certices, and no shared vertices between squares. Given a set width and length of the mesh, you can easily find out which square the player is pointing at, and then either change the uvs or use a shader that supports vertex colors, and change the color of the vertices of that square.
Thanks for your comment. It drove me towards this tutorial: https://www.youtube.com/watch?v=z1r7VjgufJ8 but I am unsure how to create a mesh with squares ins$$anonymous$$d of triangles. I have experience with unity but never created a mesh from a script. Do I create a mesh with triangles then work my code to identify the square it takes up or is there a specific method I am missing?
I think I am completely missing the concept here. It looks like I need to apply a mesh to my quad. I need to make it in a script. However when I google "unity grid mesh on quad" or similar searches I can't find anything like what I am trying to do. Any help in the right direction would be greatly appreciated.
Look for creating a mesh via script, there are tons of examples for this, including the User $$anonymous$$anual for the $$anonymous$$esh class.
You don't have to get teh triangle index, you can just get teh hit.point of your raycast, round it down to the whole coordinate, and then go from there since you know how large each tile is.
Thanks for your reply. I have been able to create a mesh of triangles but have not been able to colour them. According to other sources I need to work the triangles into quads by combining them, however I have been unable to find any resources on how to accomplish this. I applied colours to the vertices however as they aren't in quads the colours all merged together.
Answer by ShadyProductions · Jun 26, 2018 at 04:02 PM
What you need is a voxel grid.
This tutorial extends all the way to drawing on the grid itself in the later parts.
http://studentgamedev.blogspot.com/2013/08/unity-voxel-tutorial-part-1-generating.html
Answer by Domo23000 · Jun 26, 2018 at 05:45 AM
The reason your fps goes down so low is because each quads have more faces than you would need. You only need 2 faces per tile, so use a plane instead of the quad for the blue background. There are a few ways to achieve what you are trying, but you could try instantiating planes wherever the player selects on top of the background.
I tried this and painted every single tile. The frame rate drops from about 2000 to 80 with nothing else in the game. The 60x60 grid makes 3,600 planes by doing it this way. It sounds like the other comment that suggests adding and colouring a mesh will be the best option so far. I am unsure how to accomplish this though.