Files
Lucas RayanandClaude Opus 5.5 07aaf8e1a7
CI / Trivy (dependências, segredos e configuração) (push) Successful in 1m2s
CI / Testes e imagem Docker (linux/arm64) (push) Successful in 4m24s
Release / Plano (reaproveitar a imagem?) (push) Successful in 5s
Release / ci (push) Skipped
Release / Publica a imagem (linux/arm64) (push) Successful in 18s
Release / Cria a release no Gitea (push) Successful in 4s
feat: novo site e interface; tradução Braille no navegador (1.4.0)
Interface reescrita (design próprio, tema claro/escuro, a11y WCAG 2.2 AA),
seções do site e artigo IDEIA @ CSBC 2025. Tradutor portado para JS com
paridade 100% contra o Python; sem Tailwind nem axios.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 19:51:53 -03:00

174 lines
6.1 KiB
JavaScript

import * as THREE from "three";
import { STLLoader } from "three/addons/loaders/STLLoader.js";
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
// The dots stick out 0.65 mm beyond the plate's front face (apparent thickness).
// After the model's 90° rotation this protrusion is along -Y, so any surface whose
// Y is below (plate_front = minY + 0.65) belongs to a dot / orientation marker.
const APPARENT_THICKNESS = 0.65;
const PLATE_LIGHT = 0x3a5bd9;
const PLATE_DARK = 0x5b78e8;
const BG_LIGHT = 0xeef1f6;
const BG_DARK = 0x151e30;
const PLATE_COLOR = new THREE.Color(PLATE_LIGHT);
const DOT_COLOR = new THREE.Color(0xf2a93b); // amber, same as the UI dots
const reduceMotion = () => !!(window.matchMedia && window.matchMedia("(prefers-reduced-motion: reduce)").matches);
function isDarkNow() {
const t = document.documentElement.dataset.theme;
if (t === "dark") return true;
if (t === "light") return false;
return !!(window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches);
}
let state = null;
function init(container) {
const scene = new THREE.Scene();
scene.background = new THREE.Color(isDarkNow() ? BG_DARK : BG_LIGHT);
PLATE_COLOR.set(isDarkNow() ? PLATE_DARK : PLATE_LIGHT);
const camera = new THREE.PerspectiveCamera(
40,
container.clientWidth / container.clientHeight,
0.1,
5000
);
camera.up.set(0, 0, 1);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(container.clientWidth, container.clientHeight);
container.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = !reduceMotion();
controls.autoRotate = false;
controls.dampingFactor = 0.08;
// Lighting
scene.add(new THREE.AmbientLight(0xffffff, 0.8));
const keyLight = new THREE.DirectionalLight(0xffffff, 1.0);
keyLight.position.set(0.5, -1, 1);
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xffffff, 0.45);
fillLight.position.set(-1, 0.5, 0.5);
scene.add(fillLight);
state = { scene, camera, renderer, controls, container, mesh: null };
function animate() {
state.rafId = requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
animate();
const resize = () => {
if (!container.clientWidth || !container.clientHeight) { return; }
camera.aspect = container.clientWidth / container.clientHeight;
camera.updateProjectionMatrix();
renderer.setSize(container.clientWidth, container.clientHeight);
};
window.addEventListener("resize", resize);
state.resize = resize;
}
function paintByHeight(geometry) {
geometry.computeBoundingBox();
const box = geometry.boundingBox;
// The plate's front face sits exactly at (min.y + APPARENT_THICKNESS);
// Threshold ensures the full dome of every dot is colored with DOT_COLOR.
const threshold = box.min.y + APPARENT_THICKNESS - 0.01;
const position = geometry.attributes.position;
const colors = new Float32Array(position.count * 3);
for (let i = 0; i < position.count; i++) {
const color = position.getY(i) < threshold ? DOT_COLOR : PLATE_COLOR;
colors[i * 3] = color.r;
colors[i * 3 + 1] = color.g;
colors[i * 3 + 2] = color.b;
}
geometry.setAttribute("color", new THREE.BufferAttribute(colors, 3));
}
function frameModel() {
const { camera, controls, mesh, container } = state;
const box = new THREE.Box3().setFromObject(mesh);
const size = box.getSize(new THREE.Vector3());
const aspect = container.clientWidth / container.clientHeight;
const fov = (camera.fov * Math.PI) / 180;
// We look along -Y: on screen the width is X and the height is Z.
const fitHeight = (size.z / 2) / Math.tan(fov / 2);
const fitWidth = (size.x / 2) / (Math.tan(fov / 2) * aspect);
const distance = Math.max(fitHeight, fitWidth) * 1.15;
controls.target.set(0, 0, 0);
// Mostly a front view (looking along +Y), with a slight tilt so the dots read as 3D.
camera.position.set(distance * 0.08, -distance, distance * 0.16);
camera.near = Math.max(distance / 1000, 0.01);
camera.far = distance * 100;
camera.updateProjectionMatrix();
controls.update();
}
// Recolors the vertices already painted with the plate color (dots keep amber).
function repaintPlate() {
if (!state || !state.mesh) return;
const colors = state.mesh.geometry.getAttribute("color");
const position = state.mesh.geometry.attributes.position;
if (!colors) return;
state.mesh.geometry.computeBoundingBox();
const threshold = state.mesh.geometry.boundingBox.min.y + APPARENT_THICKNESS - 0.01;
for (let i = 0; i < position.count; i++) {
const c = position.getY(i) < threshold ? DOT_COLOR : PLATE_COLOR;
colors.setXYZ(i, c.r, c.g, c.b);
}
colors.needsUpdate = true;
}
window.setPreviewTheme = function (isDark) {
PLATE_COLOR.set(isDark ? PLATE_DARK : PLATE_LIGHT);
if (!state) return;
state.scene.background = new THREE.Color(isDark ? BG_DARK : BG_LIGHT);
repaintPlate();
};
window.renderSTLPreview = function (arrayBuffer, resetCamera = false) {
const container = document.getElementById("stl_preview_canvas");
if (!container) { return; }
if (!state) { init(container); }
const isFirstLoad = !state.mesh;
if (state.mesh) {
state.scene.remove(state.mesh);
state.mesh.geometry.dispose();
state.mesh.material.dispose();
state.mesh = null;
}
const geometry = new STLLoader().parse(arrayBuffer);
geometry.computeVertexNormals();
geometry.center();
paintByHeight(geometry);
const material = new THREE.MeshStandardMaterial({
vertexColors: true,
metalness: 0.1,
roughness: 0.55,
side: THREE.DoubleSide
});
const mesh = new THREE.Mesh(geometry, material);
state.scene.add(mesh);
state.mesh = mesh;
if (isFirstLoad || resetCamera) {
frameModel();
}
state.resize();
};