这是一篇年前:2021.1.28 些的日志


主要是备忘用

帮美术同学修改完 shader 后
发现烘焙后 alpha test 的 shader 都没有了投影镂空的效果

导致树叶之间的投影 乌黑一大片,然后搜索了一下

还好的是,发现 taecg 老师也在某乎上写过这个问题的处理分享


看了这篇文章就知道:unity黑箱 + 文档不说明有多 e xin:关于AlphaTest阴影烘焙后消失的问题


处理要点:

  • 主纹理名称必须是"_MainTex",只有这个名称才会被内部识别到,就像后处理Shader中也需要用到这个名称是一样的道理。
  • Shader中的Queue排序在2450-3999之间,也就是"Queue" = “AlphaTest"或者"Queue” = “Transparent”.
  • RenderType设置为"RenderType" = “Transparent"或者"RenderType” = “TransparentCutout”.

记住要处理:Forwardbase 和 ShadowCaster 两个 shader 的 tag, queue 等

如果我们要重写 ShaderGUI 那么注意处理这两点即可,如下:


References

  • 关于AlphaTest阴影烘焙后消失的问题
// jave.lin 2022/08/27 为 SanGuo_PBR_MRA_SSS shader 材质的编辑器重写using UnityEngine;
using UnityEditor;
using System;
using System.Collections.Generic;public enum JAVELIN_RenderType
{Opaque,AlphaTest,TransparentWithoutPMA,TransparentWithPMA,Additive,Subtractive,
}public enum JAVELIN_ShowFaceType
{Front,Back,DoubleSide,
}public class SanGuo_PBR_MRA_SSS_MAT_GUI : ShaderGUI
{static MaterialProperty FindAndRemoveProperty(string propertyName, List<MaterialProperty> propertyList){return FindAndRemoveProperty(propertyName, propertyList, true);}static MaterialProperty FindAndRemoveProperty(string propertyName, List<MaterialProperty> propertyList, bool propertyIsMandatory){for (var i = 0; i < propertyList.Count; i++)if (propertyList[i] != null && propertyList[i].name == propertyName){var property = propertyList[i];propertyList.RemoveAt(i);return property;}// We assume all required properties can be found, otherwise something is brokenif (propertyIsMandatory)throw new ArgumentException("Could not find MaterialProperty: '" + propertyName + "', Num properties: " + propertyList.Count);return null;}private static bool IsEnabledKW(string kw, Material target){return target.IsKeywordEnabled(kw);}// jave.lin : 参考:2020.3.37f1 builtin shader StandardParticlesShaderGUI.cs 的 SetupMaterialWithBlendMode 函数public static void SettingMatBlendMode(Material material, JAVELIN_RenderType blendMode){switch (blendMode){case JAVELIN_RenderType.Opaque:material.SetOverrideTag("RenderType", "Geometry");material.SetInt("_BlendOp", (int)UnityEngine.Rendering.BlendOp.Add);material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);material.SetInt("_ZWrite", 1);material.DisableKeyword("_ALPHATEST_ON");material.DisableKeyword("_ALPHABLEND_ON");material.DisableKeyword("_ALPHAPREMULTIPLY_ON");//material.DisableKeyword("_ALPHAMODULATE_ON");material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Geometry;break;case JAVELIN_RenderType.AlphaTest:material.SetOverrideTag("RenderType", "TransparentCutout");material.SetOverrideTag("Queue", "AlphaTest");material.SetInt("_BlendOp", (int)UnityEngine.Rendering.BlendOp.Add);material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);material.SetInt("_ZWrite", 1);material.EnableKeyword("_ALPHATEST_ON");material.DisableKeyword("_ALPHABLEND_ON");material.DisableKeyword("_ALPHAPREMULTIPLY_ON");//material.DisableKeyword("_ALPHAMODULATE_ON");material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.AlphaTest;break;case JAVELIN_RenderType.TransparentWithoutPMA:material.SetOverrideTag("RenderType", "Transparent");material.SetInt("_BlendOp", (int)UnityEngine.Rendering.BlendOp.Add);material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);material.SetInt("_ZWrite", 0);material.DisableKeyword("_ALPHATEST_ON");material.DisableKeyword("_ALPHABLEND_ON");material.DisableKeyword("_ALPHAPREMULTIPLY_ON");//material.DisableKeyword("_ALPHAMODULATE_ON");material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;break;case JAVELIN_RenderType.TransparentWithPMA:material.SetOverrideTag("RenderType", "Transparent");material.SetInt("_BlendOp", (int)UnityEngine.Rendering.BlendOp.Add);material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);material.SetInt("_ZWrite", 0);material.DisableKeyword("_ALPHATEST_ON");material.DisableKeyword("_ALPHABLEND_ON");material.EnableKeyword("_ALPHAPREMULTIPLY_ON");//material.DisableKeyword("_ALPHAMODULATE_ON");material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;break;case JAVELIN_RenderType.Additive:material.SetOverrideTag("RenderType", "Transparent");material.SetInt("_BlendOp", (int)UnityEngine.Rendering.BlendOp.Add);material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.One);material.SetInt("_ZWrite", 0);material.DisableKeyword("_ALPHATEST_ON");material.EnableKeyword("_ALPHABLEND_ON");material.DisableKeyword("_ALPHAPREMULTIPLY_ON");//material.DisableKeyword("_ALPHAMODULATE_ON");material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;break;case JAVELIN_RenderType.Subtractive:material.SetOverrideTag("RenderType", "Transparent");material.SetInt("_BlendOp", (int)UnityEngine.Rendering.BlendOp.ReverseSubtract);material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.One);material.SetInt("_ZWrite", 0);material.DisableKeyword("_ALPHATEST_ON");material.EnableKeyword("_ALPHABLEND_ON");material.DisableKeyword("_ALPHAPREMULTIPLY_ON");//material.DisableKeyword("_ALPHAMODULATE_ON");material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;break;}}public static void SettingMatShowFaceType(Material material, JAVELIN_ShowFaceType showFaceType){switch (showFaceType){case JAVELIN_ShowFaceType.Front:material.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Back);break;case JAVELIN_ShowFaceType.Back:material.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Front);break;case JAVELIN_ShowFaceType.DoubleSide:material.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);break;}}private MaterialProperty renderTypeProp;private MaterialProperty showFaceTypeProp;public void FindProperties(MaterialProperty[] props){renderTypeProp = FindProperty("_JL_RenderType", props, false);showFaceTypeProp = FindProperty("_JL_ShowFaceType", props, false);}public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties){FindProperties(properties);Material targetMat = materialEditor.target as Material;List<MaterialProperty> propertyList = new List<MaterialProperty>(properties);FindAndRemoveProperty("_Parallax", propertyList, false);FindAndRemoveProperty("_ParallaxMap", propertyList, false);FindAndRemoveProperty("_DetailMask", propertyList, false);FindAndRemoveProperty("_DetailAlbedoMap", propertyList, false);FindAndRemoveProperty("_DetailNormalMapScale", propertyList, false);FindAndRemoveProperty("_DetailNormalMap", propertyList, false);FindAndRemoveProperty("_UVSec", propertyList, false);FindAndRemoveProperty("_SpecularHighlights", propertyList, false);FindAndRemoveProperty("_GlossyReflections", propertyList, false);FindAndRemoveProperty("_ALPHATEST", propertyList, false);FindAndRemoveProperty("_ALPHABLEND", propertyList, false);FindAndRemoveProperty("_JL_RenderType", propertyList, false);FindAndRemoveProperty("_JL_ShowFaceType", propertyList, false);FindAndRemoveProperty("_Cull", propertyList, false);FindAndRemoveProperty("_ZWrite", propertyList, false);FindAndRemoveProperty("_SrcBlend", propertyList, false);FindAndRemoveProperty("_DstBlend", propertyList, false);// jave.lin : 根据变体开关来控制 材质属性的显示隐藏var alphatest_on = IsEnabledKW("_ALPHATEST_ON", targetMat);var emission_on = IsEnabledKW("_EMISSION", targetMat);//var sss_on = IsEnabledKW("_SSS_ON", targetMat);if (!alphatest_on){FindAndRemoveProperty("_Cutoff", propertyList, false);}if (!emission_on){FindAndRemoveProperty("_EmissionColor", propertyList, false);FindAndRemoveProperty("_EmissionMap", propertyList, false);}//if (!sss_on)//{//    FindAndRemoveProperty("_TranslucentDistortion", propertyList, false);//    FindAndRemoveProperty("_TranslucentPower", propertyList, false);//    FindAndRemoveProperty("_TranslucentScale", propertyList, false);//    FindAndRemoveProperty("_TranslucentAmbientScale", propertyList, false);//}//var srcRenderType = renderTypeProp.floatValue;//materialEditor.ShaderProperty(renderTypeProp, "RenderType");//var newRenderType = renderTypeProp.floatValue;//if (srcRenderType != newRenderType)//{//    renderTypeProp.floatValue = (int)newRenderType;//    materialEditor.RegisterPropertyChangeUndo("renderType");//    materialEditor.PropertiesChanged();//}// jave.lin : render type{var src_val = (JAVELIN_RenderType)((int)renderTypeProp.floatValue);var new_val = (JAVELIN_RenderType)EditorGUILayout.EnumPopup("RenderType", src_val, GUILayout.ExpandWidth(true));if (src_val != new_val){renderTypeProp.floatValue = (int)new_val;SettingMatBlendMode(targetMat, new_val);}}// jave.lin : show face type{var src_val = (JAVELIN_ShowFaceType)((int)showFaceTypeProp.floatValue);var new_val = (JAVELIN_ShowFaceType)EditorGUILayout.EnumPopup("ShowFaceType", src_val, GUILayout.ExpandWidth(true));if (src_val != new_val){showFaceTypeProp.floatValue = (int)new_val;SettingMatShowFaceType(targetMat, new_val);}}if (propertyList.Count > 0){for (int i = 0; i < propertyList.Count; i++){materialEditor.ShaderProperty(propertyList[i], propertyList[i].displayName);}}materialEditor.RenderQueueField();}
}

注意

unity - 踩坑记录 - 关于AlphaTest投影烘焙后没有镂空的问题相关推荐

  1. Unity接入AWS S3过程,AWS SDK for Unity 踩坑记录

    AWS S3,亚马逊的资源服务器服务,最近在研究这一块内容.记录一下踩坑的内容. 研究了AWS SDK for unity和AWS SDK for .net 两部分 unity很难接,主要原因是官方没 ...

  2. Unity踩坑记录—— 归一化距离向量

    →_→   Vector3.normalized 和 Vector3.Normalize 依据官方文档解释:前者(Vector3.normalized)是当前向量是不改变的,返回一个新的规范化的同方向 ...

  3. Unity踩坑记录 —— GetComponent的使用

    1. 初次遇到问题是打印物体相对于主摄像机的relativePosition和relativeRotation. 结果一直出现问题,通过画框测试发现一直没能找到正确的数值, 直到逐步打印调试后才发现问 ...

  4. 【Unity踩坑记录】导航系统NavMeshAgent

    当你想更改Nav物体的位置时,超过一定距离可能会出现瞬移的情况. 解决办法:更改Nav物体位置时,关闭Nav组件,修改过再打开. gameNav.enabled = false; gameNav.tr ...

  5. Unity AssetBundle 踩坑记录

    Unity AssetBundle 踩坑记录 editor 下选择什么平台的 ab 加载 Material doesn't have a color property '_Color' UnityEd ...

  6. Unity GPU Instance踩坑记录

    Unity GPU Instance踩坑记录 Unity中GPUInstance主要有两种:一种是unity自动进行的,或者调用Graphics.DrawMeshInstancedIndirect等A ...

  7. 【100个 Unity踩坑小知识点】| Unity调用API ,动态获取Android权限,附带所有Android权限表格

    Unity 小科普 老规矩,先介绍一下 Unity 的科普小知识: Unity是 实时3D互动内容创作和运营平台 . 包括游戏开发.美术.建筑.汽车设计.影视在内的所有创作者,借助 Unity 将创意 ...

  8. AirSim学习和踩坑记录(不定时更新)

    版权声明:本文为博主原创文章,遵循Creative Commons - Attribution-ShareAlike 4.0 International - CC BY-SA 4.0版权协议,转载请附 ...

  9. 关于原神沙漠痕迹效果的踩坑记录

    --因为个人之前活动的平台缺乏有营养的可以促进双方思考与进步的评论(毕竟平台的主打内容和用户群体不同),所以思考(与自己的懒惰做斗争)过后,技术相关的文章以后还是来知乎或CSDN之类专业性比较强的平台 ...

最新文章

  1. 比杠精网友更可怕的,是懂王同事
  2. 如果不当程序员,你会在做什么? | 每日趣闻
  3. 方向梯度直方图(HOG)和颜色直方图的一些比較
  4. phpcms如何给已有的模块添加新功能?
  5. 6.6 rsync:文件同步工具
  6. python输入多个数据存入列表_python怎么把input的值储存到一个列表
  7. 客户端渲染换为服务器端渲染_服务器与客户端渲染(AngularJS与服务器端MVC)
  8. PHP数据库调用类调用实例
  9. 左右伸缩_OPPO概念机将至!横向卷轴+左右伸缩,你期待吗
  10. mysql in 临时表_什么时候会用到临时表?MySQL临时表的使用总结
  11. CNN卷积层里的多输入多输出通道channel 动手学深度学习v2 pytorch
  12. Unity3D中粒子系统
  13. PhotoShop 之钢笔工具
  14. php 中 normdist,2021年,先进光学卫星ALOS-3将由H3-22运载火箭从种子岛宇宙中心发射...
  15. 小程序加盟怎么样?加盟小程序赚钱吗?
  16. Mach3寻边和对刀代码
  17. echarts数据可视化系列:仪表盘
  18. 沪牌一年来拍牌情况统计
  19. [转]许小年:转折点上的中国经济/中等收入陷阱
  20. 非线性光纤光学_深度解析:光纤随机激光器及其应用研究进展!

热门文章

  1. 【转载】如何理解数据集中【训练集】、【验证集】和【测试集】
  2. 289. 生命游戏。
  3. 淘宝封我账号店铺2个月了, 都没退保证金货款给我,商品客户早就确认了,聚投诉投诉淘宝,但是没人处理
  4. 敬业!华为23级大佬消耗巨资整理出2000页网络协议最全笔记
  5. Domino V11 Jams继续
  6. SourceInsight4.0.0124中文版-黑色背景主题
  7. r语言进行go富集分析_GO富集分析可视化:R语言GOplot包——准备自己的数据
  8. 神经网络的基本知识之MXNet框架
  9. sql server 数据库可疑解决方案
  10. 利用Blender建立基础汽车模型