Visam Posted December 20, 2016 Share Posted December 20, 2016 Hi is there any way to add an eventlistener to a camera? I want to do smth when camera position is changed. Brg Mikhail Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted December 20, 2016 Share Posted December 20, 2016 when are you changing the position? is it in a callable function? just add a secondary function? Quote Link to comment Share on other sites More sharing options...
Visam Posted December 20, 2016 Author Share Posted December 20, 2016 5 minutes ago, Pryme8 said: when are you changing the position? is it in a callable function? just add a secondary function? of course I can add event listeners like this canvas.addEventListener("mousewheel", function (evt).... several event listeners for different events. but for me most important thing is to know when a camera position changed to do smth. it means, instead of adding many event listeners, add only one. Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted December 20, 2016 Share Posted December 20, 2016 well what is causing the cameras position to change? you should already know when then. Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted December 20, 2016 Share Posted December 20, 2016 You obviously have a function that changes the position of the camera... inside that function just call a separate function after the camera moves to see if the position has changed and if so do a response. Quote Link to comment Share on other sites More sharing options...
Visam Posted December 20, 2016 Author Share Posted December 20, 2016 3 minutes ago, Pryme8 said: You obviously have a function that changes the position of the camera... not always. for example, a camera position will be changed when a user uses a mouse wheel for ArcRotateCamera. or arrows for FreeCamera. Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted December 20, 2016 Share Posted December 20, 2016 post the script ill show you where the response is. and show you where to strap a callback. Quote Link to comment Share on other sites More sharing options...
Visam Posted December 20, 2016 Author Share Posted December 20, 2016 3 minutes ago, Pryme8 said: post the script ill show you where the response is. and show you where to strap a callback. lets take a look at the Babylon's example http://www.babylonjs-playground.com/# N3 Rotation and Scaling var createScene = function () { var scene = new BABYLON.Scene(engine); var camera = new BABYLON.ArcRotateCamera("Camera", Math.PI, Math.PI / 8, 150, BABYLON.Vector3.Zero(), scene); camera.attachControl(canvas, true); var light = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(0, 1, 0), scene); //Creation of 3 boxes and 2 spheres var box1 = BABYLON.Mesh.CreateBox("Box1", 6.0, scene); var box2 = BABYLON.Mesh.CreateBox("Box2", 6.0, scene); var box3 = BABYLON.Mesh.CreateBox("Box3", 6.0, scene); var box4 = BABYLON.Mesh.CreateBox("Box4", 6.0, scene); var box5 = BABYLON.Mesh.CreateBox("Box5", 6.0, scene); var box6 = BABYLON.Mesh.CreateBox("Box6", 6.0, scene); var box7 = BABYLON.Mesh.CreateBox("Box7", 6.0, scene); //Moving boxes on the x axis box1.position.x = -20; box2.position.x = -10; box3.position.x = 0; box4.position.x = 15; box5.position.x = 30; box6.position.x = 45; //Rotate box around the x axis box1.rotation.x = Math.PI / 6; //Rotate box around the y axis box2.rotation.y = Math.PI / 3; //Scaling on the x axis box4.scaling.x = 2; //Scaling on the y axis box5.scaling.y = 2; //Scaling on the z axis box6.scaling.z = 2; //Moving box7 relatively to box1 box7.parent = box1; box7.position.z = -10; return scene; } Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted December 20, 2016 Share Posted December 20, 2016 var inputManager = camera.inputs; console.log(inputManager); Will hold the secret i think. give me a min. Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted December 20, 2016 Share Posted December 20, 2016 Ok, I know there is a better way to do this, but because I have to get back to work here is a quick solutionhttp://www.babylonjs-playground.com/#S418W#0 Quote Link to comment Share on other sites More sharing options...
Visam Posted December 20, 2016 Author Share Posted December 20, 2016 4 minutes ago, Pryme8 said: Ok, I know there is a better way to do this, but because I have to get back to work here is a quick solutionhttp://www.babylonjs-playground.com/#S418W#0 I am using the same solution (registerBeforeRender) now. But to much calling of registerBeforeRender :-) Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted December 20, 2016 Share Posted December 20, 2016 That's why I added a buffer, it will only call at 30 aprox fps, you can drop that even lower. do like 1000/6 or something. Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted December 20, 2016 Share Posted December 20, 2016 @adam prolly knows how to strap a callback onto the camera input responses. Quote Link to comment Share on other sites More sharing options...
Visam Posted December 20, 2016 Author Share Posted December 20, 2016 2 minutes ago, Pryme8 said: That's why I added a buffer, it will only call at 30 aprox fps, you can drop that even lower. do like 1000/6 or something. thank you! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.