Hi all,
I have two dataset in 3D. I used the CatmullRomCurve3 to visualize one data in three.js.
When I change to another data, the curve will disappear. I want to put it in the center of the window.
But they seems not shown in the scene. I know the camera may be set wrong position.
So I set the camera position as same as the mean position of these points.
But the curve still not appear.
The following are my main codes:
1.Calculate the mean position (mx , my, mz) for all points
2.Create the geometry
3. Set the geometry position as same to mean position:
mesh.position.set(mx , my, mz);
4. Set the light position
light.position.set( mx , my, mz*10 );
5. Calculate fov
fov = 2*Math.atan( (max2-my) / (max3-mz) ) * ( 180 / Math.PI );
6.Set the camera position
camera.position.set( mx , my, mz);
camera.lookAt(mx , my, mz);
7. Set the scene position
scene.position.set( mx , my, mz);
scene.lookAt(mx , my, mz);
Thanks very much.
##################################
var size = points1.length;
var mx=0; my=0; mz = 0;
for(var k=0; k < size ; k++){
mx += points1[i].x;
my += points1[i].y;
mz += points1[i].z;
Math.abs(points1[i].x) > max1 ? max1 = Math.abs(points1[i].x) : null;
Math.abs(points1[i].y) > max2 ? max2 = Math.abs(points1[i].y) : null;
Math.abs(points1[i].z) > max3 ? max3 = Math.abs(points1[i].z) : null;
}
mx = mx / size; my = my / size; mz = mz / size;
scene = new THREE.Scene();
curve = new THREE.CatmullRomCurve3( points1, false, "centripetal");
geometry = new THREE.TubeBufferGeometry( curve, size, 2, 4, closed );
geometry.name = "TubeGeometry";
geometry.elementsNeedUpdate = true;
geometry.colorsNeedUpdate = true;
// material
material = new THREE.MeshPhongMaterial( {
color: 0xFFFFFF,//0xf4f9f9,
vertexColors: THREE.VertexColors,
opacity: 1,
transparent: false,
side:THREE.DoubleSide,
wireframe: false
});
material.visible = true;
material.needsUpdate = true;
material.name = "material" + i;
// mesh
mesh = new THREE.Mesh( geometry, material );
mesh.name = "mesh" + i;
mesh.position.set(mx , my, mz);
// lights
var light = new THREE.DirectionalLight(0xbdc1c1);
light.position.set( mx , my, mz*10 );
light.name = "drlight" ;
scene.add( light );
light = new THREE.AmbientLight( 0x5b6060 );//0x222222 0xD6EAF8
light.name = "amblight" ;
scene.add( light );
fov = 2*Math.atan( (max2-my) / (max3-mz) ) * ( 180 / Math.PI );
var camera = new THREE.PerspectiveCamera(fov, 1, mz*0.01, mz*100);
camera.position.set( mx , my, mz);
camera.lookAt(mx , my, mz);
scene.add(camera);
scene.position.set( mx , my, mz);
scene.lookAt(mx , my, mz);