美文网首页
扭曲效果

扭曲效果

作者: 星易乾川 | 来源:发表于2018-07-25 14:51 被阅读0次

用GrabPass抓屏,法线图扰动UV,最好是自己用脚本抓屏,应该会省点,主要是好进行后续控制

扭曲.jpg
Shader "Artist/Effect/Distort" 
{
    Properties
    {
        _DistortStrength("DistortStrength", Range(0,1)) = 0.2
        _DistortTimeFactor("DistortTimeFactor", Range(0,1)) = 1
        _NoiseTex("NoiseTexture", 2D) = "white" {}
    }
    SubShader
    {
        ZWrite Off
        Cull Off
        
        GrabPass
        {
            
            "_GrabTex"
        }
 
        Pass
        {
            Tags
            { 
                "RenderType" = "Transparent"
                "Queue" = "Transparent + 100"
            }
 
            CGPROGRAM
            sampler2D _GrabTex;
            float4 _GrabTex_ST;
            sampler2D _NoiseTex;
            float4 _NoiseTex_ST;
            float _DistortStrength;
            float _DistortTimeFactor;
            #include "UnityCG.cginc"
            struct v2f
            {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
                float4 grabPos : TEXCOORD1;
            };
 
            v2f vert(appdata_base v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.grabPos = ComputeGrabScreenPos(o.pos);
                o.uv = TRANSFORM_TEX(v.texcoord, _NoiseTex);
                return o;
            }
 
            fixed4 frag(v2f i) : SV_Target
            {
                
                float4 offset = tex2D(_NoiseTex, i.uv - _Time.xy * _DistortTimeFactor);
                
                i.grabPos.xy -= offset.xy * _DistortStrength;
                
                fixed4 color = tex2Dproj(_GrabTex, i.grabPos);
                return color;
            }
 
            #pragma vertex vert
            #pragma fragment frag
            ENDCG
        }
    }
}


据说会有安卓机上GrabPass读帧缓存卡死的问题,那么我转载了另一篇方法也很不错

先做全屏后处理,然后渲染一张黑白mask作为遮罩的Texture,代码如下:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class DistortEffect : PostEffectBase {
 
  
    [Range(0.0f, 1.0f)]
    public float DistortTimeFactor = 0.15f;
  
    [Range(0.0f, 0.2f)]
    public float DistortStrength = 0.01f;

    public Texture NoiseTexture = null;
 
    public Shader maskObjShader = null;
 
    public int downSample = 4;
 
    private Camera mainCam = null;
    private Camera additionalCam = null;
    private RenderTexture renderTexture = null;
 
    public void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        if (_Material)
        {
            _Material.SetTexture("_NoiseTex", NoiseTexture);
            _Material.SetFloat("_DistortTimeFactor", DistortTimeFactor);
            _Material.SetFloat("_DistortStrength", DistortStrength);
            _Material.SetTexture("_MaskTex", renderTexture);
            Graphics.Blit(source, destination, _Material);
        }
        else
        {
            Graphics.Blit(source, destination);
        }
    }
 
    void Awake()
    {
      
        InitAdditionalCam();
    }
 
    private void InitAdditionalCam()
    {
        mainCam = GetComponent<Camera>();
        if (mainCam == null)
            return;
 
        Transform addCamTransform = transform.FindChild("additionalDistortCam");
        if (addCamTransform != null)
            DestroyImmediate(addCamTransform.gameObject);
 
        GameObject additionalCamObj = new GameObject("additionalDistortCam");
        additionalCam = additionalCamObj.AddComponent<Camera>();
 
        SetAdditionalCam();
    }
 
    private void SetAdditionalCam()
    {
        if (additionalCam)
        {
            additionalCam.transform.parent = mainCam.transform;
            additionalCam.transform.localPosition = Vector3.zero;
            additionalCam.transform.localRotation = Quaternion.identity;
            additionalCam.transform.localScale = Vector3.one;
            additionalCam.farClipPlane = mainCam.farClipPlane;
            additionalCam.nearClipPlane = mainCam.nearClipPlane;
            additionalCam.fieldOfView = mainCam.fieldOfView;
            additionalCam.backgroundColor = Color.clear;
            additionalCam.clearFlags = CameraClearFlags.Color;
            additionalCam.cullingMask = 1 << LayerMask.NameToLayer("Distort");
            additionalCam.depth = -999;
            
            if (renderTexture == null)
                renderTexture = RenderTexture.GetTemporary(Screen.width >> downSample, Screen.height >> downSample, 0);
        }
    }
 
    void OnEnable()
    {
        SetAdditionalCam();
        additionalCam.enabled = true;
    }
 
    void OnDisable()
    {
        additionalCam.enabled = false;
    }
 
    void OnDestroy()
    {
        if (renderTexture)
        {
            RenderTexture.ReleaseTemporary(renderTexture);
        }
        DestroyImmediate(additionalCam.gameObject);
    }
 
   
    void OnPreRender()
    {
       
        if (additionalCam.enabled)
        {
            additionalCam.targetTexture = renderTexture;
            additionalCam.RenderWithShader(maskObjShader, "");
        }
    }
}

