19

ページにターゲットdivが1つしかない場合(renderer.domElementを保持している場合)、Three.jsスクリプトは正常に実行されます。ターゲットdivの上に高さと幅が固定された別のdivを追加するとすぐに、ray.intersectObjectsはnullを返します。私が光線用に作成しているベクトルが問題を引き起こしているとは思えません。これがコードです。

var vector = new THREE.Vector3( ( event.clientX / divWidth ) * 2 - 1, -( event.clientY / divHeight ) * 2 + 1, 0.5 );

projector.unprojectVector( vector, camera );

var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );

var intersects = ray.intersectObjects( myObjects, true );

私がこれをどのように解決できるかについてのアイデア。

編集:今はTHREE.Raycaster (three.js r.56)

4

3 に答える 3

45

offset簡単な答えは、キャンバスのを考慮に入れる必要があるということです。

長い答えはあなたのコードがどのように書かれているかに依存するので、私はあなたに2つの答えを与えるでしょう、それは基本をカバーするべきです。

可能な組み合わせはたくさんあるので、実験する必要があるかもしれません。また、ブラウザが異なれば動作も異なる場合があります。

HTMLが次のようなものであると仮定します。

#canvas {
    width: 200px;
    height: 200px;
    margin: 100px;
    padding: 0px;
    position: static; /* fixed or static */
    top: 100px;
    left: 100px;
}

<body>
    <div id="canvas">
</body>

JSは次のようなものです。

var CANVAS_WIDTH = 200,
CANVAS_HEIGHT = 200;

var container = document.getElementById( 'canvas' );
document.body.appendChild( container );

renderer = new THREE.WebGLRenderer();
renderer.setSize( CANVAS_WIDTH, CANVAS_HEIGHT );
container.appendChild( renderer.domElement );

方法1次の方法が正しく機能するようにするには、キャンバスの位置をstaticに設定します。マージン>0およびパディング>0はOKです

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

方法2この代替方法では、キャンバスの位置を固定に設定します。上に設定>0、左に設定> 0; パディングは0でなければなりません。マージン>0はOKです

mouse.x = ( ( event.clientX - container.offsetLeft ) / container.clientWidth ) * 2 - 1;
mouse.y = - ( ( event.clientY - container.offsetTop ) / container.clientHeight ) * 2 + 1;

実験したい場合は、ここにフィドルがあります:http: //jsfiddle.net/cn7ecoaa/

編集:フィドルがthree.jsr.84に更新されました

于 2012-11-24T18:34:04.313 に答える
15

enent.clientXはクライアントウィンドウオフセットであるため、マウスの位置を計算するには、レンダラー要素のクライアントウィンドウオフセットも使用する必要があります。element.getBoundingClientRect()を使用して、ウィンドウの要素rectオフセットを取得します。

var rect = renderer.domElement.getBoundingClientRect();
mouse.x = ( ( event.clientX - rect.left ) / ( rect.width - rect.left ) ) * 2 - 1;
mouse.y = - ( ( event.clientY - rect.top ) / ( rect.bottom - rect.top) ) * 2 + 1;

<html>
<head>
<script src="http://threejs.org/build/three.min.js"></script>

    <link rel="stylesheet" href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" />


<style>
body {
    font-family: Monospace;
    background-color: #fff;
    margin: 0px;
    overflow: hidden;
}

#canvas {
    background-color: #000;
    width: 200px;
    height: 200px;
    border: 1px solid black;
    margin: 10px;
    padding: 0px;
    top: 10px;
    left: 100px;
}

.border {
    padding:10px; 
    margin:10px;
}

</style>
</head>
<body>
<div class="border">
	<div class="border">
		<div id="canvas"></div>
	</div>
</div>
<script>
// Three.js ray.intersects with offset canvas

var container, camera, scene, renderer, mesh,

    objects = [],
    
    count = 0,

    CANVAS_WIDTH = 200,
    CANVAS_HEIGHT = 200;

// info
info = document.createElement( 'div' );
info.style.position = 'absolute';
info.style.top = '30px';
info.style.width = '100%';
info.style.textAlign = 'center';
info.style.color = '#f00';
info.style.backgroundColor = 'transparent';
info.style.zIndex = '1';
info.style.fontFamily = 'Monospace';
info.innerHTML = 'INTERSECT Count: ' + count;
info.style.userSelect = "none";
info.style.webkitUserSelect = "none";
info.style.MozUserSelect = "none";
document.body.appendChild( info );

container = document.getElementById( 'canvas' );

renderer = new THREE.WebGLRenderer();
renderer.setSize( CANVAS_WIDTH, CANVAS_HEIGHT );
container.appendChild( renderer.domElement );

scene = new THREE.Scene();

camera = new THREE.PerspectiveCamera( 45, CANVAS_WIDTH / CANVAS_HEIGHT, 1, 1000 );
camera.position.y = 250;
camera.position.z = 500;
camera.lookAt( scene.position );
scene.add( camera );

scene.add( new THREE.AmbientLight( 0x222222 ) );

var light = new THREE.PointLight( 0xffffff, 1 );
camera.add( light );

mesh = new THREE.Mesh( 
	new THREE.BoxGeometry( 200, 200, 200, 1, 1, 1 ), 
	new THREE.MeshPhongMaterial( { color : 0x0080ff } 
) );
scene.add( mesh );
objects.push( mesh );

// find intersections
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();

// mouse listener
document.addEventListener( 'mousedown', function( event ) {
    
 var rect = renderer.domElement.getBoundingClientRect();
mouse.x = ( ( event.clientX - rect.left ) / ( rect.width - rect.left ) ) * 2 - 1;
mouse.y = - ( ( event.clientY - rect.top ) / ( rect.bottom - rect.top) ) * 2 + 1;
  
	raycaster.setFromCamera( mouse, camera );

    intersects = raycaster.intersectObjects( objects );

    if ( intersects.length > 0 ) {
        
        info.innerHTML = 'INTERSECT Count: ' + ++count;
        
    }

}, false );

function render() {

    mesh.rotation.y += 0.01;
    
    renderer.render( scene, camera );

}

(function animate() {

    requestAnimationFrame( animate );

    render();

})();

</script>
</body>
</html>

于 2016-10-18T10:40:18.507 に答える
4

WestLangley、説明ありがとうございます。いつものように本当に役に立ちました。

私の場合、チャートは絶対に配置されたdivにあるので、これを行う必要がありました。

    var offset = $('.rightBlock').offset();


    mouse.x = ( ( event.clientX - offset.left ) / renderer.domElement.width ) * 2 - 1;
    mouse.y = - ( ( event.clientY - offset.top ) / renderer.domElement.height ) * 2 + 1;

rightBlockが私のコンテナである場合、画面の70%しか使用しません。

あなたは私にインスピレーションを与え、この問題のある問題を解決するのを手伝ってくれました!どうもありがとう。

于 2015-02-25T11:04:13.030 に答える