matches the object rotation to camera rotation
13 January 2017 12:05
13 January 2017 20:58
s there a way to copy the rotation of camera to an object?Hello!
if is possible, can copy only the z axis?
There are several ways to do this via API. One of them:
...
var cam = m_scenes.get_active_camera();
var cam_rot = m_transform.get_rotation(cam);
m_util.cam_quat_to_mesh_quat(cam_rot, cam_rot); // camera coordinate system differs from object in blender
var cam_dir = new Float32Array(3);
m_util.quat_to_dir(cam_rot, m_util.AXIS_MY, cam_dir);
//--
var cube = m_scenes.get_object_by_name("Cube");
var cube_rot = m_transform.get_rotation(cube);
var cube_dir = new Float32Array(3);
m_util.quat_to_dir(cube_rot, m_util.AXIS_MY, cube_dir);
// to copy full rotation just m_transform.set_rotation_v(cube, cam_rot);
//--
var cube_dir_new = new Float32Array(3);
var r1 = Math.max(cube_dir[0]*cube_dir[0] + cube_dir[1]*cube_dir[1], 0.0000001);
var r2 = Math.max(cam_dir[0]*cam_dir[0] + cam_dir[1]*cam_dir[1], 0.0000001);
var k = Math.sqrt(r1 / r2);
cube_dir_new[0] = cam_dir[0] * k;
cube_dir_new[1] = cam_dir[1] * k;
cube_dir_new[2] = cube_dir[2];
//--
var new_quat = new Float32Array(4);
m_quat.rotationTo(cube_dir, cube_dir_new, new_quat);
m_quat.multiply(new_quat, cube_rot, new_quat);
m_transform.set_rotation_v(cube, new_quat);
You can also play around cylindrical billboards settings if you need these effect constantly (https://www.blend4web.com/doc/en/objects.html?highlight=billboards#billboard-panel)