1

So I have an object loaded through OBJLoader.js, and want it to be able to rotate 360 degrees horizontally around its center. I've been looking at both TrackballControls and Canvas Cube example, but I cannot seem to implement it together with each other without encountering several kinds of mistakes. Most of the times it returns faults about addEventListener. I've checked up that I have the right versions of both three.js and the extension. Hope you have any suggestions.

    <script type="text/javascript" src="three.min.js"></script>
    <script src="Detector.js"></script>
    <script src="stats.min.js"></script>

    <script src="trackballcontrols.js"></script>
    <script src="OBJLoader.js"></script>

    <script>

        if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

        var container, stats;

        var camera, controls, scene, renderer;

        var windowHalfX = window.innerWidth / 2;
        var windowHalfY = window.innerHeight / 2;

        init();
        animate();

        function init() {

            camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
            camera.position.z = 500;

            controls = new THREE.TrackballControls( camera );

            controls.rotateSpeed = 1.0;
            controls.zoomSpeed = 1.2;
            controls.panSpeed = 0.8;

            controls.noZoom = false;
            controls.noPan = false;

            controls.staticMoving = true;
            controls.dynamicDampingFactor = 0.3;

            controls.keys = [ 65, 83, 68 ];

            controls.addEventListener( render );

            // world

            scene = new THREE.Scene();
            scene.fog = new THREE.FogExp2( 0xcccccc, 0.002 );

            // scene

            scene = new THREE.Scene();

            var ambient = new THREE.AmbientLight( 0xFFFFFF );
            scene.add( ambient );

            // texture

            var texture = new THREE.Texture();

            var loader = new THREE.ImageLoader();
            loader.addEventListener( 'load', function ( event ) {

                texture.image = event.content;
                texture.needsUpdate = true;

            } );
            loader.load( 'test.png' );  

            // model

            var loader = new THREE.OBJLoader();
            loader.addEventListener( 'load', function ( event ) {

                var object = event.content;

                object.traverse( function ( child ) {

                    if ( child instanceof THREE.Mesh ) {

                        child.material.map = texture;
                        child.geometry.computeBoundingBox();

                    }

                } );
                scene.add( object );
                    var map = object.children[0].material.map;
                    map.anisotropy = renderer.getMaxAnisotropy();

            });
            loader.load( 'monkey.obj' );

            //

            renderer = new THREE.WebGLRenderer();
            renderer.setSize( window.innerWidth, window.innerHeight );
            container.appendChild( renderer.domElement );

            //

            window.addEventListener( 'resize', onWindowResize, false );

        }

        function onWindowResize() {

            camera.aspect = window.innerWidth / window.innerHeight;
            camera.updateProjectionMatrix();

            renderer.setSize( window.innerWidth, window.innerHeight );

            controls.handleResize();

            render();

        }

        function animate() {

            requestAnimationFrame( animate );
            controls.update();

        }

        function render() {

            renderer.render( scene, camera );
            stats.update();

        }


    </script>
fsma
  • 11
  • 2
  • Your question is not clear. You want to rotate an object using its rotation matrix or using the trackball controls. The two operations are not related in any way. – gaitat May 24 '13 at 06:46
  • Whichever way is the easiest. I just want to be able to rotate the object. If its easier using the camera than actually rotating the object, when so be it. But the two ways I've tried doesn't seem to like that i'm using OBJLoader and not a canvas element. – fsma May 24 '13 at 13:40
  • I think it will be easier just using the controls. Comment all your statements that start with "controls." and describe what is the problem or the error message. – gaitat May 24 '13 at 13:59
  • 1
    Got it to work. After some trial and error i've figured it out with the trackball controls. The problem seemed to be that the OBJloader wasn't loading properly and therefore disrupted everything. – fsma May 27 '13 at 12:19
  • @fsma Did you update your post to reflect the new answer? – drearypanoramic Oct 31 '15 at 02:40

0 Answers0