Orthographic camera and selecting objects with raycast

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 09:48:07

问题


I am running into a bit of difficulty selecting objects with the orthographic camera using the raycaster. Though, I have no problem with it when I use a perspective camera. The only thing I am changing when switching between the two is the type camera.

I am able to select faces on the orthographic view, but it is only loosely related to where I am clicking on the screen. When I can click far away from the object and it will still come back as if it has hit the object near its center.

Any ideas on what I am missing here?

I am basing much of my code on this example, and am hoping to achieve a very similar result from my code. (this example I\'m referencing uses the perspective camera)

Any help is much appreciated

<html>
<head>
  <style>
    canvas {
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      position: fixed;
      background-color: #111115;
    }
  </style>
</head>
<body id=\'c\'>
  <script src=\"js/three.js\"></script>

  <script>

    var obj = [];
    var mouse ={};
    var zoom = 2;

    var scene = new THREE.Scene();

    //switch between these two and see the difference:
    //var camera =  new THREE.OrthographicCamera(window.innerWidth / -zoom, window.innerWidth / zoom, window.innerHeight / zoom, window.innerHeight / -zoom, -1000, 1000);
    var camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );

    camera.position = new THREE.Vector3(100,100,100);
    camera.lookAt(new THREE.Vector3(0,0,0));

    // this material causes a mesh to use colors assigned to faces
    var material = new THREE.MeshBasicMaterial( 
    { color: 0xffffff, vertexColors: THREE.FaceColors } );

    var sphereGeometry = new THREE.SphereGeometry( 80, 32, 16 );
    for ( var i = 0; i < sphereGeometry.faces.length; i++ ) 
    {
      face = sphereGeometry.faces[ i ]; 
      face.color.setRGB( 0, 0, 0.8 * Math.random() + 0.2 );     
    }
    obj[\'box\'] = {};
    obj[\'box\'] = new THREE.Mesh( sphereGeometry, material );
    obj[\'box\'].castShadow = true;
    obj[\'box\'].receiveShadow = true;
    scene.add(obj[\'box\']);

    var ambientLight = new THREE.AmbientLight(0xbbbbbb);
    scene.add(ambientLight);

    var directionalLight = new THREE.DirectionalLight(0xffffff, 1);
    directionalLight.position.set(-100, 40, 100);
    directionalLight.castShadow = true;
    directionalLight.shadowOnly = true;
    directionalLight.shadowDarkness = .5;
    scene.add(directionalLight); 

    var renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.shadowMapEnabled = true;
    renderer.shadowMapSoft = true;
    document.body.appendChild(renderer.domElement);

    projector = new THREE.Projector();
    document.addEventListener( \'mousedown\', onDocumentMouseDown, false );
    function onDocumentMouseDown( event ) {
      // the following line would stop any other event handler from firing
      // (such as the mouse\'s TrackballControls)
      // event.preventDefault();

      console.log(\"Click.\");

      // update the mouse variable
      mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
      mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;

      // find intersections

      // create a Ray with origin at the mouse position
      //   and direction into the scene (camera direction)
      var vector = new THREE.Vector3( mouse.x, mouse.y, 1 );
      projector.unprojectVector( vector, camera );
      var ray = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );

      // create an array containing all objects in the scene with which the ray intersects
      var intersects = ray.intersectObjects( [obj[\'box\']] );

      // if there is one (or more) intersections
      if ( intersects.length > 0 )
      {
        console.log(\"Hit @ \" + toString( intersects[0].point ) );
        console.log(intersects);
        // change the color of the closest face.
        intersects[ 0 ].face.color.setRGB( 0.8 * Math.random() + 0.2, 0, 0 ); 
        intersects[ 0 ].object.geometry.colorsNeedUpdate = true;
      }
    }

    function toString(v) { return \"[ \" + v.x + \", \" + v.y + \", \" + v.z + \" ]\"; }

    var render = function() {
      requestAnimationFrame(render);
      renderer.render(scene, camera);
    };

    console.log(camera);
    console.log(obj[\'box\'])
    render();

    </script>
</body>

I am hoping it is something simple that I just don\'t know about yet.

three.js r60


回答1:


Here is the pattern to use when raycasting with either an orthographic camera or perspective camera:

var raycaster = new THREE.Raycaster(); // create once
var mouse = new THREE.Vector2(); // create once

...

mouse.x = ( event.clientX / renderer.domElement.clientWidth ) * 2 - 1;
mouse.y = - ( event.clientY / renderer.domElement.clientHeight ) * 2 + 1;

raycaster.setFromCamera( mouse, camera );

var intersects = raycaster.intersectObjects( objects, recursiveFlag );

three.js r.84




回答2:


One more note that might save you some trouble. If you have a camera like this:

var camera = new THREE.OrthographicCamera(0, window.innerWidth, -window.innerHeight, 0, -100, 100);

Then during raycasting, be sure to move the ray origin.z to camera.far for it to hit anything in the entire visible range:

this.ray.origin.set(0, 0, 0);
this.camera.localToWorld(this.ray.origin);
this.raycaster.setFromCamera(this.mouseCoord, this.camera);
this.ray.origin.z = this.camera.far;


来源:https://stackoverflow.com/questions/18553209/orthographic-camera-and-selecting-objects-with-raycast

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!