如何将单独的凹凸贴图添加到此splat贴图着色器(Unity3D)中的每个通道?
我在Unity3D中有一个用于splat贴图的着色器。这是一个弗兰肯斯坦怪兽,来自几个不同的怪兽,我发现我终于开始工作了。问题是它不能在纹理上进行凹凸贴图如何将单独的凹凸贴图添加到此splat贴图着色器(Unity3D)中的每个通道?,unity3d,shader,hlsl,vertex-shader,Unity3d,Shader,Hlsl,Vertex Shader,我在Unity3D中有一个用于splat贴图的着色器。这是一个弗兰肯斯坦怪兽,来自几个不同的怪兽,我发现我终于开始工作了。问题是它不能在纹理上进行凹凸贴图 Shader "Custom/Texture Splatting" { Properties { _MainTex ("Splat Map", 2D) = "white" {} [NoScaleOffset] _Texture1 ("R", 2D
Shader "Custom/Texture Splatting" {
Properties {
_MainTex ("Splat Map", 2D) = "white" {}
[NoScaleOffset] _Texture1 ("R", 2D) = "white" {}
[NoScaleOffset] _Texture2 ("G", 2D) = "white" {}
[NoScaleOffset] _Texture3 ("B", 2D) = "white" {}
[NoScaleOffset] _Texture4 ("A", 2D) = "white" {}
_BumpMap1 ("BumpMapR", 2D) = "bump" {}
_BumpMap2 ("BumpMapG", 2D) = "bump" {}
_BumpMap3 ("BumpMapB", 2D) = "bump" {}
_BumpMap4 ("BumpMapA", 2D) = "bump" {}
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex MyVertexProgram
#pragma fragment MyFragmentProgram
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _Texture1, _Texture2, _Texture3, _Texture4;
struct VertexData {
float4 position : POSITION;
float2 uv : TEXCOORD0;
};
struct Interpolators {
float4 position : SV_POSITION;
float2 uv : TEXCOORD0;
float2 uvSplat : TEXCOORD1;
};
Interpolators MyVertexProgram (VertexData v) {
Interpolators i;
i.position = UnityObjectToClipPos(v.position);
i.uv = TRANSFORM_TEX(v.uv, _MainTex);
i.uvSplat = v.uv;
return i;
}
float4 MyFragmentProgram (Interpolators i) : SV_TARGET {
float4 splat = tex2D(_MainTex, i.uvSplat);
return
tex2D(_Texture1, i.uv) * splat.r +
tex2D(_Texture2, i.uv) * splat.g +
tex2D(_Texture3, i.uv) * splat.b +
tex2D(_Texture4, i.uv) * (1 - splat.r - splat.g - splat.b);
}
ENDCG
}
}
如果有人能向我展示如何更改/添加此着色器代码,使其能够为每个RGBA通道纹理提供单独的凹凸贴图,我将不胜感激
Shader "Custom/Texture Splatting" {
Properties {
_MainTex ("Splat Map", 2D) = "white" {}
[NoScaleOffset] _Texture1 ("R", 2D) = "white" {}
[NoScaleOffset] _Texture2 ("G", 2D) = "white" {}
[NoScaleOffset] _Texture3 ("B", 2D) = "white" {}
[NoScaleOffset] _Texture4 ("A", 2D) = "white" {}
_BumpMap1 ("BumpMapR", 2D) = "bump" {}
_BumpMap2 ("BumpMapG", 2D) = "bump" {}
_BumpMap3 ("BumpMapB", 2D) = "bump" {}
_BumpMap4 ("BumpMapA", 2D) = "bump" {}
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex MyVertexProgram
#pragma fragment MyFragmentProgram
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _Texture1, _Texture2, _Texture3, _Texture4;
struct VertexData {
float4 position : POSITION;
float2 uv : TEXCOORD0;
};
struct Interpolators {
float4 position : SV_POSITION;
float2 uv : TEXCOORD0;
float2 uvSplat : TEXCOORD1;
};
Interpolators MyVertexProgram (VertexData v) {
Interpolators i;
i.position = UnityObjectToClipPos(v.position);
i.uv = TRANSFORM_TEX(v.uv, _MainTex);
i.uvSplat = v.uv;
return i;
}
float4 MyFragmentProgram (Interpolators i) : SV_TARGET {
float4 splat = tex2D(_MainTex, i.uvSplat);
return
tex2D(_Texture1, i.uv) * splat.r +
tex2D(_Texture2, i.uv) * splat.g +
tex2D(_Texture3, i.uv) * splat.b +
tex2D(_Texture4, i.uv) * (1 - splat.r - splat.g - splat.b);
}
ENDCG
}
}
}凹凸贴图通常用于影响曲面上的照明行为,增加曲面不是平面的印象。但是,上面的示例是一个未照明的顶点着色器,它没有任何照明模拟,因此不能只添加凹凸贴图,首先需要将着色器转换为照明着色器。我建议改为使用曲面着色器 如果您仍然想使用顶点着色器进行照明,这是一个完全不同的研究。你可以从中看到一些解释,但除非你真的需要: 也就是说,我制作了一个splat曲面着色器,它使用splat贴图混合两种纹理,并包含一个凹凸贴图。我建议您根据需要调整它(使用4个通道而不是2个通道),而不是尝试从头构建一个照明着色器
Shader "Custom/TerrainTwoTexturesBlend" {
Properties {
_Color("Tint", Color) = (0.5, 0.5, 0.5, 1)
_MainTex("Albedo", 2D) = "white" {}
_BumpMap("Normal Map", 2D) = "bump" {}
_MainTex2("Albedo 2", 2D) = "white" {}
_BumpMap2("Normal Map 2", 2D) = "bump" {}
_Mask("Mask", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Specular ("Specular", Range(0,1)) = 0.0
}
SubShader {
Tags {
"Queue" = "Geometry-4"
"RenderType" = "Opaque"
"PreviewType" = "Plane"
"ForceNoShadowCasting" = "True"
}
LOD 200
ZWrite Off
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf StandardSpecular
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
sampler2D _BumpMap;
sampler2D _MainTex2;
sampler2D _BumpMap2;
sampler2D _Mask;
struct Input {
float2 uv_MainTex: TEXCOORD0;
float2 uv2_MainTex2: TEXCOORD2;
float2 uv3_Mask: TEXCOORD1;
};
half _Glossiness;
half _Specular;
fixed4 _Color;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandardSpecular o) {
float2 uvterrain1 = IN.uv_MainTex;
float2 uvterrain2 = IN.uv2_MainTex2;
float2 uvmask = IN.uv3_Mask;
fixed4 c1 = tex2D(_MainTex, uvterrain1);
fixed4 n1 = tex2D(_BumpMap, uvterrain1);
fixed4 c2 = tex2D(_MainTex2, uvterrain2);
fixed4 n2 = tex2D(_BumpMap2, uvterrain2);
fixed4 m = tex2D(_Mask, uvmask);
o.Albedo = _Color * (c1 * m.r + c2 * (1 - m.r));
float3 fn1 = UnpackScaleNormal(n1, _BumpScale);
float3 fn2 = UnpackScaleNormal(n2, _BumpScale2);
o.Normal = fn1 * m.r + fn2 * (1 - m.r);
// Metallic and smoothness come from slider variables
o.Specular = _Specular;
o.Smoothness = _Glossiness;
o.Alpha = 1;
}
ENDCG
}
FallBack "Diffuse"
}
非常感谢你!我来试试这个。