【发布时间】:2020-02-01 03:15:21
【问题描述】:
您好,我在 YouTube 上关注了 Sebastian League 制作的关于程序生成的视频系列,并且我关注了他的整个视频系列,但就我而言,网格中有黑点,仅在水域。我正在为那些想知道的人使用全局模式,也使用统一 2019.4.6f1。我想摆脱已经尝试构建和运行的黑点,而黑点就在那里。
他的系列链接是:https://www.youtube.com/watch?v=wbpMiKiSKm8&list=PLFt_AvWsXl0eBW2EiBtl_sxmDtSgZBxB3 我已经在 GitHub 上下载了他的项目,他似乎没有问题,这是他的 GitHub 页面:https://github.com/SebLague/Procedural-Landmass-Generation 这里还有一张图片 -> here
我正在为地形创建自己的自定义着色器,在这里
Shader "Custom/terrain"
{
// this properties will be added to our meshMaterial
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 because of RGB
float3 baseColors[maxLayerCount];
float baseStartHeights[maxLayerCount];
float baseBlends[maxLayerCount];
float baseColorStrength[maxLayerCount];
float baseTextureScales[maxLayerCount];
float minHeight;
float maxHeight;
sampler2D testTexture;
float testScale;
UNITY_DECLARE_TEX2DARRAY(baseTextures);
struct Input {
float3 worldPos;
float worldNormal;
};
// float a is min value, float b is max value and value is current value
float inverseLerp(float a, float b, float value) {
// saturate means clamp the value between 0 and 1
return saturate((value - a)/(b - a));
}
// 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)
float3 triplanar(float3 worldPos, float scale, float3 blendAxis, int textureIndex) {
float3 scaledWorldPos = worldPos / scale;
// tripleaner mapping
float3 xProjection = UNITY_SAMPLE_TEX2DARRAY(baseTextures,
float3(scaledWorldPos.y, scaledWorldPos.z, textureIndex)) * blendAxis.x;
float3 yProjection = UNITY_SAMPLE_TEX2DARRAY(baseTextures,
float3(scaledWorldPos.x, scaledWorldPos.z, textureIndex)) * blendAxis.y;
float3 zProjection = UNITY_SAMPLE_TEX2DARRAY(baseTextures,
float3(scaledWorldPos.x, scaledWorldPos.y, textureIndex)) * blendAxis.z;
return xProjection + yProjection + zProjection;
}
// this function will be called for every pixel that our mesh is visible
// we want to set the color at that surface
void surf (Input IN, inout SurfaceOutputStandard o) {
float heightPercent = inverseLerp(minHeight, maxHeight, IN.worldPos.y);
float3 blendAxis = abs(IN.worldNormal);
blendAxis /= blendAxis.x + blendAxis.y + blendAxis.z;
for (int i = 0; i < layerCount; i++) {
float drawStrength = inverseLerp(-baseBlends[i]/2 - epsilon, baseBlends[i]/2, (heightPercent - baseStartHeights[i]));
float3 baseColor = baseColors[i] * baseColorStrength[i];
float3 textureColor = triplanar(IN.worldPos, baseTextureScales[i], blendAxis, i) * (1-baseColorStrength[i]);
// if drawStrength is 0 then we would set color to black
// but what we want is that if drawstength is 0
// then we want to use the same color, albedo * 1 + 0 will be same (what we want)
o.Albedo = o.Albedo * (1-drawStrength) + (baseColor + textureColor) * drawStrength;
}
}
ENDCG
}
FallBack "Diffuse"
}
【问题讨论】:
-
看起来可能是
drawStrength对于这些像素始终为 0。只是为了确认一下,如果你在surf函数的顶部添加o.Albedo = float3(1,0,1);行并将o.Albedo = o.Albedo *...行更改为o.Albedo = o.Albedo * (1-drawStrength) + float3(0,1,0) * drawStrength;会发生什么我很好奇你是否在黑点和度数的地方得到纯洋红色其他地方的绿色洋红色。 -
如果是这种情况,请确保最低的
baseStartHeights正好是 0。如果检查器可能显示 0,即使它实际上是非零的,请确保您输入 0 并按 在字段中输入。让我知道这是否有帮助或没有帮助。 -
@Ruzihm 我已经尝试过你的解决方案,一切都是绿色的,一种非常强烈的绿色,整个块都是绿色的,没有洋红色,黑点已经消失。关于 baseStartHeight 的问题是,我根据我们所在的网格高度将网格划分为层。水部分从基高零开始,所以我认为该值不是轻微的非零。
-
好的。我很好奇这是否是由于这些像素的高度处于 minHeight,或者法线是否也在问题中起作用。从问题中的代码开始,将
float heightPercent = ...行更改为float heightPercent = 0;。让我知道当你这样做时它的样子。如果黑点停留在原来的位置,如果一切都变黑,如果一切都变蓝,或者其他什么。 -
另外,您能否分享您分配给
baseStartHeights和baseBlends的任何c# 代码和/或检查器屏幕截图。我无法在着色器代码中看到问题,我认为参数的设置方式可能有问题。
标签: unity3d shader procedural-generation shaderlab