翼度科技»论坛 编程开发 JavaScript 查看内容

ThreeJS Shader的效果样例飞线、粒子和模型轮廓高亮(三)

4

主题

4

帖子

12

积分

新手上路

Rank: 1

积分
12
一、飞线效果
  

  功能说明:支持设置点的个数,飞线速度、起始和终止颜色值、线宽、线的大小
  原理:
    1. 首先绘制一条与线长度相同的线,线中各点的大小逐渐变小
    2. 如何让线动起来?假设点的个数总共为num个,传入的点的下标为a,通过变化的时间计算出移动的下标b,如果a+b>=num则代表,该点可见,否则不可见
    3. 如何设置移动速度?传入的uTime变化在毫秒之间,通过乘以一个具体的数据,则可以将变化的时间放大,改变乘以的数据是移动速度的关键
    4. 改变飞线大小?初始设置飞线的大小是整条线,通过控制飞线的结束位置以及调整点变化的速度就可以调整飞线的大小
  1. /**
  2. * 创建飞线
  3. * 支持设置起始和终止颜色值、飞线长度、线宽等属性、移动速率
  4. */
  5. function addFlyLine() {
  6.   const vertex = `
  7.     attribute float aIndex;
  8.     uniform float uTime;
  9.     uniform float uNum; // 线上点个数
  10.     uniform float uWidth; // 线宽
  11.     uniform float uLength; // 线宽
  12.     uniform float uSpeed; // 飞线速度
  13.     varying float vIndex; // 内部点下标
  14.     void main() {
  15.       vec4 viewPosition = viewMatrix * modelMatrix * vec4(position, 1.0);
  16.       gl_Position = projectionMatrix * viewPosition;
  17.       
  18.       vIndex = aIndex;
  19.       // 通过时间点的增加作为点移动的下标,从末端的第一个点开始,已num点为一个轮回,往复执行运动
  20.       float num = mod(floor(uTime * uSpeed * 100.0), uNum);
  21.       // 只绘制部分点,多余的不绘制
  22.       if (aIndex + num >= uNum) {
  23.         float size = (uNum - mod(aIndex + num, uNum) * (1.0 / uLength)) / uNum * uWidth;
  24.         gl_PointSize = size;
  25.       }
  26.     }
  27.   `;
  28.   const frag = `
  29.     varying float vIndex;
  30.     uniform float uTime;
  31.     uniform float uLength; // 线宽
  32.     uniform float uNum;
  33.     uniform float uSpeed;
  34.     uniform vec3 uSColor;
  35.     uniform vec3 uEColor;
  36.     void main() {
  37.       // 默认绘制的点是方形的,通过舍弃可以绘制成圆形
  38.       float distance = length(gl_PointCoord - vec2(0.5, 0.5));
  39.       if (distance > 0.5) {
  40.         discard;
  41.       }
  42.       
  43.       float num = mod(floor(uTime * uSpeed * 100.0), uNum);
  44.       // 根据点的下标计算渐变色值
  45.       vec3 color = mix(uSColor, uEColor, (num + vIndex - uNum) / uNum);
  46.       // 越靠近末端透明度越大
  47.       float opacity = (uNum - (num + vIndex - uNum)) / uNum;
  48.       // 根据长度计算显示点的个数,多余的透明显示
  49.       if (vIndex + num >= uNum && vIndex + num <= uNum * (1.0 + uLength)) {
  50.         gl_FragColor = vec4(color, opacity);
  51.       } else {
  52.         gl_FragColor = vec4(color, 0);
  53.       }
  54.     }
  55.   `;
  56.   const nums = 500;
  57.   const curve = new THREE.CubicBezierCurve3(
  58.     new THREE.Vector3(-6.0, 0.0, 0.0),
  59.     new THREE.Vector3(0.0, 3.0, 9.0),
  60.     new THREE.Vector3(4.0, 5.0, 0.0),
  61.     new THREE.Vector3(6.0, 4.0, 3.0)
  62.   );
  63.   const curveArr = curve.getPoints(nums);
  64.   const flatArr = curveArr.map(e => e.toArray());
  65.   const lastArr = flatArr.flat();
  66.   const indexArr = [...Array(nums + 1).keys()];
  67.   const geometry = new THREE.BufferGeometry();
  68.   geometry.setAttribute('position', new THREE.BufferAttribute(new Float32Array(lastArr), 3));
  69.   geometry.setAttribute('aIndex', new THREE.BufferAttribute(new Float32Array(indexArr), 1));
  70.   const material = new THREE.LineBasicMaterial({ color: 0x0000ff });
  71. // 创建一根曲线
  72.   const curveObject = new THREE.Line(geometry, material);
  73.   // scene.add(curveObject);
  74.   const uniform = {
  75.     uTime: { value: 0.01 },
  76.     uSColor: { value: new THREE.Color(0x4effd6) },
  77.     uEColor: { value: new THREE.Color(0xffffff) },
  78.     uWidth: { value: 6.0 },
  79.     uNum: { value: nums },
  80.     uSpeed: { value: 2 },
  81.     uLength: { value: 0.15 }
  82.   }
  83.   setShader(geometry, vertex, frag, undefined, uniform, true)
  84. }
