2017-10-07 10 views
2

回転球面上の緯度と経度のためにクリック緯度する]をクリックし、経度変換マウスは、私が球を持っていると私は、マウスを変換する必要があり

すべては限り球が何回転を持たないなど正常に動作しますが、それは任意の回転を持っている場合 - 私は間違った結果

ここ

を得る私のコードです:私はEarthSphereに基づいて何とかpointOnSphereを操作する必要があることをgussingい

var mouse = new THREE.Vector2(); 
mouse.x = (event.pageX/window.innerWidth) * 2 - 1; 
mouse.y = -(event.pageY/window.innerHeight) * 2 + 1; 

var raycaster = new THREE.Raycaster(); 
raycaster.setFromCamera(mouse, camera); 
var intersects = raycaster.intersectObject(EarthSphere); 
if (intersects.length!=1) return; 

var pointOnSphere = new THREE.Vector3();  
pointOnSphere.x = intersects[0].point.x; 
pointOnSphere.y = intersects[0].point.y; 
pointOnSphere.z = intersects[0].point.z; 

var EarthRadius = 20; 
var lat = 90 - (Math.acos(pointOnSphere.y/EarthRadius)) * 180/Math.PI; 
var lon = ((90 + (Math.atan2(pointOnSphere.x , pointOnSphere.z)) * 180/Math.PI) % 360) - 180; 

、任意のアイデアは?

EDITED

earth.updateMatrixWorld(TRUE)。 pointOnSphere = earth.localToWorld(pointOnSphere.clone());

私は、次のようなものが働くだろうと思った:私は考えて

+0

ローテーション行列はありますか? – Rabbid76

+0

EarthSphereはメッシュなので、EarthSphere.matrixですか? – user1732451

答えて

2

​​

..

EarthSphere.updateMatrixWorld(true); 
pointOnSphere = EarthSphere.localToWorld(pointOnSphere.clone()); 

しかし、それは動作しません、あなたはむしろ.localToWorld()よりも.woldToLocal()を使用する必要があります。したがって、あなたの球のローカル座標でポイントを取得します。

documetation約.worldToLocal()言う:

はワールド空間からローカル空間へのベクトルを更新します。

これは、この関数に渡されたベクトルが更新されることを意味します。新しいTHREE.Vector3()の作成はありません。

これらの関数はどちらも、渡した既存のベクトルを更新するだけです。

var raycaster = new THREE.Raycaster(); 
var mouse = new THREE.Vector2(); 
var intersects; 
var pointOfIntersection = new THREE.Vector3(); 
var localPoint = new THREE.Vector3(); 
var spherical = new THREE.Spherical(); 
var lat, lon; 

window.addEventListener("mousedown", sphereClick, false); 

function sphereClick(event) { 
    mouse.x = (event.clientX/window.innerWidth) * 2 - 1; 
    mouse.y = -(event.clientY/window.innerHeight) * 2 + 1; 
    raycaster.setFromCamera(mouse, camera); 
    intersects = raycaster.intersectObjects([sphere]); 
    if (intersects.length == 0) return; 
    pointOfIntersection = intersects[0].point; 
    sphere.worldToLocal(localPoint.copy(pointOfIntersection)); // that's how it works with translation from the world coordinate system to the local one 
    createPoint(localPoint); 
} 

function createPoint(position) { 
    var point = new THREE.Mesh(new THREE.SphereGeometry(0.0625, 16, 12), new THREE.MeshBasicMaterial({ 
    color: 0x777777 + Math.random() * 0x777777 
    })); 
    point.position.copy(position); 
    sphere.add(point); 
    var color = point.material.color.getHexString(); 

    // three.js already has useful object to get polar coordinates from the given vector - 'position', which is in local coordinates already 
    spherical.setFromVector3(position); 
    lat = THREE.Math.radToDeg(Math.PI/2 - spherical.phi); 
    lon = THREE.Math.radToDeg(spherical.theta); 

    pointList.innerHTML += "<span style='color:#" + color + "'>lat: " + lat + "; lon: " + lon + "</span><br>"; 
} 

jsfiddle例r87。

+0

完璧!ありがとう – user1732451

関連する問題