Attempting to showcase an OBJ 3D model as a wireframe that can be interactively moved using OrbitControls in Three.js. Being new to three.js, I apologize if I'm overlooking something obvious.
Successfully displayed a wireframe cube with OrbitControls. Also managed to display the OBJ 3D model as a wireframe independently. The issue arises when combining both methods.
Two approaches were tried:
- Inserting the OBJLoader into the cube environment (most promising).
- Adding OrbitControls to the wireframe OBJ environment (unsuccessful).
Experimented with different script orders like moving three.min.js
, OrbitControls.js
, OBJLoader.js
, and model.js
around, but to no avail.
Below is the code snippet:
<head>
<style media="screen"> html, body { margin: 0; } #model_container { background-color: #333333; margin: 50px; } </style>
<title>OBJ Wireframe</title>
<script defer src="script/three/build/three.min.js"></script>
<script defer src="script/three/examples/jsm/controls/OrbitControls.js"></script>
<script defer src="script/three/examples/jsm/loaders/OBJLoader.js"></script>
<script defer src="script/model.js"></script>
</head>
<body>
<div id="model_container"></div>
</body>
const globalWidth = window.innerWidth - 100;
const globalHeight = window.innerHeight - 100;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, globalWidth / globalHeight, 0.1, 1000);
camera.position.x = 300;
camera.position.y = -6;
camera.position.z = 1;
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true
});
renderer.setClearColor( 0xffffff, 0);
renderer.setSize(globalWidth, globalHeight);
const canvas = document.getElementById("model_container");
canvas.appendChild( renderer.domElement );
const ambient = new THREE.AmbientLight(0xffffff);
scene.add(ambient);
const loader = new THREE.OBJLoader();
loader.load("assets/castle.obj", function(object) {
const geometry = object.children[0].geometry;
THREE.GeometryUtils.center(geometry);
const material = new THREE.MeshLambertMaterial({});
const mesh = new THREE.Mesh(geometry, material);
mesh.material.wireframe = true;
scene.add(mesh);
})
const controls = new THREE.OrbitControls(camera);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
controls.rotateSpeed = 0.1;
controls.target.set(0, 0, 0);
var animate = function () {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
};
animate();
Struggling with these errors even after extensive research:
OrbitControls.js:9
:Uncaught SyntaxError: Unexpected token {
OBJLoader.js:5
:Uncaught SyntaxError: Unexpected token {
Uncaught TypeError: THREE.OBJLoader is not a constructor at model.js:23
Errors seem to originate from the THREE.js files themselves, making it challenging to resolve. Exhausted all resources and documentation available.
Hosting this on MAMP, but also tested on a server to rule out local file issues.