跳到正文
DEV Community· Himanshu Yeole·· 2 小時前AI 評分40

一張照片生成 .STL:可離線運行的 3D 製作工具

One Photo In, One .STL Out: A Lazy 3D Maker That Runs Offline

AI 導讀

這個靜態網站可在瀏覽器內把物件照片轉成實心 3D 模型,並匯出 .glb 或 .stl,無需帳戶或上傳照片。工具以 U²-Net 分離物件、以 Depth Anything V2 Small 估算深度,並補上模型背面。首次載入後可關閉 Wi-Fi 繼續使用;單張照片生成的背面是正面的鏡像。

正文

This is a submission for the Hacktoberfest Open-Source AI Challenge Week 3: Slow and Steady

What I Built

A website that starts with a film. Wukong stands on a cliff in the mist with his back to you, tiny beside four giant guardians. The film ends, and he doesn't leave. The video hands over to a real 3D model standing in the same spot at the same size, and you can drag him round to see his face.

Scroll down and the page does something lazier and more useful. Drop in a photo of an object and it hands back a solid 3D shape. You can spin it, download it as .glb for a game or website, or download it as .stl and print it. No account, no upload, no Blender.

The people who get to be lazier are anyone who has a toy, a figurine or a prop on their desk and needs a 3D version of it: makers, game-jam developers, teachers.

Demo

Live site: https://admirable-trifle-93524f.netlify.app/

To try it: let the intro play, drag Wukong, scroll to the tool, press "Try a sample" or drop your own photo, then download the .stl.

Code

Wukong relief site

Static site. No build step, no server code.

Run locally

python3 -m http.server 8000      # then open http://localhost:8000

Opening index.html from disk will not work (browsers block module and model loading over file://).

Deploy

  • Render: new Static Site from this folder (render.yaml included). Publish directory: .
  • Custom domain (.tech): add it in Render's Custom Domains, then set the DNS records Render shows.

Fully offline depth model (optional)

By default the depth model downloads from Hugging Face on first use and the browser caches it. To bundle it instead, download onnx-community/depth-anything-v2-small and place it at: models/onnx-community/depth-anything-v2-small/ (config.json, preprocessor_config.json, onnx/…) The page checks models/ first.

Structure

  • index.html: intro film, handoff to the 3D Wukong model, scroll sections
  • tool.js, tool-core.js: photo to solid 3D object (finds the object, mirrors the back, .glb and .stl export)
  • assets/: film, music, wukong.glb (3D model, 75 MB, used as supplied and not recompressed), sample…

How I Built It

The site is static files with no server code. Everything runs in your browser.

  1. Find the object. U²-Net (small, Apache-2.0) runs with onnxruntime-web and separates the object from its background. The 4.5 MB model ships with the site.
  2. Measure the depth. Depth Anything V2 Small, loaded through transformers.js, estimates how far each part sticks out toward you. If it can't load, the tool falls back to a quick depth estimate. [Confirm after you test AI depth on your machine, and edit this sentence if needed.]
  3. Close the shape. My own code builds the surface from the cutout and the depth, rounds the edges, mirrors the front onto the back and joins them at the outline. The page then checks that the shape has no open edges, so a slicer can print it.

Rendering and export use three.js, with an optional GrabCut outline-sharpening step from OpenCV.js.

Two things broke along the way. My first object finder compared each pixel with the photo's border colour. It worked on a fox on a plain background and failed on a clay Earth floating in a starfield, returning a bent shape with part of the nebula attached. I replaced it with U²-Net and tested it on that same image, plus a Moon and a swirling orange planet. And at one point the whole page froze because OpenCV's loader resolved to itself and my code waited on it forever. I found that by running the page in a real headless browser instead of guessing.

The intro uses a 75 MB model of Wukong that I generated with Tripo, a closed tool. I used it as one asset. The open pieces are what run the site and the tool.

Why Does Open Innovation Matter?

It works offline. After the first load, switch Wi-Fi off and the tool still runs. [Add a short clip or screenshot of the Wi-Fi-off test.]

Your photo never leaves the tab. A figurine on your desk or a family keepsake never touches a server I don't control.

I could fix it when it broke. Because the models are just files, I swapped the object finder for a better one in an afternoon and tested it on the exact image that failed. With a closed API, I would have been waiting for someone else to fix it.

It costs nothing per object. There is no per-generation fee and no usage limit. [Only compare with a paid tool if you checked its real price.]

The Lazy Payoff

[Write only what is true. For example: "I photographed a [object], dropped it in, and had a printable .stl in [X seconds]. I printed it and it came out [result]."]

Step Time on my machine
Page and Wukong model load [X s on Y connection]
Find the object [X s]
Depth for one photo [X s]
Build the solid [X ms]

What it can't do: one photo shows one side, so the back is a mirror of the front. That is a good guess for a ball or a bottle and only a guess for most other things, and from the side the surface texture looks stretched. The first AI depth run also downloads a model of [X MB] once.

Credits

Fan work inspired by Black Myth: Wukong by Game Science, not affiliated. The sample objects are renders of CC0 models from the Khronos glTF Sample Assets, and the Fox needs the credits listed in the README.

來源:DEV Community · dev.to