unity中shader的Cutout问题「建议收藏」

unity中shader的Cutout问题「建议收藏」unity中自带的shader里有Cutout类型的shader,zhe’zho

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

unity中自带的shader里有Cutout类型的shader,今天发现这种类型很诡异,先

贴几种自带的:

// Unlit alpha-cutout shader.
// – no lighting
// – no lightmap support
// – no per-material color

Shader “Unlit/Transparent Cutout” {

Properties {

    _MainTex (“Base (RGB) Trans (A)”, 2D) = “white” {}
    _Cutoff (“Alpha cutoff”, Range(0,1)) = 0.5
}
SubShader {

    Tags {“Queue”=”AlphaTest” “IgnoreProjector”=”True” “RenderType”=”TransparentCutout”}
    LOD 100

    Lighting Off

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

            struct appdata_t {

                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
            };

            struct v2f {

                float4 vertex : SV_POSITION;
                half2 texcoord : TEXCOORD0;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            fixed _Cutoff;

            v2f vert (appdata_t v)
            {

                v2f o;
                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
                return o;
            }
            
            fixed4 frag (v2f i) : COLOR
            {

                fixed4 col = tex2D(_MainTex, i.texcoord);
                clip(col.a – _Cutoff);
                return col;
            }
        ENDCG
    }
}

}

Shader “Transparent/Cutout/Diffuse” {

Properties {

    _Color (“Main Color”, Color) = (1,1,1,1)
    _MainTex (“Base (RGB) Trans (A)”, 2D) = “white” {}
    _Cutoff (“Alpha cutoff”, Range(0,1)) = 0.5
}

SubShader {

    Tags {“Queue”=”AlphaTest” “IgnoreProjector”=”True” “RenderType”=”TransparentCutout”}
    LOD 200
    
CGPROGRAM
#pragma surface surf Lambert alphatest:_Cutoff

sampler2D _MainTex;
fixed4 _Color;

struct Input {

    float2 uv_MainTex;
};

void surf (Input IN, inout SurfaceOutput o) {

    fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    o.Albedo = c.rgb;
    o.Alpha = c.a;
}
ENDCG
}

Fallback “Transparent/Cutout/VertexLit”
}

Shader “Transparent/Diffuse” {

Properties {

    _Color (“Main Color”, Color) = (1,1,1,1)
    _MainTex (“Base (RGB) Trans (A)”, 2D) = “white” {}
}

SubShader {

    Tags {“Queue”=”Transparent” “IgnoreProjector”=”True” “RenderType”=”Transparent”}
    LOD 200

CGPROGRAM
#pragma surface surf Lambert alpha

sampler2D _MainTex;
fixed4 _Color;

struct Input {

    float2 uv_MainTex;
};

void surf (Input IN, inout SurfaceOutput o) {

    fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    o.Albedo = c.rgb;
    o.Alpha = c.a;
}
ENDCG
}

Fallback “Transparent/VertexLit”
}

我现在做的某个项目,模型需要用transparent/diffuse的效果,但是有的建筑窗户是贴图,贴图中的边缘导入时alpha是0,

我综合上述代码写了个shader,能再电脑上的unity的Android平台上运行,但是在手机上会死机。。Android手机只能同时支持

lightmap,Alphatest,ZTest 中的两种。

Shader “Custom/TranDifExt” {

    Properties {

    _Color (“Main Color”, Color) = (1,1,1,1)
    _MainTex (“Base (RGB) Trans (A)”, 2D) = “white” {}
    _Cutoff (“Alpha cutoff”, Range(0,1)) = 0.5
}

SubShader {

    Tags {“Queue”=”AlphaTest” “IgnoreProjector”=”True” “RenderType”=”TransparentCutout”}
    LOD 200

CGPROGRAM
//#pragma surface surf Lambert alpha
#pragma surface surf Lambert
#include “UnityCG.cginc”
sampler2D _MainTex;
fixed4 _Color;
fixed _Cutoff;

struct Input {

    float2 uv_MainTex;
};

void surf (Input IN, inout SurfaceOutput o) {

    fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    o.Albedo = c.rgb;
    o.Alpha = c.a;
    clip(o.Alpha-_Cutoff);
}
ENDCG
}

Fallback “VertexLit”
}

