webgl实现canvas多彩赛道跟踪穿越效果代码

代码语言:html

所属分类:三维

代码描述:webgl实现canvas多彩赛道跟踪穿越效果代码

代码标签: 多彩 赛道 跟踪 穿越 效果

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >

<head>

  <meta charset="UTF-8">

  
  
<style>
canvas {
    display:block; width:100%; height:100vh;
}
</style>


</head>

<body translate="no" >

  
      <script type="module">
import * as $ from '//unpkg.com/three@0.124.0/build/three.module.js';
import { OrbitControls } from '//unpkg.com/three@0.124.0/examples/jsm/controls/OrbitControls.js';
import { EffectComposer } from '//unpkg.com/three@0.124.0/examples/jsm/postprocessing/EffectComposer';
import { RenderPass } from '//unpkg.com/three@0.124.0/examples/jsm/postprocessing/RenderPass';
import { UnrealBloomPass } from '//unpkg.com/three@0.124.0/examples/jsm/postprocessing/UnrealBloomPass';
import { Curves } from '//unpkg.com/three@0.124.0/examples/jsm/curves/CurveExtras';
// ---- boot

const renderer = new $.WebGLRenderer({});
const scene = new $.Scene();
const camera = new $.PerspectiveCamera(75, 2, .01, 5000);
window.addEventListener('resize', () => {
  const { clientWidth, clientHeight } = renderer.domElement;
  renderer.setSize(clientWidth, clientHeight, false);
  renderer.setPixelRatio(window.devicePixelRatio);
  camera.aspect = clientWidth / clientHeight;
  camera.updateProjectionMatrix();
});
document.body.prepend(renderer.domElement);
window.dispatchEvent(new Event('resize'));

// ---- setup

scene.fog = new $.FogExp2('black', 0.05);
scene.add(new $.HemisphereLight('cyan', 'orange', 2));

// ---- const

const mpms = 20 / 1e3;
const steps = 2000;

// ---- mesh

const shape = new $.Shape();
// cw
shape.moveTo(-5, -1);
shape.quadraticCurveTo(0, -4, 5, -1);
shape.lineTo(6, -1);
shape.quadraticCurveTo(0, -5, -6, -1);
const extrudePath = new Curves.TorusKnot();
const UVGenerator = (() => {
  let i = 0; // face id
  return {
    generateTopUV(...xs) {// for 2 "cap" faces
      return [new $.Vector2(), new $.Vector2(), new $.Vector2()];
    },
    generateSideWallUV(_geom, _vs, _a, _b, _c, _d) {// all side faces
      const segments = 5; // (shape-related; NOT eq `curveSegments`)
      if (i < segments * steps) {// ignore bottom road faces
        ++i;
        return [new $.Vector2(), new $.Vector2(), new $.Vector2(), new $.Vector2()];
      }
      const n = i - segments * steps; // offseted face idx
      const total_col_segments = 7; // (shape-related) 
      const col = n / steps | 0;
      const left = col / total_col_segments; // normalize
      const right = (col + 1) / total_col_segments; // normalize
      const row = n % steps;
      const bottom = row / steps; // normalize 
      const top = (row + 1) / steps; // normalize
      ++i;
      return [
      new $.Vector2(left, bottom), // bottom left 
      new $.Vector2(right, bottom), // bottom right
      new $.Vector2(right.........完整代码请登录后点击上方下载按钮下载查看

网友评论0