Codels
760 tris0.16 × 0.40 × 0.05 m

Use it in code

Remix in the editor
Self-contained ES module — only depends on three. Call the factory and add the group to your scene.
/**
 * Ornate Key — Codels 3D model library
 * License: CC0-1.0 · Units: meters · Y-up · Front: +Z · Origin: center of the model (it floats — pickups, planets)
 * Triangles: 760 · Size (W × H × D): 0.16 × 0.40 × 0.05 m
 *
 * Usage:
 *   import { createKey } from './key.js';
 *   
 *   const model = createKey();
 *   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';

// 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 createKey() {
  const root = new THREE.Group();
  root.name = 'Key';

  // Materials
  const metalMat = new THREE.MeshStandardMaterial({ name: 'metal', color: 0xe9b949, roughness: 0.32, metalness: 0.8, flatShading: true });
  const gemMat = new THREE.MeshStandardMaterial({ name: 'gem', color: 0xe8364f, roughness: 0.15, emissive: 0xe8364f, emissiveIntensity: 0.25, flatShading: true });

  // Shared geometries
  const torusGeo = new THREE.TorusGeometry(0.021, 0.0065, 4, 10);

  // Parts
  const key = new THREE.Group();
  key.name = 'key';
  key.position.set(-0.002, -0.0115, 0);
  root.add(key);
  const bow = new THREE.Mesh(new THREE.TorusGeometry(0.062, 0.016, 6, 18), metalMat);
  bow.name = 'bow';
  bow.position.set(0, 0.105, 0);
  key.add(bow);
  const filigree1 = new THREE.Mesh(torusGeo, metalMat);
  filigree1.name = 'filigree1';
  filigree1.position.set(0, 0.129, 0);
  key.add(filigree1);
  const filigree2 = new THREE.Mesh(torusGeo, metalMat);
  filigree2.name = 'filigree2';
  filigree2.position.set(-0.0208, 0.093, 0);
  key.add(filigree2);
  const filigree3 = new THREE.Mesh(torusGeo, metalMat);
  filigree3.name = 'filigree3';
  filigree3.position.set(0.0208, 0.093, 0);
  key.add(filigree3);
  const gem = new THREE.Mesh(new THREE.OctahedronGeometry(0.017, 0), gemMat);
  gem.name = 'gem';
  gem.position.set(0, 0.105, 0);
  gem.scale.set(1, 1.2, 0.8);
  key.add(gem);
  const finial = new THREE.Mesh(new THREE.SphereGeometry(0.017, 6, 4), metalMat);
  finial.name = 'finial';
  finial.position.set(0, 0.195, 0);
  key.add(finial);
  const collarKnob = new THREE.Mesh(new THREE.SphereGeometry(0.024, 7, 5), metalMat);
  collarKnob.name = 'collarKnob';
  collarKnob.position.set(0, 0.031, 0);
  collarKnob.scale.set(1, 0.8, 1);
  key.add(collarKnob);
  const collarRing1 = new THREE.Mesh(new THREE.CylinderGeometry(0.026, 0.026, 0.012, 8), metalMat);
  collarRing1.name = 'collarRing1';
  collarRing1.position.set(0, 0.007, 0);
  key.add(collarRing1);
  const collarRing2 = new THREE.Mesh(new THREE.CylinderGeometry(0.021, 0.021, 0.01, 8), metalMat);
  collarRing2.name = 'collarRing2';
  collarRing2.position.set(0, -0.011, 0);
  key.add(collarRing2);
  const shaft = new THREE.Mesh(new THREE.CylinderGeometry(0.0135, 0.0135, 0.193, 8), metalMat);
  shaft.name = 'shaft';
  shaft.position.set(0, -0.0855, 0);
  key.add(shaft);
  const tip = new THREE.Mesh(new THREE.CylinderGeometry(0.018, 0.018, 0.016, 8), metalMat);
  tip.name = 'tip';
  tip.position.set(0, -0.182, 0);
  key.add(tip);
  const bit = new THREE.Mesh(extrude([[0.006, -0.172], [0.068, -0.172], [0.068, -0.15], [0.05, -0.15], [0.05, -0.136], [0.068, -0.136], [0.068, -0.108], [0.034, -0.108], [0.034, -0.094], [0.006, -0.094]], 0.012, [], [0.002, 0.002, 1]), metalMat);
  bit.name = 'bit';
  key.add(bit);

  // Animations ('idle') — play them with THREE.AnimationMixer
  root.animations = codelsClips(THREE, [
    { target: key, anim: [{ type: 'spin', axis: 'y', speed: 90 }, { type: 'bob', axis: 'y', distance: 0.05, period: 2 }] },
  ]);

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

export default createKey;

Ratings & comments

—
No ratings yet — be the first.
5
0
4
0
3
0
2
0
1
0

Sign in to rate and comment.

0 comments

No comments yet. Share how you used this model or what could be better.

Related models

Same category or similar tags