shaderToy 转换shaderLab

https://www.shadertoy.com/view/lt2GDd

 


Shader "ShaderToy/Texture"
	{

	Properties{
	_MainTex ("MainTex", 2D) = "white" {}
	}

	SubShader
	{
	Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }

	Pass
	{
	ZWrite Off
	Blend SrcAlpha OneMinusSrcAlpha

	CGPROGRAM
	#pragma vertex vert
	#pragma fragment frag
	#include "UnityCG.cginc"

	struct VertexInput {
    fixed4 vertex : POSITION;
	fixed2 uv:TEXCOORD0;
    fixed4 tangent : TANGENT;
    fixed3 normal : NORMAL;
	//VertexInput
	};


	struct VertexOutput {
	fixed4 pos : SV_POSITION;
	fixed2 uv:TEXCOORD0;
	//VertexOutput
	};

	//Variables
sampler2D _SecondTex;
sampler2D _MainTex;



#define USEHASH

fixed4 hash4( fixed2 p ) { return frac(sin(fixed4( 1.0+dot(p,fixed2(37.0,17.0)), 
                                              2.0+dot(p,fixed2(11.0,47.0)),
                                              3.0+dot(p,fixed2(41.0,29.0)),
                                              4.0+dot(p,fixed2(23.0,31.0))))*103.0); }

fixed4 tex2DNoTile( sampler2D samp, in fixed2 uv )
{
    fixed2 iuv = floor( uv );
    fixed2 fuv = frac( uv );

#ifdef USEHASH    
    // generate per-tile transform (needs GL_NEAREST_MIPMAP_LINEARto work right)
    fixed4 ofa = tex2D( _SecondTex, (iuv + fixed2(0.5,0.5))/256.0 );
    fixed4 ofb = tex2D( _SecondTex, (iuv + fixed2(1.5,0.5))/256.0 );
    fixed4 ofc = tex2D( _SecondTex, (iuv + fixed2(0.5,1.5))/256.0 );
    fixed4 ofd = tex2D( _SecondTex, (iuv + fixed2(1.5,1.5))/256.0 );
#else
    // generate per-tile transform
    fixed4 ofa = hash4( iuv + fixed2(0.0,0.0) );
    fixed4 ofb = hash4( iuv + fixed2(1.0,0.0) );
    fixed4 ofc = hash4( iuv + fixed2(0.0,1.0) );
    fixed4 ofd = hash4( iuv + fixed2(1.0,1.0) );
#endif
    
    fixed2 _ddx = ddx( uv );
    fixed2 _ddy = ddy( uv );

    // transform per-tile uvs
    ofa.zw = sign(ofa.zw-0.5);
    ofb.zw = sign(ofb.zw-0.5);
    ofc.zw = sign(ofc.zw-0.5);
    ofd.zw = sign(ofd.zw-0.5);
    
    // uv's, and derivarives (for correct mipmapping)
    fixed2 uva = uv*ofa.zw + ofa.xy; fixed2 ddxa = _ddx*ofa.zw; fixed2 ddya = _ddy*ofa.zw;
    fixed2 uvb = uv*ofb.zw + ofb.xy; fixed2 ddxb = _ddx*ofb.zw; fixed2 ddyb = _ddy*ofb.zw;
    fixed2 uvc = uv*ofc.zw + ofc.xy; fixed2 ddxc = _ddx*ofc.zw; fixed2 ddyc = _ddy*ofc.zw;
    fixed2 uvd = uv*ofd.zw + ofd.xy; fixed2 ddxd = _ddx*ofd.zw; fixed2 ddyd = _ddy*ofd.zw;
        
    // fetch and blend
    fixed2 b = smoothstep(0.25,0.75,fuv);
    
    return lerp( lerp( tex2Dgrad( samp, uva, ddxa, ddya ), 
                     tex2Dgrad( samp, uvb, ddxb, ddyb ), b.x ), 
                lerp( tex2Dgrad( samp, uvc, ddxc, ddyc ),
                     tex2Dgrad( samp, uvd, ddxd, ddyd ), b.x), b.y );
}





	VertexOutput vert (VertexInput v)
	{
	VertexOutput o;
	o.pos = UnityObjectToClipPos (v.vertex);
	o.uv = v.uv;
	//VertexFactory
	return o;
	}
	fixed4 frag(VertexOutput i) : SV_Target
	{
	
	fixed2 uv = i.uv / 1;
	
	fixed f = smoothstep( 0.4, 0.6, sin(_Time.y    ) );
    fixed s = smoothstep( 0.4, 0.6, sin(_Time.y*0.5) );
        
    uv = (4.0 + 16.0*s)*uv + _Time.y*0.1;
        
	fixed3 cola = tex2DNoTile( _MainTex, uv ).xyz;
    fixed3 colb = tex2D( _MainTex, uv ).xyz;
    
    fixed3 col = lerp( cola, colb, f );
    
	return fixed4( col, 1.0 );

	}
	ENDCG
	}
  }
}

转换过程中,tex2Dgrad需要提供屏幕坐标x和y方向上的梯度来确定应该使用的纹理层。

偏导函数,分为 HLSL : ddx 和 ddy , GLSL : dFdx 和 dFdy,分别对应 x, y 轴上,在屏幕空间中,像素块中各种变量的变化率。

    fixed2 ddx = dFdx( uv );  需要改为 ddx 和ddy
 

最后别忘记设置MIP MAPS

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值