Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to Blit shader output into a texture?

Discussion in 'Shaders' started by glennmarshall, Jul 28, 2016.

  1. glennmarshall

    glennmarshall

    Joined:
    Apr 19, 2016
    Posts:
    6
    Hi
    I have a shader (imported from Shadertoy) - which needs to render to a texture channel - to be received by the shader itself on channel0. The shader is currently on a full screen quad. My understanding is that I would need to create a script on the camera which Blits to a texture - which I can then assign to my channel0 input on the shader. Do I create a Render Texture - or can a texture be created programmatically within the scripting. And how do I link everything up.. This shader uses the texture as a buffer to store 16 bit data - so the texture needs to be ARGBHalf. I'm new to unity, so forgive me I'm still learning.

    I'd be grateful for any advice. I've pasted the shader code below.

    Shader "Shadertoy/myshader" {
    Properties{
    iMouse ("MousePos", 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 iGlobalTime _Time.y
    #define mod fmod
    #define mix lerp
    #define atan atan2
    #define fract frac
    #define texture2D tex2D
    // 屏幕的尺寸
    #define iResolution _ScreenParams
    // 屏幕中的坐标,以pixel为单位
    #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) {

    vec2 uv = fragCoord / iResolution.xy;
    vec2 direction = normalize(vec2(sin(iGlobalTime * 1.7) * cos(0.2 + iGlobalTime * 0.4), -6.0));
    vec4 lastColor = 0.333 * (texture2D(iChannel0, uv + direction * 0.02) + texture2D(iChannel0, uv + direction * 0.04) + texture2D(iChannel0, uv + direction * 0.06));
    float emissionAmount = .5 + .333 * (sin(uv.y * 6.1 + iGlobalTime * -1.12) + cos(1.0 + uv.x * 5.7 - iGlobalTime * 0.7) + sin(1.7 + uv.x * 7.3 + iGlobalTime * 2.3));
    vec3 newColor = vec3(0.85, 0.3, 0.9) * emissionAmount * max(0.0, 0.8 - 30.0 * uv.y);
    return vec4(newColor + .9 * lastColor.rgb * vec3(0.94 + 0.05 * (0.5 * (sin(1.8 + iGlobalTime * 2.1) +sin(iGlobalTime * 1.3 + 0.6))), 0.9, 1.0),1.0);

    }

    ENDCG

    SubShader {
    Pass {
    CGPROGRAM

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

    ENDCG
    } ​
    }
    FallBack Off
    }