o3d 轨迹球贴码

使用轨迹球ARCBALL,你可以使用鼠标拖拉旋转物体!应用简单方便。

var g_root=pack.createobject('Transform');//场景的根

var g_dragging=false;

第一步:创建轨迹球对象,并初始化两个4X4矩阵

var g_aball = o3djs.arcball.create(g_client.width, g_client.height);

var g_lastRot = g_math.matrix4.identity();
var g_thisRot = g_math.matrix4.identity();

第二步:注册三个鼠标事件

o3djs.event.addEventListener(g_o3dElement, 'mousedown', startDragging);
o3djs.event.addEventListener(g_o3dElement, 'mousemove', drag);
o3djs.event.addEventListener(g_o3dElement, 'mouseup', stopDragging);

第三步:操作函数

function startDragging(e) {
g_lastRot = g_thisRot;

g_aball.click([e.x, e.y]);

g_dragging = true;
}

function drag(e) {
if (g_dragging) {
    var rotationQuat = g_aball.drag([e.x, e.y]);
    var rot_mat = g_quaternions.quaternionToRotation(rotationQuat);
    g_thisRot = g_math.matrix4.mul(g_lastRot, rot_mat);

    var m = g_root.localMatrix;
    g_math.matrix4.setUpper3x3(m, g_thisRot);
    g_root.localMatrix = m;//设置根的本地矩阵为m矩阵
}
}

function stopDragging(e) {
g_dragging = false;
}

 

你可能感兴趣的:(3D)