我的着色器可在台式机上使用,但在Android上屏幕却变黑

时间:2019-12-08 01:59:27

标签: c# android unity3d mobile shader

您可以在下面找到着色器代码。我在Unity中将其用作“图像效果着色器”。然后,将其与C#脚本(在着色器下面)一起应用于我的相机。在台式机上看起来很棒,但是在Android上,它只会使屏幕变黑。我可能正在使用在Android上无法使用的功能,也许有人可以指出正确的方向进行修复?

    //#<!--
//#    CRT-simple shader
//#
//#    Copyright (C) 2011 DOLLS. Based on cgwg's CRT shader.
//#
//#    Modified by fontmas: 2015-03-06
//#
//#    This program is free software; you can redistribute it and/or modify it
//#    under the terms of the GNU General Public License as published by the Free
//#    Software Foundation; either version 2 of the License, or (at your option)
//#    any later version.
//#    -->
Shader "Custom/CRT"
{
    Properties
    {
        _MainTex ("Base (RGB)", 2D) = "white" {}
    }

    SubShader
    {
        Pass
        {

        CGPROGRAM
        #pragma vertex vert_img
        #pragma fragment frag
        #include "UnityCG.cginc"
        #define CURVATURE
        #pragma target 3.0
        #define PI 3.141592653589
        uniform sampler2D _MainTex;
        uniform float2 _InputSize;
        uniform float2 _OutputSize;
        uniform float2 _TextureSize;
        uniform float2 _One;
        uniform float2 _Texcoord;
        uniform float _Factor;
        uniform float _Distortion = 0.1f; // 0.1f
        uniform float _Gamma = 1.0f; // 1.0f
        uniform float _curvatureSet1 = 0.5f; // 0.5f
        uniform float _curvatureSet2 = 0.5f; // 0.5f
        uniform float _YExtra = 0.5f; // 0.5f;
        uniform float _rgb1R = 1.0f; // 1.0f
        uniform float _rgb1G = 1.0f; // 1.0f
        uniform float _rgb1B = 1.0f; // 1.0f
        uniform float _rgb2R = 1.0f; // 1.0f
        uniform float _rgb2G = 1.0f; // 1.0f
        uniform float _rgb2B = 1.0f; // 1.0f
        uniform float _dotWeight = 2.0f; // 2.0f
        float2 RadialDistortion(float2 coord)
        {
            coord *= _TextureSize / _InputSize;
            float2 cc = coord - _curvatureSet1;
            float dist = dot(cc, cc) * _Distortion;
            return (coord + cc * (_curvatureSet2 + dist) * dist) * _InputSize / _TextureSize;
        }

        float4 ScanlineWeights(float distance, float4 color)
        {
            float4 width = 2.0f + 2.0f * pow(color, float4(4.0f, 4.0f, 4.0f, 4.0f));
            float4 weights = float4(distance / 0.5f, distance / 0.5f, distance / 0.5f, distance / 0.5f);
            return 1.4f * exp(-pow(weights * rsqrt(0.5f * width), width)) / (0.3f + 0.2f * width);
        }

        float4 frag(v2f_img i) : COLOR
        {
            _Texcoord = i.uv;
            _One = 1.0f / _TextureSize;
            _OutputSize = _TextureSize;
            _InputSize = _TextureSize;
            _Factor = _Texcoord.x * _TextureSize.x * _OutputSize.x / _InputSize.x;

            //float4 ScreenGamma = pow(tex2D(_MainTex, _Texcoord), _Gamma);

            #ifdef CURVATURE
            float2 xy = RadialDistortion(_Texcoord);
            #else
            float2 xy = _Texcoord;
            #endif

            float2 ratio = xy * _TextureSize - float2(0.5f, 0.5f);
            float2 uvratio = frac(ratio);

            xy.y = (floor(ratio.y) + _YExtra) / _TextureSize;
            float4 col = tex2D(_MainTex, xy);
            float4 col2 = tex2D(_MainTex, xy + float2(0.0f, _One.y));

            float4 weights = ScanlineWeights(uvratio.y, col);
            float4 weights2 = ScanlineWeights(1.0f - uvratio.y, col2);
            float3 res = (col * weights + col2 * weights2).rgb;

            float3 rgb1 = float3(_rgb1R, _rgb1G, _rgb1B);
            float3 rgb2 = float3(_rgb2R, _rgb2G, _rgb2B);

            float3 dotMaskWeights = lerp(rgb1, rgb2, floor(fmod(_Factor, _dotWeight)));
            res *= dotMaskWeights;

            return float4(pow(res, float3(1.0f / _Gamma, 1.0f / _Gamma, 1.0f / _Gamma)), 1.0f);
            //return float4(pow(res, float3(1.0f / ScreenGamma.x, 1.0f / ScreenGamma.y, 1.0f / ScreenGamma.z)), 1.0f);


        }
        ENDCG
        }
    }
}



