Forum

Obtain camera aligned Object-Bounding-Box in viewport coordinates

09 June 2016 13:14
Hi @ll,

exist there already some functionality in the Blend4Web-Engine for obtaining the camera-aligned Bounding-Box of an object in viewport/canvas coordinates?

The closest path to a solution I found would be to project each object-vertex-point to canvas
m_camera.project_point(camobj, point3D, destCanvas2D)

This would require to access object-vertice in world-space coordinates. How could I access them?

A more optimal approach would be to gain access to the already 'Model-, View-, Projection-Matrix' transformed vertex-data of an object. Is there a way to intercept / tap into, or an example or hint to the right corner in the Blend4Web-Engine, to recreate the transformation (or access the data)?

Perhaps there exists already a function or a similar (internal) feature, but I couldn't find it, yet. Please help.

Cheers, Daniel.

@ Emotional3D
10 June 2016 11:46
Hello!

Sorry for the delay!

A more optimal approach would be to gain access to the already 'Model-, View-, Projection-Matri
The closest path to a solution I found would be to project each object-vertex-point to canvas
m_camera.project_point(camobj, point3D, destCanvas2D)

This would require to access object-vertice in world-space coordinates. How could I access them?
This solution is the most accurate for your task.
To access vertex coordinates in world space you need:

1) Get an array of materials names for specified object via method m_material.get_materials_names(obj)

2) Use extract_vertex_array to get vertex coordinates in object space. This method requires "Dynamic Geometry" option to be enabled:

It takes 3 arguments: object name, material name and attribute name (a_position in your case) and returns Float32Array array. So method call shoud look like:
var vert_array = m_geometry.extract_vertex_array(obj, mat_name, 'a_position');

The method should be called for every material.

3) Then you should use m_tsr.transform_vec3 with object's world_tsr as parameter for every vertex:
...
m_transform.get_tsr(obj, world_tsr);
m_tsr.transform_vec3(vert_coord, world_tsr, vect_coord);


Blend4Web Team - developer
Twitter
LinkedIn
10 June 2016 11:53

Perhaps there exists already a function or a similar (internal) feature, but I couldn't find it, yet. Please help.
There is a m_transform.get_object_bounding_box method, but it returns world-spaced bounding box, so it's canvas projection won't be always accurate


A more optimal approach would be to gain access to the already 'Model-, View-, Projection-Matrix' transformed vertex-data of an object.
The projection matrix isn't accessible in our engine

[UPD]
The model matrix can be simply obtained via object's world_tsr:
...
m_transform.get_tsr(obj, world_tsr);
m_tsr.to_mat4(world_tsr, model_matrix);

The view matrix can be obtainde like this:
...
m_transform.get_tsr(cam, cam_tsr);

var trans = m_tsr.get_trans_view(cam_tsr);
var quat = m_tsr.get_quat_view(cam_tsr);
m_mat4.fromRotationTranslation(quat, trans, world_matrix);

m_mat4.rotateX(world_matrix , -Math.PI/2, world_matrix );
m_mat4.invert(world_matrix , view_matrix);

Blend4Web Team - developer
Twitter
LinkedIn
10 June 2016 13:25
Thank you Konstantin, for your detailed anwer.

Changing the Object-Rendering-Property to 'is Dynamic Geometry' will have an impact in Memory usage and performance, right?
(Bigger memory footprint / the object-mesh-data maintains in main-memory? Perhaps less fps, too?)
@ Emotional3D
10 June 2016 14:26
Changing the Object-Rendering-Property to 'is Dynamic Geometry' will have an impact in Memory usage and performance, right?
(Bigger memory footprint / the object-mesh-data maintains in main-memory? Perhaps less fps, too?)
Yes, dynamic objects (animated/have shape keys/have physics/have dynamic geometry etc) affect performance. They store more dynamic data and cause additional drawcalls.
Blend4Web Team - developer
Twitter
LinkedIn
 
Please register or log in to leave a reply.