depthmap_navigation.xml 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <krpano>
  2. <!--
  3. krpano Depthmap Navigation Controls
  4. - Arrow-keys or WASD-keys navigation on desktop
  5. - On-Screen-Touchpad for mobile devices
  6. - VR controllers for WebVR usage
  7. -->
  8. <!-- remove the default keyboard controls -->
  9. <control keycodesleft="" keycodesright="" keycodesup="" keycodesdown="" />
  10. <!-- keyboard keycodes -->
  11. <keyb up="38" down="40" left="37" right="39" w="87" a="65" s="83" d="68" q="81" y="89" z="90" shift="16" ctrl="17" space="32" pageup="33" pagedown="34" />
  12. <!-- state variables for walking around in the depthmap pano -->
  13. <walkaround forward="0" backward="0" left="0" right="0" up="0" down="0" faster="0" />
  14. <!-- keyboard event handling -->
  15. <events name="depthmap_handlekey" keep="true" onkeydown="depthmap_handlekey(1);" onkeyup="depthmap_handlekey(0);" />
  16. <action name="depthmap_handlekey" args="keypressed" scope="local">
  17. if( keycode == keyb.up OR keycode == keyb.w, copy(walkaround.forward, keypressed);
  18. , keycode == keyb.down OR keycode == keyb.s, copy(walkaround.backward, keypressed);
  19. , keycode == keyb.left OR keycode == keyb.a, copy(walkaround.left, keypressed);
  20. , keycode == keyb.right OR keycode == keyb.d, copy(walkaround.right, keypressed);
  21. , keycode == keyb.shift, copy(walkaround.faster, keypressed);
  22. , keycode == keyb.ctrl, copy(walkaround.strafe, keypressed);
  23. , keycode == keyb.q, copy(walkaround.up, keypressed);
  24. , keycode == keyb.y OR keycode == keyb.z, copy(walkaround.down, keypressed);
  25. );
  26. </action>
  27. <!-- walking/flying controls -->
  28. <action name="depthmap_walkaroundmovement" autorun="onstart" type="Javascript"><![CDATA[
  29. var speed = 0.5;
  30. var friction = 0.9;
  31. var walkaround = krpano.get("walkaround");
  32. var vx=0, vy=0, vz=0;
  33. var rx=0;
  34. var touchpad_last_axis = [[0,0],[0,0]];
  35. var touchpad_move_speed = 5.0;
  36. var touchpad_rotate_speed = 1.5;
  37. var joystick_move_speed = 1.0;
  38. var joystick_rotate_speed = 1.0;
  39. // add a global 'depthmap_movemode' variable for setting the control mode: "walking" or "flying"
  40. krpano.depthmap_movemode = "walking";
  41. krpano.actions.renderloop( function()
  42. {
  43. vx *= friction;
  44. vy *= friction;
  45. vz *= friction;
  46. rx *= friction;
  47. if (vx*vx + vy*vy + vz*vz < 0.001)
  48. vx = vy = vz = 0;
  49. if (rx*rx < 0.01)
  50. rx = 0;
  51. var h = krpano.view.hlookat * Math.PI / 180.0;
  52. var v = krpano.view.vlookat * Math.PI / 180.0;
  53. // 2D direction vector (walking)
  54. var lx2 = Math.sin(h);
  55. var lz2 = Math.cos(h);
  56. // 3D direction vector (flying)
  57. var lx3 = krpano.view.dir.x;
  58. var ly3 = krpano.view.dir.y;
  59. var lz3 = krpano.view.dir.z;
  60. var wx = walkaround.right - walkaround.left;
  61. var wz = walkaround.forward - walkaround.backward;
  62. var wy = walkaround.up - walkaround.down;
  63. // handle the touchpad or joystick input from the vr-controllers
  64. var vrcontroller = (krpano.webvr && krpano.webvr.enabled) ? krpano.webvr.vrcontroller : null;
  65. if (vrcontroller)
  66. {
  67. var vrcontroller_count = vrcontroller.length;
  68. for (var i=0; i < vrcontroller_count; i++)
  69. {
  70. var controller = vrcontroller[i];
  71. var axes = controller.axes;
  72. if (axes)
  73. {
  74. // when having a depthmap: move around (1), otherwise only rotate the pano (0)
  75. var controlmode = (krpano.display.havedepthmap || krpano.display.depthbuffer) ? 1 : 0;
  76. // when having two controllers use the touchpad/joystick from the right one only for rotating
  77. if (vrcontroller_count == 2 && controller.hand == "right")
  78. controlmode = 0;
  79. // joystick or touchpad?
  80. var y_axis_scale = +1.3;
  81. var is_touchpad = false;
  82. if (controller.id == "Daydream Controller" || controller.id == "Oculus Go Controller")
  83. {
  84. is_touchpad = true;
  85. }
  86. else if(controller.id == "OpenVR Gamepad") // HTC Vive Controller
  87. {
  88. is_touchpad = true;
  89. y_axis_scale *= -1.0;
  90. }
  91. if (krpano.webvr.iswebxr)
  92. {
  93. // WebXR: axes[0,1] = touchpad, axes[2,3] = thumbstick
  94. if ( axes[0] != 0 || axes[1] != 0 )
  95. {
  96. is_touchpad = true;
  97. }
  98. else
  99. {
  100. // thumbstick - map axes for further processing
  101. axes = [axes[2],axes[3]];
  102. }
  103. }
  104. if (is_touchpad)
  105. {
  106. // special touchpad control (swiping like)
  107. if (axes[0] != 0 && axes[1] != 0)
  108. {
  109. var dx = +(axes[0] - touchpad_last_axis[i][0]);
  110. var dz = -(axes[1] - touchpad_last_axis[i][1]) * y_axis_scale;
  111. touchpad_last_axis[i][0] = axes[0];
  112. touchpad_last_axis[i][1] = axes[1];
  113. if (Math.abs(dx) > 0.3) dx = 0; // too fast changes are probably no swipes
  114. if (Math.abs(dz) > 0.3) dz = 0;
  115. if (controlmode == 0) // rotate
  116. {
  117. rx += touchpad_rotate_speed * dx;
  118. }
  119. else // move
  120. {
  121. vx += touchpad_move_speed * ( dx*lz2 + dz*lx2);
  122. vz += touchpad_move_speed * (-dx*lx2 + dz*lz2);
  123. }
  124. }
  125. }
  126. else
  127. {
  128. // joystick - direct control
  129. if (controlmode == 0) // rotate
  130. {
  131. if (Math.abs(axes[0]) > 0.2)
  132. {
  133. rx = joystick_rotate_speed * axes[0];
  134. }
  135. }
  136. else // move
  137. {
  138. // ignore too small values, some vr-controllers, e.g. Windows MR ones, are constantly reporting small wrong values
  139. if ( Math.abs(axes[0]) > 0.2 ) wx += joystick_move_speed * axes[0];
  140. if ( Math.abs(axes[1]) > 0.2 ) wz -= joystick_move_speed * axes[1];
  141. }
  142. }
  143. }
  144. }
  145. }
  146. var wl = Math.sqrt(wx*wx + wz*wz);
  147. if (wl > 0)
  148. {
  149. // normalize the moving speed
  150. wl = speed / wl;
  151. if (walkaround.faster > 0)
  152. wl *= 3;
  153. wx *= wl;
  154. wz *= wl;
  155. if (wx)
  156. {
  157. vx += wx*lz2;
  158. vz -= wx*lx2;
  159. }
  160. if (wz)
  161. {
  162. if (krpano.depthmap_movemode == "flying")
  163. {
  164. vx += wz*lx3;
  165. vz += wz*lz3;
  166. vy += wz*ly3;
  167. }
  168. else
  169. {
  170. vx += wz*lx2;
  171. vz += wz*lz2;
  172. }
  173. }
  174. }
  175. if (wy)
  176. {
  177. vy -= 0.5*speed*wy;
  178. }
  179. krpano.view.tx += vx;
  180. krpano.view.ty += vy;
  181. krpano.view.tz += vz;
  182. if (rx != 0)
  183. {
  184. krpano.webvr.hlookatoffset += rx;
  185. }
  186. });
  187. ]]></action>
  188. <!-- some buttons -->
  189. <style name="depthmap_button" type="text" css="text-align:center;" padding="4 8" mergedalpha="false" bgborder="0 0xFFFFFF 1" bgroundedge="1" bgshadow="0 1 4 0x000000 1.0" ondown="set(bgcolor, 0xDDDDDD);" onup="set(bgcolor, 0xFFFFFF);" />
  190. <style name="depthmap_info" type="text" css="color:#FFFFFF;text-align:center;" bg="false" txtshadow="0 1 4 0x000000 1.0" enabled="false" />
  191. <layer name="moveup" keep="true" style="depthmap_button" html="▲" align="rightbottom" x="20" y="50" ondown="set(walkaround.up,1);" onup="set(walkaround.up,0);" />
  192. <layer name="movedn" keep="true" style="depthmap_button" html="▼" align="rightbottom" x="20" y="20" ondown="set(walkaround.down,1);" onup="set(walkaround.down,0);" />
  193. <!-- info texts -->
  194. <layer name="depthmap_walkinfo" keep="true" style="depthmap_info" align="center" y="+25%" html="Walk around using the[br]Keyboard Arrow- or W,A,S,D-keys" devices="desktop" />
  195. <!-- drag area for touch devices -->
  196. <layer name="walkinfo_touch" keep="true" type="text" align="bottom"
  197. y="85"
  198. html="Hold down here[br]and drag around[br]for walking" bgalpha="0.3" devices="handheld"
  199. css="color:#FFFFFF;text-align:center;" txtshadow="0 1 4 0x000000 1.0"
  200. vcenter="true"
  201. width="140" height="140" bgroundedge="180"
  202. ondown="dragcontrol();"
  203. />
  204. <events name="walkinfo_touch" keep="true" devices="mobile"
  205. onresize="if(stagewidth GT stageheight,
  206. set(layer[walkinfo_touch], align=rightbottom, x=80, y=40);
  207. ,
  208. set(layer[walkinfo_touch], align=bottom, x=0, y=85);
  209. );
  210. "
  211. />
  212. <action name="dragcontrol" scope="local">
  213. copy(mx,mouse.x);
  214. copy(my,mouse.y);
  215. tween(caller.alpha,0);
  216. asyncloop(caller.pressed,
  217. calc(walkaround.forward, (mouse.y - my) * -0.25);
  218. calc(walkaround.left, (mouse.x - mx) * -0.25);
  219. copy(mx, mouse.x);
  220. copy(my, mouse.y);
  221. ,
  222. set(walkaround.left, 0);
  223. set(walkaround.forward, 0);
  224. tween(caller.alpha,1);
  225. );
  226. </action>
  227. </krpano>