How do you create a web app using three.js but standalone/without dependencies? - three.js

I posted a similar thread:
How do you correctly include three.js and gltf.js?
Now that I've looked more into three.js and gltfloader.js, it seems there's a convoluted mix of packages and modules and imports and exports. My goal is to make an .html file, or web app, that will use three.js to put a 3D file over the user's face in the camera. I started off with Jeeliz's face filter repository, and modified it by putting the scripts (that it imported or had for) fully into the .html file. This worked for the cube, but then I realized I needed to use GLTFLoader.js to import a .glb file instead of the cube being over my face.
Now that I've looked into the repositories more, it's less clear how to completely include three.js and gltfloader.js into 1 html file. Every way I've tried (three.min.js and GLTFLoader.js copied into html file, three.js and GLTFLoader.js copied into html file, GLTFLoader.js copied into three.js which is also copied into the html file, and a few other ways) gives me Uncaught Error: GLTFLoader is not defined or Uncaught Error: THREE.GLTFLoader is not a constructor. I'm trying to use it in my main script with this
const loader = new GLTFLoader();
loader.load( '/your-glb-file2.glb', function ( gltf ) {
threeStuffs.faceObject.add( gltf.scene );
} );
but that first line is what is causing the error. Even when I copied the contents of GLTFLoader.js into the contents of three.js (which gave me ~37k lines of code), and copied that mega-script into my html file, it still gave me either of the 2 Uncaught Errors.
I Google'd how to use three.js with no dependencies, but it was going into package.json and npm stuff, even though I'm simply trying to copy three.js into the file itself. I couldn't find any guides on a "standalone" three.js web app, and I figured here is a good place to ask.
I know I can copy three.js into a script in an HTML file, but what I'm asking is how do I get GLTFLoader.js AND three.js into an HTML file and working so I can use
const loader = new GLTFLoader();
to load a glb file into the scene.
I've also tried
import { GLTFLoader } from '/GLTFLoader.js';
at the top of my "demo" script with GLTFLoader.js (from the correct current GitHub repository) in the same directory and it says "Cannot use import statement outside a module"
Thanks in advance!
EDIT:
Now I have tried a few more things, and it's like playing wackamole. At first I realized it's probably best to not combine revisions of three.js so now I've copied three.module.js into the folder and changed <script scr='../../libs/three/r112/three.js> to
<script type="importmap">
{
"imports": {
"THREE": "../../../libs/three/v112/three.js"
}
}
</script>
This got it to stop with some errors, but then it said the helper script is calling THREE and it's not defined. So I put
import * as THREE from 'path/to/three.js'
(of course with the correct path) and then it said the helper script is not defined in main.
I basically turned every script tag into a type="module" and imported three.js into each .js that needed it, and I keep running into errors, and it's becoming clear this is not the best way to set up three.js and gltfloader.js
Any help is appreciated.

Related

Issues adding texture to Reality Converter?

Anyone having issues with Reality Converter by Apple. Mainly, when I add an .obj file, it’s able to display the white object. However, when I go ahead and add a texture .png files into the Materials folder, nothing gets updated. I end up with a plain white 3d object (even after restarting/exporting).
The only way it works is if I upload a .gITF folder, where it will actually add in the textures/color.
Not sure if this is a glitch? Or if I’m doing something wrong?
In order to apply a texture to .obj file you need not only a texture file but also its inseparable companion .mtl file (Material Template Library) – a special material definitions file for .obj.

Does three.js load the .mtl file silently? Are they necessary at all for mapping textures?

I noticed the .mtl file that gets exported along with my .obj exports isn't something I'm required to directly load when using Three.js, but I was wondering if the library loads it silently if it can find it in the same directory that it pulls the .obj from. I noticed in a directory where I didnt include the .mtls, the textures didn't map to my models, and I'm trying to understand what importance they have to Three.js if any. Does three.js load the .mtl file silently? Are they necessary at all for mapping textures?
Does three.js load the .mtl file silently?
No. If you just use OBJLoader, any related MTL files are ignored and default materials are applied. You have to use MTLLoader like demonstrated in the following example to load any material definitions. As you can see in the source code, you essentially perform two request. You first load the MTL and then the OBJ file.
https://threejs.org/examples/webgl_loader_obj_mtl.html
Are they necessary at all for mapping textures?
No, texture coordinates are defined in the OBJ file. So you can load the OBJ and apply a texture manually after loading like shown here:
https://threejs.org/examples/webgl_loader_obj.html

