[游戏开发][Shader]ShaderToy通用模板转Unity-CG语言

 这个通用模板貌似是Candcat写的,漏了几个宏定义,我这给补一下,例如:

#define iTime _Time.y

#define atan atan2

对照表如下

 矩阵与向量相乘

//原代码,向量乘矩阵会报错。
vec3 rd = vec3(1,1,1);
mat2 matrix = mat2(1,2,3,4);
rd.xy *= matrix;
rd.xz *= matrix;

//新代码
vec3 rd = vec3(1,1,1);
mat2 matrix = mat2(1,2,3,4);
rd.xy = mul(matrix,rd.xy);
rd.xz = mul(matrix,rd.xz);
Shader "Shadertoy/Template" {
    Properties{
        iMouse("Mouse Pos", Vector) = (100, 100, 0, 0)
        iChannel0("iChannel0", 2D) = "white" {}
        iChannelResolution0("iChannelResolution0", Vector) = (100, 100, 0, 0)
    }

        CGINCLUDE
#include "UnityCG.cginc"   
#pragma target 3.0      

#define vec2 float2
#define vec3 float3
#define vec4 float4
#define mat2 float2x2
#define mat3 float3x3
#define mat4 float4x4
#define iGlobalTime _Time.y
#define iTime _Time.y
#define mod fmod
#define mix lerp
#define fract frac
#define texture2D tex2D
#define iResolution _ScreenParams
#define gl_FragCoord ((_iParam.scrPos.xy/_iParam.scrPos.w) * _ScreenParams.xy)

#define PI2 6.28318530718
#define pi 3.14159265358979
#define halfpi (pi * 0.5)
#define oneoverpi (1.0 / pi)

            fixed4 iMouse;
        sampler2D iChannel0;
        fixed4 iChannelResolution0;

        struct v2f {
            float4 pos : SV_POSITION;
            float4 scrPos : TEXCOORD0;
        };

        v2f vert(appdata_base v) {
            v2f o;
            o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
            o.scrPos = ComputeScreenPos(o.pos);
            return o;
        }

        vec4 main(vec2 fragCoord);

        fixed4 frag(v2f _iParam) : COLOR0{
            vec2 fragCoord = gl_FragCoord;
            return main(gl_FragCoord);
        }

            vec4 main(vec2 fragCoord) {
            return vec4(1, 1, 1, 1);
        }

        ENDCG

            SubShader{
                Pass {
                    CGPROGRAM

                    #pragma vertex vert    
                    #pragma fragment frag    
                    #pragma fragmentoption ARB_precision_hint_fastest     

                    ENDCG
                }
        }
            FallBack Off
}

 测试Shader的代码,把这个脚本挂到相机上,把shader托进去即可,如果有的shader需要material,则把material拖进去。

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

[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class ScreenEffect : MonoBehaviour
{

    public Shader effectShader;
    public Material effectMaterial;
    public Material EffectMaterial
    {
        get
        {
            if (effectMaterial == null)
            {
                effectMaterial = CreateMaterial(effectShader, effectMaterial);
                return effectMaterial;
            }
            else
            {
                return effectMaterial;
            }
        
        }
    }

    [Range(0.0f, 3.0f)]
    public float brightness = 1.0f;  //亮度

    [Range(0.0f, 3.0f)]
    public float saturation = 1.0f;  //饱和度

    [Range(0.0f, 3.0f)]
    public float contrast = 1.0f;    //对比度


    void OnRenderImage(RenderTexture src, RenderTexture dest)
    {
        if (EffectMaterial != null)
        {
            EffectMaterial.SetFloat("_Brightness", brightness);
            EffectMaterial.SetFloat("_Saturation", saturation);
            EffectMaterial.SetFloat("_Contrast", contrast);
            //对渲染纹理进行处理
            Graphics.Blit(src, dest, EffectMaterial);
        }
        else
        {
            Graphics.Blit(src, dest);
        }
    }

    private void Start()
    {
        bool isSupported = CheckSupport();

        if (isSupported == false)
        {
            this.enabled = false;
        }
    }


    // 检测当前平台是否支持屏幕特效
    private bool CheckSupport()
    {
        if (SystemInfo.supportsImageEffects == false || SystemInfo.supportsRenderTextures == false)
        {
            Debug.LogWarning("当前平台不支持!");
            return false;
        }
        return true;
    }


    // 创建材质
    private Material CreateMaterial(Shader shader, Material material)
    {
        if (shader == null)
        {
            return null;
        }

        if (shader.isSupported && material && material.shader == shader)
            return material;

        if (!shader.isSupported)
        {
            return null;
        }
        else
        {
            material = new Material(shader);
            material.hideFlags = HideFlags.DontSave;
            if (material)
                return material;
            else
                return null;
        }
    }
}

你可能感兴趣的:(渲染与shader,unity,图形渲染)