【ShaderToy中图形效果转译到UnityShaderlab案例分享_Hexagon】

代码展示了如何使用ShaderToy的源码在Unity中创建复杂的图形效果,包括六边形图案、噪声纹理和颜色处理。通过hexagon函数计算距离边界和中心的距离,以及使用noise函数添加细节。在Unity中,这些效果被应用于图形的着色器,实现了时间依赖的颜色变化和阴影效果。
摘要由CSDN通过智能技术生成

ShaderToy内的源码如下

#define AA 2

// { 2d cell id, distance to border, distnace to center )
vec4 hexagon( vec2 p ) 
{
	vec2 q = vec2( p.x*2.0*0.5773503, p.y + p.x*0.5773503 );
	
	vec2 pi = floor(q);
	vec2 pf = fract(q);

	float v = mod(pi.x + pi.y, 3.0);

	float ca = step(1.0,v);
	float cb = step(2.0,v);
	vec2  ma = step(pf.xy,pf.yx);
	
    // distance to borders
	float e = dot( ma, 1.0-pf.yx + ca*(pf.x+pf.y-1.0) + cb*(pf.yx-2.0*pf.xy) );

	// distance to center	
	p = vec2( q.x + floor(0.5+p.y/1.5), 4.0*p.y/3.0 )*0.5 + 0.5;
	float f = length( (fract(p) - 0.5)*vec2(1.0,0.85) );		
	
	return vec4( pi + ca - cb*ma, e, f );
}

float hash1( vec2  p ) { float n = dot(p,vec2(127.1,311.7) ); return fract(sin(n)*43758.5453); }

float noise( in vec3 x )
{
    vec3 p = floor(x);
    vec3 f = fract(x);
	f = f*f*(3.0-2.0*f);
	vec2 uv = (p.xy+vec2(37.0,17.0)*p.z) + f.xy;
	vec2 rg = textureLod( iChannel0, (uv+0.5)/256.0, 0.0 ).yx;
	return mix( rg.x, rg.y, f.z );
}