Using library extensions with TypeScript + requirejs

I'm currently messing around with THREE.js and socket.io to make a little game - I'm wanting to use the OrbitControls extension for THREE.js (i've used it before in a non-TS project) for camera controls.
The problem is that the client can't seem to find it (it compiles fine):
Uncaught TypeError: undefined is not a function
at line:
this.cameraControls = new THREE.OrbitControls(this.camera, this.renderer.domElement);
I'm including in my TS file using:
///<reference path="../typings/threejs/three-orbitcontrols.d.ts"/>
import OrbitControls = require("three-orbitcontrols");
At the bottom of my three-orbitcontrols.d.ts file I have my usual export that I seem to need at the bottom of some typing files:
declare module 'three-orbitcontrols' {
export=THREE.OrbitControls;
}
But i'm not sure if this is the right format, I've tried a few things like directing requirejs to the right file in the config (since the filename is a bit different):
paths: {
"three-orbitcontrols": "three.orbitcontrols"
}
Interestingly, after compiling with Gulp (using AMD), three-orbitcontrols does not appear in the require at the top of the resulting .js file.
My question is: how do you properly include an 'extension' library for a library you already have with TS and requirejs? Is there some form of merge I need to do? Do i need to manually merge the d.ts and js files? (I hope not!)
Thanks!
how do you properly include an 'extension' library for a library you already have with TS and requirejs?
Since you are calling THREE. here : new THREE.OrbitControls. You need to put import OrbitControls = require("three-orbitcontrols"); on THREE so:
import OrbitControls = require("three-orbitcontrols");
THREE.OrbitControls = OrbitControls;
Note: There isn't an idiomatic way of doing this since there isn't an idiomatic way that JavaScript libraries do this. The solution here is specific to your use case and this extension.

Three.js ColladaLoader failed to load model: some elements seem to have no vertices

I tried to import models from the 3D Warehouse, but some models fail to load (error in ColladaLoader.js (line 2808)
Uncaught TypeError: Cannot read property 'input' of null). I experienced this error with a lot of different models from 3D warehouse.
I prepared a fiddle (elcsiga/rep1z1xt/4), it loads the model from a different domain via ajax (CORS violation), however it worked me in Chrome.
A fully working example (fails to load model):
http://projecthost.hu/webview/jsfiddle/metro.html
Exactly same code with the monster model from three.js repository (it works):
http://projecthost.hu/webview/jsfiddle/monster.html
The collada model is available here: projecthost.hu/webview/jsfiddle/metro.dae
First I tried to download it as a .kmz file, unzip and use the .dae file in it directly. Additionally, I tried to download the .skp file, opened it in Sketchup and exported it into .dae format, but got the same error in ColladaLoader.js.
Do these models contain an unsupported geometry, or is it a bug in Collada loader?
Thanks.
The Model don't have any vertices so the Loader cant create a geometry. (maybe I'm wrong)
You created the Model via Google SketchUp right?
https://github.com/mrdoob/three.js/wiki/Using-SketchUp-Models
find
var vertexData = sources[ this.vertices.input['POSITION'].source ].data;
in ColladaLoader.js, replace it with
var vertexData = [];
if (this.vertices && this.vertices.input['POSITION'].source) {
vertexData = sources[ this.vertices.input['POSITION'].source ].data;
}
this can fix my problem by avoiding empty vertices. It may lost some info of dae file, but it won't harm ColladaLoader.js's function.

Three.js dentmodifier

I have been trying to use dent modifier for 3D vehicle loaded using objmtl loader. The model is loaded using multiple obj and mtl files. While I am trying to create dent it is not crating the dent on selected portion. Also when I call computeFaceNormals the model itself is getting distorted. I am not sure if the issue is in my obj files itself. I have attached all obj files as well as the html that I am trying. Can someone please guide me where I am going wrong #mrdoob
issue can be found at http://tinyurl.com/oc4j76a

Resources