unity3d - 如何修复着色器上的法线
问题描述
我用一些代码制作/拼凑在一起的着色器有问题
最初我的计划是在运行时 着色器着色器禁用对象? 但这似乎是错误的做法,因为我现在理解着色器不能在运行时交换。
所以我希望能够纠正我拥有的着色器,这样我就可以从颜色渐变到灰度,但不会让模型的一部分消失。我真的不明白着色器...
我到目前为止的代码
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"
}
模型的一部分消失了……
出现奇怪的伪影
它应该看起来如何
感谢娜塔莉
解决方案
关于在运行时交换着色器 - 您不需要在运行时交换着色器。您可以使用正确的着色器简单地启用/禁用不同的游戏对象。
// 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);
}
}
推荐阅读
- python - 查询模板中与当前用户相关的项目
- textures - SKScene 纹理到 SceneKit 问题透明度
- spring-boot - ApplicationContextInitializer 由 Spring Cloud Bootstrap 上下文和 Spring Boot 加载两次
- java - Java中静态反序列化的差异取决于何时序列化对象
- jquery - 停止在空间上关闭结果
- redux - Redux:为什么reducers被调用了两次(初学者)
- elasticsearch - 添加 script.max_size_in_bytes: 10000000 不在 elastisearch 中考虑
- c# - 如何 Activator.CreateInstance 一个没有构造函数的类型?
- atom-editor - 是否可以根据文件扩展名对两个不同的包使用相同的键绑定?
- scala - 将 1 列拆分为多列时显示编译错误