I'm currently working on a small project that involves creating an interactive 3D scene on the web. To achieve this, I utilized three.js to import objects designed in Blender. Initially, I attempted to export the objects as JSON files, but encountered issues with importing them onto the web. As a result, I resorted to exporting the objects as individual .obj files and then imported each one separately.
My current task is to enable users to move and rotate each object along the Y-axis only. Since my design is based on a house plan, I specifically need the ability to manipulate each room through rotation and movement. Any assistance in finding a source for implementing this feature would be greatly appreciated.
Below is the code after setting up the scene and importing the objects individually:
var obj1 = {
// Initialization
scene: null,
camera: null,
renderer: null,
container: null,
controls: null,
clock: null,
stats: null,
init: function() {
// create main scene
this.scene = new THREE.Scene();
this.scene.fog = new THREE.FogExp2(0xcce0ff, 0.0003);
var SCREEN_WIDTH = window.innerWidth,
SCREEN_HEIGHT = window.innerHeight;
// prepare camera
var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 1, FAR = 2000;
this.camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR);
this.scene.add(this.camera);
this.camera.position.set(0, 100, 300);
this.camera.lookAt(new THREE.Vector3(0,0,0));
// prepare renderer
this.renderer = new THREE.WebGLRenderer({ antialias:true });
this.renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
this.renderer.setClearColor(this.scene.fog.color);
this.renderer.shadowMapEnabled = true;
this.renderer.shadowMapSoft = true;
// prepare container
this.container = document.createElement('div');
document.body.appendChild(this.container);
this.container.appendChild(this.renderer.domElement);
// events
THREEx.WindowResize(this.renderer, this.camera);
// prepare controls (OrbitControls)
this.controls = new THREE.OrbitControls(this.camera, this.renderer.domElement);
this.controls.target = new THREE.Vector3(0, 0, 0);
this.controls.maxDistance = 2000;
// prepare clock
this.clock = new THREE.Clock();
// prepare stats
this.stats = new Stats();
this.stats.domElement.style.position = 'absolute';
this.stats.domElement.style.left = '50px';
this.stats.domElement.style.bottom = '50px';
this.stats.domElement.style.zIndex = 1;
this.container.appendChild( this.stats.domElement );
// add spot light
var spLight = new THREE.SpotLight(0xffffff, 1.75, 2000, Math.PI / 3);
spLight.castShadow = true;
spLight.position.set(-100, 300, -50);
this.scene.add(spLight);
// add simple ground
var ground = new THREE.Mesh( new THREE.PlaneGeometry(200, 200, 10, 10), new THREE.MeshLambertMaterial({color:0x999999}) );
ground.receiveShadow = true;
ground.position.set(0, 0, 0);
ground.rotation.x = -Math.PI / 2;
this.scene.add(ground);
// load a model
this.loadModel();
},
loadModel: function() {
// Loading models of different rooms
// Add multiple loader functions for each room here
}
};
// Animate the scene
function animate() {
requestAnimationFrame(animate);
render();
update();
}
// Update controls and stats
function update() {
obj1.controls.update(obj1.clock.getDelta());
obj1.stats.update();
}
// Render the scene
function render() {
if (obj1.renderer) {
obj1.renderer.render(obj1.scene, obj1.camera);
}
}
// Initialize lesson on page load
function initializeObj() {
obj1.init();
animate();
}
if (window.addEventListener)
window.addEventListener('load', initializeObj, false);
else if (window.attachEvent)
window.attachEvent('onload', initializeObj);
else
window.onload = initializeObj;