Codels
604 треуг.1,48 × 2,60 × 0,68 m

Использовать в коде

Ремикс в редакторе
Самодостаточный ES-модуль — зависит только от three. Вызовите фабрику и добавьте группу в сцену.
/**
 * Checkpoint Flag — Codels 3D model library
 * License: CC0-1.0 · Units: meters · Y-up · Front: +Z · Origin: center of the base (y = 0)
 * Triangles: 604 · Size (W × H × D): 1.48 × 2.60 × 0.68 m
 *
 * Usage:
 *   import { createCheckpointFlag } from './checkpoint-flag.js';
 *   
 *   const model = createCheckpointFlag();
 *   model.position.set(0, 0, 0);
 *   scene.add(model);
 *   
 *   // Animations: idle
 *   const mixer = new THREE.AnimationMixer(model);
 *   mixer.clipAction(THREE.AnimationClip.findByName(model.animations, 'idle')).play();
 *   // in your render loop: mixer.update(clock.getDelta());
 */
import * as THREE from 'three';

const deg = THREE.MathUtils.degToRad;

// Revolve a 2D profile [[radius, y], ...] around the Y axis.
function lathe(points, segments, phiStart = 0, phiLength = Math.PI * 2) {
  return new THREE.LatheGeometry(points.map(([x, y]) => new THREE.Vector2(x, y)), segments, phiStart, phiLength);
}

// Extrude a 2D outline along Z (centered). bevel = [thickness, size, segments].
function extrude(outline, depth, holes = [], bevel = null) {
  const shape = new THREE.Shape(outline.map(([x, y]) => new THREE.Vector2(x, y)));
  for (const hole of holes) shape.holes.push(new THREE.Path(hole.map(([x, y]) => new THREE.Vector2(x, y))));
  const options = bevel
    ? { depth, bevelEnabled: true, bevelThickness: bevel[0], bevelSize: bevel[1], bevelSegments: bevel[2] }
    : { depth, bevelEnabled: false };
  return new THREE.ExtrudeGeometry(shape, options).translate(0, 0, -depth / 2);
}

