unity3d - 使用 Unity HDRP 进行三平面纹理
问题描述
我在 Unity 中使用默认渲染管道编写了一个表面着色器,以在没有 UV 的网格上使用三平面纹理,这很好用,使用以下代码:
Shader "Custom/TerrainShader"
{
// These properties can be modified from the material inspector.
Properties{
_MainTex("Ground Texture", 2D) = "white" {}
_WallTex("Wall Texture", 2D) = "white" {}
_TexScale("Texture Scale", Float) = 1
}
// You can have multiple subshaders with different levels of complexity. Unity will pick the first one
// that works on whatever machine is running the game.
SubShader{
Tags { "RenderType" = "Opaque" } // None of our terrain is going to be transparent so Opaque it is.
LOD 200 // We only need diffuse for now so 200 is fine. (higher includes bumped, specular, etc)
CGPROGRAM
#pragma surface surf Standard fullforwardshadows // Use Unity's standard lighting model
#pragma target 3.0 // Lower target = fewer features but more compatibility.
// Declare our variables (above properties must be declared here)
sampler2D _MainTex;
sampler2D _WallTex;
float _TexScale;
// Say what information we want from our geometry.
struct Input {
float3 worldPos;
float3 worldNormal;
};
// This function is run for every pixel on screen.
void surf(Input IN, inout SurfaceOutputStandard o) {
float3 scaledWorldPos = IN.worldPos / _TexScale; // Get a the world position modified by scale.
float3 pWeight = abs(IN.worldNormal); // Get the current normal, using abs function to ignore negative numbers.
pWeight /= pWeight.x + pWeight.y + pWeight.z; // Ensure pWeight isn't greater than 1.
// Get the texture projection on each axes and "weight" it by multiplying it by the pWeight.
float3 xP = tex2D(_WallTex, scaledWorldPos.yz) * pWeight.x;
float3 yP = tex2D(_MainTex, scaledWorldPos.xz) * pWeight.y;
float3 zP = tex2D(_WallTex, scaledWorldPos.xy) * pWeight.z;
// Return the sum of all of the projections.
o.Albedo = xP + yP + zP;
}
ENDCG
}
FallBack "Diffuse"
}
但是,当切换到新的 RP(HD 或 LW)时,使用它的材料会变成粉红色。我知道这是因为 Unity 不再支持表面着色器,所以我的问题是,如何使用新的 RP 实现三平面纹理?
解决方案
通过着色器图支持三平面纹理。只需在图形编辑器中点击空格并搜索“三平面”,它就会显示出来。
HDRP 着色器使用延迟渲染,因此它的着色器看起来完全不同。如果您想学习,我建议您在着色器图中创建一个基本着色器,然后右键单击主节点并选择“复制着色器”。然后,您可以将着色器代码粘贴到文本编辑器中并尝试对其进行逆向工程。SRP GitHub 也是一个很好的参考:
- https://github.com/Unity-Technologies/Graphics/tree/master/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary
- https://github.com/Unity-Technologies/Graphics/tree/master/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass
对于 LWRP,我发现这个模板着色器非常有用:
推荐阅读
- java - OpenCV java CascadeClassifier异常
- javascript - 通过自定义协议处理程序(Chrome 到 IE)使用 URL 传递参数
- c++ - 返回 std::tuple 并移动语义/复制省略
- jquery - jQuery selectpicker - 如何选择空选项?
- ios - 将 UIStackView 动态调整为内部 UIView 的宽度
- r - R Plotly 使用范围选择器按钮选择范围
- python-3.x - 从python中的目录读取图像
- python - Google Chrome - 可以在 Windows 上退出,但不能在 Mac 上退出
- javascript - 使用 ArcGis Api 显示 InfoWindo
- haskell - 使用 HXT 修改 XML