using UnityEngine;
using System.Collections;

public enum CRTScanLinesSizes { S32 = 32, S64 = 64, S128 = 128, S256 = 256, S512 = 512, S1024 = 1024 };

[ExecuteInEditMode]
public class CRT : MonoBehaviour
{

    #region Variables
    public Shader curShader;
    public float Distortion = 0.1f;
    public float Gamma = 1.0f;
    public float YExtra = 0.5f;
    public float CurvatureSet1 = 0.5f;
    public float CurvatureSet2 = 1.0f;
    public float DotWeight = 1.0f;
    public CRTScanLinesSizes scanSize = CRTScanLinesSizes.S512;
    public Color rgb1 = Color.white;
    public Color rgb2 = Color.white;
    private Material curMaterial;

    #endregion

    #region Properties
    Material material
    {
        get
        {
            if (curMaterial == null)
            {
                curMaterial = new Material(curShader);
                curMaterial.hideFlags = HideFlags.HideAndDontSave;
            }
            return curMaterial;
        }
    }
    #endregion
    // Use this for initialization
    void Start()
    {
        if (!SystemInfo.supportsImageEffects)
        {
            enabled = false;
            return;
        }
    }

    void OnRenderImage(RenderTexture sourceTexture, RenderTexture destTexture)
    {
        if (curShader != null)
        {
            material.SetFloat("_Distortion", Distortion);
            material.SetFloat("_Gamma", Gamma);
            material.SetFloat("_curvatureSet1", CurvatureSet1);
            material.SetFloat("_curvatureSet2", CurvatureSet2);
            material.SetFloat("_YExtra", YExtra);
            material.SetFloat("_rgb1R", rgb1.r);
            material.SetFloat("_rgb1G", rgb1.g);
            material.SetFloat("_rgb1B", rgb1.b);
            material.SetFloat("_rgb2R", rgb2.r);
            material.SetFloat("_rgb2G", rgb2.g);
            material.SetFloat("_rgb2B", rgb2.b);
            material.SetFloat("_dotWeight", DotWeight);
            material.SetVector("_TextureSize", new Vector2((float)scanSize, (float)scanSize));
            Graphics.Blit(sourceTexture, destTexture, material);
        }
        else
        {
            Graphics.Blit(sourceTexture, destTexture);
        }


    }

    // Update is called once per frame
    void Update()
    {

    }

    void OnDisable()
    {
        if (curMaterial)
        {
            DestroyImmediate(curMaterial);
        }

    }


}

2 个答案:

答案 0 :(得分:0)

我不知道如何编码任何着色器,但是无论如何我都会尽力帮助您(我们永远不知道也许我能帮上点忙)。您可能应该检查android支持的内容,或者您​​的计算机支持的某些元素不在android上。使用UnityRemote时是否出现任何错误?没有着色器就可以使用吗?

我希望至少能对您有所帮助。如果没有,我们会...希望您能找到解决方案。

答案 1 :(得分:0)

可能是RenderTexture,如果在Unity中创建RenderTexture,则将深度缓冲区更改为0,如下所示:

 rt = new RenderTexture(*width*, *height*, 0, RenderTextureFormat.ARGB32);

如果您在Unity中创建RenderTexture,请将“深度缓冲区”设置为“无深度缓冲区”,

如果更改深度没有任何影响,也可能是Graphics.Blit()。