i need to use gltfloader in nodejs,i want to get glb model and deal with the glb to get some data and output this data in nodejs,i find create mesh is ok,but loader model cannot success ,i try something but not good,Anybody has a working solution?or
recommend me some example about threejs use gltfloader in nodejs
//glb model: t1.glb
//three r145 type:module
import * as THREE from 'three'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
const scene = new THREE.Scene();
var geometry = new THREE.BoxGeometry(100, 100, 100);
var material = new THREE.MeshLambertMaterial({
color: 0x0000ff
});
var mesh = new THREE.Mesh(geometry, material);
//success
scene.add(mesh);
//how to scene.add(glb model?)
undefined:1
glTF
^
SyntaxError: Unexpected token g in JSON at position 0
at JSON.parse (<anonymous>)
at GLTFLoader.parse (file:///D:/work/20221012/node_modules/three/examples/jsm/loaders/GLTFLoader.js:315:21)
at file:///D:/work/20221012/test.js:22:12
at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
at async loadESM (node:internal/process/esm_loader:88:5)
at async handleMainPromise (node:internal/modules/run_main:61:12)
it’s not easily possible. gltfloader relies on web xhr fetch requests and web workers to function. i’ve seen people try to emulate this under node but i haven’t seen something that actually works.
if with “some data” you mean structural data there could be a way. i have forked the loader here GitHub - pmndrs/gltfjsx: 🎮 Turns GLTFs into JSX components this is a node tool, it allows me to read out all structural data, the scene, materials, all props, positions, userData, etc, i have removed everything that had to do with texture and geometry buffers to make it work. the loader is under /src/bin
last but not least, if you want to actually display the results in some way i would suggest you use puppeteer, a headless browser environment for node.
I don’t know your use case, but if you do not need textures, edit the source to disable the texture loading stuff should do the trick.
Otherwise, and other use case maybe, try a different approach with https://gltf-transform.donmccurdy.com/
glTF Transform is a good choice if you want to read/edit/write glTF files in a Node.js environment. It has prebuilt functions for many complex operations (e.g. Draco compression, texture resizing) and a scripting API to do anything more custom. Works in Web, Node.js, and CLI.
But if you’re trying to load the glTF file into native three.js objects like THREE.GLTFLoader would create (THREE.Mesh, THREE.Group, etc.), … things are harder. three.js uses many web APIs, Node.js doesn’t have many of the APIs we need, and so there’s no there’s no simple way to get all three.js features working in Node.js. Getting your project to work will depend heavily on which three.js features you plan to use.
In my case, Even if the GLTF file has texture.
It is still successfully loaded into threejs objects but without textures.
This is sufficient for me to do some calculations stuff on the server.
Hey Guys,
I was also looking for a solution. The solution from @benzsuankularb was already helpful, but there were still problems with the exporter that I had to solve.
npm install three-stdlib and then import {GLTFLoader, GLTFExporter} from "three-stdlib"; instead of threejs/examples/jsm/...
Edit: sorry, after clean up I notice I still need the polyfill code above for the loader, but in combination with three-stdlib I got a working GLTFExporter and GLTFLoader in Node.js.