187 lines
13 KiB
JavaScript
187 lines
13 KiB
JavaScript
// Hand-rolled corona: all leaf surfaces, veins, grain, foil and ash are procedural.
|
|
// X is the long axis; head at -3, lit foot at +2.35. Every pose is absolute.
|
|
import * as THREE from 'three';
|
|
import {clamp01, lerp, smooth, rng} from '../kit/util.js';
|
|
|
|
export const L = 6;
|
|
export const BURN_X = 2.35;
|
|
const TAU = Math.PI * 2;
|
|
const R = 0.53;
|
|
export const rAt = (x) => R * (1 + 0.025 * Math.sin((x + 2.6) * 0.8)) * (1 - 0.045 * smooth((x - 0.6) / 2)) * (1 - 0.58 * smooth((-2.53 - x) / 0.47));
|
|
const point = (x, a, scale = 1) => new THREE.Vector3(x, rAt(x) * scale * Math.sin(a), rAt(x) * scale * Math.cos(a));
|
|
|
|
// A profiled cylindrical leaf, with local micro-wrinkles and UVs for tobacco grain.
|
|
function shell(x0, x1, a0 = 0, a1 = TAU, scale = 1) {
|
|
const p = [], uv = [], ix = [];
|
|
const nx = 40, na = Math.max(16, Math.round(72 * (a1 - a0) / TAU));
|
|
for (let i = 0; i <= nx; i++) for (let j = 0; j <= na; j++) {
|
|
const x = lerp(x0, x1, i / nx), a = lerp(a0, a1, j / na);
|
|
const rr = 1 + 0.003 * Math.sin(x * 47 + a * 17) + 0.002 * Math.sin(a * 53 - x * 21);
|
|
const v = point(x, a, scale * rr);
|
|
p.push(v.x, v.y, v.z); uv.push(i / nx, j / na);
|
|
if (i < nx && j < na) {const k = i * (na + 1) + j; ix.push(k, k + 1, k + na + 1, k + 1, k + na + 2, k + na + 1);}
|
|
}
|
|
const g = new THREE.BufferGeometry();
|
|
g.setAttribute('position', new THREE.Float32BufferAttribute(p, 3));
|
|
g.setAttribute('uv', new THREE.Float32BufferAttribute(uv, 2));
|
|
g.setIndex(ix); g.computeVertexNormals(); return g;
|
|
}
|
|
|
|
function grainTexture() {
|
|
const n = 256, random = rng(7341), data = new Uint8Array(n * n * 4);
|
|
for (let y = 0; y < n; y++) for (let x = 0; x < n; x++) {
|
|
const a = (y * n + x) * 4;
|
|
const k = 145 + random() * 65 + 17 * Math.sin(y * 0.7 + x * 0.032) + 9 * Math.sin(y * 2.3 + x * 0.11);
|
|
data[a] = k; data[a + 1] = k; data[a + 2] = k; data[a + 3] = 255;
|
|
}
|
|
const t = new THREE.DataTexture(data, n, n); t.wrapS = t.wrapT = THREE.RepeatWrapping;
|
|
t.repeat.set(3, 2); t.needsUpdate = true; return t;
|
|
}
|
|
|
|
export function buildCigar() {
|
|
const root = new THREE.Group(), anchors = {}, clear = [], random = rng(713);
|
|
root.name = 'CR-7 / Torcidora';
|
|
const grain = grainTexture();
|
|
const leaf = (color, roughness = 0.68) => new THREE.MeshPhysicalMaterial({color, roughness, metalness: 0, clearcoat: 0.12, clearcoatRoughness: 0.55, side: THREE.DoubleSide, map: grain, bumpMap: grain, bumpScale: 0.009});
|
|
const M = {
|
|
wrapper: leaf(0x7a4a22), veins: leaf(0x402611, 0.8), binder: leaf(0x99804b, 0.82),
|
|
seco: leaf(0xb2925b, 0.88), viso: leaf(0x80502c, 0.85), ligero: leaf(0x432916, 0.9),
|
|
band: new THREE.MeshPhysicalMaterial({color: 0x6d1428, roughness: 0.42, clearcoat: 0.4, side: THREE.DoubleSide}),
|
|
gold: new THREE.MeshStandardMaterial({color: 0xcfa64a, metalness: 0.82, roughness: 0.3}),
|
|
char: new THREE.MeshStandardMaterial({color: 0x201a15, roughness: 1}),
|
|
ash: new THREE.MeshStandardMaterial({color: 0xbcb7ab, roughness: 1}),
|
|
ashDark: new THREE.MeshStandardMaterial({color: 0x68635c, roughness: 1}),
|
|
ember: new THREE.MeshStandardMaterial({color: 0x6a2106, emissive: 0xff5a13, emissiveIntensity: 1.8, roughness: 1}),
|
|
hot: new THREE.MeshBasicMaterial({color: new THREE.Color(0xff9e38).multiplyScalar(2.2)}),
|
|
smoke: new THREE.MeshBasicMaterial({color: 0x8d877d, transparent: true, opacity: 0.19, depthWrite: false, side: THREE.DoubleSide}),
|
|
};
|
|
const mesh = (parent, geo, mat) => {const m = new THREE.Mesh(geo, mat); parent.add(m); return m;};
|
|
const group = (name) => {const g = new THREE.Group(); g.name = name; root.add(g); return g;};
|
|
const anchor = (key, obj, at) => anchors[key] = {obj, at: new THREE.Vector3(...at)};
|
|
const tube = (parent, pts, radius, mat, segments = 32) => mesh(parent, new THREE.TubeGeometry(new THREE.CatmullRomCurve3(pts), segments, radius, 4, false), mat);
|
|
const ring = (parent, x, rr, radius, mat) => {const m = mesh(parent, new THREE.TorusGeometry(rr, radius, 8, 80), mat); m.rotation.y = Math.PI / 2; m.position.x = x; return m;};
|
|
const disk = (parent, x, rr, mat) => {const m = mesh(parent, new THREE.CircleGeometry(rr, 64), mat); m.rotation.y = Math.PI / 2; m.position.x = x; return m;};
|
|
const veins = (g, x0, x1, a0, a1, scale = 1, count = 14) => {
|
|
for (let i = 0; i < count; i++) {
|
|
const xa = lerp(x0, x1 - 0.18, random()), aa = lerp(a0 + 0.02, a1 - 0.02, random());
|
|
const length = Math.min(x1 - xa, 0.28 + random() * 0.75), drift = (random() - 0.5) * 0.4;
|
|
const pts = Array.from({length: 9}, (_, j) => point(xa + length * j / 8, Math.max(a0, Math.min(a1, aa + drift * j / 8)), scale * 1.006));
|
|
tube(g, pts, 0.0023 + random() * 0.002, M.veins, 12);
|
|
}
|
|
};
|
|
const body = group('Wrapper / fixed shoulder and back');
|
|
mesh(body, shell(-2.83, -1.02), M.wrapper); mesh(body, shell(1.45, BURN_X), M.wrapper);
|
|
mesh(body, shell(-1.02, 1.45, Math.PI / 2, Math.PI * 1.5), M.wrapper);
|
|
veins(body, -2.83, -1.02, 0, TAU, 1, 34); veins(body, 1.45, BURN_X, 0, TAU, 1, 18);
|
|
veins(body, -1.02, 1.45, Math.PI / 2, Math.PI * 1.5, 1, 25);
|
|
// Fine spiral overlap seam makes the single wrapper leaf legible in the assembled hero.
|
|
for (const [a, b] of [[-2.8, -1.03], [1.46, BURN_X]]) {
|
|
tube(body, Array.from({length: 110}, (_, i) => {const x = lerp(a, b, i / 109); return point(x, x * 4.2 + 0.6, 1.008);}), 0.003, M.veins, 110);
|
|
}
|
|
|
|
const cap = group('CAP');
|
|
mesh(cap, shell(-3, -2.83), M.wrapper); disk(cap, -3, rAt(-3), M.viso);
|
|
for (let i = 0; i < 26; i++) {const a = random() * TAU, rr = random() * rAt(-3) * 0.95; const m = mesh(cap, new THREE.SphereGeometry(0.014, 5, 4), [M.seco, M.viso, M.ligero][i % 3]); m.position.set(-3.003, rr * Math.sin(a), rr * Math.cos(a));}
|
|
ring(cap, -2.845, rAt(-2.845) * 1.006, 0.005, M.veins);
|
|
anchor('CAP', cap, [-2.94, 0.12, 0.12]);
|
|
|
|
const band = group('BAND');
|
|
mesh(band, shell(-2.22, -1.62, 0, TAU, 1.026), M.band);
|
|
for (const x of [-2.22, -2.18, -1.66, -1.62]) ring(band, x, rAt(x) * 1.031, 0.009, M.gold);
|
|
// Raised oval foil cartouche and embossed monogram, facing the viewing side.
|
|
const plaque = mesh(band, new THREE.SphereGeometry(1, 32, 24), M.gold); plaque.scale.set(0.22, 0.24, 0.025); plaque.position.set(-1.92, 0, 0.558);
|
|
const inset = mesh(band, new THREE.SphereGeometry(1, 32, 24), M.band); inset.scale.set(0.18, 0.20, 0.024); inset.position.set(-1.92, 0, 0.576);
|
|
for (let i = 0; i < 20; i++) {const a = TAU * i / 20; const b = mesh(band, new THREE.SphereGeometry(0.011, 7, 6), M.gold); b.position.set(-1.92 + Math.sin(a) * 0.195, Math.cos(a) * 0.218, 0.583);}
|
|
tube(band, [new THREE.Vector3(-1.83, .10, .607), new THREE.Vector3(-1.99, .10, .607), new THREE.Vector3(-2.02, 0, .607), new THREE.Vector3(-1.99, -.10, .607), new THREE.Vector3(-1.83, -.10, .607)], .012, M.gold, 24);
|
|
tube(band, [new THREE.Vector3(-1.94, .11, .608), new THREE.Vector3(-1.94, -.11, .608)], .009, M.gold, 8);
|
|
for (let i = 0; i < 24; i++) {const a = TAU * i / 24; tube(band, [point(-2.13, a, 1.036), point(-2.07, a + .08, 1.036)], .004, M.gold, 4); tube(band, [point(-1.76, a, 1.036), point(-1.70, a + .08, 1.036)], .004, M.gold, 4);}
|
|
anchor('BAND', band, [-1.92, 0.07, 0.6]);
|
|
|
|
const shutters = [];
|
|
for (const sign of [1, -1]) {
|
|
const g = group(sign === 1 ? 'Wrapper / upper shutter' : 'Wrapper / lower shutter');
|
|
const content = new THREE.Group(); g.add(content);
|
|
const hinge = new THREE.Vector3(0, sign * R, 0);
|
|
g.position.copy(hinge); content.position.copy(hinge).multiplyScalar(-1);
|
|
const a0 = sign === 1 ? 0.006 : -Math.PI / 2, a1 = sign === 1 ? Math.PI / 2 : -0.006;
|
|
mesh(content, shell(-1.02, 1.45, a0, a1, 1.002), M.wrapper);
|
|
veins(content, -1.02, 1.45, a0, a1, 1.005, 35);
|
|
tube(content, Array.from({length: 35}, (_, i) => point(lerp(-1.02, 1.45, i / 34), sign * .035, 1.012)), .004, M.veins, 34);
|
|
shutters.push({g, sign, hinge});
|
|
if (sign === 1) anchor('WRAPPER', content, [.3, .27, .47]);
|
|
}
|
|
|
|
const binder = group('BINDER');
|
|
const binderLeaf = new THREE.Group(); binder.add(binderLeaf); binder.position.y = -R * .91; binderLeaf.position.y = R * .91;
|
|
mesh(binderLeaf, shell(-1, 1.43, -Math.PI / 2, Math.PI / 2, .91), M.binder);
|
|
veins(binderLeaf, -1, 1.43, -Math.PI / 2, Math.PI / 2, .914, 28);
|
|
const binderBack = group('Binder / back'); mesh(binderBack, shell(-2.80, 2.33, Math.PI / 2, Math.PI * 1.5, .91), M.binder);
|
|
anchor('BINDER', binderLeaf, [.3, 0, .485]);
|
|
|
|
const filler = group('FILLER'); anchor('FILLER', filler, [.6, 0, .1]);
|
|
const leaves = [];
|
|
for (let k = 0; k < 3; k++) {
|
|
const g = new THREE.Group(); filler.add(g); g.name = ['SECO', 'VISO', 'LIGERO'][k];
|
|
const angle = TAU * k / 3, off = new THREE.Vector3(0, .19 * Math.sin(angle), .19 * Math.cos(angle));
|
|
const mat = [M.seco, M.viso, M.ligero][k];
|
|
// Irregular folded leaf ribbons, tightly bunched in longitudinal rolls.
|
|
for (let n = 0; n < 10; n++) {
|
|
const phi = TAU * n / 10, rad = .07 + .09 * random();
|
|
const pts = Array.from({length: 38}, (_, j) => {const x = lerp(-2.83 + .02 * k, 2.33 - .025 * n, j / 37), a = phi + .9 * x + k; return new THREE.Vector3(x, off.y + Math.sin(a) * rad, off.z + Math.cos(a) * rad);});
|
|
const strand = tube(g, pts, .024 + random() * .02, mat, 55); strand.scale.z = .8;
|
|
tube(g, pts.map((p) => p.clone().add(new THREE.Vector3(0, .018, .017))), .0025, M.veins, 45);
|
|
}
|
|
anchor(['SECO', 'VISO', 'LIGERO'][k], g, [.5, off.y, off.z]); leaves.push({g, angle});
|
|
}
|
|
|
|
const foot = group('FOOT');
|
|
mesh(foot, shell(2.29, 2.47, 0, TAU, 1.013), M.char);
|
|
disk(foot, 2.48, R * .87, M.char);
|
|
ring(foot, 2.43, R * .94, .023, M.ember);
|
|
// Coals cluster at the foot rather than glowing across the unburnt wrapper.
|
|
for (let i = 0; i < 95; i++) {
|
|
const a = random() * TAU, rr = Math.sqrt(random()) * R * .91;
|
|
const m = mesh(foot, new THREE.IcosahedronGeometry(.014 + random() * .025, 0), i % 4 === 0 ? M.hot : M.ember);
|
|
m.position.set(2.49 + random() * .035, Math.sin(a) * rr, Math.cos(a) * rr); m.scale.x = .4;
|
|
}
|
|
anchor('FOOT', foot, [2.48, .18, .22]);
|
|
const ash = group('ASH');
|
|
const ashFixed = group('Ash / attached collar');
|
|
const ashSurface = (g, a, b) => {
|
|
mesh(g, shell(a, b, 0, TAU, .97), M.ash);
|
|
for (let x = a; x <= b; x += .043) ring(g, x, rAt(x) * (.96 + random() * .03), .009 + random() * .006, random() > .6 ? M.ashDark : M.ash);
|
|
for (let i = 0; i < 25; i++) {const x = lerp(a, b, random()), th = random() * TAU; tube(g, [point(x, th, .992), point(Math.min(b, x + .08), th + .12, .992), point(Math.min(b, x + .13), th + .06, .992)], .005, M.ashDark, 6);}
|
|
};
|
|
ashSurface(ashFixed, 2.49, 2.64); ashSurface(ash, 2.65, 3.03); disk(ash, 3.03, rAt(3.03) * .97, M.ashDark);
|
|
for (let i = 0; i < 60; i++) {const a = random() * TAU, rr = Math.sqrt(random()) * .46; const m = mesh(ash, new THREE.IcosahedronGeometry(.018 + random() * .025, 0), i % 3 ? M.ash : M.ashDark); m.position.set(3.035, Math.sin(a) * rr, Math.cos(a) * rr); m.scale.x = .2;}
|
|
anchor('ASH', ash, [2.86, .14, .36]);
|
|
const smoke = group('Smoke ribbons');
|
|
const ribbons = [];
|
|
for (let k = 0; k < 3; k++) {
|
|
const pts = Array.from({length: 48}, (_, j) => {const u = j / 47; return new THREE.Vector3(2.56 + .12 * Math.sin(u * 8 + k), .44 + u * 1.8, .08 * Math.sin(u * 12 + k * 2) + k * .05);});
|
|
const m = tube(smoke, pts, .009 + .004 * k, M.smoke, 55); clear.push(m); ribbons.push(m);
|
|
}
|
|
|
|
function pose(s = {}) {
|
|
const t = s.t ?? 0, ex = clamp01(s.explode ?? 0), op = (key) => clamp01(s[key] ?? 0);
|
|
root.rotation.y = (s.turn ?? 0) * Math.PI / 180;
|
|
body.position.x = -1.25 * ex;
|
|
cap.rotation.set(0, -.28 * op('cap'), -.25 * op('cap'));
|
|
const capPivot = new THREE.Vector3(-2.93, 0, 0), capRotated = capPivot.clone().applyEuler(cap.rotation);
|
|
cap.position.copy(capPivot).sub(capRotated).add(new THREE.Vector3(-.64 * op('cap') - 3.06 * ex, .60 * op('cap') + .15 * ex, .18 * op('cap')));
|
|
const bandSlide = smooth(op('band') / .72), bandLift = smooth((op('band') - .72) / .28);
|
|
band.position.set(-1.65 * bandSlide - 2.375 * ex, .65 * bandLift + .95 * ex, .35 * bandLift); band.rotation.set(.36 * bandLift, 0, -.22 * bandLift);
|
|
const wrap = Math.max(op('wrapper'), op('binder'), op('tripa'));
|
|
for (const {g, sign, hinge} of shutters) {g.position.copy(hinge); g.position.x = -.65 * ex; g.position.y += sign * .35 * ex; g.rotation.x = -sign * 1.14 * Math.max(wrap, ex);}
|
|
binder.position.set(.5 * ex, -R * .91 - .35 * ex, 0); binder.rotation.x = 1.48 * Math.max(op('binder'), op('tripa'), ex);
|
|
binderBack.position.x = .5 * ex;
|
|
filler.position.x = 2.56 * ex;
|
|
leaves.forEach(({g, angle}, k) => {const spread = Math.max(op('tripa'), ex); g.position.set((k - 1) * .26 * spread, Math.sin(angle) * .54 * spread, Math.cos(angle) * .66 * spread); g.rotation.set(0, (k - 1) * .055 * spread, (k - 1) * .035 * spread);});
|
|
foot.position.x = 5.19 * ex + .13 * op('foot'); ashFixed.position.x = foot.position.x;
|
|
const drop = op('ashDrop'); ash.position.set(6.25 * ex + .24 * drop, -1.12 * drop * drop + .18 * ex, .15 * drop); ash.rotation.z = -.65 * drop;
|
|
M.ember.emissiveIntensity = 1 + 2 * clamp01(.35 + .45 * op('ember') + .16 * Math.sin(t * 8.7) + .08 * Math.sin(t * 17.3));
|
|
ribbons.forEach((m, i) => {m.position.set(foot.position.x + .06 * Math.sin(t * .9 + i), .025 * Math.sin(t * 1.1 + i), .075 * Math.sin(t * .7 + i)); m.rotation.x = .07 * Math.sin(t * .8 + i); m.scale.y = .95 + .08 * Math.sin(t * .9 + i);});
|
|
}
|
|
pose(); return {root, anchors, clear, M, pose};
|
|
}
|