然后又被同伙找了个shader:”Transparent/Cutout/VertexLit”,可以在手机Android上执行,同时有lightmap,Alphatest,ZTest 三种(不死机)

Shader “Transparent/Cutout/VertexLit” {

Properties {

    _Color (“Main Color”, Color) = (1,1,1,1)
    _SpecColor (“Spec Color”, Color) = (1,1,1,0)
    _Emission (“Emissive Color”, Color) = (0,0,0,0)
    _Shininess (“Shininess”, Range (0.1, 1)) = 0.7
    _MainTex (“Base (RGB) Trans (A)”, 2D) = “white” {}
    _Cutoff (“Alpha cutoff”, Range(0,1)) = 0.5
}

SubShader {

    Tags {“Queue”=”AlphaTest” “IgnoreProjector”=”True” “RenderType”=”TransparentCutout”}
    LOD 100
    
    // Non-lightmapped
    Pass {

        Tags { “LightMode” = “Vertex” }
        Alphatest Greater [_Cutoff]
        AlphaToMask True
        ColorMask RGB
        Material {

            Diffuse [_Color]
            Ambient [_Color]
            Shininess [_Shininess]
            Specular [_SpecColor]
            Emission [_Emission]    
        }
        Lighting On
        SeparateSpecular On
        SetTexture [_MainTex] {

            Combine texture * primary DOUBLE, texture * primary
        }
    }
    
    // Lightmapped, encoded as dLDR
    Pass {

        Tags { “LightMode” = “VertexLM” }
        Alphatest Greater [_Cutoff]
        AlphaToMask True
        ColorMask RGB
        
        BindChannels {

            Bind “Vertex”, vertex
            Bind “normal”, normal
            Bind “texcoord1”, texcoord0 // lightmap uses 2nd uv
            Bind “texcoord”, texcoord1 // main uses 1st uv
        }
        SetTexture [unity_Lightmap] {

            matrix [unity_LightmapMatrix]
            constantColor [_Color]
            combine texture * constant
        }
        SetTexture [_MainTex] {

            combine texture * previous DOUBLE, texture * primary
        }
    }
    
    // Lightmapped, encoded as RGBM
    Pass {

        Tags { “LightMode” = “VertexLMRGBM” }
        Alphatest Greater [_Cutoff]
        AlphaToMask True
        ColorMask RGB
        
        BindChannels {

            Bind “Vertex”, vertex
            Bind “normal”, normal
            Bind “texcoord1”, texcoord0 // lightmap uses 2nd uv
            Bind “texcoord1”, texcoord1 // unused
            Bind “texcoord”, texcoord2 // main uses 1st uv
        }
        
        SetTexture [unity_Lightmap] {

            matrix [unity_LightmapMatrix]
            combine texture * texture alpha DOUBLE
        }
        SetTexture [unity_Lightmap] {

            constantColor [_Color]
            combine previous * constant
        }
        SetTexture [_MainTex] {

            combine texture * previous QUAD, texture * primary
        }
    }
    
    // Pass to render object as a shadow caster
    Pass {

        Name “Caster”
        Tags { “LightMode” = “ShadowCaster” }
        Offset 1, 1
        
        Fog {Mode Off}
        ZWrite On ZTest LEqual Cull Off

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_shadowcaster
#include “UnityCG.cginc”

struct v2f {
    V2F_SHADOW_CASTER;
    float2  uv : TEXCOORD1;
};

uniform float4 _MainTex_ST;

v2f vert( appdata_base v )
{

    v2f o;
    TRANSFER_SHADOW_CASTER(o)
    o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    return o;
}

uniform sampler2D _MainTex;
uniform fixed _Cutoff;
uniform fixed4 _Color;

float4 frag( v2f i ) : COLOR
{

    fixed4 texcol = tex2D( _MainTex, i.uv );
    clip( texcol.a*_Color.a – _Cutoff );
    
    SHADOW_CASTER_FRAGMENT(i)
}
ENDCG

    }
    
    // Pass to render object as a shadow collector
    Pass {

        Name “ShadowCollector”
        Tags { “LightMode” = “ShadowCollector” }
        
        Fog {Mode Off}
        ZWrite On ZTest LEqual

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_shadowcollector

#define SHADOW_COLLECTOR_PASS
#include “UnityCG.cginc”

struct v2f {

    V2F_SHADOW_COLLECTOR;
    float2  uv : TEXCOORD5;
};

uniform float4 _MainTex_ST;

v2f vert (appdata_base v)
{

    v2f o;
    TRANSFER_SHADOW_COLLECTOR(o)
    o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    return o;
}

uniform sampler2D _MainTex;
uniform fixed _Cutoff;
uniform fixed4 _Color;

fixed4 frag (v2f i) : COLOR
{

    fixed4 texcol = tex2D( _MainTex, i.uv );
    clip( texcol.a*_Color.a – _Cutoff );
    
    SHADOW_COLLECTOR_FRAGMENT(i)
}
ENDCG

    }
}

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/189933.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Spring中,三级缓存解决循环依赖[通俗易懂]

    Spring中,三级缓存解决循环依赖[通俗易懂]发现Spring三级缓存没有写到博客里,这里从自己的笔记迁移一下,补上:创建的都是单例,如果是构造方法注入,不能解决;如果是设值方法注入,用三级缓存解决:DefaultSingletonBeanRegistry:singletonObjectsearlySingletonObjectssingletonFactories代码流程:bean在createBeanInstance后,populateBean前:DefaultSingletonBeanRegistry:addSingl

    2022年5月21日
    67
  • Element-UI + Vue,孙鑫Java视频教程百度网盘

    Element-UI + Vue,孙鑫Java视频教程百度网盘},},}[]()分页功能的实现==========================================================================mysql分页查询:SELECTid,name,bir,sex,addressFROMt_userLIMIT#{start},#{rows}后端控制器:@RestController@RequestMapping(“/user”)..

    2022年5月17日
    42
  • RDN怎么配置环境

    RDN怎么配置环境非root怎么安装linux版本的lua和torchhttps://blog.csdn.net/BRAVE_NO1/article/details/90260800

    2022年6月18日
    30
  • python和Java语法对比

    python和Java语法对比定义变量:java:inta=1;python:a=1#python中不需要分号结尾,python用缩进判断代码块Python定义变量的时候不需要给出类型,直接定义即可,Python会自动判断变量类型。String类型:java:Stringa=newString();a="123";python:a="123"…

    2022年7月8日
    19
  • 什么是pisa测试_PISA测试真相:哪些学校代表中国考取第一名

    什么是pisa测试_PISA测试真相:哪些学校代表中国考取第一名原标题:PISA测试真相:哪些学校代表中国考取第一名在北京金融行业工作的王鑫如,去年女儿出生后就开始规划送她去哪里接受教育,留在北京,还是随着一个工作机会去香港,或者全家移民国外?她说,将来女儿读大学很大可能会去国外,但基础教育阶段有没有必要出去?中国的基础教育竞争力到底强不强?大学有各种国际排行榜单,不同国家的中小学质量要如何对比?12月3日公布的第七轮国际学生评估结果(Programmefor…

    2022年5月3日
    56
  • 永恒之蓝病毒win7解决方法_win10永恒之蓝漏洞补丁

    永恒之蓝病毒win7解决方法_win10永恒之蓝漏洞补丁简介:NSA武器库的公开被称为是网络世界“核弹危机”,其中有十款影响Windows个人用户的黑客工具,包括永恒之蓝、永恒王者、永恒浪漫、永恒协作、翡翠纤维、古怪地鼠、爱斯基摩卷、文雅学者、日食之翼和尊重审查。这些工具能够远程攻破全球约70%的Windows系统,无需用户任何操作,只要联网就可以入侵电脑,就像冲击波、震荡波等著名蠕虫一样可以瞬间血洗互联网,木马黑产很可能改造NSA的武器…

    2022年10月16日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号