以下shader挂在模型上,对全屏图片处理并使用mask做遮罩



Shader "Custom/DistortPostEffect"
{
    Properties
    {
        _MainTex("Base (RGB)", 2D) = "white" {}
        _NoiseTex("Noise", 2D) = "black" {}
        _MaskTex("Mask", 2D) = "black" {}
    }
 
    CGINCLUDE
    #include "UnityCG.cginc"
    uniform sampler2D _MainTex;
    uniform sampler2D _NoiseTex;
    uniform sampler2D _MaskTex;
    uniform float _DistortTimeFactor;
    uniform float _DistortStrength;
 
    fixed4 frag(v2f_img i) : SV_Target
    {
    
        
        float4 noise = tex2D(_NoiseTex, i.uv - _Time.xy * _DistortTimeFactor);
        
        float2 offset = noise.xy * _DistortStrength;
        
        fixed4 factor = tex2D(_MaskTex, i.uv);
        
        float2 uv = offset * factor.r + i.uv;
        return tex2D(_MainTex, uv);
    }
 
    ENDCG
 
    SubShader
    {
        Pass
        {
            ZTest Always
            Cull Off
            ZWrite Off
            Fog{ Mode off }
 
            CGPROGRAM
            #pragma vertex vert_img
            #pragma fragment frag
            #pragma fragmentoption ARB_precision_hint_fastest 
            ENDCG
        }
    }
    Fallback off
}

mask专用shader


Shader "ApcShader/MaskObjPrepass"
{
    //子着色器  
    SubShader
    {
        Pass
        {   
            Cull Off
            CGPROGRAM
            #include "UnityCG.cginc"
            
            struct v2f
            {
                float4 pos : SV_POSITION;
            };
            
            v2f vert(appdata_full v)
            {
                v2f o;
                o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                return o;
            }
            
            fixed4 frag(v2f i) : SV_Target
            {
                //这个Pass直接输出颜色
                return fixed4(1,1,1,1);
            }
            
            //使用vert函数和frag函数
            #pragma vertex vert
            #pragma fragment frag
            ENDCG
        }
    }
}

相关文章

  • 扭曲效果

    用GrabPass抓屏,法线图扰动UV,最好是自己用脚本抓屏,应该会省点,主要是好进行后续控制 据说会有安卓机上G...

  • SpriteShader备注——UV旋转扭曲动画效果

    效果:UV旋转扭曲动画效果.gif

  • 扭曲艺术

    扭曲艺术 电脑软件用到适当地方,会产生意想不到的效果,扭曲很有意思,任何正常的图像旋转扭曲,会产生非常有趣的效果,...

  • CSS变形与动画

    2019-04-15 2D变形 旋转效果: transform:rotate 扭曲效果: transform:sk...

  • ImageEffects_Twirl(转)

    Twirl是一个全屏画面扭曲的效果,新仙剑的战斗切换有用到这个效果。 主要有三个设置: center扭曲的中心点 ...

  • Unity护盾效果笔记

    然后就是接下来详细分析的动态色彩/消融/UV扭曲/面描边/辉光效果。 动态色彩/UV扭曲 我希望的效果是每个面都有...

  • AI 绘图总结

    绘制DNA 双链直线工具 --> 效果 --> 扭曲和变换 --> 波纹效果 --> 勾选预览,选择平滑,调整...

  • fcpx插件:Pixel Film Studios ProSto

    Pixel Film Studios ProStortion一款专业的扭曲变形效果(形态似热浪气流波动效果),可以...

  • Canvas常用方法解析第一篇

    1. 图像扭曲 Canvas中提供了一个drawBitmapMesh方法,通过该方法可以实现位图的扭曲效果,下面来...

  • 平铺文字练习

    之前一直好奇阿里云上的平面图是怎样做出完美的平铺效果的,扭曲?变形?斜切?试了之后效果都不是很好,今天才发现扭曲也...

网友评论

      本文标题:扭曲效果

      本文链接:https://www.haomeiwen.com/subject/xpvpmftx.html