three+webgl实现飞机引擎运行空气流动粒子动画效果代码

代码语言:html

所属分类:粒子

代码描述:three+webgl实现飞机引擎运行空气流动粒子动画效果代码

代码标签: 引擎 运行 空气 流动 粒子 动画 效果

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

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

<head>

  <meta charset="UTF-8">
  

  
  
<style>
body {
  margin: 0;
  height: 100vh;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #222;
}
</style>



</head>

<body >
  

  
      <script type="module">
import * as THREE from "https://threejs.org/build/three.module.js";
import { OrbitControls } from "https://threejs.org/examples/jsm/controls/OrbitControls.js";

let camera, scene, renderer;
let mainPropeller, bladeSetA, bladeSetB, bladeSetC;
let step = 0;
const { PI, random, sin, cos } = Math;
const TAU = 2 * PI;
const uniforms = {
  time: { value: 0 } };


const map = (value, sMin, sMax, dMin, dMax) => {
  return dMin + (value - sMin) / (sMax - sMin) * (dMax - dMin);
};
const range = (n, m = 0) =>
Array(n).
fill(m).
map((i, j) => i + j);
const vec = (x = 0, y = 0, z = 0) => new THREE.Vector3(x, y, z);
const polar = (ang, r = 1) => [r * cos(ang), r * sin(ang)];
const bladeGeometry = getBladeGeometry();

function init() {
  scene = new THREE.Scene();

  renderer = new THREE.WebGLRenderer({ antialias: true });
  renderer.setPixelRatio(window.devicePixelRatio);
  renderer.setSize(window.innerWidth, window.innerHeight);
  document.body.appendChild(renderer.domElement);

  camera = new THREE.PerspectiveCamera(
  60,
  window.innerWidth / window.innerHeight,
  1,
  1000);

  camera.position.set(-44.93624743716165, 27.39247486400745, 85.03167637469498);
  camera.rotation.x = -0.31164690514668836;
  camera.rotation.y = -0.46605157569574196;
  camera.rotation.z = -0.1437609504660436;

  const controls = new OrbitControls(camera, renderer.domElement);

  addAmbientLight(scene);
  addDirectionalLight(scene);
  addResizeHandler(renderer, camera);
  addObjects(scene);
  render();
}

function render() {
  update();

  renderer.render(scene, camera);
  requestAnimationFrame(render);
}

function addObjects(scene) {
  createAxis(scene);

  mainPropeller = createMainPropeller(scene);

  bladeSetB = createBladeSetStageB(scene);
  bladeSetC = createBladeSetStageC(scene);
  bladeSetA = createBladeSetStageA(scene);
  createAirParticles(scene, uniforms);
  createGasParticles(scene, uniforms);
}

function update() {
  mainPropeller.rotation.x += 0.02;
  bladeSetA.rotation.x += 0.01;
  bladeSetB.rotation.x -= 0.01;
  bladeSetC.rotation.x += 0.01;

  step = (step + 1) % 500;

  uniforms.time.value = step / 500;
}
init();

function createAxis(scene) {
  function getPath() {
    const c1 = curve(
    [3.94, 8.851],
    [3.98, 12.526],
    [2.123, 14.144],
    [2.063, 17.139]);

    const c2 = line([2.063, 17.139], [1.76, 38.948]);
    const c3 = curve(
    [1.76, 38.948],
    [2.349, 40.783],
    [3.988, 42.028],
    [4.119, 43.994]);

    const c4 = line([4.119, 43.994], [4.119, 47.009]);
    const c5 = curve(
    [4.119, 47.009],
    [3.988, 51.596],
    [-0.01, 55.266],
    [0.056, 55.266]);


    const path = new THREE.CurvePath();
    path.add(c1);
    path.add(c2);
    path.add(c3);
    path.add(c4);
    path.add(c5);
    return path.getSpacedPoints(100);
  }
  const geometry = new THREE.LatheGeometry(getPath(), 32);
  const material = mat("#1a508b");
  const lathe = new THREE.Mesh(geometry, material);
  lathe.rotation.z = PI / 2;

  lathe.position.x = 30;
  scene.add(lathe);
}

function addResizeHandler(renderer, camera) {
  window.addEventListener(
  "resize",
  () => {
    const { innerWidth: w, innerHeight: h } = window;
    renderer.setSize(w, h);
    camera.aspect = w / h;
    camera.updateProjectionMatrix();
  },
  false);

}

function addDirectionalLight(scene) {
  const color = 0xffffff;
  const intensity = 0.6;
  const light = new THREE.DirectionalLight(color, intensity);
  light.position.set(-1, 2, 4);
  scene.add(light);
}

function addAmbientLight(scene) {
  const color = 0xffffff;
  const intensity = 0.6;
  const light = new THREE.AmbientLight(color, intensity);
  scene.add(light);
}

function createGasParticles(scene, uniforms) {
  const particles = 10000;

  function getPathTexture() {
    const points = 1000;
    const c1 = curve(
    [0.222, -0.013],
    [0.47, 0.365],
    [0.866, 1.179],
    [0.979, 2.012]);

    const c2 = curve(
    [0.979, 2.012],
    [1.415, 4.167],
    [1.487, 6.031],
    [1.366, 7.944]);

    const path = new THREE.CurvePath();
    path.add(c1);
    path.add(c2);

    const data = path.
    getSpacedPoints(points).
    reduce((acc, { x, y, z }) => [...acc, x, y, z], []);

    return new THREE.DataTexture(
    new Float32Array(data),
    points + 1,
    1,
    THREE.RGBFormat,
    THREE.FloatType);

  }

  const fragmentShader = `
varying float alpha;
varying vec3 vColor;
float map(float value, float sMin, float sMax, float dMin, float .........完整代码请登录后点击上方下载按钮下载查看

网友评论0