// Build animation clips from simple descriptions (Codels). Play them with THREE.AnimationMixer.
function codelsClips(THREE, entries) {
  var TAU = Math.PI * 2;
  var DEG = Math.PI / 180;
  var AX = { x: new THREE.Vector3(1, 0, 0), y: new THREE.Vector3(0, 1, 0), z: new THREE.Vector3(0, 0, 1) };
  var periodOf = function (a) { return a.type === 'spin' ? 360 / Math.abs(a.speed) : a.period; };
  var clips = [];

  // loops → 'idle'
  var loops = [];
  entries.forEach(function (e) {
    var l = e.anim.filter(function (a) { return a.type !== 'hinge'; });
    if (l.length) loops.push({ target: e.target, anim: l });
  });
  if (loops.length) {
    var periods = [];
    loops.forEach(function (e) { e.anim.forEach(function (a) { periods.push(periodOf(a)); }); });
    var longest = Math.max.apply(null, periods);
    var D = longest;
    var bestErr = Infinity;
    for (var m = 1; m <= 6 && bestErr > 0.01; m++) {
      var err = 0;
      periods.forEach(function (p) {
        var n = Math.max(1, Math.round((longest * m) / p));
        err = Math.max(err, Math.abs((longest * m) / n - p) / p);
      });
      if (err < bestErr - 1e-9) { bestErr = err; D = longest * m; }
    }
    var cycles = function (a) { return Math.max(1, Math.round(D / periodOf(a))); };
    var steps = 24;
    loops.forEach(function (e) { e.anim.forEach(function (a) { steps = Math.max(steps, cycles(a) * 24); }); });
    steps = Math.min(steps, 720);
    var tracks = [];
    loops.forEach(function (e) {
      var o = e.target;
      var q0 = o.quaternion.clone(), p0 = o.position.clone(), s0 = o.scale.clone();
      var q = new THREE.Quaternion(), turn = new THREE.Quaternion(), p = new THREE.Vector3(), s = new THREE.Vector3();
      var has = function (type) { return e.anim.some(function (a) { return a.type === type; }); };
      var turns = has('spin') || has('swing'), moves = has('bob'), grows = has('pulse');
      var times = [], qv = [], pv = [], sv = [];
      for (var k = 0; k <= steps; k++) {
        var t = (D * k) / steps;
        q.copy(q0); p.copy(p0); s.copy(s0);
        e.anim.forEach(function (a) {
          var n = cycles(a);
          var w = Math.sin(TAU * ((n * t) / D + (a.phase || 0)));
          if (a.type === 'spin') q.multiply(turn.setFromAxisAngle(AX[a.axis], (a.speed < 0 ? -TAU : TAU) * ((n * t) / D)));
          else if (a.type === 'swing') q.multiply(turn.setFromAxisAngle(AX[a.axis], a.angle * DEG * w));
          else if (a.type === 'bob') p.addScaledVector(AX[a.axis], a.distance * w);
          else if (a.type === 'pulse') s.multiplyScalar(1 + a.amount * w);
        });
        times.push(t);
        if (turns) qv.push(q.x, q.y, q.z, q.w);
        if (moves) pv.push(p.x, p.y, p.z);
        if (grows) sv.push(s.x, s.y, s.z);
      }
      if (turns) tracks.push(new THREE.QuaternionKeyframeTrack(o.uuid + '.quaternion', times, qv));
      if (moves) tracks.push(new THREE.VectorKeyframeTrack(o.uuid + '.position', times, pv));
      if (grows) tracks.push(new THREE.VectorKeyframeTrack(o.uuid + '.scale', times, sv));
    });
    clips.push(new THREE.AnimationClip('idle', D, tracks));
  }

  // hinge moves → one-shot clips ('open' …)
  var shots = {};
  entries.forEach(function (e) {
    e.anim.forEach(function (a) {
      if (a.type !== 'hinge') return;
      var name = a.clip || 'open';
      (shots[name] = shots[name] || []).push({ target: e.target, a: a });
    });
  });
  Object.keys(shots).forEach(function (name) {
    var tracks = [];
    var length = 0;
    shots[name].forEach(function (h) {
      var o = h.target, q0 = o.quaternion.clone(), q = new THREE.Quaternion(), turn = new THREE.Quaternion();
      var n = Math.max(4, Math.ceil(Math.abs(h.a.angle) / 10));
      var times = [], qv = [];
      for (var k = 0; k <= n; k++) {
        var f = k / n;
        var eased = f * f * (3 - 2 * f);
        q.copy(q0).multiply(turn.setFromAxisAngle(AX[h.a.axis], h.a.angle * DEG * eased));
        times.push(h.a.duration * f);
        qv.push(q.x, q.y, q.z, q.w);
      }
      length = Math.max(length, h.a.duration);
      tracks.push(new THREE.QuaternionKeyframeTrack(o.uuid + '.quaternion', times, qv));
    });
    clips.push(new THREE.AnimationClip(name, length, tracks));
  });
  return clips;
}

