为移动设备优化球形蒙版着色器

时间:2019-03-04 11:23:32

标签: unity3d optimization shader

我使用的着色器对于移动性能非常重要。有人可以帮助我改进它以获得更好的性能吗?

我注意到使用此着色器的FPS从60变为30。

Shader "Custom/Sphered2"
{Properties
{
  [HDR] _Color ("Color", Color) = (1,1,1,1)

    _MainTex ("Albedo (RGB)", 2D) = "white" {}
    _ColorStrength("Color Strenth", Range(1,4)) = 1

    _EmissionColor ("_EmissionColor Color", Color) = (1,1,1,1)
    _EmissionTex ("_EmissionColor (RGB)", 2D) = "white" {}
    _EmissionStrength("_EmissionColor Strenth", Range(0,4)) = 1

    _Glossiness ("Smoothness", Range(0,1)) = 0.5
    _Metallic ("Metallic", Range(0,1)) = 0.0
//    _position ("World Position", Vector) = (0,0,0,0)
  //  _Radius ("Sphere Radius", Range(0,100)) = 0
    //_Softness("Sphere Softness", Range(0,100)) = 0
}
SubShader
{
    Tags { "RenderType"="Opaque" }
    LOD 200

    CGPROGRAM
    // Physically based Standard lighting model, and enable shadows on all light types
    #pragma surface surf Standard fullforwardshadows

    // Use shader model 3.0 target, to get nicer looking lighting
    #pragma target 3.0

    sampler2D _MainTex,_EmissionTex;

    struct Input
    {
        float2 uv_MainTex;
        float2 uv_EmissionTex;
        float3 worldPos;
    };

    half _Glossiness;
    half _Metallic;
     fixed4 _Color,_EmissionColor;
    half _ColorStrength,_EmissionStrength;

  uniform     float4 GLOBALmask_position2;
uniform    half GLOBALmask_Radius2;
uniform    half  GLOBALmask_Softness;
    // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    // #pragma instancing_options assumeuniformscaling
    UNITY_INSTANCING_BUFFER_START(Props)
        // put more per-instance properties here
    UNITY_INSTANCING_BUFFER_END(Props)

    void surf (Input IN, inout SurfaceOutputStandard o)
    {
        // Albedo comes from a texture tinted by color
        fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
        half grayscale = (c.r + c.g + c.b) * 0.333;
        fixed3 cgg = fixed3(grayscale,grayscale,grayscale);

fixed4 e = tex2D (_EmissionTex, IN.uv_EmissionTex) * _EmissionColor *_EmissionStrength;

        half d = distance(GLOBALmask_position2,IN.worldPos);
        half sum = saturate((d - GLOBALmask_Radius2)/ -GLOBALmask_Softness);
        fixed4 lerpColor = lerp(fixed4(cgg,1),c * _ColorStrength,sum);
        fixed4 lerpEmission = lerp(fixed4(0,0,0,0),e,sum);

        o.Albedo =c.rgb;
        // Metallic and smoothness come from slider variables
        o.Metallic = _Metallic;
        o.Emission = lerpEmission.rgb;
        o.Smoothness = _Glossiness;
        o.Alpha = c.a;
    }
    ENDCG
}
FallBack "Diffuse"
  }

我这样使用它:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RayCastMovePos : MonoBehaviour
{
public Transform playerTransform;
public float radius2,sm;
public Vector4 myPos2;

void Update()
{
    myPos2= new Vector4 ( playerTransform.position.x , 
playerTransform.position.y, playerTransform.position.z, 0); 

    //radius =Mathf .Clamp ( ( myVisualizer.allRythem 
[0]+myVisualizer.allRythem [1]+myVisualizer.allRythem [2]) * 170,2,1000);
//  radius2 =Mathf .Clamp ( ( myVisualizer.allRythem 
[3]+myVisualizer.allRythem [4]+myVisualizer.allRythem [5]) * 150,2,1000);

    Shader.SetGlobalVector    ("GLOBALmask_position2", myPos2);

    Shader.SetGlobalFloat  ("GLOBALmask_Radius2", radius2);
    Shader.SetGlobalFloat  ("GLOBALmask_Softness", sm );


   }
 }

我是着色器的新手,但是如果有任何发光精灵的建议,我会很高兴听到。

1 个答案:

答案 0 :(得分:0)

如果这是用于精灵,那么您真的需要使用表面着色器吗?这些包括大量的开销,例如光照贴图支持,阴影投射/接收,前向光支持等。我相信顶点/片段着色器可以很好地满足您的目的。您只需在最终输出中添加额外的颜色即可获得发光效果。

这是Unity的精灵着色器主体的外观:

https://github.com/TwoTailsGames/Unity-Built-in-Shaders/blob/master/CGIncludes/UnitySprites.cginc