void mainImage( out vec4 fragColor, in vec2 fragCoord ) 
{
    vec3 tot = vec3(0.0);
    
    #if AA>1
    for( int mm=0; mm<AA; mm++ )
    for( int nn=0; nn<AA; nn++ )
    {
        vec2 off = vec2(mm,nn)/float(AA);
        vec2 uv = (fragCoord+off)/iResolution.xy;
        vec2 pos = (-iResolution.xy + 2.0*(fragCoord+off))/iResolution.y;
    #else    
    {
        vec2 uv = fragCoord/iResolution.xy;
        vec2 pos = (-iResolution.xy + 2.0*fragCoord)/iResolution.y;
    #endif

        // distort
        pos *= 1.2 + 0.15*length(pos);

        // gray
        vec4 h = hexagon(8.0*pos + 0.5*iTime);
        float n = noise( vec3(0.3*h.xy+iTime*0.1,iTime) );
        vec3 col = 0.15 + 0.15*hash1(h.xy+1.2)*vec3(1.0);
        col *= smoothstep( 0.10, 0.11, h.z );
        col *= smoothstep( 0.10, 0.11, h.w );
        col *= 1.0 + 0.15*sin(40.0*h.z);
        col *= 0.75 + 0.5*h.z*n;

        // shadow
        h = hexagon(6.0*(pos+0.1*vec2(-1.3,1.0)) + 0.6*iTime);
        col *= 1.0-0.8*smoothstep(0.45,0.451,noise( vec3(0.3*h.xy+iTime*0.1,0.5*iTime) ));

        // red
        h = hexagon(6.0*pos + 0.6*iTime);
        n = noise( vec3(0.3*h.xy+iTime*0.1,0.5*iTime) );
        vec3 colb = 0.9 + 0.8*sin( hash1(h.xy)*1.5 + 2.0 + vec3(0.1,0.9,1.1) );
        colb *= smoothstep( 0.10, 0.11, h.z );
        colb *= 1.0 + 0.15*sin(40.0*h.z);

        col = mix( col, colb, smoothstep(0.45,0.451,n) );
        
        col *= 2.5/(2.0+col);

        col *= pow( 16.0*uv.x*(1.0-uv.x)*uv.y*(1.0-uv.y), 0.1 );

        tot += col;
	}	
 	#if AA>1
    tot /= float(AA*AA);
    #endif
        
	fragColor = vec4( tot, 1.0 );
}

下面是在Unity内的效果展示:

请添加图片描述

Shader"Hexagon"
{
    Properties
    {
        _MainTex("MainTex", 2D) = "white"{}
    }
    SubShader
    {
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma fragmentoption ARB_precision_hint_fastest
            #include "UnityCG.cginc"
            
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };
            uniform sampler2D  _MainTex;

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert(appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            fixed4 hexagon( fixed2 p ) 
            {
                fixed2 q = fixed2( p.x*2.0*0.5773503, p.y + p.x*0.5773503 );
                
                fixed2 pi = floor(q);
                fixed2 pf = frac(q);

                fixed v = fmod(pi.x + pi.y, 3.0);

                fixed ca = step(1.0,v);
                fixed cb = step(2.0,v);
                fixed2  ma = step(pf.xy,pf.yx);
                
                // distance to borders
                fixed e = dot( ma, 1.0-pf.yx + ca*(pf.x+pf.y-1.0) + cb*(pf.yx-2.0*pf.xy) );

                // distance to center	
                p = fixed2( q.x + floor(0.5+p.y/1.5), 4.0*p.y/3.0 )*0.5 + 0.5;
                fixed f = length( (frac(p) - 0.5)*fixed2(1.0,0.85) );		
                
                return fixed4( pi + ca - cb*ma, e, f );
            }

            fixed hash1( fixed2  p ) 
            {
                fixed n = dot(p,fixed2(127.1,311.7) ); 
                return frac(sin(n)*43758.5453); 
            }

            fixed noise( in fixed3 x )
            {
                fixed3 p = floor(x);
                fixed3 f = frac(x);
                f = f*f*(3.0-2.0*f);
                fixed2 uv = (p.xy+fixed2(37.0,17.0)*p.z) + f.xy;
                fixed2 rg = tex2Dlod( _MainTex,float4( (uv+0.5)/256.0, 0.0 ,0)).yx;
                return lerp( rg.x, rg.y, f.z );
            }


            fixed4 frag(v2f i) : SV_Target
            {
                fixed2 uv = i.uv.xy/1;
                fixed2 pos = (-1 + 2.0*i.uv.xy)/1;
                
                // distort
                pos *= 0.5 * (sin(_Time.y * 5)+2) + 0.3*length(pos);
                
                // gray
                fixed4 h = hexagon(8.0*pos + 0.5*_Time.y);
                fixed n = noise( fixed3(0.3*h.xy+_Time.y*0.1,_Time.y) );
                fixed3 col = 0.15 + 0.15*hash1(h.xy+1.2)*fixed3(1.0,1.0,1.0);
                col *= smoothstep( 0.01, 1, h.z );
                col *= smoothstep( 0.01, 1, h.w );
                col *= 1.0 + 0.15*sin(40.0*h.z);
                col *= 0.75 + 0.5*h.z*n;
                

                // red
                h = hexagon(6.0*pos + 5*_Time.y);
                n = noise( fixed3(0.3*h.xy+_Time.y*0.1,_Time.y) );
                fixed3 colb = 0.9 + 0.8*sin( hash1(h.xy)*1.5 + 2.0 + fixed3(0.0,1.0,1.0) );
                colb *= smoothstep( 0.01, 0.1, h.z );
                colb *= 1.0 + 0.15*sin(40.0*h.z);
                colb *= 0.75 + 0.5*h.z*n;

                h = hexagon(6.0*(pos+0.1*fixed2(-1.3,1.0)) + 0.6*_Time.y);
                col *= 1.0-0.8*smoothstep(0.45,0.451,noise( fixed3(0.3*h.xy+_Time.y*0.1,_Time.y) ));

                col = lerp( col, colb, smoothstep(0.45,0.451,n) );

                col *= pow( 16.0*i.uv.x*(1.0-i.uv.x)*i.uv.y*(1.0-i.uv.y), 0.5 );
                
                return fixed4( col, 1.0 );
            }
            ENDCG
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暴走约伯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值