前段时间做的 ShaderToy 材质效果 UE 引擎复现
原 ShaderToy 效果
引擎内效果
Custom 代码如下
经过项目测试发现一些问题,当开启光追的时候,Custom 节点内的 Struct 结构体中的参数不允许通过 input 动态修改,所以需要将结构体内的参数拿出来。所以,前几篇文章中放出来的代码,在开启光追的时候,会报错,需要进行修改。
只需要如下图代码,将参数拿到结构体外面即可
struct Functions
{
int mapstep;
int raystep;
float3 stepsize;
float3 colorlerp;
float3 c0;
float3 c1;
float3 c2;
float lerppower;
float2 csqr( float2 a )
{
return float2( a.x*a.x - a.y*a.y, 2.*a.x*a.y );
}
float map(in float3 p, float2 sctime)
{
float res = 0.;
float3 c = p;
c.xy = c.xy * sctime.x + float2(c.y, c.x) * sctime.y;
for (int i = 0; i < mapstep; ++i)
{
p =.7*abs(p)/dot(p,p) -.7;
p.yz= csqr(p.yz);
p=p.zxy;
res += exp(-19. * abs(dot(p,c)));
}
return res/2.;
}
float3 raymarch( in float3 ro, float3 rd,float2 sctime)
{
float3 one3 = float3(1.,1.,1.);
float3 t = one3;
// float3 StepSize = float3(.07, 0.02, 0.05);
float3 col= float3(0.0,0.0,0.0);
float3 c = one3 * 0.;
for( int i=0; i<raystep; i++ )
{
// float3 ColorLerp = float3(2.0, 3.0, 4.0);
t+=stepsize*exp(-colorlerp*c);
// float3 a = step(t,one3);
float3 pos = ro-t*rd;
c.x = map(ro-t.x*rd, sctime);
c.y = map(ro-t.y*rd, sctime);
c.z = map(ro-t.z*rd, sctime);
col = .99*col+ lerppower*c*c*c;
}
// float3 c0 = float3(0.4,0.3,0.99);
// float3 c1 = float3(0.9,0.7,0.0);
// float3 c2 = float3(0.9,0.1,0.2);
return c0 * col.x + c1 * col.y + c2 * col.z;
}
};
Functions f;
f.mapstep = MapStep;
f.raystep = RayStep;
f.stepsize = StepSize;
f.colorlerp = ColorLerp;
f.c0 = C0;
f.c1 = C1;
f.c2 = C2;
f.lerppower = LerpPower;
// raymarch
float3 col = f.raymarch(ro,rd,float2(sin(Time), cos(Time)));
// shade
col = .5 *(log(1.+col));
col = clamp(col,0.,1.);
return col;
另外,为了适配引擎中的旋转、缩放、位移,RO 和 RD 对应的 World Position 和 Camera Vector 需要由世界坐标改为局部坐标。
百度云链接 引擎版本4.26
提取码:hq31