<?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 Sergio Azizi on Medium]]></title>
        <description><![CDATA[Stories by Sergio Azizi on Medium]]></description>
        <link>https://medium.com/@checo272?source=rss-52c70e11d513------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*RCfOV9I4OLGwKU9RtkJ8oA.png</url>
            <title>Stories by Sergio Azizi on Medium</title>
            <link>https://medium.com/@checo272?source=rss-52c70e11d513------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 12:34:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@checo272/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[Create an Elastic Material with Three.js]]></title>
            <link>https://blog.devgenius.io/create-an-elastic-material-with-three-js-ea8d0a394bcf?source=rss-52c70e11d513------2</link>
            <guid isPermaLink="false">https://medium.com/p/ea8d0a394bcf</guid>
            <category><![CDATA[webgl]]></category>
            <category><![CDATA[threejs]]></category>
            <category><![CDATA[shaders]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[elastic]]></category>
            <dc:creator><![CDATA[Sergio Azizi]]></dc:creator>
            <pubDate>Sun, 27 Mar 2022 01:45:41 GMT</pubDate>
            <atom:updated>2023-09-05T17:18:16.210Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*KbuSClV9bToRVEoyO57_jA.gif" /><figcaption>Demo, featuring Piplup and Snorlax 🐧</figcaption></figure><p>I felt inspired to create an elastic material and wanted to share how to replicate it. Knowledge of Javascript, Three.js and some notions about shaders will be helpful for following along!</p><p>The snorlax/piplup demo above can be viewed <a href="https://elastic-material.vercel.app/"><em>here</em></a> and the source code is available <a href="https://github.com/arcaneCheco"><em>here</em></a>. Alright…</p><h4>Creating our scene</h4><p>This is our starting point. A basic Three.js scene with a camera, renderer and a red plane.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Fkg43wx&amp;display_name=CodeSandbox&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Fkg43wx&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2Fkg43wx%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/56b81a5c1ae61e26d590f4d44191ef6f/href">https://medium.com/media/56b81a5c1ae61e26d590f4d44191ef6f/href</a></iframe><h4>Setting up a shader-material</h4><p>Let’s replace the material in our scene with a Three.js <strong>ShaderMaterial. </strong>This class provides us with most of the necessary constants and attributes (position’s, uv’s, modelViewProjectionMatrices and others).</p><p>Create two new files, <em>fragment.glsl </em>and <em>vertex.glsl.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e619e9ac5df8ddac6d5752dcc39b75dd/href">https://medium.com/media/e619e9ac5df8ddac6d5752dcc39b75dd/href</a></iframe><p>Import the files into our script and instantiate the material.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/813fe92ea5ef51d83c2348a1c68d3f9a/href">https://medium.com/media/813fe92ea5ef51d83c2348a1c68d3f9a/href</a></iframe><p>And we should be back to seeing our red plane again.🥳</p><h4>Using a ray-caster</h4><p>Next, let’s tackle interactivity.</p><p>We‘ll add a Three.js <strong>Raycaster </strong>to determine if the user is trying to drag the red plane.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7e67bbc1e1195c55de7e316afe000e42/href">https://medium.com/media/7e67bbc1e1195c55de7e316afe000e42/href</a></iframe><p>Inside the callback of an event-listener, we are calling the <em>setFromCamera</em> method which shoots a ray from the position of the camera to a specified point on the screen. The method expects the point-position to be formatted as <em>normalised-device-coordinates</em> (NDC), i.e. our viewport dimensions, mapped to the range -1 =&gt; 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_fSY0uNl4yECObBAG7gdXw.png" /></figure><p>The <em>intersectObject</em> method checks for an intersection between the ray and an object that we pass it as an argument. If we get a hit, it’ll return a Javascript object inside an array that contains the position of the intersection point, it’s uv-position, distance to the camera and other information. We’ll later pass this information to our shader via uniforms.</p><h4>Implementing Drag-Controls</h4><p>We need three event listeners set our controls, <em>mousedown</em>, <em>mousemove</em> and <em>mouseup</em>.</p><p>onMouseDown:</p><ul><li>use the ray-caster to check if the user has clicked our plane.</li><li>if yes, pass the position to our material as a uniform and set boolean that shows that the user is currently dragging.</li></ul><p>onMouseMove:</p><ul><li>if the user is dragging, use the ray-caster to get an intersection and pass its position to our material. Since our plane doesn’t cover the entire viewport of our screen, we’ll need to create a dummy-plane that spans across the entire viewport of our screen.</li></ul><p>onMouseUp:</p><ul><li>if the user was dragging the plane, reset our boolean to show that the user has released the plane.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/86736953d4a80a9ecb7722de2a1987e4/href">https://medium.com/media/86736953d4a80a9ecb7722de2a1987e4/href</a></iframe><h4>Distorting our plane</h4><p>Back to shaders. We’ll modify our vertex-shader to distort our plane. First, ramp up the number of height and width segments in our geometry.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f0f0028fc4a566c0941eefe7756966cc/href">https://medium.com/media/f0f0028fc4a566c0941eefe7756966cc/href</a></iframe><p>Using the start and target positions from our drag-controls, we’ll create a sphere of influence to determine how much distortion we need to apply at what position. This should, a) be centred at the start position, b) be strongest at its centre, c) fall off gradually towards its edge and d) grow in size with the distance between the start and target positions.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/77d06ed46d378a37b31e129f1d4e3cd3/href">https://medium.com/media/77d06ed46d378a37b31e129f1d4e3cd3/href</a></iframe><p>We can pass the distortion to our fragment-shader to visualise it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e593c2a40f2c0e70fe0c4c214adab964/href">https://medium.com/media/e593c2a40f2c0e70fe0c4c214adab964/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*4NpjgHjy4MxE83Xt_1vv3w.gif" /></figure><p>Finally, use the distortion to create an offset-vector and add it to our position attribute:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8cc761d9aff9667467a33045e5b58c0f/href">https://medium.com/media/8cc761d9aff9667467a33045e5b58c0f/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*5n2q3poC8mtI4VKKgSjKuw.gif" /></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Fl7th3o&amp;display_name=CodeSandbox&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Fl7th3o&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2Fl7th3o%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/6b1cd52ce68ca40e66a27e0a3108fb5f/href">https://medium.com/media/6b1cd52ce68ca40e66a27e0a3108fb5f/href</a></iframe><h4>Releasing</h4><p>Let’s add an animation to restore the shape of plane when the user releases it. We’ll need two additional uniforms, a variable telling us that controls have been released, <em>uDragRelease</em>, and the time since the release <em>uDragReleaseTime. </em>We’ll also need a uniform for the global time which we’ll update inside the render loop.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/54dcc4e9bd1f773a4a837a6b4b8a220c/href">https://medium.com/media/54dcc4e9bd1f773a4a837a6b4b8a220c/href</a></iframe><p>Modify the vertex-shader to dampen the distortion.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a8e9f4db9a3529338d9031ed4034971/href">https://medium.com/media/7a8e9f4db9a3529338d9031ed4034971/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*hAbe0hoJ4JufAgD-ij24Qw.gif" /></figure><p>Add a transformation to make it look more “rubbery”. A sin function works well.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f848f209f8d3a709d6f9df80970d429/href">https://medium.com/media/0f848f209f8d3a709d6f9df80970d429/href</a></iframe><p>And Voila..</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/427/1*QHeesoJyR-67nz0lpzTakw.gif" /></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Fl62wzl&amp;display_name=CodeSandbox&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Fl62wzl&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2Fl62wzl%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/9f7e451224aed754bc63fd38d635b0cf/href">https://medium.com/media/9f7e451224aed754bc63fd38d635b0cf/href</a></iframe><h4>Going further</h4><p>Try different release animations, modify the rules we used to create the area of influence, or change the shape of the distortion.</p><p>Thanks for reading. Have a good day!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ea8d0a394bcf" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/create-an-elastic-material-with-three-js-ea8d0a394bcf">Create an Elastic Material with Three.js</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>