<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Joe Hsu</title>
    <description>The latest articles on DEV Community by Joe Hsu (@jhsu).</description>
    <link>https://dev.to/jhsu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F362438%2Ff3d3b758-14a7-42f3-a130-dc41f2faecee.png</url>
      <title>DEV Community: Joe Hsu</title>
      <link>https://dev.to/jhsu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jhsu"/>
    <language>en</language>
    <item>
      <title>Creating a HUD in react-three-fiber</title>
      <dc:creator>Joe Hsu</dc:creator>
      <pubDate>Thu, 30 Apr 2020 16:31:02 +0000</pubDate>
      <link>https://dev.to/jhsu/creating-a-hud-in-react-three-fiber-1953</link>
      <guid>https://dev.to/jhsu/creating-a-hud-in-react-three-fiber-1953</guid>
      <description>&lt;p&gt;&lt;em&gt;Follow along as I try building a virtual reality application using react-three-fiber. See &lt;a href="https://dev.to/jhsu/hello-webvr-with-react-three-fiber-4k45"&gt;my fist post on building the scene&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;After creating a basic scene with react-three-fiber, I wanted to add a HUD for the user.&lt;/p&gt;

&lt;h2&gt;
  
  
  Single Camera
&lt;/h2&gt;

&lt;p&gt;On my first several attempts, I tried to render multiple cameras, a &lt;code&gt;PerspectiveCamera&lt;/code&gt; for the player and an &lt;code&gt;OrthographicCamera&lt;/code&gt; for the HUD. After thinking about targeting a virtual-reality context, I realized I needed a &lt;a href="https://www.ibm.com/design/v1/language/experience/vrar/ux-patterns/spatial/"&gt;spatial UI&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;use the mouse to drag the camera and look around&lt;/em&gt;&lt;br&gt;
&lt;iframe src="https://codesandbox.io/embed/r3f-vr-hud-example-yi7xy"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Having the HUD follow the camera.
&lt;/h2&gt;

&lt;p&gt;Instead of sticking the HUD directly to the camera, I used &lt;code&gt;react-spring/three&lt;/code&gt; to animate the HUD's group with a spring to bounce to the camera orientation.&lt;/p&gt;

&lt;p&gt;Inside of the &lt;code&gt;useFrame&lt;/code&gt; hook, I grabbed the camera's position and orientation. Using the &lt;code&gt;set&lt;/code&gt; provided by &lt;code&gt;useSpring&lt;/code&gt;, I update the HUD's target position and rotation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next Steps
&lt;/h2&gt;

&lt;h3&gt;
  
  
  HUD positioning
&lt;/h3&gt;

&lt;p&gt;I need to figure out how to get the headset position and place the HUD based on that. Simply using the &lt;code&gt;PerspectiveCamera&lt;/code&gt; position doesn't seem to place it HUD based on the headset position, but maybe the camera's position is the play area?&lt;/p&gt;

&lt;h3&gt;
  
  
  HUD interaction
&lt;/h3&gt;

&lt;p&gt;Now that I have a HUD, I will need to to allow the player to interact with the HUD and figure out a layout.&lt;/p&gt;

</description>
      <category>threejs</category>
      <category>r3f</category>
      <category>vr</category>
    </item>
    <item>
      <title>Hello WebVR with react-three-fiber</title>
      <dc:creator>Joe Hsu</dc:creator>
      <pubDate>Thu, 23 Apr 2020 19:06:07 +0000</pubDate>
      <link>https://dev.to/jhsu/hello-webvr-with-react-three-fiber-4k45</link>
      <guid>https://dev.to/jhsu/hello-webvr-with-react-three-fiber-4k45</guid>
      <description>&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/r3f-hello-world-3qdm3"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;I have been experimenting with &lt;a href="https://github.com/react-spring/react-three-fiber"&gt;react-three-fiber&lt;/a&gt; and also trying to use it to build virtual reality scenes. Starting form somewhere simple, I decided to rebuild the &lt;a href="https://aframe.io/examples/showcase/helloworld/"&gt;"Hello WebVR"&lt;/a&gt; example found in &lt;a href="https://aframe.io/"&gt;A-Frame&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The first thing that caught me off guard was that after the scene is setup by react-three-fiber, it calls &lt;code&gt;camera.lookAt(0,0,0)&lt;/code&gt;, even when passing a rotation to &lt;code&gt;&amp;lt;Canvas camera={{ rotation: [x, y, z] }}  /&amp;gt;&lt;/code&gt;. I created a &lt;code&gt;Camera&lt;/code&gt; component that takes the camera from &lt;code&gt;useThree()&lt;/code&gt; and reset the camera rotation inside a &lt;code&gt;useEffect()&lt;/code&gt; react hook.&lt;/p&gt;

&lt;p&gt;I created some wrapper components for the basic shapes (Cylinder, Sphere, Box, Plane) and define a flat set of props that would be passed to &lt;code&gt;&amp;lt;mesh /&amp;gt;&lt;/code&gt; and its corresponding geometry component. This caused some interesting TypeScript issues and took me a bit to figure out how to take react-three-fiber's types and merge them with additional props.&lt;/p&gt;

&lt;p&gt;Using generics, I created a single type I could use for each of the shape components&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;MeshShape&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;P&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Overwrite&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;
  &lt;span class="nx"&gt;ReactThreeFiber&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Object3DNode&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;THREE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Mesh&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;THREE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Mesh&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;ConstructorParameters&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;P&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Box Props&lt;/span&gt;
&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;IBox&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;MeshShape&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;
  &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;THREE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;BoxBufferGeometry&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;ReactThreeFiber&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I ported some of the &lt;a href="https://github.com/aframevr/aframe/blob/master/docs/components/look-controls.md"&gt;look-controls&lt;/a&gt; component from A-Frame to get drag mouse look working, excluding touch events.&lt;/p&gt;

&lt;p&gt;My next exploration will be to create a HUD.&lt;/p&gt;

</description>
      <category>react</category>
      <category>reactthreefiber</category>
      <category>threejs</category>
    </item>
  </channel>
</rss>
