DigiHz Data Posted November 19, 2016 Share Posted November 19, 2016 Hello everyone. I need the math/code for calculating the rotation from a light's direction. Example: I have a spotlight with a direction. I want to show a cone mesh at that light's position with the rotation that the light is facing. I not use parent for anything! I only need a code example of how to get the rotation values in x,y and z from the ligh'ts x,y,z direction. So in short.....When the light is changing direction, i get the rotation values back so that i can apply that to the cone mesh. Quote Link to comment Share on other sites More sharing options...
jerome Posted November 19, 2016 Share Posted November 19, 2016 http://doc.babylonjs.com/classes/2.4/Vector3#static-rotationfromaxis-axis1-axis2-axis3-rarr-vector3-classes-2-4-vector3- Quote Link to comment Share on other sites More sharing options...
DigiHz Data Posted November 19, 2016 Author Share Posted November 19, 2016 @jerome Can you give me a code example plz? Quote Link to comment Share on other sites More sharing options...
DigiHz Data Posted November 19, 2016 Author Share Posted November 19, 2016 I have this function i am working on: lightType is "spotLight" OR "pointLight". light is the actuall light itself. pseudoMeshLight is either a cone mesh or a spere mesh. function updatePseudoMeshLight(lightType,light,pseudoMeshLight){ pseudoMeshLight.position=light.position; /* HERE i want to get the direction of the light and transform it into a rotation so that the pseudo mesh is rotated pseudoMeshLight.rotation=??? */ } Quote Link to comment Share on other sites More sharing options...
adam Posted November 19, 2016 Share Posted November 19, 2016 I think you can do this: var quat = BABYLON.Quaternion.Identity(); light.getWorldMatrix().decompose(BABYLON.Vector3.Zero(), quat, BABYLON.Vector3.Zero()); mesh.rotationQuaternion = quat; Quote Link to comment Share on other sites More sharing options...
DigiHz Data Posted November 19, 2016 Author Share Posted November 19, 2016 @adam And if i not want to use Quaternion ? I am used to only use rotation. mesh.rotation NOT mesh. rotationQuaternion Quote Link to comment Share on other sites More sharing options...
adam Posted November 19, 2016 Share Posted November 19, 2016 quat.toEulerAnglesToRef(mesh.rotation); Quote Link to comment Share on other sites More sharing options...
DigiHz Data Posted November 19, 2016 Author Share Posted November 19, 2016 I give it a try @adam. Quote Link to comment Share on other sites More sharing options...
DigiHz Data Posted November 19, 2016 Author Share Posted November 19, 2016 Sorry @Adam, but i don't get this to work. I am working on a PG. Maybe you can test it there later so you see what i mean. I notify here when the PG is up. Any other one have a solution for me about this? Quote Link to comment Share on other sites More sharing options...
DigiHz Data Posted November 19, 2016 Author Share Posted November 19, 2016 ok. my PG: http://www.babylonjs-playground.com/#QMWL6 it is messy code right now, but you get the idea. Only put in code between row 46 to 49. Quote Link to comment Share on other sites More sharing options...
DigiHz Data Posted November 19, 2016 Author Share Posted November 19, 2016 Updated my PG: http://www.babylonjs-playground.com/#QMWL6#1 Still need the rotation to work! Quote Link to comment Share on other sites More sharing options...
DigiHz Data Posted November 20, 2016 Author Share Posted November 20, 2016 I been trying and trying to get tis to work, but i can't get it to work. Is it not mathematically possible to get a light's vector3 direction and turn it into a rotation value? Is it only mathematically possible to do the other way around? (a rotation value from a mesh to a light's vector3 direction?). Quote Link to comment Share on other sites More sharing options...
jerome Posted November 20, 2016 Share Posted November 20, 2016 assume that L1 is the vector direction of the light and X the X axis vector (1, 0, 0) C1 = cross(L1, Y) gives you a vector orthogonal to L1 and X C2 = cross(L1, C1) gives you a vector orthogonal to L1 and C1, so (L1, C1, C2) is now an orthogonal system. Let's call it the target system rotationFromAxis(L1, C1, C2) should return the rotation triplet to be applied to mesh to align it on this target system... If I'm wrong just swap C2 and C1 Quote Link to comment Share on other sites More sharing options...
DigiHz Data Posted November 20, 2016 Author Share Posted November 20, 2016 Hi @jerome Can you try to put the code in my PG? http://www.babylonjs-playground.com/#QMWL6#1 Put it in between line 46 and 49. Quote Link to comment Share on other sites More sharing options...
DigiHz Data Posted November 20, 2016 Author Share Posted November 20, 2016 If we all could solve this... Then people could do some more cool stuff with lights. Imagine for example a moving texture material in a pseudo mesh for the light Imagine a flashlight(having a alpha texture for the pseudo mesh) Imagine a pulsating point light with a pseudo mesh growing and shrinking Edit: well point lighs are no problem because they cast light in every direction. Quote Link to comment Share on other sites More sharing options...
DigiHz Data Posted November 20, 2016 Author Share Posted November 20, 2016 18 hours ago, adam said: I think you can do this: var quat = BABYLON.Quaternion.Identity(); light.getWorldMatrix().decompose(BABYLON.Vector3.Zero(), quat, BABYLON.Vector3.Zero()); mesh.rotationQuaternion = quat; This example that you gave me @adam returns 0,0,0 console.log(quat); console.log(mesh.rotationQuaternion); console.log(mesh.rotation); Quote Link to comment Share on other sites More sharing options...
adam Posted November 20, 2016 Share Posted November 20, 2016 using Jerome's answer: http://playground.babylonjs.com/#2DT16W#4 jerome 1 Quote Link to comment Share on other sites More sharing options...
DigiHz Data Posted November 20, 2016 Author Share Posted November 20, 2016 I try to strip that code down then, and see if it works. Edit: I still looking for code to put in between row 46 and 49 in my PG. Quote Link to comment Share on other sites More sharing options...
DigiHz Data Posted November 20, 2016 Author Share Posted November 20, 2016 @adam I stripped your PG down to this: http://playground.babylonjs.com/#2DT16W#5 But i don't see how to affect spot.direction.z Quote Link to comment Share on other sites More sharing options...
adam Posted November 20, 2016 Share Posted November 20, 2016 http://playground.babylonjs.com/#2DT16W#6 Quote Link to comment Share on other sites More sharing options...
DigiHz Data Posted November 20, 2016 Author Share Posted November 20, 2016 In my PG......the user sets the direction in x, y and z! Why don't anyone understand what i try to accomplish? i made my PG for getting help from someone to implement code between row 46 to 49 to make it work. I am not good in this kind of math/calculations. Quote Link to comment Share on other sites More sharing options...
adam Posted November 20, 2016 Share Posted November 20, 2016 6 minutes ago, DigiHz Data said: In my PG......the user sets the direction in x, y and z! Why don't anyone understand what i try to accomplish? i made my PG for getting help from someone to implement code between row 46 to 49 to make it work. I am not good in this kind of math/calculations. Look at lines 71 and 72. You need to have better forum etiquette. Quote Link to comment Share on other sites More sharing options...
DigiHz Data Posted November 20, 2016 Author Share Posted November 20, 2016 I did look at those lines. And i not mean to be rude or anything like that. I do appriciate any help from anyone. I just don't know how to make it work. Maybee it is not possible to get a light's x,y,z direction and get a rotation value back? Quote Link to comment Share on other sites More sharing options...
adam Posted November 20, 2016 Share Posted November 20, 2016 Did you try using the getRotation function that I wrote for you? Quote Link to comment Share on other sites More sharing options...
DigiHz Data Posted November 20, 2016 Author Share Posted November 20, 2016 yes i did. But when i use that function, the pseudo mesh jumps around when directions go over or under 0. 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.