<?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 Dominika Niziołek on Medium]]></title>
        <description><![CDATA[Stories by Dominika Niziołek on Medium]]></description>
        <link>https://medium.com/@dominikanizioek?source=rss-a2bc9662ad87------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*l-3bi1x0MPmD08BjBskoWg.jpeg</url>
            <title>Stories by Dominika Niziołek on Medium</title>
            <link>https://medium.com/@dominikanizioek?source=rss-a2bc9662ad87------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 18:08:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dominikanizioek/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[React Three Fiber — advanced concepts]]></title>
            <link>https://medium.com/@dominikanizioek/react-three-fiber-advanced-concepts-c31c5edb6816?source=rss-a2bc9662ad87------2</link>
            <guid isPermaLink="false">https://medium.com/p/c31c5edb6816</guid>
            <category><![CDATA[web-animation]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-three-fiber]]></category>
            <dc:creator><![CDATA[Dominika Niziołek]]></dc:creator>
            <pubDate>Fri, 21 Apr 2023 14:10:59 GMT</pubDate>
            <atom:updated>2023-04-21T16:28:43.832Z</atom:updated>
            <content:encoded><![CDATA[<h3>React Three Fiber — advanced concepts</h3><p>Once upon a time, I’ve prepared a small workshop about using Three.js in the React ecosystem, along with a helpful <a href="https://medium.com/@dominikanizioek/how-to-configure-and-build-3d-models-in-react-application-using-react-three-fiber-31bec3bd90c2">article</a>. I did enjoyed it and I was planning to write more tutorials on this topic; however, as usual, I didn’t. More than a year passed when a WIT chapter from my office asked for follow-up event and I felt that this is a nudge I needed to get back to this topic.</p><p>And I did! I hate public speaking since it makes me nauseous, but I did quite enjoyed planning the material, creating the demo and thinking through how to balance between important stuff and fun aspects. The effect was three-part workshop that ended with creating landing page for the Internship Program in less than 8 minutes.</p><p>And — of course — I felt that written tutorial would be helpful for folks that would like to learn those things in practice. So today, here comes part 1 of 3.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Fword-changes-1-z5s62h%3Ffile%3D%2Fsrc%2FBox.js&amp;display_name=CodeSandbox&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Fz5s62h&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2Fz5s62h%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f39202fddcce95aacc0731c3fa57d0d/href">https://medium.com/media/1f39202fddcce95aacc0731c3fa57d0d/href</a></iframe><p>This is where we’ll start — alongside with a little reminder of the basics of three.js:</p><pre>&lt;Canvas<br>  camera={{ position: [0, 3, 3], near: 0.025 }}<br>  style={{ backgroundColor: &#39;black&#39; }}&gt;<br>  &lt;OrbitControls /&gt;<br>  &lt;ambientLight intensity={0.5} /&gt;<br>  &lt;spotLight position={[10, 10, 10]} angle={0.15} penumbra={1} /&gt;<br>  &lt;pointLight position={[-10, -10, -10]} /&gt;<br> <br><br>  &lt;gridHelper /&gt;<br>  &lt;axesHelper args={[5]} /&gt;<br>&lt;/Canvas&gt;</pre><ol><li><strong>Canvas </strong>is a component that does some important setup work behind the scenes, so it sets up a scene and a camera and it renders our scene every frame, so you don’t need a traditional render loop. Also canvas is responsive to fit the parent node, so you can control how big it is by changing the parents width and height.</li><li><strong>Grid helper</strong> is a two-dimensional grid, and the <strong>axes helper</strong> is an object that helps visualise the 3 axes. The x axis is red, the y axis is green and the z axis is blue.</li><li><strong>Orbit controls</strong> allow the camera to orbit around a target.</li><li><strong>PointLight</strong> is a light that gets emitted from a single point in all directions.</li></ol><p>Also I’ve prepared beforehand a box component which we used here twice. <strong>BoxGeometry</strong> is a geometry class for a rectangular cuboid and <strong>MeshPhongMaterial</strong> is a material for shiny surfaces with specular highlights.</p><pre>&lt;mesh<br>  {...props}<br>  ref={ref}&gt;<br>  &lt;boxGeometry /&gt;<br>  &lt;meshPhongMaterial color={color} /&gt;<br>&lt;/mesh&gt;</pre><p>Now, with this setup we may add some listeners on the boxes. Firstly lets add some state management for hovering and rotating of the box in Box.js:</p><pre>const [hovered, setHover] = useState(false)<br>const [rotate, setRotate] = useState(false)</pre><p>To create rotation we’ll use <strong>useFrame() </strong>— so a specific for react-three-fiber hook. The code in useFrame execute before every rendered frame. When React calls it, it gives us a state object of the scene and delta, which is a clock that counts milliseconds since it was set recently.</p><pre>useFrame((_, delta) =&gt; {<br>  if (rotate) {<br>    ref.current.rotation.x += delta<br>    ref.current.rotation.y += 0.5 * delta<br>  }<br>})</pre><p>In the mesh, let’s ass onPointerDown event and use it to change rotate state. So after every click the box should start or stop rotating.</p><pre>&lt;mesh<br>  {...props}<br>  ref={ref}<br>  onPointerDown={() =&gt; setRotate(!rotate)}&gt;<br>&lt;/mesh&gt;<br></pre><p>To change the size of the box, we can set the scale on mesh based on <em>hovered</em> state and then add two additional event listeners — <em>onPointerOver </em>and <em>onPointerOut</em>.</p><pre>&lt;mesh<br>  {...props}<br>  ref={ref}<br>  scale={hovered ? [1.5, 1.5, 1.5] : [1, 1, 1]}<br>  onPointerDown={() =&gt; setRotate(!rotate)}<br>  onPointerOver={() =&gt; setHover(true)}<br>  onPointerOut={() =&gt; setHover(false)}&gt;<br>&lt;/mesh&gt;</pre><p>But — to be honest — this simple colors look boring af. To make boxes look a little more entertaining, let’s download <a href="https://ambientcg.com/view?id=Tiles074">two</a> <a href="https://ambientcg.com/view?id=Bricks059">different</a> textures and put them in the project public directory.</p><p>To load the textures we will use the TextureLoader from three.js in combination with useLoader that will allow us to pass the location of the texture and get the map back. And then we can pass them as a prop to Box components, so we could attach them to our geometries.</p><pre>import { TextureLoader } from &#39;three/src/loaders/TextureLoader&#39;<br><br>  const [bricks, tiles] = useLoader(TextureLoader, [<br>    &#39;./textures/bricks.jpg&#39;,<br>    &#39;./textures/tiles.jpg&#39;<br>  ])</pre><pre>&lt;meshPhongMaterial color={color} map={mapTexture} /&gt;</pre><p>Last feature we’ll ad to this scene is interactive color changer for our boxes. We’ll use for it <a href="https://github.com/pmndrs/leva">Leva</a> library, a Graphical User Interface component that you can add to your React Three Fiber application and interact with your scene.</p><pre>import { useControls } from ‘leva’</pre><p>To create options, we’ll use <strong>useMemo</strong> hook. We’ll need to set the default color and also attach controls to a specific Box.</p><pre>const options = useMemo(() =&gt; {<br>  return {<br>    color: { value: &#39;blue&#39; }<br>  }<br>}, [])<br><br>const pA = useControls(&#39;A&#39;, options)<br>const pB = useControls(&#39;B&#39;, options)<br><br><br><br>&lt;Box position={[-0.75, 0.5, 0]} name=&quot;A&quot; color={pA.color} /&gt;<br>&lt;Box position={[0.75, 0.5, 0]} name=&quot;B&quot; color={pB.color} /&gt;</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Fword-changes-1-z5s62h%3Ffile%3D%2Fsrc%2FBox.js&amp;display_name=CodeSandbox&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Fz5s62h&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2Fz5s62h%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f39202fddcce95aacc0731c3fa57d0d/href">https://medium.com/media/1f39202fddcce95aacc0731c3fa57d0d/href</a></iframe><p>And that’s it! Very easy to make, user-friendly interactive component with usage of most important and essential three.js components.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c31c5edb6816" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to configure and build 3D Models in React application using REACT-THREE-FIBER]]></title>
            <link>https://medium.com/@dominikanizioek/how-to-configure-and-build-3d-models-in-react-application-using-react-three-fiber-31bec3bd90c2?source=rss-a2bc9662ad87------2</link>
            <guid isPermaLink="false">https://medium.com/p/31bec3bd90c2</guid>
            <category><![CDATA[threejs]]></category>
            <category><![CDATA[react-three-fiber]]></category>
            <category><![CDATA[react-three-cannon]]></category>
            <category><![CDATA[react-three-drei]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Dominika Niziołek]]></dc:creator>
            <pubDate>Wed, 17 Nov 2021 00:54:40 GMT</pubDate>
            <atom:updated>2023-01-15T17:12:07.653Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>ThreeJS</strong> is an awesome tool that I’ve recently discovered — probably a little later than most of the developers that try to be up-to-date, but nevertheless I’ve fallen in love with it immediately. It has artsy vibe and gives you so many possibilities to build really creative and breathtaking projects. By coincidence, around that time when I was going through every possible tutorial online, I’ve been contacted by Woman in Technology branch in my company and asked to prepare some interesting workshop for employees, preferably in React. Immediately I’ve thought — <strong>react-three-fiber</strong>. I’ve decided to prepare something about the basics, for the newbies that knows and likes React, but maybe this kind of cool library would make them more crazy about this topic. This article is not only some kind of cheat sheet for the workshop participants, but hopefully useful tutorial for developers who don’t know <strong>react-three-fiber</strong> yet, but they are willing to give it a try.</p><p>Here’s a sneak peak what you’ll learn in this article:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2F2m4jw&amp;display_name=CodeSandbox&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2F2m4jw&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2F2m4jw%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/25735347e2edb46ca1676f6208dfe092/href">https://medium.com/media/25735347e2edb46ca1676f6208dfe092/href</a></iframe><p><a href="https://threejs.org/">ThreeJS</a> is cross-browser JavaScript library used to create and display animated 3d computer graphics in a web browser using WebGL. But who would want to write in VanillaJS? Obviously, we are all using only React for a couple of years. Fortunately, we don’t need to go to the basics and we can use <a href="https://docs.pmnd.rs/react-three-fiber/getting-started/introduction">react-three-fiber</a>, awesome react renderer for ThreeJS.</p><h4>1. Setup</h4><p>After <a href="https://create-react-app.dev/">creating react app</a>, let’s install some important dependencies:</p><pre>npm i three @react-three/fiber @react-three/drei @react-three/cannon</pre><p>We need three, obviously, and react-three-fiber. <a href="https://github.com/pmndrs/drei">Drei</a> and <a href="https://github.com/pmndrs/use-cannon">cannon</a> are some really amazing helpers, that would make our first experience with three even more fun.</p><h4>2. Plane</h4><p>Firstly, in our little empty world, let’s create a function called <em>Scene, </em>which will return <em>Canvas</em> component.</p><pre><br>const Scene = () =&gt; {<br>   return (<br>      &lt;Canvas&gt;<br>      &lt;/Canvas&gt;<br>   )<br>}</pre><pre>const App =() =&gt; {<br>   return (<br>      &lt;Scene/&gt;<br>   )<br>}</pre><p><em>Canvas</em> is the most important object in our code — it defines our react-three-fiber scene. And that means, that everything inside the Canvas — any component we’ll put in there — will not be rendered by react-dom anymore. Instead, it will be rendered by react-three-fiber and will be a component inside a 3d environment.</p><p>Now let’s put some objects inside our Canvas.</p><pre>const Scene = () =&gt; {<br>   return (<br>      &lt;Canvas&gt;<br>         &lt;mesh&gt;<br>            &lt;planeGeometry args={[100, 100]}/&gt;<br>         &lt;/mesh&gt;<br>      &lt;/Canvas&gt;<br>   )<br>}</pre><p><em>PlaneGeometry</em> comes from three.js, so no import is needed. The arguments it takes — two values in the array — are width and height of the plane.</p><p>For the future use — let’s extract that plane into separate function.</p><pre>const Plane = () =&gt; {<br>   return (<br>      &lt;mesh&gt;<br>         &lt;planeBufferGeometry args={[100, 100]}/&gt;<br>      &lt;/mesh&gt;<br>   )<br>}</pre><pre>const Scene = () =&gt; {<br>   return (<br>      &lt;Canvas&gt;<br>         &lt;mesh&gt;<br>            &lt;Plane/&gt;<br>         &lt;/mesh&gt;<br>      &lt;/Canvas&gt;<br>   )<br>}</pre><p>If we want to add some colour to it, we can add some <a href="https://threejs.org/docs/index.html?q=materia#api/en/constants/Materials">material</a> to it. Let’s go with<em> meshStandardMaterial.</em></p><pre>const Plane = () =&gt; {<br>   return (<br>      &lt;mesh&gt;<br>         &lt;planeBufferGeometry args={[100, 100]}/&gt;<br>         &lt;meshStandardMaterial color=&#39;lightpink&#39;/&gt;</pre><pre>      &lt;/mesh&gt;<br>   )<br>}</pre><p>But… is it black?</p><p>Well, of course it is. We don’t have any light!</p><pre>&lt;pointLight position={[10,10,10]}/&gt;</pre><p>And now it looks properly.</p><h4>3. Animations</h4><p>Let’s dive in into animations now. What we need to do, is build an animation loop by using <em>useFrame</em>.</p><p><em>useFrame</em> is a Fiber hook that lets you execute code on every single frame of Fiber’s render loop.</p><blockquote>Let’s remember: we can only call Fiber hooks inside a &lt;Canvas/&gt; parent!</blockquote><p>We’ll write an arrow function called, spoiler — RotatingBox.</p><pre>const RotatingBox = () =&gt; {<br>   useFrame(({clock}) =&gt; {<br>      const a = clock.getElapsedTime();<br>      rotatingBoxMesh.current.rotation.x = a;<br>   });<br>   const rotatingBoxMesh = React.useRef();<br>   return (<br>      &lt;mesh ref={rotatingBoxMesh}&gt;<br>         &lt;boxGeometry/&gt;<br>         &lt;meshPhongMaterial color=&#39;purple&#39;/&gt;<br>      &lt;/mesh&gt;<br>}</pre><p>Soooo… what happened here?</p><p>The callback we pass to useFrame will be executed every frame and it will be passing an object containing the state of our Fiber scene. We want to directly mutate our mesh each frame — so firstly, we are getting a reference for it, so we could use <a href="https://reactjs.org/docs/hooks-reference.html#useref"><em>useRef </em></a>React hook.</p><p>So r<em>otatingBoxMesh</em> variable now hold a reference to the actual three.js object, that we can mutate in useFrame.</p><p>Let’s take a closer look into this fragment of code:</p><pre>useFrame(({clock}) =&gt; {<br>      const a = clock.getElapsedTime();<br>      rotatingBoxMesh.current.rotation.x = a;<br>   });</pre><p>We are destructing clock from the argument passed to useFrame. Then we are accessing the rotation.x property of <em>rotatingBoxMesh.current</em> object (which is a reference to our mesh object). Next, we are assigning value <em>a</em> to the rotation on x axis, meaning our object will now infinitely rotate between -1 and 1 radians around x axis.</p><p>Easy.</p><h4>4. OrbitControls</h4><p>Adding <em>OrbitControls</em> is like making a camera — we can move screen and view each side of our 3d object. For this we’ll use another React hook, <a href="https://reactjs.org/docs/hooks-reference.html#useeffect"><em>useEffect</em></a>.</p><pre>const CameraController = () =&gt; {</pre><pre>   const { camera, gl } = useThree();</pre><pre>   useEffect(() =&gt; {</pre><pre>      const controls = new OrbitControls(camera, gl.domElement);</pre><pre>      controls.minDistance = 3;</pre><pre>      controls.maxDistance = 200;</pre><pre>      return () =&gt; {</pre><pre>         controls.dispose();</pre><pre>      };</pre><pre>   }, [camera, gl]);</pre><pre>   return null;</pre><pre>};</pre><p><em>useThree</em> gives you a camera object, which is used to move on screen, and <em>gl</em> indicates the area on which you are moving. In <em>useEffect</em>, we combined both the <em>OrbitControls</em> objects and then we added the <em>minDistance</em> and <em>maxDistance</em> parameters to restrict the movement on screen.</p><h4>5. Starry sky</h4><p>Let’s try some <em>drei</em>.</p><pre>&lt;Canvas&gt;<br>   ...<br>   &lt;Sky <em>distance</em>={450000} <em>sunPosition</em>={[-100, -100, 100]} <em>inclination</em>={0} <em>azimuth</em>={0.25}  /&gt;<br>   &lt;Stars <em>radius</em>={100} <em>depth</em>={50} <em>count</em>={5000} <em>factor</em>={4} <em>saturation</em>={0} <em>fade speed</em>={1} /&gt;<br>&lt;/Canvas&gt;</pre><h4>6. Physics</h4><p>Our small world is really pretty now, but it lacks the most entertaining thing — <em>Physics</em>. Let’s import it.</p><pre>import {Physics} from &quot;@react-three/cannon&quot;;</pre><p>So the components we want to have in our Physics environment should be wrapped in Physics tag. We’ll create another box — but this one will be answering to gravity.</p><pre>const FallingBox = () =&gt; {</pre><pre>   const [fallingBoxMesh, api] = useBox(() =&gt; ({mass: 1, position:    [2, 2, 0]}));</pre><pre>   return (</pre><pre>      &lt;mesh ref={fallingBoxMesh} position={[2, 2, 0]}</pre><pre>      onClick={() =&gt; {api.velocity.set(-5, -2, 0);}}&gt;</pre><pre>         &lt;boxGeometry /&gt;</pre><pre>         &lt;meshStandardMaterial color=&quot;blue&quot; /&gt;</pre><pre>      &lt;/mesh&gt;</pre><pre>   );</pre><pre>};</pre><p>Now we need to get a ref inside our <em>FallingBox</em> component to the Physics box representation. We are using imported method <em>useBox</em>, and in the callback we can add some properties — like the mass of the box. Now the gravity — and other objects inside physics world — can actually affect it!</p><p>But the box falls through the plane now — we need to add plane to our cannon hook.</p><pre>const Plane = () =&gt; {<br>   const [planeRef] = usePlane(() =&gt; ({rotation: [-Math.PI / 2, 0, 0]]}));<br>   return (<br>   &lt;mesh ref={planeRef} position={[0, 0, 0]} rotation={[-Math.PI / 2, 0, 0]}&gt;<br>      ...<br>   &lt;/mesh&gt;<br>   )<br>}</pre><h4>7. Let’s move some stuff around</h4><p><em>Cannon</em> gives you one more really cool thing — ability to interact with created element. Using an <em>api</em>, we can apply <em>onClick</em> positions, rotations, velocities, forces and impulses.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2F2m4jw&amp;display_name=CodeSandbox&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2F2m4jw&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2F2m4jw%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/25735347e2edb46ca1676f6208dfe092/href">https://medium.com/media/25735347e2edb46ca1676f6208dfe092/href</a></iframe><p>Above you can find only the basic components. Their knowledge will be a good start to creating some really advanced, impressive webpages, in-browser games and other projects.</p><p>Below you can find repository with the code to play with 👩🏼‍💻.</p><p><a href="https://github.com/uglyandfatpigeon/small-world">GitHub - uglyandfatpigeon/small-world</a></p><p><em>EDIT 2023: In the repository there is a new, updated version that takes into consideration newest releases of all the used libraries. The code changes however are pretty minor; planeBufferGeometry is replaced simply by planeGeometry and there is a little different configuration of props for &lt;Sky /&gt; and &lt;Stars /&gt; coponents. The mentions in the article are changed appropriately.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=31bec3bd90c2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>