mesh_parent_common.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * central location for mesh_parent script, called by mesh_parent_JSON.html & mesh_parent_inline.html
  3. */
  4. var breadMan;
  5. var ground;
  6. var meshes;
  7. var delta = new BABYLON.Vector3(0, 0, -.1);
  8. var running = true;
  9. var parenting = true;
  10. definedFacingForward = true;
  11. function animate(scene){
  12. if (!breadMan) breadMan = scene.getMeshByID("Gus");
  13. if (!ground ) ground = scene.getMeshByID("Ground");
  14. if (!meshes ) meshes = scene.meshes;
  15. if (running){
  16. breadMan.rotation.y += 0.02;
  17. breadMan.rotation.z += 0.02;
  18. movePOV(0, 0, 0.1);
  19. }
  20. }
  21. /** "Borrowed" from Automaton
  22. * Perform relative position change from the the point of view of behind the front of the mesh.
  23. * This is performed taking into account the meshes current rotation, so you do not have to care.
  24. * @param {number} amountRight
  25. * @param {number} amountUp
  26. * @param {number} amountForward
  27. */
  28. function movePOV(amountRight, amountUp, amountForward) {
  29. var rotMatrix = new BABYLON.Matrix();
  30. var rotQuaternion = (breadMan.rotationQuaternion) ? breadMan.rotationQuaternion : BABYLON.Quaternion.RotationYawPitchRoll(breadMan.rotation.y, breadMan.rotation.x, breadMan.rotation.z);
  31. rotQuaternion.toRotationMatrix(rotMatrix);
  32. var translationDelta = BABYLON.Vector3.Zero();
  33. var defForwardMult = definedFacingForward ? -1 : 1;
  34. BABYLON.Vector3.TransformCoordinatesFromFloatsToRef(amountRight * defForwardMult, amountUp, amountForward * defForwardMult, rotMatrix, translationDelta);
  35. breadMan.position.addInPlace(translationDelta);
  36. };
  37. function pausePlay() {
  38. running = !running;
  39. }
  40. function orphanConceive() {
  41. console.log("in orphan ");
  42. parenting = !parenting;
  43. for (index = 0; index < meshes.length; index++) {
  44. var mesh = meshes[index];
  45. if (mesh === breadMan || mesh === ground) continue;
  46. mesh.parent = parenting ? breadMan : null;
  47. }
  48. }