- Home /
Painting materials onto meshes at runtime?
So I'm creating a map editor for my game (kinda plays like an xcom dungeon crawler) And for indoor environments it currently works pretty well, but I've been trying to add outdoor environments to it, but it just looks way too blocky with the way I'm spawning the map and I was wondering if anyone knew how I could paint my materials onto the cubes I spawn for the map.
ignore the WoW tiles :P easy to grab for testing
I have different shaped tiles to fill out the edge of holes like in the image for stuff like water, lava, slime, tar or just plain darkness.
I wouldn't mind if I could just have two materials to paint with per cube (I know a lot of terrains engines only have 4 layers and I don't wanna do some mad blending between a textures, just a smooth transition between two would be enough)
So if anyone has any ideas on how I could achieve this that'd be awesome
Answer by Klarzahs · Oct 06, 2020 at 03:56 PM
Hi @Systemfre1
I'm unsure what you are aiming for.
Do you want to have tiles that form a smooth border between two adjacent tilesets, or do you want to "physically" paint your texture onto the tiles, for example like Splatoon, but with a mouse? The former is easy, the latter hard.
If you mean the "physically" painting one, you would have to raycast from your current mouse position, which can be done via Physics.Raycast, using this example as your ray origin/direction.
You then get a gameobject you collided with, as well as the target UV coordinates.
The next step would be t o create a shader that interpolates between X textures according to the uv value. You can set the UV value at the ray collision point from earlier to your currently selecting texture. The problem starts when you dont simply want to texture a single pixel, but a pixel area. The CPU is really bad at setting that, you would be better off using your GPU for that, so even more shaders. There are things like that at the AssetStore (eg this one), but they arent cheap.
Overall I'd advise you to create a split gameobject, that you can put in between 2 tilesets. You can either physically divide the rectangle into two triangles, or use a simple 2 texture shader for it.
Hope this helps, I'm happy to answer more questions or point you towards shader resources (they are fun!)
Ok awesome, I am hoping to achieve a terrain like painting effect like when you're painting materials on the terrain object in the editor.
I did think about having a Warcraft 3 like just adding some kind of border to neighbouring tiles but I'm not 100% sold on the look, still ends up quite blocky.
Ideally painting a pixel area sounds like what I'm after, using something akin to a vertex painter but ingame.
I don't suppose you know of any other resources where I could possible recreate my own version of that asset?
Any other information would be great!
A good start would be this tutorial, as it starts from CPU painting. This solution works best for few clicks/second and also on a well uv-mapped mesh.
If you want to have a terrain like painter, why not use your custom mesh as terrain base? There seems to be a way to do it, but i haven't tested it. This might be the easiest way
Yeah I was wondering if merging the mesh I created would make it easier and maybe that would work in this situation, but are you able to paint a terrain at runtime? Because that's the other thing I want to achieve But thanks for the links I'm going to look into it further