复制代码
添加飞线
  1. function setShader(geometry, vertexShader, fragmentShader, position = [0, 0, 0], uniforms = {}, isLine = false) {
  2.   material = new THREE.ShaderMaterial({
  3.     vertexShader: vertexShader,
  4.     fragmentShader: fragmentShader,
  5.     side: THREE.DoubleSide,
  6.     uniforms: uniforms,
  7.     transparent: true
  8.   });
  9.   let planeMesh = new THREE.Mesh(geometry, material);
  10.   if (isLine) {
  11.     planeMesh = new THREE.Points(geometry, material);
  12.   }
  13.   planeMesh.position.x = position[0];
  14.   planeMesh.position.y = position[1];
  15.   planeMesh.position.z = position[2];
  16.   scene.add(planeMesh);
  17. }
复制代码
添加材质 
二、实现粒子效果 
  

  实现原理:
    1. 根据AI生成一个特殊形状的点模型
    2. 通过 distance 将立方体点改为球形
    3. 计算点的距离,越远的点透明度越高,增加层次感
  1. /**
  2. * 创建一个立体的涵道,根据传入的时间自动缩放
  3. */
  4. function addHole() {
  5.   const vertex = `
  6.     varying vec3 vColor;
  7.     uniform float uTime;
  8.     void main() {
  9.       float scale = mod(uTime / 3.0 + 0.3, 1.0);
  10.       vec4 viewPosition = viewMatrix * modelMatrix * vec4(position, 1.0);
  11.       gl_Position = projectionMatrix * viewPosition * vec4(vec3(scale), 1.0);
  12.       gl_PointSize = 6.0;
  13.     }
  14.   `;
  15.   const frag = `
  16.     void main() {
  17.       float distance = length(gl_PointCoord.xy - 0.5);
  18.       // 默认绘制的点是立方体的,通过判断uv点的距离大小可以绘制出球形
  19.       if (distance > 0.5) {
  20.         discard;
  21.       }
  22.       // 边缘模糊化,点的半径较大时效果比较明显
  23.       float opacity = smoothstep(0.5, 0.1, distance);
  24.       gl_FragColor = vec4(0.24, 0.12, 0.96, opacity);
  25.     }
  26.   `;
  27.   // 创建一个自定义的几何体
  28.   const geometry = new THREE.BufferGeometry();
  29.   const vertices = [];
  30.   const indices = [];
  31.   // 使用三维参数方程生成心形的顶点
  32.   const segmentsU = 100;
  33.   const segmentsV = 30;
  34.   for (let i = 0; i <= segmentsU; i++) {
  35.       const u = (i / segmentsU) * 2 * Math.PI;
  36.       for (let j = 0; j <= segmentsV; j++) {
  37.           const v = (j / segmentsV) * 2 * Math.PI;
  38.           const x = (1 + Math.cos(v)) * Math.cos(u);
  39.           const y = (1 + Math.cos(v)) * Math.sin(u);
  40.           const z = Math.sin(v);
  41.           vertices.push(x, y, z);
  42.       }
  43.   }
  44.   // 生成索引数据
  45.   for (let i = 0; i < segmentsU; i++) {
  46.       for (let j = 0; j < segmentsV; j++) {
  47.           const a = i * (segmentsV + 1) + j;
  48.           const b = (i + 1) * (segmentsV + 1) + j;
  49.           const c = (i + 1) * (segmentsV + 1) + j + 1;
  50.           const d = i * (segmentsV + 1) + j + 1;
  51.           indices.push(a, b, d);
  52.           indices.push(b, c, d);
  53.       }
  54.   }
  55.   // 将顶点数据和索引数据添加到几何体中
  56.   geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));
  57.   geometry.setIndex(indices);
  58.   setShader(geometry, vertex, frag, undefined, { uTime: { value: 0.01 } }, true)
  59. }
复制代码
使用UnrealBloomPass实现高亮效果  
 
 
  
  1. function setShader(geometry, vertexShader, fragmentShader, position = [0, 0, 0], uniforms = {}, isLine = false) {
  2.   material = new THREE.ShaderMaterial({
  3.     vertexShader: vertexShader,
  4.     fragmentShader: fragmentShader,
  5.     side: THREE.DoubleSide,
  6.     uniforms: uniforms,
  7.     transparent: true,
  8.     blending: THREE.AdditiveBlending, // 多个元素的颜色相互叠加,颜色可能会变亮,会叠加setClearColor设置的背景色
  9.   });
  10.   material.depthTest = true;
  11.   material.depthWrite = false;
  12.   let planeMesh = new THREE.Mesh(geometry, material);
  13.   if (isLine) {
  14.     planeMesh = new THREE.Points(geometry, material);
  15.   }
  16.   planeMesh.position.x = position[0];
  17.   planeMesh.position.y = position[1];
  18.   planeMesh.position.z = position[2];
  19.   scene.add(planeMesh);
  20. }
复制代码
使用UnrealBloomPass实现球体辉光效果      

 

来源:https://www.cnblogs.com/codeOnMar/p/18233672
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具