Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我的着色器在桌面上工作,但在Android上屏幕变黑_C#_Android_Unity3d_Mobile_Shader - Fatal编程技术网

C# 我的着色器在桌面上工作,但在Android上屏幕变黑

C# 我的着色器在桌面上工作,但在Android上屏幕变黑,c#,android,unity3d,mobile,shader,C#,Android,Unity3d,Mobile,Shader,您可以在下面找到着色器代码。我是作为Unity中的“图像效果着色器”来做这件事的。然后我将其与C#脚本(在着色器下方)一起应用到我的相机上。在桌面上看起来很棒,但在Android上它只会让屏幕变黑。我可能正在使用一些在Android上不起作用的东西,也许有人能为我指出正确的方向来修复它 //#<!-- //# CRT-simple shader //# //# Copyright (C) 2011 DOLLS. Based on cgwg's CRT 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);
        }

    }


}
//#
着色器“自定义/CRT”
{
性质
{
_MainTex(“基本(RGB)”,2D)=“白色”{}
}
子阴影
{
通过
{
CGP程序
#pragma顶点顶点
#布拉格碎片碎片
#包括“UnityCG.cginc”
#定义曲率
#布拉格目标3.0
#定义PI 3.141592653589
均匀取样器2D_MainTex;
均匀浮动2_输入大小;
统一浮点2_输出大小;
均匀浮动2_纹理化;
均匀浮动2_一;
均匀浮动2_Texcoord;
均匀浮动系数;
均匀浮动_失真=0.1f;//0.1f
均匀浮点_Gamma=1.0f;//1.0f
均匀浮动_曲率t1=0.5f;//0.5f
均匀浮动_曲率t2=0.5f;//0.5f
均匀浮动_YExtra=0.5f;//0.5f;
均匀浮点_rgb1R=1.0f;//1.0f
均匀浮点_rgb1G=1.0f;//1.0f
均匀浮点_rgb1B=1.0f;//1.0f
均匀浮点_rgb2R=1.0f;//1.0f
均匀浮点_rgb2G=1.0f;//1.0f
均匀浮点_rgb2B=1.0f;//1.0f
均匀浮动_dotwweight=2.0f;//2.0f
浮动2径向变形(浮动2坐标)
{
坐标*=\u纹理化/\u输入大小;
float2 cc=坐标-曲率t1;
浮动距离=点(cc,cc)*\u失真;
返回(坐标+cc*(_曲率t2+dist)*dist)*_输入大小/_纹理化;
}
浮动4扫描线宽(浮动距离、浮动4颜色)
{
浮动4宽度=2.0f+2.0f*pow(颜色,浮动4(4.0f,4.0f,4.0f,4.0f));
浮动4重量=浮动4(距离/0.5f,距离/0.5f,距离/0.5f,距离/0.5f);
返回1.4f*exp(-pow(重量*rsqrt(0.5f*宽度),宽度))/(0.3f+0.2f*宽度);
}
浮动4框架(v2f_img i):颜色
{
_Texcoord=i.uv;
_一个=1.0f/_纹理化;
_OutputSize=_TextureSize;
_InputSize=_TextureSize;
_因子=Texcoord.x*TextureSize.x*OutputSize.x/InputSize.x;
//浮动4屏幕伽马=功率(tex2D(_MainTex,_Texcoord),_伽马);
#ifdef曲率
float2 xy=径向畸变(_Texcoord);
#否则
浮动2 xy=_Texcoord;
#恩迪夫
浮动2比率=xy*_纹理化-浮动2(0.5f,0.5f);
浮动比率=分形(比率);
xy.y=(地板(比率y)+纹理化;
float4 col=tex2D(_MainTex,xy);
float4 col2=tex2D(_MainTex,xy+float2(0.0f,_One.y));
float4权重=扫描线宽(uvratio.y,col);
float4-weights s2=扫描线宽(1.0f-uvratio.y,col2);
float3 res=(col*权重+col2*权重2).rgb;
float3 rgb1=float3(_rgb1R,_rgb1G,_rgb1B);
float3 rgb2=float3(_rgb2R,_rgb2G,_rgb2B);
浮动3点加权=lerp(rgb1,rgb2,楼层(fmod(_因子,_点加权));
res*=点马斯克威茨;
返回浮点4(功率(res,浮点3(1.0f/_伽马,1.0f/_伽马,1.0f/_伽马)),1.0f);
//返回浮点4(功率(分辨率,浮点3(1.0f/ScreenGamma.x,1.0f/ScreenGamma.y,1.0f/ScreenGamma.z)),1.0f;
}
ENDCG
}
}
}
使用UnityEngine;
使用系统集合;
公共枚举crtscanlineassizes{S32=32,S64=64,S128=128,S256=256,S512=512,S1024=1024};
[执行编辑模式]
公共类CRT:单一行为
{
#区域变量
公共着色器游标;
公共浮动失真=0.1f;
公共浮动伽马=1.0f;
公共浮动YExtra=0.5f;
公共浮子曲率t1=0.5f;
公共浮子曲率t2=1.0f;
公共浮点数权重=1.0f;
公共CRTScanLinesSizes scanSize=CRTScanLinesSizes.S512;
公共颜色rgb1=Color.white;
公共颜色rgb2=Color.white;
私人材料;
#端区
#区域属性
材料
{
得到
{
如果(curMaterial==null)
{
curMaterial=新材料(游标);
curMaterial.hideFlags=hideFlags.hideAndOntSave;
}
退料;
}
}
#端区
//用于初始化
void Start()
{
如果(!SystemInfo.supportsImageEffects)
{
启用=错误;
返回;
}
}
void OnRenderImage(RenderTexture sourceTexture、RenderTexture destTexture)
{
if(游标!=null)
{
材料。设置浮动(“变形”,变形);
材料。设置浮动(“伽马”,伽马);
材料。设置浮动(“曲率1”,曲率1);
材料。设置浮动(“曲率设置2”,曲率设置2);
材料。设置浮动(“YExtra”,YExtra);
设置浮动(“rgb1R”,rgb1.r);
设置浮动(“rgb1G”,rgb1.g);
设置浮动(“rgb1B”,rgb1.b);
material.SetFloat(“_rgb2R”,rgb2.r);
设置浮动(“rgb2G”,rgb2.g);
material.SetFloat(“_rgb2B”,rgb2.b);
材料。设置浮点数(“_点权重”,点权重);
material.SetVector(“_TextureSize”,新的Vector2((float)scanSize,(float)scanSize));
图形.Blit(源纹理、目标纹理、材质);
}
其他的
{
Blit(sourceTexture,destTexture);
}
}
//每帧调用一次更新
无效更新()
{
 rt = new RenderTexture(*width*, *height*, 0, RenderTextureFormat.ARGB32);