Forum

Pick up real Coordinates of an object's spot

09 February 2017 22:18
Hello all,

I was wondering if there is a way i can pick the real coordinate of a spot after a click on the canvas.
For example: In blender, wherever i take the 3d cursor i can have it's coordinates(x,y,z) on the right panel. Can i do the same in the browser after i click on the canvas? Any ideas?

Thanks!!
10 February 2017 11:22
Hi, you can use the calc_ray method from the camera.js module to do something like this. It uses canvas coordinates to cast a ray from this point in the 3d space. The main problem is that the 2-dimensional canvas coordinates don't give us full information about the point in 3d. You'll have a ray but you'll need to determine how far from the casting point you want the spot to be.

For example, if you want the spot to be 5 meters ahead of the current camera position:
var m_cam = require("camera");
var m_math = require("math");
var m_scenes = require("scenes");

var _ray_tmp = m_math.create_pline();
var _vec3_tmp = new Float32Array(3);

function get_3d_point(x, y) {
    var camera = m_scenes.get_active_camera();
    var ray = m_cam.calc_ray(camera, x, y, _ray_tmp);
    
    // calculate the point 5 meters ahead of the camera in the ray direction
    var point_3d = m_math.calc_pline_point(ray, 5, _vec3_tmp);
}


However, Blender seems to has a more complicated system than that. If there is an object under the cursor, when after the click the cursor "adheres" to this object at the intersection point between the "virtual" ray (cast from the mouse) and the object itself. There is a possibility to do that in blend4web via the special append_ray_test method, but it works only for objects with physics - this means that you should manually set physics settings for all that objects in Blender, which can be very inconvenient.
You can see an example which illustrates the "append_ray_test" method here: Ray Test.
Blend4Web Team
11 February 2017 20:33
This is exactly what i was looking for. You saved me from a lot of trouble. Thank tou very much!
16 February 2017 19:49
Hello again, as i followed your instructions i came up to the follow "problem". I'm attaching two images so i explain the difference:



In the above picture there is a cube added from blender. Using the ray Test example i'm adding some spots on the obj.




In the second picture i'm importing an obj in blender exported from mesh lab. After doing the same procedure with the first example the spots on this obj seems not to be attached to the obj but stay a distance away. Maybe z axis?


Any ideas why this is happening?
Thanks in advance
17 February 2017 10:37
Hi! I think this depends on how you set up the physics on this object. There are 2 variants: object physics and material physics.

The first one uses one of the simple bounding objects for optimization purposes: box, sphere, cylinder, … This type of physics can be set up on the Physics tab in Blender.

Material physics can use the whole high-poly mesh. This can be set on the Material tab in the "Special: Collision" section. You can find more info here: Static Physics Type.

I guess that you're currently using object physics and ray test in your example is working with the box. You should disable it on the Physics panel and enable the material physics.
Blend4Web Team
17 February 2017 20:35
Hello and thank you for your reply. First of all you mean enable material physics of my Obj and not the plane obj created for adding the spot. Is that right? Secondly what if my Obj does not have any materials?
18 February 2017 16:13
First of all you mean enable material physics of my Obj and not the plane obj created for adding the spot. Is that right?
yes, exactly

Secondly what if my Obj does not have any materials?
if so, you should create a material (a default simple gray material at least) and enable its physics settings
Blend4Web Team
19 February 2017 02:29
I've followed your instructions and the following error appears in the console:

Sorry for all those questions but i really need this to work and blend4web is my only hope.
20 February 2017 11:07
Hmm, this looks like a bug with the physics initialization. The Ray Test code snippet uses the same approach - material physics and the "append_ray_test_ext" API method. Does it work for you?

Also, can you provide us with a minimal example where this error can be reproduced?
Blend4Web Team
20 February 2017 15:46
Hello again,

Ray Test code works just fine with object physics on a cube i created inside blender. Then, after i imported my obj i followed your instructions and i enabled material physics but the error occurred.

Here is the link with my blender file, can you take a look and let me know if you find anything strange?

Thank you

Url: link
 
Please register or log in to leave a reply.