问题描述
我在用一些代码制作/拼凑而成的着色器时遇到了问题
I have a problem with a shader I made/cobbled together from bits of code
最初我的计划是在运行时创建一个着色器在禁用对象上着色?但这似乎是错误的事情,因为据我所知,着色器无法在运行时交换.
Initially my plan was to was a shader at runtime Shader on disabled object?But this seems to be the wrong thing to do, as its my understanding now that shaders can't be swapped at runtime.
所以我希望能够校正我拥有的着色器,以便可以从颜色淡入灰度但模型的一部分不会消失.我真的不懂着色器...
So I would like to be able to correct the shader I have so that I can fade from colour to greyscalebut not have part of the model disappearing. I really don't understand shaders...
我到目前为止拥有的代码
The code I have so far
Shader "Custom/GreyScaleToColour"
{
Properties
{
[PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
_Color("Tint", Color) = (1,1,1,1)
[MaterialToggle] PixelSnap("Pixel snap", Float) = 0
[HideInInspector] _RendererColor("RendererColor", Color) = (1,1,1,1)
[HideInInspector] _Flip("Flip", Vector) = (1,1,1,1)
[PerRendererData] _AlphaTex("External Alpha", 2D) = "white" {}
[PerRendererData] _EnableExternalAlpha("Enable External Alpha", Float) = 0
_EffectAmount("Effect Amount", Range(0, 1)) = 1.0
_Brightness("Brightness", Range(0, 1.5)) = 0.5
}
SubShader
{
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
"PreviewType" = "Plane"
"CanUseSpriteAtlas" = "True"
}
//Cull Off
Lighting Off
ZWrite Off
Blend One OneMinusSrcAlpha
CGPROGRAM
#pragma surface surf Lambert vertex:vert nofog nolightmap nodynlightmap keepalpha noinstancing
#pragma multi_compile _ PIXELSNAP_ON
#pragma multi_compile _ ETC1_EXTERNAL_ALPHA
#include "UnitySprites.cginc"
struct Input
{
float2 uv_MainTex;
fixed4 color;
};
void vert(inout appdata_full v, out Input o)
{
v.vertex.xy *= _Flip.xy;
#if defined(PIXELSNAP_ON)
v.vertex = UnityPixelSnap(v.vertex);
#endif
UNITY_INITIALIZE_OUTPUT(Input, o);
o.color = v.color * _Color * _RendererColor;
}
uniform float _EffectAmount;
uniform float _Brightness;
void surf(Input IN, inout SurfaceOutput o)
{
fixed4 c = SampleSpriteTexture(IN.uv_MainTex) * IN.color;
c.rgb = lerp(c.rgb, dot(c.rgb, float3(0.3, 0.59, 0.11)), _EffectAmount);
//o.Albedo = c.rgb * c.a;
o.Albedo = c.rgb * _Brightness;
//o.Albedo = (c.r + c.g + c.b) / 3 * _Brightness;
o.Alpha = c.a;
}
ENDCG
}
Fallback "Sprite/Diffuse"
}
部分模型消失了...
Part of the model disappears...
出现奇怪的伪像
看起来应该怎样
谢谢娜塔莉(Natalie)
With thanksNatalie
推荐答案
关于在运行时交换着色器-不需要在运行时交换着色器.您可以简单地启用/禁用带有正确着色器的不同GameObject.
Regarding the swapping of Shaders at runtime - you don't need to swap the Shaders at runtime. You can simply enable/disable different GameObjects with the correct shaders on them.
// Assigned in the Editor
GameObject disabledGO; // Contains the Material with the Disabled Shader
GameObject normalGO; // Contains the Material with the Normal Shader
bool isDisabled = false;
private void Start()
{
disabledGO.SetActive(isDisabled);
normalGO.SetActive(!isDisabled);
}
private void Update()
{
if (Input.GetKey(KeyCode.E))
{
isDisabled = !isDisabled;
disabledGO.SetActive(isDisabled);
normalGO.SetActive(!isDisabled);
}
}
这篇关于如何在着色器上修复法线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!