gitea stl preview

gitea stl preview via threejs

当前为 2025-04-12 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         gitea stl preview
// @namespace    http://tampermonkey.net/
// @version      2025-04-11
// @description  gitea stl preview via threejs
// @author       You
// @match        https://<gitea>/**.stl
// @icon         https://www.google.com/s2/favicons?sz=64&domain=undefined.gitea
// @license MIT
// @grant        unsafeWindow
// ==/UserScript==


const s = document.createElement('script');
s.type="importmap";
const threeVersion = '0.160.1';
s.textContent = `
       {
      "imports": {
        "three": "https://cdn.jsdelivr.net/npm/three@${threeVersion}/build/three.module.js",
        "three/examples/jsm/loaders/STLLoader.js": "https://cdn.jsdelivr.net/npm/three@${threeVersion}/examples/jsm/loaders/STLLoader.js",
        "three/examples/jsm/controls/OrbitControls.js": "https://cdn.jsdelivr.net/npm/three@${threeVersion}/examples/jsm/controls/OrbitControls.js"
      }
    }
    `;
document.head.appendChild(s);


 const loadScript = (src) => new Promise((resolve, reject) => {
    const s = document.createElement('script');
     s.type="module";
     s.textContent = `

         import { OrbitControls } from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/controls/OrbitControls.js';
         import * as THREE from 'https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js';
         import { STLLoader } from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/loaders/STLLoader.js';

        const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
    75, window.innerWidth / window.innerHeight, 0.1, 1000
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

const ambient = new THREE.AmbientLight(0x404040);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1); // Directional light
directionalLight.position.set(1, 1, 1).normalize(); // Position the light source
scene.add(ambient, directionalLight);
camera.position.z = 5;

// Animation loop
function animate() {
    requestAnimationFrame(animate);
    //cube.rotation.x += 0.01;
   // cube.rotation.y += 0.01;
    renderer.render(scene, camera);
}

camera.position.set(0, 0, 100);

const controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 0, 0);
controls.update();


const loader = new STLLoader();
loader.load(location.href.replace("/src/","/raw/"), function (geometry) {
    debugger;
    const material = new THREE.MeshPhongMaterial({ color: 0x2194ce });
    const mesh = new THREE.Mesh(geometry, material);

    geometry.computeBoundingBox();
    const center = geometry.boundingBox.getCenter(new THREE.Vector3());
    mesh.geometry.center(); // Center the model
    mesh.scale.set(0.5, 0.5, 0.5);
     const bbox = geometry.boundingBox;
  const size = new THREE.Vector3();
  bbox.getSize(size);
  const maxDim = Math.max(size.x, size.y, size.z);

  // Position mesh in front of the camera
  mesh.position.z = -maxDim * 0.6;

    scene.add(mesh);
    animate();

      controls.target.copy(mesh.position);  // Make controls orbit around the mesh
  controls.update();
});
// Handle window resize
window.addEventListener('resize', () => {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize(window.innerWidth, window.innerHeight);
});


    `;
   
    s.onerror = reject;
    document.head.appendChild(s);
  });


loadScript()