- Home /
How can I blend a lot of textures on one mesh ?
Hello, I'm new ! :) And I have a question : how can I blend a lot of textures in one mesh ? When I search on google, it screams "Use a shader and vertice color !!!" but there is a big problem... The color of a vertex contains only 4 variables : R, G, B, A and if I want to fade for example 15 textures, I am lost and I don't know how to do that. So, have you a solution ? Thank you in advance ! :)
Answer by siaran · Mar 24, 2015 at 02:07 PM
Well, 2 ways to do this. The easy one is to use Photoshop or GIMP or some other image editing program to blend your textures into 1, then use that texture.
If you need to do it runtime - you will need to write a shader to do it. Yes, a shader color only has 4 variables, r,g,b,a. You don't need more than that - the output is always going to be just one color, namely the one that gets drawn.
As I repeatedly say on this forum, I am not really good at shaders, but what you'd need to do is something like
Shader "MyShader" {
Properties {
_MyTexture1 ("Texture1", 2D) = "white" { }
_MyTexture2 ("Texture2", 2D) = "white" { }
_MyTexture3 ("Texture3", 2D) = "white" { }
///and so on
}
SubShader {
#pragma surface surf Lambert
struct Input {
float2 uv_Tex1;
float2 uv_Tex2;
float2 uv_Tex3;
};
sampler2D _MyTexture1;
sampler2D _MyTexture2;
sampler2D _MyTexture3;
void surf (Input IN, inout SurfaceOutput o) {
//get colors of textures
float4 col1 = tex2D (_MainTex, IN.uv_Tex1).rgb;
float4 col2 = tex2D (_MainTex, IN.uv_Tex2).rgb;
float4 col3 = tex2D (_MainTex, IN.uv_Tex3).rgb;
//blend them somehow - here I just use average color
float finalR = col1.r/3 + col2.r/3 + col3.r/3;
float finalG = col1.g/3 + col2.g/3 + col3.g/3;
float finalB = col1.b/3 + col2.b/3 + col3.b/3;
float4 final;
final.r = finalR;
final.g = finalG;
final.b = finalB;
o.Albedo = final;
}
ENDCG
}
}
Don't expect that to actually work, I'm not good at shaders and the shaders I do write are always vertex and fragment shaders, I'm sure there is a lot of syntax in there that's incorrect. I think a surface shader should do for just blending textures though.
Thank you for the reply ! :) So, you say that "Yes, a shader color only has 4 variables, r,g,b,a. You don't need more than that - the output is always going to be just one color, namely the one that gets drawn." But I don't understand how can I blend more than 4 textures with only 4 variables. I've schematized the problem : http://image.noelshack.com/fichiers/2015/13/1427218019-schema.png 1) There are vertices, with there colors, for the exemple, each vertice have only one color (red, blue or green). They have not any other color. 2) So this is after the blend 3) And here I replace the green by the brick texture and I don't see any problem 4) And here I replace a random color (the color at the right of the picture's number) by another texture. Normally I should have just white and no texture because I have not set any vertice color with this specific color. But we can see that there is still a texture. And all the problem is here !
You should look at the Texture2D class (ReadPixel and SetPixel method) to perform your logic on images. It's easier to develop and debug a script than develop your custom shader. Shader will be usefull for you if performance become an issue.
White contains RGB values of 255,255,255 so when you check for te vertex color given by the vertex shader for red it will still blend it because it has a 255 (max value) for red therefore the texture is drawn this also applies to values between 0 and 255 it will still be drawn if you do not give the color channel a value of zero.
Like the answer above you can use your own values of color, paint them in 3ds max and blend the linked texture to it wit hthe shader in unity.
In my exampled I blended 3 textures by getting the average color of the 3, so apparently that's not what you want?
How do you want to blend the textures then, it's not clear to me. Something to do with the result of the previous blending operation?
Ohh.... I think I understand what happends, I'm going to explain what I've understood : You superpose textures directly on the mesh, a little like layers ! But me I speak about vertices. Imagine a triangle, like this : http://ricardocabello.com/files/blog/mrdoob_4pointgradient7.png each vertice has a color, and I speak about this color, not the color of texture ! And after, with a shader I replace the red color by the texture1, the green by the texture2, the blue by the texture3, and the alpha by the texture4. That's how I blend my texture on my mesh. And the problem is that I can't blend more than 5 textures with this method. (And I need to blend more because my game is a sandbox) So I hope that I am clear and I hope that I have understood Thank you in advance
Answer by KevinLongtime · Mar 24, 2015 at 03:43 PM
In Unity 5 you can blend textures with the standard shader.
Here's a great video going through the standard shader and more specifically what you want at 12:11 minute mark:
https://youtu.be/9DmRNlIJIp0?t=12m11s
However it's been a while since I've seen that vid but so I'm not completely sure about the 15 texture blend but you can atleast blend 2 sets.
Thank you for the reply ! Yes but the texture in the video not depends of vertices
Answer by GrichDesign · Nov 13, 2016 at 10:20 PM
@Esu, Kilian was onto something there. Use Vertex blending to apply colors that match a splat map. Then apply your textures through Triplanar projection so you don't have to worry about UVs as you dig.
Answer by timpih · May 12, 2017 at 09:59 AM
You guys seem to all talk about different things here so I'll add my few cents. The main question was about vertex blend, not splatmaps or vertex coloring.
I work with vertex blend/heighmaps in a custom engine, but I havent seen any good examples in unity yet. There might be good shader programmers that can make this for you or you do it in shader forge, amplify shader. There are a lot if tutorials out there. Look into vert blending with heightmaps as a go to solution. More work to setup, but worth it.
In general about vert blending. Based on the comments, I'm not sure how much vertex blending you guys have done, but there are performance reasons why vertex blend is still superior to splat maps and masks. Spatmaps are ok for custom areas, but waste memory. If you want realistic detail and want to keep it cheap you use a bunch of tileables, up to 4 textures/materials, each linked to separate rgba channels. Each vert channel acts like a white mask, similar to the rgba channels in photoshop. The colors are there as channel names only. Vertex color and vertex blend are totally separate things. Vertex color can be used to color the assets, bake cheap lighting, shadow, while same channels can also be used to blend between materials. If you need more than 4 textures, you need to create more materials where one texture is shared where the blend occurs.
For vertex blends, try to stay away from 4 texture since the more you add, the more expensive it runs. Rather use max 2-3, and create more materials that take over where the first material ends. As an example fir a 2 texture setup. mat1:grass+rock...mat2: grass+mud...mat3: mud+water. You set the rgba channels so grass is always R, mud always B...so when you paint, both materials can be blended and there is no seams where the material swap occurs. Multi material blends are usually done in maya or 3dsmax. Assigning the materials to the terrain polygons manually, then blending in maya or in unity. More work but efficient if you create large games. Unity paints the blends with plugins like ikari vertex painter. Stick to one material as a start to learn the technique before looking into multi material blends. That can get tricky :)
Another way the blends are done is with separate decals, or projection decals that get added on top. This is not recommended for mobile games since it adds to overdraw, but worth knowing about. With these setups, you use a 3 material vert/height blend setup at bottom, and add decals on top. As a 3rd pass, you add rock, grass meshes to make it look real. Expensive but works in next gen systems.
Substance painter is used for custom assets, or custom terrain, but keep in mind that every texture adds up. Memory, overdraw and drawcalls is a severe problem still. Always keep things simple. The fewer materials you use, the faster it will run.
Anyway, explaining the differences here and how we use it in the gaming industry. There seems to be a lot of confusion about vertex color, vertex blend, splatmaps, masks in general. Aim to keep things simple and cheap for terrain. It'll make things easier once changes occur. Memory and time issues is the main reason AAA games use a vert/heightmap solution still. Masks are great, but too memory consuming if every area needs to be custom.
/ Tim
Answer by Oris · Dec 07, 2018 at 07:28 AM
I have this shader that i think is what you want: https://oris-romero.itch.io/parallax-shader-unity
Your answer
Follow this Question
Related Questions
Splatmapping / blending between two textures in an atlas? 0 Answers
How do I blend multiple textures on a mesh? 1 Answer
Different prefabs with same mesh but different textures(materials). 1 Answer
can anybody help with script that takes webcam texture pixel information to deform mesh vertices? 0 Answers
Realtime mesh deformation or texturing 0 Answers