嵌入3D模型查看器

时间:2014-03-20 23:13:07

标签: javascript html three.js embed

我试图实现this 3D模型查看器,但是我想将其嵌入到已设置的div中,而不是像这样制作新的div。所以我已经编辑了这样的代码,但它还没有奏效。任何帮助将不胜感激。

<script>
    // This is where our model viewer code goes.
    var container;
    var camera, scene, renderer;
    var mouseX = 0, mouseY = 0;

    var windowHalfX = document.getElementById('viewer').clientHeight / 2;
    var windowHalfY = document.getElementById('viewer').clientHeight / 2;

    init();
    animate();

    // Initialize
    function init() {
      // This <div> will host the canvas for our scene.
      container = document.getElementById( 'viewer' );
      //document.body.appendChild( container );

      // You can adjust the cameras distance and set the FOV to something
      // different than 45°. The last two values set the clippling plane.
      camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
      camera.position.z = 100;

      // These variables set the camera behaviour and sensitivity.
      controls = new THREE.TrackballControls( camera );
      controls.rotateSpeed = 5.0;
      controls.zoomSpeed = 5;
      controls.panSpeed = 2;
      controls.noZoom = false;
      controls.noPan = false;
      controls.staticMoving = true;
      controls.dynamicDampingFactor = 0.3;

      // This is the scene we will add all objects to.
      scene = new THREE.Scene();

      // You can set the color of the ambient light to any value.
      // I have chose a completely white light because I want to paint
      // all the shading into my texture. You propably want something darker.
      var ambient = new THREE.AmbientLight( 0xffffff );
      scene.add( ambient );

      // Uncomment these lines to create a simple directional light.
      // var directionalLight = new THREE.DirectionalLight( 0xffeedd );
      // directionalLight.position.set( 0, 0, 1 ).normalize();
      // scene.add( directionalLight );

      // Texture Loading
      var manager = new THREE.LoadingManager();
      manager.onProgress = function ( item, loaded, total ) {
        console.log( item, loaded, total );
      };
      var texture = new THREE.Texture();
      var loader = new THREE.ImageLoader( manager );

      // You can set the texture properties in this function. 
      // The string has to be the path to your texture file.

      loader.load( 'img/sickletexture.png', function ( image ) {
        texture.image = image;
        texture.needsUpdate = true;
        // I wanted a nearest neighbour filtering for my low-poly character,
        // so that every pixel is crips and sharp. You can delete this lines
        // if have a larger texture and want a smooth linear filter.
        texture.magFilter = THREE.NearestFilter;
        texture.minFilter = THREE.NearestMipMapLinearFilter;
      } );

      // OBJ Loading
      var loader = new THREE.OBJLoader( manager );

      // As soon as the OBJ has been loaded this function looks for a mesh
      // inside the data and applies the texture to it.
      loader.load( 'obj/sickle.obj', function ( event ) {
        var object = event;
        object.traverse( function ( child ) {
          if ( child instanceof THREE.Mesh ) {
            child.material.map = texture;
          }
        } );

        // My initial model was too small, so I scaled it upwards.
        object.scale = new THREE.Vector3( 2, 2, 2 );

        // You can change the position of the object, so that it is not
        // centered in the view and leaves some space for overlay text.
        object.position.y -= 2.5;
        scene.add( object );
      });

      // We set the renderer to the size of the window and
      // append a canvas to our HTML page.
      renderer = new THREE.WebGLRenderer();
      renderer.setSize( document.getElementById('viewer').innerWidth, document.getElementById('viewer').innerHeight );
      container.appendChild( renderer.domElement );
    }

    // The Loop 
    function animate() {
      // This function calls itself on every frame. You can for example change
      // the objects rotation on every call to create a turntable animation.
      requestAnimationFrame( animate );

      // On every frame we need to calculate the new camera position
      // and have it look exactly at the center of our scene.
      controls.update();
      camera.lookAt(scene.position);
      renderer.render(scene, camera);
    }

  </script>

2 个答案:

答案 0 :(得分:2)

我找到了一个相当简单的解决方案,我很惊讶我之前没有找到它。

在单独的html文档中创建3D(使用原始脚本,而不是OP中已编辑的脚本),然后在div <embed src="3d.html"></embed>

中创建

答案 1 :(得分:2)

我正在尝试自己开始工作,这段代码适用于最新版本(66)的三个版本。它与你的例子有点不同,因为我使用的是vrml模型而不是obj而且我处理的材质不同。但它确实运行良好。

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>three.js webgl - loaders - vrml loader</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
        <style>
            threewindow {
                border: 1px solid black;
                }
        </style>

        <script src="../three.js/build/three.min.js"></script>
        <script src="../three.js/examples/js/controls/TrackballControls.js"></script>
        <script src="../three.js/examples/js/loaders/VRMLLoader.js"></script>
        <script src="../three.js/examples/js/Detector.js"></script>
        <script src="../three.js/examples/js/libs/stats.min.js"></script>

        <script>

            if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
            var container, stats;
            var camera, controls, scene, renderer;
            var cross;

            function init() {
                alert("init");
                camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.01, 1e10 );
                camera.position.z = 6;

                controls = new THREE.TrackballControls( camera );
                controls.rotateSpeed = 5.0;
                controls.zoomSpeed = 5;
                controls.panSpeed = 2;
                controls.noZoom = false;
                controls.noPan = false;
                controls.staticMoving = true;
                controls.dynamicDampingFactor = 0.3;

                scene = new THREE.Scene();
                scene.add( camera );

                var sphereMaterial =
                      new THREE.MeshLambertMaterial(
                        {
                          color: 0xCC0000
                        });

                // light

                var dirLight = new THREE.DirectionalLight( 0xffffff );
                dirLight.position.set( 200, 200, 1000 ).normalize();

                camera.add( dirLight );
                camera.add( dirLight.target );

                var loader = new THREE.VRMLLoader();
                loader.addEventListener( 'load', function ( event ) {
                    var object = event.content; 
                    object.traverse( function ( child ) {
                          if ( child instanceof THREE.Mesh ) {
                            //child.material.map = texture;
                            //child.material = sphereMaterial;
                            child.material.side = THREE.DoubleSide;
                          }
                       } );

                    scene.add(object);

                } );
//              loader.load( "models/vrml/house.wrl" );
                loader.load( "cayley.wrl" );

                // renderer

                renderer = new THREE.WebGLRenderer( { antialias: false } );
                renderer.setSize( window.innerWidth, window.innerHeight );

                renderer.setSize(200, 200);
                document.getElementById("threewindow").appendChild(renderer.domElement);
//              container = document.createElement( 'div' );
//              document.body.appendChild( container );
//              container.appendChild( renderer.domElement );

//              stats = new Stats();
//              stats.domElement.style.position = 'absolute';
//              stats.domElement.style.top = '0px';
//              container.appendChild( stats.domElement );

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

                animate();
            }

            function onWindowResize() {

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

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

                controls.handleResize();

            }

            function animate() {
                requestAnimationFrame( animate );
                controls.update();
                renderer.render( scene, camera );
                //stats.update();
            }

        </script>

    </head>

    <body onload="init()">

    <h1>Cubic surfaces</h1>
    <p>All the surfaces defined by cubics equations.</p>
    <ul><li><a href="parade/Cubics.php">Singularities of cubic surfaces</a>.</li>
    <li>A <a href="parade/index.html">pictorial introduction</a> to singularity theory.</li>
    </ul>
    <div id="threewindow"></div>


    </body>
</html>