export function createCheckpointFlag() {
  const root = new THREE.Group();
  root.name = 'CheckpointFlag';

  // Materials
  const baseMat = new THREE.MeshStandardMaterial({ name: 'base', color: 0x3b4250, roughness: 0.6, metalness: 0.3, flatShading: true });
  const bandMat = new THREE.MeshStandardMaterial({ name: 'band', color: 0xe8443a, roughness: 0.5, flatShading: true });
  const poleMat = new THREE.MeshStandardMaterial({ name: 'pole', color: 0xe9edf2, roughness: 0.35, metalness: 0.5, flatShading: true });
  const flagMat = new THREE.MeshStandardMaterial({ name: 'flag', color: 0xe8443a, roughness: 0.75, flatShading: true });
  const lampMat = new THREE.MeshStandardMaterial({ name: 'lamp', color: 0xf5aba6, roughness: 0.3, emissive: 0xf0857f, emissiveIntensity: 1.1, flatShading: true });

  // Parts
  const base = new THREE.Mesh(lathe([[0, 0], [0.34, 0], [0.34, 0.06], [0.27, 0.13], [0.11, 0.15], [0.08, 0.19], [0, 0.19]], 14), baseMat);
  base.name = 'base';
  base.position.set(-0.3975, 0, 0);
  root.add(base);
  const baseBand = new THREE.Mesh(new THREE.TorusGeometry(0.31, 0.03, 4, 18), bandMat);
  baseBand.name = 'baseBand';
  baseBand.position.set(-0.3975, 0.075, 0);
  baseBand.rotation.set(deg(90), 0, 0);
  root.add(baseBand);
  const pole = new THREE.Mesh(new THREE.CylinderGeometry(0.035, 0.045, 2.23, 8), poleMat);
  pole.name = 'pole';
  pole.position.set(-0.3975, 1.285, 0);
  root.add(pole);
  const lampCap = new THREE.Mesh(new THREE.CylinderGeometry(0.06, 0.05, 0.05, 8), baseMat);
  lampCap.name = 'lampCap';
  lampCap.position.set(-0.3975, 2.415, 0);
  root.add(lampCap);
  const lamp = new THREE.Mesh(new THREE.SphereGeometry(0.09, 8, 6), lampMat);
  lamp.name = 'lamp';
  lamp.position.set(-0.3975, 2.51, 0);
  root.add(lamp);
  const flag = new THREE.Group();
  flag.name = 'flag';
  flag.position.set(-0.3975, 0, 0);
  root.add(flag);
  const cloth = new THREE.Mesh(extrude([[0.035, -0.012], [0.0961, -0.0186], [0.1572, -0.0356], [0.2183, -0.056], [0.2794, -0.0702], [0.3406, -0.0695], [0.4017, -0.0486], [0.4628, -0.0089], [0.5239, 0.0303], [0.585, 0.0608], [0.6461, 0.0766], [0.7072, 0.0745], [0.7683, 0.0549], [0.8294, 0.0217], [0.8906, -0.0183], [0.9517, -0.057], [1.0128, -0.0866], [1.0739, -0.1011], [1.135, -0.0976], [1.135, -0.0736], [1.0739, -0.0771], [1.0128, -0.0626], [0.9517, -0.033], [0.8906, 0.0057], [0.8294, 0.0457], [0.7683, 0.0789], [0.7072, 0.0985], [0.6461, 0.1006], [0.585, 0.0848], [0.5239, 0.0543], [0.4628, 0.0151], [0.4017, -0.0246], [0.3406, -0.0455], [0.2794, -0.0462], [0.2183, -0.032], [0.1572, -0.0116], [0.0961, 0.0054], [0.035, 0.012]], 0.7), flagMat);
  cloth.name = 'cloth';
  cloth.position.set(0, 1.99, 0);
  cloth.rotation.set(deg(-90), 0, 0);
  flag.add(cloth);

  // Animations ('idle') — play them with THREE.AnimationMixer
  root.animations = codelsClips(THREE, [
    { target: flag, anim: [{ type: 'swing', axis: 'y', angle: 12, period: 2.4 }] },
  ]);

  root.traverse((o) => {
    if (o.isMesh) {
      o.castShadow = true;
      o.receiveShadow = true;
    }
  });
  return root;
}

export default createCheckpointFlag;

Оценки и комментарии

—
Оценок пока нет — будьте первым.
5
0
4
0
3
0
2
0
1
0

Войдите, чтобы ставить оценки и оставлять комментарии.

0 комментариев

Комментариев пока нет. Расскажите, как вы использовали эту модель или что можно улучшить.

Похожие модели

Та же категория или похожие теги