基于模型的扩散效果(学习`精神小伙`的代码)

基于模型的扩散效果(学习精神小伙的代码)

最终效果:
在这里插入图片描述

shadertoy:

// 应用: 基于模型的扩散效果
#define PI 3.1415926

float lerp (float x,float y,float t ) {
    return ( 1.0 - t ) * x + t * y;
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
  // 当前像素的位置
  vec2 uv = (fragCoord.xy/iResolution.xy-0.5);
  // 圆环扩散的速度
  const float u_speed = .05;
  // 扩散的最大半径
  const float u_radius = 0.5;
  // 圆环的宽度
  const float u_width = 0.08;
  // 圆环内的颜色
  const vec3 hightColor = vec3(1.,0.,0.);
  // 圆环外的颜色
  const vec3 baseColor = vec3(.3,.3,.3);

  vec3 vColor;
  float u_time = u_speed * iTime;

  // rangeLen: 0~u_radius之间周期变化
  float rangeLen = mod(u_time, u_radius);

  // 当前像素点到中心(0,0)的距离
  float distance = length(uv);

  // 满足在圆环内部的像素,高亮着色
  if ( distance> rangeLen && distance < rangeLen + u_width) {
      // 占整个圆环宽度的百分比
      float i = (distance-rangeLen) / u_width;

      // 过渡更平滑一些,圆环两边模糊,中间清楚
      float d = sin(i*PI);

      // 线性差值
      float r = lerp(baseColor.r, hightColor.r, d);
      float g = lerp(baseColor.g, hightColor.g, d);
      float b = lerp(baseColor.b, hightColor.b, d);
      vColor = vec3(r, g, b);
  } else {
    // 圆环外部的点,正常颜色着色
    vColor = baseColor;
  }

  fragColor = vec4(vColor,1.);
}

绘图模型:
在这里插入图片描述

关键步骤解释:

float d = sin(i*PI);

取sin:
在这里插入图片描述

效果:
在这里插入图片描述

实际应用

把距离换成是模型的相对距离在顶点着色器中传过来就行.

顶点:

varying vec3 v_position;
void main() {
    v_position = position;
    gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}

片元:

vec2 curr = vec2(v_position.x, v_position.z);
float distance = distanceTo(vec2(0.0, 0.0), curr);
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值