Use an .obj file like a normal BoxGeometry with Three JS - javascript

I would like to use an object file the same way I would use a BoxGeometry in three.js (like adding material and stuff), is there a way to do this?

Related

How to use three JS JSON models in A-frame

I want to use some low poly models from https://clara.io/ as a ThreeJS JSON model in A-frame. I want to use this because it is much more efficient and smaller than a 3D model, which will load much faster also. But there is no description of how to use it.
As I see there are some other people (1,2,3,4) who have trouble with that but no one suggested any good idea.
So do anybody has a good description or step-by-step guide, or something like that? I tried to use as a script, mentioned in the official description, but didn't worked:
<a-scene>
</a-scene>
<script type="text/javascript">
var loader = new THREE.ObjectLoader();
loader.load("globe.json",function ( obj ) {
document.getElementsByTagName('a-scene').add( obj );
});
</script>
Tried to use it with object loader, but I've got the dependency error module is not defined. So now I'm a bit confused about this,, how to use, or how to start using models in this way.
Any suggestion, any idea about this?
tldr: afaik JSON models are deprecated, and trying to make them work would mean creating a new loader from scratch.
I'm afraid the best solution is to:
download blender
download the model in a *.blend format
open it up in blender
export as glb
load it up as a gltf-model.
non - tldr:
Trying to load up a json model via the THREE.ObjectLoader, it tries to use JSONLegacyLoader.
To find it, you need to check out threejs R110 before it was completely removed (loader here).
After all, it needs the deprecated Geometry. When imported, something crashes in the rendering pipeline. Doesn't look like its worth the effort.
Tried with this model.

Download threejs model to .stl

I came to find this library that downloads stl from threejs.
https://github.com/mrdoob/three.js/blob/master/examples/js/exporters/STLExporter.js
I am new to js / threejs, so I was wondering how could I incorporate this within my script, or how to link it as a library or other file.
Please note that the model I want to download is either one built from scratch in threejs or one that I imported as glb/gltb and changed some of its features in threejs.
Thank you.
There is an official example that demonstrates the usage of STLExporter. I suggest you study the respective source code and use it as a foundation for your own app. You can find the mentioned example right here:
https://threejs.org/examples/misc_exporter_stl
Please note that the model I want to download is either one built from scratch in threejs or one that I imported as glb/gltb and changed some of its features in threejs.
Please keep in mind that STL has no concept of materials. It only saves the raw geometry data. STL is also unable to export hierarchical information of a scene (e.g. child-parent relationships). So depending on what you are going to change in your scene, it's likely that the respective data can't be exported as expected.
There is two implementation for export,ASCII & Binary,
function exportASCII() {
const result = exporter.parse( mesh );
saveString( result, 'box.stl' );
}
function exportBinary() {
const result = exporter.parse( mesh, { binary: true } );
saveArrayBuffer( result, 'box.stl' );
}

How to convert OBJ/FBX to GLTF before loading to scene (using Threejs)

I am trying to create to create a webapp in which users can upload obj/fbx files and view them in the browser. I have used the OBJLoader and FBXLoader which Threejs provides and it is all working fine. I however want to convert both these models to GTLF before loading them into the scene (for the sake of consistency).
How can I achieve this in threejs? I know that threejs has a GTLF exporter but I am unaware of how to leverage it for my purposes.
There are two ways you could do this:
Method 1:
Convert using from OBJ or FBX to glTF using native libraries on your server, like obj2gltf and fbx2gltf.
Load the glTF file on the web app.
Method 2:
Load the OBJ or FBX using OBJLoader or FBXLoader, giving you a THREE.Mesh or other object.
Convert that THREE.Mesh to a glTF file using GLTFExporter.
Load the glTF file using GLTFLoader, giving you a THREE.Mesh or other object again.
Method 1 is harder, but may give you better conversion results because the native libraries have access to the FBX SDK. Method 2 is fairly easy, but I can’t think of any benefit to doing that… the Mesh you have after the loading the OBJ or FBX will be (at best) the same as the Mesh you get at the end. At worst, the roundtrip through GLTFExporter->GLTFLoader may introduce performance or correctness problems.

Three.js : How to return the script of a 3D Object?

I'm playing with three.js and especially the Editor where you can attach Script on 3D object.
In Unity 3D you can access a script by using something like :
targetGameObject.GetComponent (scriptName).targetVariable;
How can you do this with three.js?
If I understand correctly, "Scripts" aren't a fundamental component of ThreeJS -- not in the same way as they are in Unity3D. i.e., The "scripts" in ThreeJS Editor would be better off named "scripting code" or something like that. When you publish your app, these script functions get exported to JSON as strings and get evaluated upon their associated events -- but no tangible "Script" object/component actually results from them.
In other words, the "Scripts" you're referring to in Unity3D would be something you would have to build out yourself or use an existing library.

Colada texture export for Three.js

I have a files in .FBX and I need to convert them in collada so I can use them in Three.js.
I managed to convert them with FBX Converter but then I lost textures.
How can I convert them so I can use textures.
Here is the FBX converted with FBX Converter:
pearl.dae
And here is the link of model which I exported as FBX_DAE in Maya 2013
model2.dae
I have just import the FBX and export as FBX_DAE
Model exported in Maya has texture if I do the quick preview on Mac but when I load it in Three.js it has no textures.
And the pearl.dae converted with FXB converter has no texture neather in quick previw nor in Three.js
Here is my loader code:
var Loader = new THREE.ColladaLoader();
Loader.options.convertUpAxis = true;
Loader.load('./models/pearl.dae', function(collada){
Bracelet = collada.scene;
Skin = collada.skins[0];
Bracelet.scale.x = Bracelet.scale.y = Bracelet.scale.z = 1;
Bracelet.updateMatrix();
init();
render();
});
Please help.
I have looked over your model files, pearl.dae contains no references to any textures. You might want to double-check your FBX converter settings and any errors or warning you may get. model2.dae on the other hand does reference a texture with the relative path of "../../Model/Nialaya_perla.fbm/Perla_diffuse.jpg" (the texture is not contained within the model file itself). I don't recall if Three.JS will automatically load textures from Collada model files, but unless that path is correct it will definitely fail. In my experience with Three.JS, it's usually better to load textures, create shaders, and apply them to plain models with just code. This will also give you the greatest control over the final look.

Categories