Unity3d 此水着色器的哪些部分负责透明度?

Unity3d 此水着色器的哪些部分负责透明度?,unity3d,shader,hlsl,Unity3d,Shader,Hlsl,我这里有一个水着色器,我想知道什么代码负责透明度 目前水是透明的,这是我想改变的另一件事。我希望RenderType是“RenderType”=“透明的”,但它是“RenderType”=“不透明的”。。。。我在这个着色器中找不到透明代码 也许你知道部分代码导致了透明度 这是水着色器 Shader "FX/Water" { Properties { _WaveScale ("Wave scale", Range (0.02,0.15)) = 0.063 _ReflDisto

我这里有一个水着色器,我想知道什么代码负责透明度

目前水是透明的,这是我想改变的另一件事。我希望RenderType是“RenderType”=“透明的”,但它是“RenderType”=“不透明的”。。。。我在这个着色器中找不到透明代码

也许你知道部分代码导致了透明度

这是水着色器

Shader "FX/Water" {
 Properties {
     _WaveScale ("Wave scale", Range (0.02,0.15)) = 0.063
     _ReflDistort ("Reflection distort", Range (0,1.5)) = 0.44
     _RefrDistort ("Refraction distort", Range (0,1.5)) = 0.40
     _RefrColor ("Refraction color", COLOR)  = ( .34, .85, .92, 1)
     [NoScaleOffset] _Fresnel ("Fresnel (A) ", 2D) = "gray" {}
     [NoScaleOffset] _BumpMap ("Normalmap ", 2D) = "bump" {}
     WaveSpeed ("Wave speed (map1 x,y; map2 x,y)", Vector) = (19,9,-16,-7)
     [NoScaleOffset] _ReflectiveColor ("Reflective color (RGB) fresnel (A)", 2D) = "" {}
     _HorizonColor ("Simple water horizon color", COLOR)  = ( .172, .463, .435, 1)
     [HideInInspector] _ReflectionTex ("Internal Reflection", 2D) = "" {}
     [HideInInspector] _RefractionTex ("Internal Refraction", 2D) = "" {}

     //
     _EmissionColor("Color", Color) = (0.000000,0.000000,0.000000,1.000000)
     _EmissionMap("Emission", 2D) = "white" { }
     [Toggle] _DynamicEmissionLM("Dynamic Emission (Lightmapper)", Int) = 0
 }


 // -----------------------------------------------------------
 // Fragment program cards


 Subshader {
     Tags { "WaterMode"="Refractive" "RenderType"="Opaque" }
     Pass {
 CGPROGRAM
 #pragma vertex vert
 #pragma fragment frag
 #pragma multi_compile_fog
 #pragma multi_compile WATER_REFRACTIVE WATER_REFLECTIVE WATER_SIMPLE

 #if defined (WATER_REFLECTIVE) || defined (WATER_REFRACTIVE)
 #define HAS_REFLECTION 1
 #endif
 #if defined (WATER_REFRACTIVE)
 #define HAS_REFRACTION 1
 #endif


 #include "UnityCG.cginc"

 uniform float4 _WaveScale4;
 uniform float4 _WaveOffset;

 #if HAS_REFLECTION
 uniform float _ReflDistort;
 #endif
 #if HAS_REFRACTION
 uniform float _RefrDistort;
 #endif

 struct appdata {
     float4 vertex : POSITION;
     float3 normal : NORMAL;
 };

 struct v2f {
     float4 pos : SV_POSITION;
     #if defined(HAS_REFLECTION) || defined(HAS_REFRACTION)
         float4 ref : TEXCOORD0;
         float2 bumpuv0 : TEXCOORD1;
         float2 bumpuv1 : TEXCOORD2;
         float3 viewDir : TEXCOORD3;
     #else
        float2 bumpuv0 : TEXCOORD0;
         float2 bumpuv1 : TEXCOORD1;
         float3 viewDir : TEXCOORD2;
     #endif
     UNITY_FOG_COORDS(4)
 };

 v2f vert(appdata v)
 {
     v2f o;
     o.pos = UnityObjectToClipPos(v.vertex);


     // scroll bump waves
     float4 temp;
     float4 wpos = mul (unity_ObjectToWorld, v.vertex);
     //temp.xyzw = wpos.xzxz * _WaveScale4 + _WaveOffset;
     temp.xyzw = wpos.yzyz * _WaveScale4 + _WaveOffset;
     o.bumpuv0 = temp.xy;
     o.bumpuv1 = temp.wz;

     // object space view direction (will normalize per pixel)
     o.viewDir.xzy = WorldSpaceViewDir(v.vertex);

     #if defined(HAS_REFLECTION) || defined(HAS_REFRACTION)
     o.ref = ComputeNonStereoScreenPos(o.pos);
     #endif

     UNITY_TRANSFER_FOG(o,o.pos);
     return o;
 }

 #if defined (WATER_REFLECTIVE) || defined (WATER_REFRACTIVE)
 sampler2D _ReflectionTex;
 #endif
 #if defined (WATER_REFLECTIVE) || defined (WATER_SIMPLE)
 sampler2D _ReflectiveColor;
 #endif
 #if defined (WATER_REFRACTIVE)
 sampler2D _Fresnel;
 sampler2D _RefractionTex;
 uniform float4 _RefrColor;
 #endif
 #if defined (WATER_SIMPLE)
 uniform float4 _HorizonColor;
 #endif
 sampler2D _BumpMap;

 half4 frag( v2f i ) : SV_Target
 {
     i.viewDir = normalize(i.viewDir);

     // combine two scrolling bumpmaps into one
     half3 bump1 = UnpackNormal(tex2D( _BumpMap, i.bumpuv0 )).rgb;
     half3 bump2 = UnpackNormal(tex2D( _BumpMap, i.bumpuv1 )).rgb;
     half3 bump = (bump1 + bump2) * 0.5;

     // fresnel factor
     half fresnelFac = dot( i.viewDir, bump );

     // perturb reflection/refraction UVs by bumpmap, and lookup colors

     #if HAS_REFLECTION
     float4 uv1 = i.ref; uv1.xy += bump * _ReflDistort;
     half4 refl = tex2Dproj( _ReflectionTex, UNITY_PROJ_COORD(uv1) );
     #endif
     #if HAS_REFRACTION
     float4 uv2 = i.ref; uv2.xy -= bump * _RefrDistort;
     half4 refr = tex2Dproj( _RefractionTex, UNITY_PROJ_COORD(uv2) ) * _RefrColor;
     #endif

     // final color is between refracted and reflected based on fresnel
     half4 color;

 #if defined(WATER_REFRACTIVE)
     half fresnel = UNITY_SAMPLE_1CHANNEL( _Fresnel,         
      float2(fresnelFac,fresnelFac) );
     color = lerp( refr, refl, fresnel );
     #endif

     #if defined(WATER_REFLECTIVE)
     half4 water = tex2D( _ReflectiveColor, float2(fresnelFac,fresnelFac) );
     color.rgb = lerp( water.rgb, refl.rgb, water.a );
     color.a = refl.a * water.a;
     #endif

     #if defined(WATER_SIMPLE)
     half4 water = tex2D( _ReflectiveColor, float2(fresnelFac,fresnelFac) );
     color.rgb = lerp( water.rgb, _HorizonColor.rgb, water.a );
     color.a = _HorizonColor.a;
     #endif

     UNITY_APPLY_FOG(i.fogCoord, color);

     //o.Emission = c.rgb * tex2D(_Illum, IN.uv_Illum).a;


     return color;
 }
 ENDCG

     }
 }

 }
#pragma multi_compile WATER_REFLECTIVE WATER_REFLECTIVE WATER_SIMPLE
行允许脚本根据全局配置具有不同的模式。从文件中:

多重编译的工作原理

指令示例:

#pragma multi_compile FANCY_STUFF_OFF FANCY_STUFF_ON
此示例指令生成两个着色器变体:一个禁用了FANCY_STUFF_,另一个启用了FANCY_STUFF。在运行时,Unity基于材质或全局着色器关键字激活其中一个。如果这两个关键字都未启用,那么Unity将使用第一个关键字(在本例中,FANCY_STUFF_OFF)

因此,在这个问题中,
WATER\u
在默认情况下处于启用状态

如果查看片段着色器,您将看到以下
#ifdefined
部分:

#if defined(WATER_REFRACTIVE)
half fresnel = UNITY_SAMPLE_1CHANNEL( _Fresnel,         
float2(fresnelFac,fresnelFac) );
color = lerp( refr, refl, fresnel );
#endif
要回答您的直接问题,透明度效果是折射颜色
refr
在反射颜色
refl
上可见,这是由
\u Fresnel
\u BumpMap
纹理确定的

通过将此处的着色器更改为仅使用
color=refl,可以使水完全反射取而代之

但是,另一种方法是首先更改着色器的模式,方法是在水的
材质上使用
以启用
水\u SIMPLE
关键字:

water.material.EnableKeyword(“water_SIMPLE”);
执行此操作将使用顶点着色器中的此
#ifdefined
部分,而不是前面提到的部分:

#if defined(WATER_SIMPLE)
half4 water = tex2D( _ReflectiveColor, float2(fresnelFac,fresnelFac) );
color.rgb = lerp( water.rgb, _HorizonColor.rgb, water.a );
color.a = _HorizonColor.a;
#endif


我们可以看到,它将输出颜色的alpha设置为
\u HorizonColor.a
。这意味着使用此模式,您可以通过更改
\u HorizonColor

的alpha直接设置水的透明度。请编辑问题,将其限制为特定问题,并提供足够详细的信息,以确定适当的答案。避免同时问多个不同的问题。请参阅页面以获取澄清此问题的帮助。我尝试编辑它,但我有两个问题,并在文本中显示了它们。我需要向该着色器添加一个发射,并且需要知道哪个代码对透明度负责。fotr说那个人需要阴影代码来帮助我。我希望它现在更好我已经编辑了这个问题,使它关于一个特定的问题,这样它对于堆栈溢出来说就不会太宽了。很好的广泛的回答:)虽然简短的版本是“这个着色器是不透明的”(它只是看起来像它,因为它显示了一个捕捉到的背后的纹理)很好的回答非常有用