gpt4 book ai didi

c# - 使用自定义着色器会导致黑色图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:52:52 26 4
gpt4 key购买 nike

我使用 unity 2018.3.5f1。我想在渲染图像时覆盖自定义着色器。以下是我的 onRenderImage 函数。

void OnRenderImage(RenderTexture src, RenderTexture dest) {
// shaderMaterial renders the image with Barrel distortion and disparity effect
Graphics.Blit(camTextureHolder.mainTexture, nullRenderTexture, shaderMaterial);

// measure average frames per second
m_FpsAccumulator++;
if (Time.realtimeSinceStartup > m_FpsNextPeriod) {
m_CurrentFps = (int)(m_FpsAccumulator / fpsMeasurePeriod);
m_FpsAccumulator = 0;
m_FpsNextPeriod += fpsMeasurePeriod;
}
}

问题是在我尝试这样做时整个屏幕看起来都是黑色的。我可以知道如何解决这个问题吗?

更新:

这是我正在使用的着色器的代码

Shader "Custom/FakeAR"
{
Properties{
_MainTex("", 2D) = "white" {}
[HideInInspector]_FOV("FOV", Range(1, 2)) = 1.6
[HideInInspector]_Disparity("Disparity", Range(0, 0.3)) = 0.1
[HideInInspector]_Alpha("Alpha", Range(0, 2.0)) = 1.0
}

SubShader{

Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"

struct v2f {
float4 pos : POSITION;
float2 uv : TEXCOORD0;
};

// Default Vertex Shader
v2f vert(appdata_img v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = MultiplyUV(UNITY_MATRIX_TEXTURE0, v.texcoord.xy);
return o;
}

// Parameters
sampler2D _MainTex;
float _FOV;

// Alpha is the ratio of pixel density: width to height
float _Alpha;
// Disparity is the portion to separate
// larger disparity cause closer stereovision
float _Disparity;


// Fragment Shader: Remap the texture coordinates to combine
// barrel distortion and disparity video display
fixed4 frag(v2f i) : COLOR {
float2 uv1, uv2, uv3;
float t1, t2;
float offset;

// uv1 is the remap of left and right screen to a full screen
uv1 = i.uv - 0.5;
uv1.x = uv1.x * 2 - 0.5 + sign(i.uv.x < 0.5);

t1 = sqrt(1.0 - uv1.x * uv1.x - uv1.y * uv1.y);
t2 = 1.0 / (t1 * tan(_FOV * 0.5));

// uv2 is the remap of side screen with barrel distortion
uv2 = uv1 * t2 + 0.5;

// black color for out-of-range pixels
if (uv2.x >= 1 || uv2.y >= 1 || uv2.x <= 0 || uv2.y <= 0) {
return fixed4(0, 0, 0, 1);
}
else {
offset = 0.5 - _Alpha * 0.5 + _Disparity * 0.5 - _Disparity * sign(i.uv.x < 0.5);
// uv3 is the remap of image texture
uv3 = uv2;
uv3.x = uv2.x * _Alpha + offset;
return tex2D(_MainTex, uv3);
}
}
ENDCG
}
}
FallBack "Diffuse"

是不是shader的原因?

最佳答案

好吧,我认为它正在工作,只是着色器没有任何要渲染的东西,这意味着它会变成黑色。您可能希望着色器使用现有的相机纹理。那么也许是这个?

void OnRenderImage(RenderTexture src, RenderTexture dest) {
// shaderMaterial renders the image with Barrel distortion and disparity effect
Graphics.Blit(src, dst, shaderMaterial);

// measure average frames per second
m_FpsAccumulator++;
if (Time.realtimeSinceStartup > m_FpsNextPeriod) {
m_CurrentFps = (int)(m_FpsAccumulator / fpsMeasurePeriod);
m_FpsAccumulator = 0;
m_FpsNextPeriod += fpsMeasurePeriod;
}
}

尝试让着色器真正成为默认值,看看它是否渲染了一些东西。所以尝试一个看起来像这样的着色器:

Shader "Examples/ExampleDisplacement"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always

Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "UnityCG.cginc"

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};

struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};

v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}

sampler2D _MainTex;

float4 frag (v2f i) : SV_Target
{
float4 col = tex2D(_MainTex, i.uv);
return col;
}
ENDCG
}
}
}

关于c# - 使用自定义着色器会导致黑色图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55844223/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com