【问题标题】:alpha value captured by camera in iosios中相机捕获的alpha值
【发布时间】:2015-02-05 01:29:24
【问题描述】:

我正在尝试使用 RenderTexture 制作假阴影

我动态地创建一个相机,并将一个 RenderTexture 分配给相机作为渲染目标。 RenderTexture 的格式设置为 ARGB32

然后我使用 RenderTexture 作为平面上的纹理,使用自定义着色器将颜色更改为黑色并调整 alpha 值,以便正确显示阴影

下面是我使用的着色器:

Shader "Fake Shadow" {
    Properties {
        _ShadowTex ("Fake Shadow", 2D) = "white" { TexGen ObjectLinear }
        _SAlpha ("Shadow Intensity", float) = 0.35
    }
    Category {
        Tags { "Queue" = "Transparent-1" }
        Lighting Off
        ZWrite On
        Cull Back
        Blend SrcAlpha OneMinusSrcAlpha, one one
        Subshader { 
            LOD 200

            Pass {
                SetTexture[_ShadowTex] {
                    ConstantColor(0,0,0,[_SAlpha])
                    matrix [_ProjMatrix] 
                      Combine texture * constant, texture * constant
                }
            }
        }
    }
}

当我在 Unity3D 编辑器和 Android 上测试游戏时,一切正常 但是,在ios中结果不正确

结果图片在下面的链接中(抱歉我还不能发布图片)

shadow in editor
shadow in ios

似乎 ios 在 RenderTexture 中将 alpha 设置为 1,即使是在相机没有捕捉到任何内容的像素上

我找到了解决此问题的方法,即在播放器设置中检查 32 位显示 但我担心检查 32 位显示会影响性能

有没有其他不检查32位显示的解决方案?

【问题讨论】:

    标签: ios unity3d


    【解决方案1】:

    我正在做同样的事情(用于模拟阴影的相机和投影仪),并且我使用基于此页面上的着色器:http://en.wikibooks.org/wiki/Cg_Programming/Unity/Projectors

    着色器如下:

    Shader "Cg projector shader for drop shadows" {
       Properties {
          _ShadowTex ("Projected Image", 2D) = "white" {}
          _ShadowStrength ("Shadow Strength", Float) = 0.65
       }
       SubShader {
          Pass {      
    //          Blend Off
              Blend Zero OneMinusSrcAlpha // attenuate color in framebuffer 
                // by 1 minus alpha of _ShadowTex
                // fb_color = black + fb_color * OneMinusShadowAlpha;
             CGPROGRAM
    
             #pragma vertex vert
             #pragma fragment frag
    
             // User-specified properties
             uniform sampler2D _ShadowTex;
    
             uniform float _ShadowStrength;
    
             // Projector-specific uniforms
             uniform float4x4 _Projector; // transformation matrix 
                // from object space to projector space 
    
              struct vertexInput {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
             };
             struct vertexOutput {
                float4 pos : SV_POSITION;
                float4 posProj : TEXCOORD0;
                   // position in projector space
             };
    
             vertexOutput vert(vertexInput input) 
             {
                vertexOutput output;
    
                output.posProj = mul(_Projector, input.vertex);
                output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
                return output;
             }
    
    
             float4 frag(vertexOutput input) : COLOR
             {
                if (input.posProj.w > 0.0) // in front of projector?
                {
                   return tex2D(_ShadowTex , 
                      float2(input.posProj) / input.posProj.w) * _ShadowStrength;
                }
                else // behind projector
                {
                   return float4(0.0);
                }
             }
    
             ENDCG
          }
       }  
       // The definition of a fallback shader should be commented out 
       // during development:
       // Fallback "Projector/Light"
    }
    

    【讨论】:

    • 你的意思是相机渲染到RenderTexture,并用投影仪显示阴影?
    • 是的。其他需要注意的是,该相机具有明确的标志 = “纯色”,背景 = 白色,alpha = 0。它有一个面具,所以它只能看到你需要假阴影的物体,绝对不能看到地面。同样,投影仪忽略除地面之外的所有层。如果您有其他问题,请告诉我,因为我的设置听起来和您想要的一样。您希望将黑点投射到地面上,而黑点是物体的非彩色渲染,是吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    • 1970-01-01
    相关资源
    最近更新 更多