根据屏幕尺寸

时间:2017-08-02 19:43:14

标签: unity3d shader shaderlab

我想画一个圆圈,里面用红色填充,外面用黑色填充。

我想从代码中设置圆圈的大小。 0是最小尺寸,100是圆的最大尺寸,100表示圆圈必须覆盖屏幕。

以下是代码:

Shader "Hidden/Circle"
{
    Properties
    {
        _MainTex("Texture", 2D) = "white" {}
    _radius("Radius",Range(0, 100)) = 100
        _radiusOffet("Offset",Range(0, 100)) = 72
        _discardedColor("Dicard Color", Color) = (0, 0, 0, 0) // color
    }
        SubShader
    {
        //Tags{ "Queue" = "Transparent" }
        // No culling or depth
        Cull Off ZWrite Off ZTest Always

        Pass
    {
        CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "UnityCG.cginc"


    sampler2D _MainTex;
    float4 _discardedColor;

    float _radius;
    float _radiusOffet;

    float minMinSlide = 0; //0.314
    float minMaxSlide = 100.0;

    struct v2f
    {
        float2 uv : TEXCOORD0;
    };

    v2f vert(
        float4 vertex : POSITION, // vertex position input
        float2 uv : TEXCOORD0, // texture coordinate input
        out float4 outpos : SV_POSITION // clip space position output
    )
    {
        v2f o;
        o.uv = uv;
        outpos = UnityObjectToClipPos(vertex);
        return o;
    }

    float mapValue(float mainValue, float inValueMin, float inValueMax, float outValueMin, float outValueMax)
    {
        return (mainValue - inValueMin) * (outValueMax - outValueMin) / (inValueMax - inValueMin) + outValueMin;
    }

    fixed4 frag(v2f i, UNITY_VPOS_TYPE screenPos : VPOS) : SV_Target
    {
        //Set default values
        minMinSlide = 0;
    minMaxSlide = 100;

    //fixed4 col = tex2D(_MainTex, i.uv);

    float4 fragColor = 0;
    //float2 fragCoord = i.vertex.xy;
    float2 fragCoord = screenPos;
    float2 screenCoord = fragCoord.xy;

    float minOutRadius = 0;

    //THIS IS WRONG? WHAT'S THE PROPER WAY OF DOING THIS?
    float maxOutRadius = (_ScreenParams.x / 2) + _radiusOffet;

    //Calcuate the scaled radius
    float rad = mapValue(_radius, minMinSlide, minMaxSlide, minOutRadius, maxOutRadius);

    float2 middlePoint = _ScreenParams.xy * 0.5 - screenCoord;
    float middleLength = middlePoint.x * middlePoint.x + middlePoint.y * middlePoint.y;
    if (middleLength > rad * rad)
    {
        //Use black color
        fragColor = _discardedColor;
    }
    else {
        //Use red color
        fragColor = float4(1, 0, 0, 0);
    }
    return fragColor;
    }
        ENDCG
    }
    }
}

此代码在编辑器上100%有效。

Windows和Android版本中存在一个小问题。

问题是,当值设置为100时,圆圈不会完全覆盖屏幕100%。这在编辑器中工作正常。

我认为问题在于这一行:

float maxOutRadius = (_ScreenParams.x / 2) + _radiusOffet;

如何根据设备的屏幕尺寸正确计算圆的最大半径?

这行代码只能在编辑器上正常工作。

1 个答案:

答案 0 :(得分:2)

圆的最大半径是从中心到屏幕角落的距离。那将是:

float x = _ScreenParams.x / 2;
float y = _ScreenParams.y / 2;
float maxOutRadius = sqrt(x*x+y*y); // + _radiusOffet; 

你可以使着色器更简单顺便说一下。例如:

uniform float _Percent;   
fixed4 frag (float4 pos:VPOS) : SV_Target
{
    float4 col;
    float2 center = _ScreenParams.xy/2;

    float maxradius = length(center); 
    float radius = maxradius * abs(sin(_Time.y)); // * (_Percent/100);

    if(distance(pos.xy, center) > radius){
        col = float4(0,0,0,1);
    }else{
        col = float4(1,0,0,1);
    }
    return col;
}