如何解决需要帮助将自定义着色器转换为 URP
最近几天,我一直在关注 Sebastian Lague 的关于程序生成的视频。由于我的项目基于 URP,并且负责在网格上渲染纹理的自定义着色器只是为 SRP 编写的,因此我必须进行转换。我一直在修补着色器图,并找到了一个可能的转换解决方案。但是,色调和纹理都没有得到渲染。我将添加两个代码:一个原始代码和一个尝试覆盖的代码。
编辑:我从修补中获得的唯一结果是,当我在着色器图中手动设置“图层”值时,它允许我更改所述图层的基色。尽管如此,它只影响一个单层,不应用任何纹理,只在全局空间中的特定 y 值下工作
这是设置变量的代码。两者都一样:
public void ApplyToMaterial(Material material)
{
material.SetInt("layerCount",layers.Length);
material.SetColorArray("baseColours",layers.Select(x => x.tint).ToArray());
material.SetFloatArray("baseStartHeights",layers.Select(x => x.startHeight).ToArray());
material.SetFloatArray("baseBlends",layers.Select(x => x.blendStrength).ToArray());
material.SetFloatArray("baseColourStrength",layers.Select(x => x.tintStrength).ToArray());
material.SetFloatArray("baseTextureScales",layers.Select(x => x.textureScale).ToArray());
Texture2DArray texturesArray = GenerateTextureArray(layers.Select(x => x.texture).ToArray());
material.SetTexture("baseTextures",texturesArray);
UpdateMeshHeights(material,savedMinHeight,savedMaxHeight);
}
public void UpdateMeshHeights(Material material,float minHeight,float maxHeight)
{
savedMaxHeight = maxHeight;
savedMinHeight = minHeight;
material.SetFloat("minHeight",minHeight);
material.SetFloat("maxHeight",maxHeight);
}
原始着色器:
Shader "Custom/Terrain" {
Properties{
testTexture("Texture",2D) = "white"{}
testScale("Scale",Float) = 1
}
SubShader{
Tags { "RenderType" = "Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model,and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target,to get nicer looking lighting
#pragma target 3.0
const static int maxLayerCount = 8;
const static float epsilon = 1E-4;
int layerCount;
float3 baseColours[maxLayerCount];
float baseStartHeights[maxLayerCount];
float baseBlends[maxLayerCount];
float baseColourStrength[maxLayerCount];
float baseTextureScales[maxLayerCount];
float minHeight;
float maxHeight;
sampler2D testTexture;
float testScale;
UNITY_DECLARE_TEX2DARRAY(baseTextures);
struct Input {
float3 worldPos;
float3 worldnormal;
};
float inverseLerp(float a,float b,float value) {
return saturate((value - a) / (b - a));
}
float3 triplanar(float3 worldPos,float scale,float3 blendAxes,int textureIndex) {
float3 scaledWorldPos = worldPos / scale;
float3 xProjection = UNITY_SAMPLE_TEX2DARRAY(baseTextures,float3(scaledWorldPos.y,scaledWorldPos.z,textureIndex)) * blendAxes.x;
float3 yProjection = UNITY_SAMPLE_TEX2DARRAY(baseTextures,float3(scaledWorldPos.x,textureIndex)) * blendAxes.y;
float3 zProjection = UNITY_SAMPLE_TEX2DARRAY(baseTextures,scaledWorldPos.y,textureIndex)) * blendAxes.z;
return xProjection + yProjection + zProjection;
}
void surf(Input IN,inout SurfaceOutputStandard o) {
float heightPercent = inverseLerp(minHeight,maxHeight,IN.worldPos.y);
float3 blendAxes = abs(IN.worldnormal);
blendAxes /= blendAxes.x + blendAxes.y + blendAxes.z;
for (int i = 0; i < layerCount; i++) {
float drawStrength = inverseLerp(-baseBlends[i] / 2 - epsilon,baseBlends[i] / 2,heightPercent - baseStartHeights[i]);
float3 baseColour = baseColours[i] * baseColourStrength[i];
float3 textureColour = triplanar(IN.worldPos,baseTextureScales[i],blendAxes,i) * (1 - baseColourStrength[i]);
o.Albedo = o.Albedo * (1 - drawStrength) + (baseColour + textureColour) * drawStrength;
}
}
ENDCG
}
FallBack "Diffuse"
}
尝试加上着色器图:
const static int maxLayerCount = 8;
const static float epsilon = 1E-4;
float layerCount;
float3 baseColours[maxLayerCount];
float baseStartHeights[maxLayerCount];
float baseBlends[maxLayerCount];
float baseColourStrength[maxLayerCount];
float baseTextureScales[maxLayerCount];
float3 triplanar(float3 worldPos,Texture2DArray textures,SamplerState ss,int textureIndex) {
float3 scaledWorldPos = worldPos / scale;
float3 xProjection = SAMPLE_TEXTURE2D_ARRAY(textures,ss,float2(scaledWorldPos.y,scaledWorldPos.z),textureIndex) * blendAxes.x;
float3 yProjection = SAMPLE_TEXTURE2D_ARRAY(textures,float2(scaledWorldPos.x,textureIndex) * blendAxes.y;
float3 zProjection = SAMPLE_TEXTURE2D_ARRAY(textures,scaledWorldPos.y),textureIndex) * blendAxes.z;
return xProjection + yProjection + zProjection;
}
float inverseLerp(float a,float c)
{
return saturate((c - a) / (b - a));
}
void layer_terrain_float(float3 worldPos,float heightPercent,float3 worldnormal,int layerCount,out float3 albedo) {
float3 blendAxes = abs(worldnormal);
blendAxes /= blendAxes.x + blendAxes.y + blendAxes.z;
albedo = 0.0f;
for (int i = 0; i < layerCount; i++) {
float drawStrength = inverseLerp(-baseBlends[i] / 2 - epsilon,heightPercent - baseStartHeights[i]);
float3 baseColour = baseColours[i] * baseColourStrength[i];
float3 textureColour = triplanar(worldPos,textures,i) * (1 - baseColourStrength[i]);
albedo = albedo * (1 - drawStrength) + (baseColour + textureColour) * drawStrength;
}
}
我连续几天都在这里撞墙。因此,将非常感谢您的帮助
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。