Shader"ShaderToy/ParticleField"
{
Properties
{
_iMouse("iMouse", Vector) = (0,0,0,0)
}
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;
};
float4 _iMouse;
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;
}
#define POINTS 100
fixed t;
fixed hash (fixed i)
{
return 2.0*frac(sin(i*7467.25)*1e5) - 1.0;
}
fixed4 hash4(fixed i)
{
return fixed4(hash(i),hash(i-0.1),hash(i-0.3),hash(i+0.1));
}
fixed2 P(fixed i)
{
fixed4 c = hash4(i);
return fixed2(
cos(t*c.x-c.z)+.5*cos(2.765*t*c.y+c.w),
(sin(t*c.y-c.w)+.5*sin(1.893*t*c.x+c.z))/1.5
);
}
fixed4 frag(v2f i) : SV_Target
{
t = _Time.y;
fixed2 uv = i.uv;
fixed m = (_iMouse.z<=0.0) ? 0.1*t/6.283 : 0.5*_iMouse.x/1;
fixed fMODE = (1.0-cos(6.283*m))/200.0;
const int R = 1;
fixed v=0.0; fixed2 V=fixed2(0.0,0.0);
for (int i=1; i<POINTS; i++)
{
fixed2 p = P(fixed(i));
for (int y=-R; y<=R; y++)
for (int x=-R; x<=R; x++)
{
fixed2 d = p+2.0*fixed2(fixed(x),fixed(y)) -uv;
fixed r2 = dot(d,d);
V += d / r2;
}
}
v = length(V);
v *= 1.0/(9.0*fixed(POINTS));
v *= 2.0+fMODE;
return fixed4(sin(v),sin(v/2.0),sin(v/4.0),1.0);
}
ENDCG
}
}
}
【ShaderToy中图形效果转译到UnityShaderlab案例分享,实现粒子场_ParticleField】
于 2023-07-23 19:46:11 首次发布
该代码示例展示了如何在Shader中使用GLSL语言创建一个粒子场效果。它通过迭代计算每个点的位置并应用sin和cos函数来生成动态图形,同时利用_iMouse变量来控制某些参数,如时间和鼠标位置,以交互式地改变视觉效果。
摘要由CSDN通过智能技术生成