Vision-JEV-128M / tools /render_can.html
topabaem's picture
Vision · Decisions API: light theme, ShoreCrab banner, typed questions, new samples, no-overlap table; RTS removed
cc231dc verified
Raw History Blame Contribute Delete
4.6 kB
<!doctype html>
<html><head><meta charset="utf-8"><title>can render</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<style>body{margin:0;background:#fff}</style></head>
<body><div id="out"></div>
<script>
// Procedural soda can for the demo samples (original artwork; generic brand). Call render(dent) -> PNG data URL.
function labelTexture() {
const c = document.createElement('canvas'); c.width = 1024; c.height = 512; const g = c.getContext('2d');
const grd = g.createLinearGradient(0, 0, 0, 512); grd.addColorStop(0, '#d92b3a'); grd.addColorStop(1, '#a3131f'); g.fillStyle = grd; g.fillRect(0, 0, 1024, 512);
g.fillStyle = '#ffffff'; g.fillRect(0, 300, 1024, 26); g.fillStyle = '#f6c343'; g.fillRect(0, 334, 1024, 10);
g.fillStyle = '#fff'; g.font = 'bold 120px Helvetica, Arial'; g.textAlign = 'center'; g.fillText('FIZZ', 256, 250); g.fillText('FIZZ', 768, 250);
g.font = '600 34px Helvetica, Arial'; g.fillText('sparkling citrus', 256, 400); g.fillText('sparkling citrus', 768, 400);
for (let i = 0; i < 40; i++) { g.fillStyle = 'rgba(255,255,255,0.08)'; g.beginPath(); g.arc(Math.random() * 1024, Math.random() * 280, 6 + Math.random() * 18, 0, 7); g.fill(); }
const t = new THREE.CanvasTexture(c); t.anisotropy = 8; return t;
}
function render(dent, angle) {
const W = 960, H = 720;
const renderer = new THREE.WebGLRenderer({ antialias: true, preserveDrawingBuffer: true });
renderer.setSize(W, H); renderer.setPixelRatio(1); renderer.shadowMap.enabled = true; renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.toneMapping = THREE.ACESFilmicToneMapping; renderer.outputEncoding = THREE.sRGBEncoding;
const scene = new THREE.Scene(); scene.background = new THREE.Color(0xf3f1ec);
const cam = new THREE.PerspectiveCamera(32, W / H, 0.1, 100); cam.position.set(0, 2.2, 7.4); cam.lookAt(0, 1.25, 0);
scene.add(new THREE.HemisphereLight(0xffffff, 0xd8d2c8, 0.55));
const key = new THREE.DirectionalLight(0xffffff, 1.15); key.position.set(-3.5, 6, 4.5); key.castShadow = true; key.shadow.mapSize.set(2048, 2048); key.shadow.radius = 6; scene.add(key);
const rim = new THREE.DirectionalLight(0xffffff, 0.55); rim.position.set(4, 3, -3); scene.add(rim);
const fill = new THREE.PointLight(0xffffff, 0.35); fill.position.set(2.5, 2, 5); scene.add(fill);
const floor = new THREE.Mesh(new THREE.PlaneGeometry(30, 30), new THREE.MeshStandardMaterial({ color: 0xe9e5dd, roughness: 0.9 }));
floor.rotation.x = -Math.PI / 2; floor.receiveShadow = true; scene.add(floor);
// body: open cylinder with many segments so a dent can be sculpted into the vertices
const R = 0.66, Hc = 2.45, geo = new THREE.CylinderGeometry(R, R, Hc, 160, 120, true);
const pos = geo.attributes.position, v = new THREE.Vector3();
for (let i = 0; i < pos.count; i++) {
v.fromBufferAttribute(pos, i); const y = v.y; let r = R;
const t = Math.abs(y) / (Hc / 2); if (t > 0.9) r = R * (1 - 0.1 * ((t - 0.9) / 0.1) ** 1.6); // tapered shoulders
if (dent) {
const a = Math.atan2(v.x, v.z), da = a - dent.angle, dy = y - dent.y;
const w = Math.exp(-(da * da) / (2 * dent.sa * dent.sa) - (dy * dy) / (2 * dent.sy * dent.sy));
const crease = 1 + 0.35 * Math.sin(dy * 14 + da * 6) * w; // small creases inside the dent
r -= dent.depth * w * crease;
}
const a = Math.atan2(v.x, v.z); pos.setXYZ(i, Math.sin(a) * r, y, Math.cos(a) * r);
}
geo.computeVertexNormals();
const body = new THREE.Mesh(geo, new THREE.MeshStandardMaterial({ map: labelTexture(), metalness: 0.55, roughness: 0.32 }));
body.position.y = Hc / 2 + 0.06; body.rotation.y = angle; body.castShadow = true; scene.add(body);
const metal = new THREE.MeshStandardMaterial({ color: 0xd9dce1, metalness: 0.95, roughness: 0.22 });
const top = new THREE.Mesh(new THREE.CylinderGeometry(R * 0.86, R * 0.9, 0.08, 96), metal); top.position.y = Hc + 0.08; top.castShadow = true; scene.add(top);
const lip = new THREE.Mesh(new THREE.TorusGeometry(R * 0.87, 0.035, 16, 96), metal); lip.rotation.x = Math.PI / 2; lip.position.y = Hc + 0.12; scene.add(lip);
const tab = new THREE.Mesh(new THREE.BoxGeometry(0.34, 0.02, 0.18), metal); tab.position.set(0.05, Hc + 0.13, 0.08); scene.add(tab);
const base = new THREE.Mesh(new THREE.CylinderGeometry(R * 0.9, R * 0.8, 0.1, 96), metal); base.position.y = 0.05; base.castShadow = true; scene.add(base);
renderer.render(scene, cam);
return renderer.domElement.toDataURL('image/png');
}
window.renderCan = render;
</script></body></html>