<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by James O&#39;Reilly on Medium]]></title>
        <description><![CDATA[Stories by James O&#39;Reilly on Medium]]></description>
        <link>https://medium.com/@jamesoreilly_36928?source=rss-6d6c702ea32------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*2aZ5m4RG2-DVnYCEkj_oQg.jpeg</url>
            <title>Stories by James O&amp;#39;Reilly on Medium</title>
            <link>https://medium.com/@jamesoreilly_36928?source=rss-6d6c702ea32------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 02:25:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jamesoreilly_36928/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Getting Started with React Three Renderer]]></title>
            <link>https://medium.com/@jamesoreilly_36928/getting-started-with-react-three-renderer-3df3280c9fdf?source=rss-6d6c702ea32------2</link>
            <guid isPermaLink="false">https://medium.com/p/3df3280c9fdf</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[3d]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[James O'Reilly]]></dc:creator>
            <pubDate>Mon, 20 Nov 2017 00:42:30 GMT</pubDate>
            <atom:updated>2017-11-20T00:42:30.449Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Introduction</strong></h3><p>The goal in this post will be to render a simple 3d cube in the browser. To integrate our 3d crate with React we’ll be looking at a library built on top of three.js, React Three Renderer. I’ll give a brief overview here, but the focus will be on how to implement R3R in your JSX. For more detail check out the R3R and three.js docs.</p><p>Run these commands in your terminal and let’s dive in!</p><pre>npm install — save react@15.6.1 react-dom@15.6.1 three@0.86.0<br>npm install — save react-three-renderer</pre><h3><strong>The Basics of 3d</strong></h3><p>So you want to start rendering 3d objects into your project. I know from experience that this can look like an overwhelming task, but let’s break it down a little. All 3d rendering requires 3 main pieces.</p><p><strong>Scene</strong></p><p>Think of your scene as a container that will hold the rest of the information needed to build and view an object.</p><p><strong>Camera</strong></p><p>The camera is just as it sounds. Depending on what type of camera you choose or what attributes you assign, the view of your object will be changed. For this exercise we will be using a perspective camera.</p><p><strong>Object</strong></p><p>The object is your actual 3d picture. It exists inside the scene, and the camera will point at it.</p><h3><strong>Why React Three Renderer?</strong></h3><p>Before we look at anything from React Three Renderer, let’s look at an example from three.js…</p><pre>&lt;script&gt;<br>  var scene = new THREE.Scene();<br>  var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 );</pre><pre>  var renderer = new THREE.WebGLRenderer();<br>  renderer.setSize( window.innerWidth, window.innerHeight );</pre><pre>  document.body.appendChild( renderer.domElement );</pre><pre>  var geometry = new THREE.BoxGeometry( 1, 1, 1 );<br>  var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );<br>  var cube = new THREE.Mesh( geometry, material );</pre><pre>  scene.add( cube );</pre><pre>  camera.position.z = 5;<br>  var animate = function () {<br>  requestAnimationFrame( animate );</pre><pre>  cube.rotation.x += 0.1;<br>  cube.rotation.y += 0.1;</pre><pre>  renderer.render(scene, camera);</pre><pre>  };<br>  animate();</pre><pre>&lt;/script&gt;</pre><p>Check out the rest of this example <a href="https://threejs.org/docs/#manual/introduction/Creating-a-scene">here</a>. Understanding how three.js accomplishes this basic scene will be important to understanding how React Three Renderer is useful.</p><p>First, notice that everything is wrapped in a script tag. That’s right. Our three.js example would be written directly into your projects html file. This doesn’t get along well with React, which aims to render everything from components written in JSX.</p><p>Speaking of JSX, our example above is crowded and unorganized compared to well written React components.</p><p>React Three Renderer aims to solve this problem by turning the different aspects of three.js into native tags that you can plug into the JSX returned by a component. Take a look at the return in the snippet below for what our finished code will look like.</p><pre>import React from &#39;react&#39;<br>import React3 from &#39;react-three-renderer&#39;<br>import * as THREE from &#39;three&#39;<br>import ReactDOM from &#39;react-dom&#39;</pre><pre>export default class Shape extends React.Component {<br>  constructor(<em>props</em>) {<br>    super(props);<br>    this.cameraPosition = new THREE.Vector3(0, 0, 10);</pre><pre>    this.state = {<br>      cubeRotation: new THREE.Euler()<br>    }</pre><pre>    this._onAnimate = () =&gt; {<br>      this.setState({<br>        cubeRotation: new THREE.Euler(<br>        this.state.cubeRotation.x + 0.01,<br>        this.state.cubeRotation.y + 0.01,<br>        0)<br>      })<br>    }<br>  }</pre><pre>  render() {<br>    const width = window.innerWidth / 4; <em>// canvas width<br>    </em>const height = window.innerHeight / 4; <em>// canvas height<br>    </em>const type = this.props.type;<br>    <br>    return (<br>      &lt;React3<br>        mainCamera=&quot;camera&quot;<br>        width={width}<br>        height={height}<br>        clearColor={0x0c2340}<br>        alpha={true}<br>        clearAlpha={0}<br>        onAnimate={this._onAnimate}<br>      &gt;<br>        &lt;scene&gt;<br>          &lt;perspectiveCamera<br>            name=&quot;camera&quot;<br>            fov={75}<br>            aspect={width / height}<br>            near={0.1}<br>            far={1000}<br>            position={this.cameraPosition}<br>          /&gt;</pre><pre>          &lt;mesh rotation={this.state.cubeRotation}&gt;<br>            &lt;boxGeometry width={3} height={3} depth={3} /<br>            &lt;meshLambertMaterial color={0xF3FFE2} /&gt;<br>          &lt;/mesh&gt;</pre><pre>          &lt;ambientLight intensity={.6} /&gt;<br>          &lt;pointLight <br>            color={0xFFFFFF}<br>            distance={10000} <br>            position={new THREE.Vector3(3, 3, 3)} <br>          /&gt;<br>        &lt;/scene&gt;<br>      &lt;/React3&gt;<br>    )}<br>}</pre><p>Remember when I said to think of your scene as a container? In R3R that’s exactly how you’ll treat it. Everything you add to a scene is simply wrapped by the &lt;scene&gt; tag. Much cleaner than defining variables to add and then adding with a function!</p><p>With that said, the above code block is a lot to take in all at once. Let’s break this down piece by piece.</p><h3><strong>Adding A Camera</strong></h3><p>It’s time to add our perspective camera into the mix. Instead of the constructor function taking arguments we saw in the basic three.js example, R3R again uses a native tag with defined attributes. Note that the name tag must match the “mainCamera” attribute assigned to the React3 component.</p><pre>&lt;scene&gt;<br>  &lt;perspectiveCamera<br>    name=&quot;camera&quot;<br>    fov={75}<br>    aspect={width / height}<br>    near={0.1}<br>    far={1000}<br>    position={this.cameraPosition}<br>  /&gt;<br>&lt;/scene&gt;</pre><h3><strong>Adding Mesh</strong></h3><p>Mesh is where we will build our object. For our cube we’ll have two tags nested inside of the mesh tag provided by R3R, geometry and material.</p><p>Geometry will define the shape and size of the object.</p><p>Material is what our object will be made up of. Different materials will appear to have different textures or respond to light differently.</p><p>There is a lot to choose from for both categories so definitely take a look at the three.js docs for deeper explanations.</p><pre>&lt;mesh&gt;</pre><pre>  //Creates a box with equal length, width, and height.<br>  &lt;boxGeometry <br>    width={3}<br>    height={3}<br>    depth={3} <br>  /&gt;</pre><pre>  //We&#39;ll use mesh lambert material because it will work <br>  //well with the light we add later<br>  &lt;meshLambertMaterial color={0xF3FFE2} /&gt;</pre><pre>&lt;/mesh&gt;</pre><h3><strong>Rotation</strong></h3><p>So we have our camera, scene, and our object defined. However, if you run this now our cube still looks like a flat 2d object. Just to prove that we’re actually building something 3d, we’ll add some rotation to our cube.</p><p>First, we need local state to our constructor…</p><pre>constructor(<em>props</em>) {<br>  super(props);<br>  this.cameraPosition = new THREE.Vector3(0, 0, 10);<br>  this.state = {<br>    cubeRotation: new THREE.Euler(),<br>  }<br>}</pre><p>Just like with anything kept on state we initialize here with a place holder. The new Three.Euler() here holds no value. It simply gives an idea to what kind of value we can expect. So how do we want to set state for the cubeRotation key?</p><p>Take a look back at the opening tag for our React3 component, specifically the attribute onAnimate. It takes the function this._onAnimate.</p><pre>this._onAnimate = () =&gt; {<br>  this.setState({<br>    cubeRotation: new THREE.Euler(<br>    this.state.cubeRotation.x + 0.01,<br>    this.state.cubeRotation.y + 0.01,<br>    0)<br>  })<br>}</pre><p>THREE.Euler constantly rotates the angle of our cube by .01 radians.</p><h3>Lighting</h3><p>Finally, let’s add a little more depth the the object that we’re creating. Lights and shadows are a perfect way to make 3d renderings seem more realistic. In this example, I’ll be using two types of lighting.</p><p><strong>Ambient Light: </strong>This type of light permeates the entirety of your scene.</p><p><strong>Point Light: </strong>Just like the name implies, this light points only at a specific spot that you declare.</p><pre>  &lt;ambientLight intensity={.6} /&gt;</pre><pre>  &lt;pointLight <br>    color={0xFFFFFF} //Color can be customized to any Hex value!<br>    distance={10000} <br>    //position is how you declare where your camera points<br>    position={new THREE.Vector3(3, 3, 3)}<br>  /&gt;</pre><pre>&lt;/scene&gt;</pre><p>Again R3R turns our lights into JSX tags that are both easy to use and React friendly.</p><h3>Conclusion</h3><p>Whew, we went through a lot here. Let’s take a moment to sum everything up. three.js is a deep and powerful 3d rendering library, but doesn’t fit very well with React.js.</p><p>React Three Renderer does a very good job of fixing this problem by taking the large number of properties that exist in three.js and turning as many as possible into familiar and clean JSX tags.</p><p>One last note. R3R is still experimental and very much on the cutting edge of React libraries. Be sure to check for changes to the documentation or github repo if you decide to use it in your projects.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3df3280c9fdf" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>