Jump to content

Checking mesh visibility by camera


3Dlove
 Share

Recommended Posts

Hello guys,

 

I try to check mesh visibility by camera, if the mesh is hidden or not by other meshes.

 

Here is my code : I need to put on a mouseMove event ;)

 

The problem is that the text message 'Je te vois !!!' is showed even if the sphere is behind the plane :

var canvas = document.getElementById("renderCanvas");var engine = new BABYLON.Engine(canvas, true);var createScene = function () {			// This creates a basic Babylon Scene object (non-mesh)			var scene = new BABYLON.Scene(engine);			// This creates and positions a free camera (non-mesh)			var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 2, -10), scene);			camera.attachControl(canvas, true);			camera.applyGravity = true;			camera.checkCollisions = true;			camera.minZ = 0.1;			camera.keysUp.push('Z'.charCodeAt(0));			camera.keysDown.push('S'.charCodeAt(0));			camera.keysLeft.push('Q'.charCodeAt(0));			camera.keysRight.push('D'.charCodeAt(0));			// This creates a light, aiming 0,1,0 - to the sky (non-mesh)			var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);			light.intensity = 0.7;						// Our built-in 'ground' shape. Params: name, width, depth, subdivs, scene			var ground = BABYLON.Mesh.CreateGround("ground1", 200, 200, 2, scene);			ground.checkCollisions = true;			// Our built-in 'sphere' shape. Params: name, subdivs, size, scene			var sphere = BABYLON.Mesh.CreateSphere("sphere1", 16, 2, scene);			sphere.position.y = 1;						// Plane			var plane = BABYLON.Mesh.CreatePlane('plane', 8, scene);			plane.material = new BABYLON.StandardMaterial('mat', scene);			plane.material.backFaceCulling = false;			plane.material.emissiveColor = BABYLON.Color3.Red();			plane.material.alpha = 0.5;			plane.material.hasAlpha = true;			plane.checkCollisions = true;			plane.position = new BABYLON.Vector3(0, 4, -2);						return scene;		};                var scene = createScene();        engine.runRenderLoop(function () {            scene.render();        });        // Resize        window.addEventListener("resize", function () {            engine.resize();        });				document.getElementById('renderCanvas').addEventListener('mousemove', function (evt) {			var pickResult = scene.pick(evt.clientX, evt.clientY, function (mesh) {								if (  scene.isActiveMesh(mesh) && (mesh.name === 'sphere1') ) return true;				return false;			});                			if (pickResult.hit && pickResult.distance < 11) {				console.warn('Je te vois !!!');			} else {				console.info('Je ne te vois plus');			}		});
Link to comment
Share on other sites

Thank you a lot ! =D =D =D

 

Does exist a better solution to do that with less memory consumption ?

$('#canvas').on('mousemove', function (evt) {	var pickResult = scene.pick(scene.pointerX, scene.pointerY, null, false);			if (pickResult.hit && pickResult.distance < 11) {		var mesh = pickResult.pickedMesh;		var parentName = (mesh.parent !== undefined)?mesh.parent.name:false;		var isValidMesh = (scene.isActiveMesh(mesh) && (mesh.name.substring(0, 3) === 'tot' || mesh.name === 'titi' || parentName === 'tata'));				if (isValidMesh === true) {			console.info('Valid !');		} else {			console.warn('Not valid !');		}	}	else {		console.warn('Not valid !');	}	});
Link to comment
Share on other sites

If the meshes are static, you could optimize the process using octree - http://doc.babylonjs.com/tutorials/Optimizing_Your_Scene_with_Octrees

 

But I am also not too sure there is a way other than using ray-tracing for that. Maybe using a different event and not the mousemove event will be better. 

 

And, just off topic - the new doc site is AWESOME!!!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...