<?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 Clément Chenebault on Medium]]></title>
        <description><![CDATA[Stories by Clément Chenebault on Medium]]></description>
        <link>https://medium.com/@mad_clem?source=rss-5f256f375d3b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*tjE5GPF4qcVOIIm4.jpg</url>
            <title>Stories by Clément Chenebault on Medium</title>
            <link>https://medium.com/@mad_clem?source=rss-5f256f375d3b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 15:17:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mad_clem/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[Night Eyes: case study]]></title>
            <link>https://medium.com/@mad_clem/night-eyes-case-study-19eb54068379?source=rss-5f256f375d3b------2</link>
            <guid isPermaLink="false">https://medium.com/p/19eb54068379</guid>
            <category><![CDATA[webgl]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[experiment]]></category>
            <category><![CDATA[case-study]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Clément Chenebault]]></dc:creator>
            <pubDate>Tue, 10 Jan 2017 11:19:50 GMT</pubDate>
            <atom:updated>2017-01-10T11:42:24.015Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LcWheKAA1Pq7iAkHX1ci2A.png" /></figure><p>For the 4th edition of Christmas Experiment, Wen and I were invited by <a href="https://medium.com/u/af2723158b35">David Ronai</a> to create a digital experiment for the opening of the Calendar. We received some great help from Bertrand Carrara for the designs, and we ended up with this WebGL experience:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F198673756&amp;url=https%3A%2F%2Fvimeo.com%2F198673756&amp;image=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F611712002_1280.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=vimeo" width="1852" height="1080" frameborder="0" scrolling="no"><a href="https://medium.com/media/87a3ca061c332755e6d83a9d58174616/href">https://medium.com/media/87a3ca061c332755e6d83a9d58174616/href</a></iframe><p><strong>You can try it by clicking on the following link:</strong></p><p><a href="http://christmasexperiments.com/2016/01/night-eye/">Night Eye</a></p><p>If you’re interested in making some [really cool] <strong>WebVR</strong>, you should check out Wen’s case study <a href="http://blog.bongiovi.tw/case-study-night-eye/">here</a>!</p><p>If you’re interested in how we made the lines (and how they draw into animals), you should continue to read :)</p><p>For the purpose of this experience, we used <strong>javascript</strong> / <strong>native WebGL</strong>. Being fairly new to WebGL, I hope this case study won’t be full of bad practices/misconceptions!</p><p>Summary:</p><ul><li><strong>Concept</strong></li><li><strong>Draw a WebGL 3d Line</strong> <em>(draw a line, draw a curve, animate it)</em></li></ul><p>And the following parts will focus on the “Night Eyes” experiment itself:</p><ul><li><strong>Different Line behaviours</strong> <em>(wandering around, draw into an animal, leaving the animal shape)</em></li><li><strong>Final Scene</strong></li><li><strong>Optimisations</strong></li><li><strong>How to ease with no libs</strong></li></ul><p>Notes: All the original sources are available <a href="https://github.com/yiwenl/Christmas_Experiment_2016/tree/master/build">here</a>, and the examples developed for the case study can be found <a href="https://github.com/clementchenebault/xps/tree/master/casestudy">here</a>!</p><h3>Concept / Idea</h3><p>What we wanted for our experiment can be summarised by: “beautiful” and “peaceful” scenery (modestly speaking…). To add some christmassy atmosphere and animation into the scene, we used an idea from the studio <a href="http://www.differantly.com/"><em>Differantly</em></a><em> </em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/482/1*t2TcYq--n71-sRMKmpj_2g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/420/1*3Jn0_Zk6c6oncqtz0iy4Cg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/472/1*nrRqFJBn3jVVbT6K2qfY0Q.png" /><figcaption><strong>animals drawn with a single line</strong></figcaption></figure><h3>Draw a [good looking] 3D Line</h3><p><strong>1. Draw a line</strong></p><p>Finding sources about how to draw a WebGL line is really hard. Two people however made things easier and I can’t thank them enough for that:</p><p><a href="https://medium.com/u/e455eba64dcd">Matt DesLauriers</a> (<a href="https://mattdesl.svbtle.com/drawing-lines-is-hard">https://mattdesl.svbtle.com/drawing-lines-is-hard</a>) and <a href="https://medium.com/u/7d80d30f321">Jaume Sanchez Elias</a> (<a href="https://github.com/spite/THREE.MeshLine">https://github.com/spite/THREE.MeshLine</a>),</p><p><em>MERCI BEAUCOUP</em>!</p><p><strong>The basic idea:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/992/1*C_HZuFWk5O-RigLWNDIP4A.png" /><figcaption><a href="http://codeflow.org/entries/2012/aug/05/webgl-rendering-of-solid-trails/">http://codeflow.org/entries/2012/aug/05/webgl-rendering-of-solid-trails/</a></figcaption></figure><p>And here comes the class <strong>Line.js</strong> (heavily inspired by the <a href="https://github.com/spite/THREE.MeshLine">Spite meshline</a>):</p><p><a href="https://github.com/clementchenebault/mcgl/blob/master/src/mcgl/geometry/Line.js">clementchenebault/mcgl</a></p><p>This class will pass the following attributes to the vertex shader:</p><ul><li>a_position (different points composing the line) (“Current” in schema above)</li><li>a_previous (“Last” in schema above)</li><li>a_next (“next” in schema above)</li><li>a_width (width of the line for each position, by default equals to .1)</li><li>a_direction (the green arrows on the scheme, -1 and 1 alternatively)</li></ul><p>(and some other attributes we didn’t need in this experiment)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*aW-WJZ9aieUbsS6mRlMOFw.png" /></figure><p><em>Notes: the lines are in fact quads, that’s why there are 6 vertices (two triangles) (</em><em>[n, n+1, n+2],[n+2, n+1, n+3])</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/324/1*c2MzrVbXSk8YA6uW9Eml2A.png" /><figcaption>TADAA (/ <strong>FAIL</strong>)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Nhb9ZSnaTjEgxkRWC2dXFw.png" /><figcaption>Few hours[sss..] later: “<strong>PUTAIN</strong>” (French exclamation of triumph)</figcaption></figure><p><strong>2. Draw a curve</strong></p><blockquote>Now I have a 3D line</blockquote><p>We still need to make it pretty… Therefore we must draw some curves between the points!</p><p><em>a) First: Bezier Curve!</em></p><p><em>I can’t remember if I wrote all these following functions :O please poke me if you feel like you deserve a credit on these ones!</em></p><p>I ended up with these following functions :</p><pre>let myPoints = [] // to fill yourself with... points</pre><pre>...</pre><pre>let finalPoints = this.getBezierPoints(myPoints, myPoints.length * 3); // the bezier curve!</pre><p>The “bezier process” itself :</p><pre>level(i) {<br>   if(i==0) return 1;<br>   else return i*this.level(i-1);<br>}</pre><pre>binCoefficient(n, i) { <strong>// THIS IS FREAKING EXPENSIVE</strong><br>   return this.level(n) / ( this.level(i) * this.level(n-i) );<br>}</pre><pre>getBezierPoints(points, numSeg) {<br>   let bezierPoints = [];<br>   let numPoints = points.length;<br>   let t, tx, ty, tz;<br>   let bc, pow1, pow2;<br>   <br>   for(let i=0; i&lt;numSeg; i++) {<br>      t = i/numSeg;</pre><pre>      tx = ty = tz = 0;<br>   <br>      for(let j=0; j&lt;points.length; j++) {<br>         bc = this.binCoefficient(numPoints-1, j);<br>         pow1 = Math.pow((1-t), numPoints-j-1);<br>         pow2 = Math.pow(t, j);<br>         tx += bc * pow1 * pow2 * points[j][0];<br>         ty += bc * pow1 * pow2 * points[j][1];<br>         tz += bc * pow1 * pow2 * points[j][2];<br>      }</pre><pre>      let p = [tx, ty, tz];<br>      let index = Math.floor(points.length * t);<br>      <br>      if(index == points.length) index = points.length-1;<br>      bezierPoints.push(p);<br>   }</pre><pre>   return bezierPoints;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5tppd6YrtWPkY3xdxtR5qA.png" /><figcaption>Hallelujah! Wait… What!?</figcaption></figure><p>This is looking good, so <strong>what’s the heck</strong>!? This is really expensive… Specially the part to get the <strong>binomial coefficient</strong>. Calculating it each frame to animate the points would make it very hard to use… :/</p><p><em>b) Second: Catmull-Rom Spline!</em></p><p>You can find plently of implementations of the Catmull-Rom Spline over the internet, this is the one I used (shamelessly inspired by the Three.js one):</p><p><a href="https://github.com/clementchenebault/mcgl/blob/master/src/mcgl/libs/Spline.js">clementchenebault/mcgl</a></p><pre><em>// really I should remove this empty array!</em><br>let spline = new Spline([]);</pre><pre>let myPoints = [] <em>// to fill</em></pre><pre>...</pre><pre>let finalPoints = this.<strong>getPoints</strong>(myPoints);</pre><pre>// -----------------------------------</pre><pre><strong>getPoints</strong>(pts){<br>   spline.points = pts;<br>   <br>   <em>// n_sub is the number of subdivision between two points<br>   // the more the merrier (and the more expensive too)</em><br>   let indexArray, n_sub = 3; </pre><pre>   let newPoints = [];<br>   for (let i = 0; i &lt; pts.length * n_sub; i ++ ) {<br>      indexArray = i / ( pts.length * n_sub );</pre><pre><em>      // we pass newPoints as the &#39;out&#39; array</em><br>      <strong>spline.getPoint</strong>( indexArray,  newPoints); <br>   }</pre><pre>   return newPoints;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pBniRphDu8Za06QeOGs2rQ.png" /><figcaption>Starring the <strong>catmull-rom</strong> Spline!</figcaption></figure><p>Good looking and way faster :O</p><p><a href="https://medium.freecodecamp.com/nerding-out-with-bezier-curves-6e3c0bc48e2f"><em>This is a great link</em></a><em> to get your head into if you want to learn more about Bezier Curves.</em></p><p><strong>3. Animate the line</strong></p><p>Animating the line is fairly simple. The main idea is to get the first point (<em>head</em> of the line) and animate it… Then make the other points follow!</p><p>Following the previous example:</p><pre><em>// simplified, pass a list of points to the line</em><br>let line = new Line(finalPoints); </pre><p>When the mouse moves:</p><pre>mouseMove(x, y) {<br><strong>   this.currentPos</strong> = [x, y]; <em>// the head of the line will follow that point<br>   // depth of the line follows a cosinus wave<br>   </em>this.tick++;<br>   <strong>this.z</strong> = Math.cos(this.tick/25) * 1000 - 700;<br>}</pre><p>In the render function, called each frame:</p><pre>render(){<br><em>   ... // webgl stuff, binding the right shaders etc.</em></pre><pre>   var newPoints = this.createPoints(line);<br>   line.render(newPoints);</pre><pre><em>   ... // draw the line, etc.</em><br>}</pre><p>And finally, we <strong>move the points</strong>:</p><pre>createPoints(line) {</pre><pre><em>   // move the first point aka line&#39;s &#39;head&#39;</em><br>   let pt0 = line.points[0];<br>   pt0[0] = <strong>this.currentPos[0]</strong>;<br>   pt0[1] = <strong>this.currentPos[1]</strong>;<br>   pt0[2] = <strong>this.z</strong>;</pre><pre><em>   // then, make the other points follow<br></em>   for (let i = 1; i &lt; line.points.length; i++) {<br>      <em>// get the direction between two points</em><br>      let dir = Matrices.normalize(Matrices.subtractVectors(line.points[i], line.points[i-1]));<br>      line.points[i][0] = line.points[i-1][0] + dir[0] * 20;<br>      line.points[i][1] = line.points[i-1][1] + dir[1] * 20;<br>      line.points[i][2] = line.points[i-1][2] + dir[2] * 20;<br>    }</pre><pre><em>    // finally, make a curve from the points</em><br>    return getPoints(line.points)<br>}</pre><figure><a href="http://work.goodboydigital.com/codevember/clem/03/"><img alt="" src="https://cdn-images-1.medium.com/max/480/1*AzVVVfeIGCQl5jH6w0sJFQ.gif" /></a><figcaption>Try it here: <a href="http://work.goodboydigital.com/codevember/clem/03/">http://work.goodboydigital.com/codevember/clem/03/</a></figcaption></figure><h3>The “Night Eyes” case!</h3><h3><strong>1. Different line behaviours</strong></h3><ul><li><strong>wandering around</strong></li></ul><p>The first thing was to make the line move around, like waiting for something to happen.</p><p>As explained above, only the first point of each line needs to be animated, and the others will follow… The hard part was to find some maths to make the lines move in an organic way <em>(ish)</em>…</p><p>We ended up with 3 ways to move the lines around:</p><pre><em>// pseudo code</em></pre><pre><em>// OPTIONS</em><br><strong>perlin</strong>:  this.perlin,<br><strong>time</strong>:  Math.random() * 0xFF,<br><strong>radius</strong>:  Math.floor(Math.random() * 3) + 2,<br><strong>targetPoint</strong>:  this.targetPoint,<br><strong>xoff</strong>:  Math.random() * 100, <em>// for perlin noise</em><br><strong>yoff</strong>:  Math.random() * 100  <em>// for perlin noise</em></pre><pre><strong><em>// ----- to make it move in a circle</em></strong><br>targetPoint[0] = Math.cos(time/20) * radius;<br>targetPoint[2] = Math.sin(time/20) * radius;</pre><pre><strong><em>// ----- to make it move in a circle with a perlin noise controlling the y position</em></strong><br>targetPoint[0] = Math.cos(time/20) * radius;<br>targetPoint[2] = Math.sin(time/20) * radius;</pre><pre>xoff += .01 *  1; <em>// perlin noise</em><br>yoff += .01 *  1; <em>// perlin noise</em><br>let p = perlin.perlin2(xoff, yoff);<br>targetPoint[1] += p/20;<br>targetPoint[1] += Math.sin(Math.tan(Math.cos(time/80) * 1.2)) * .01;</pre><pre><strong><em>// ----- THE SNAAAAKE ONE</em></strong><br>targetPoint[0] = Math.cos(time/40) * radius;<br>targetPoint[2] = Math.sin(time/50) * radius * 1.2 ;</pre><pre>targetPoint[1] = Math.abs(Math.sin(time / 100) * 4) - 2;<br>targetPoint[0] += Math.cos(Math.pow(8, Math.sin(time/40))) * .5;<br>targetPoint[1] += Math.sin(Math.pow(8, Math.sin(time/20))) * 1;</pre><p>You can check out the sources here:</p><p><a href="https://github.com/clementchenebault/xps/tree/master/casestudy/line_wandering/app/scripts">clementchenebault/xps</a></p><p>And more particularly, <a href="https://github.com/clementchenebault/xps/blob/master/casestudy/line_wandering/app/scripts/views/LinesMotions.js"><strong>the line motions</strong></a> and the <a href="https://github.com/clementchenebault/xps/blob/master/casestudy/line_wandering/app/scripts/views/ViewLine.js"><strong>ViewLine.js</strong></a></p><figure><a href="http://work.goodboydigital.com/christmas/clement/casestudy/line_wandering/"><img alt="" src="https://cdn-images-1.medium.com/max/790/1*9BAMgXbUpLl81lH5_jH-cQ.gif" /></a><figcaption>Try it <a href="http://work.goodboydigital.com/christmas/clement/casestudy/line_wandering/"><strong>here</strong></a>: <a href="http://work.goodboydigital.com/christmas/clement/casestudy/line_drawing/">http://work.goodboydigital.com/christmas/clement/casestudy/line_wandering/</a></figcaption></figure><ul><li><strong>Draw into an animal</strong></li></ul><p>For this Christmas Experiment, we worked with <a href="https://twitter.com/bertrandcarrara?lang=en">Bertrand Carrara</a> who made the animal drawings:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B-QYevIXtzEp0CB03Ej0Gw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hbISqMzehrCWlOfrIAY81A.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J7IJuIPFGey7D6rDg6OrSg.jpeg" /><figcaption>❤ Looking sweet ❤</figcaption></figure><p>From these 2d drawings, I roughly placed the different points to get the main shape.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QXLuwvojpF3f3wRNiMsbOA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/876/1*RSBJd1_6zldOnhKswl0whg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/1*fEI6kzTPnosAKkpNIIrvkg.png" /></figure><p>Because these are 2d points, we add a depth by applying a cosinus when drawing the shape:</p><pre>for (var i = 0; i &lt; this.vertices.length; i++) {<br>    ...<br>    this.tick++;<br>    this.vertices[i][2] = Math.cos(this.tick/10) * .4;<br>    ...<br>}</pre><p><em>Note: Because the “y-axis” is inversed (or is it?) in WebGL, don’t forget to inverse it again or you’ll end up with this:</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wQVVdShFeNXWxMV6qDjdWw.png" /><figcaption>Mr Bear is looking pretty weird right now</figcaption></figure><p>To <strong>transform our line into an animal</strong>, there are some essential steps:</p><ol><li>Get the <strong>same number of points</strong> for the <strong>line</strong> and the <strong>animal shape </strong>(by adding some points on the line).</li><li>Add some points in between to generate a path FROM the line TO the shape (blue points in the following drawing)</li><li>get the full path (line points + path to go to the shape + animal points) into an array</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q6YQ0TgIr6hI9Qj8OWhf_A.jpeg" /><figcaption>From the line to the shape, there is the full path.</figcaption></figure><p>The idea is to make the <strong>line points</strong> move to the corresponding <strong>animal point</strong>.</p><p>With the full path filled with 3D points, we now want the line to move and transform into an animal.</p><p>To achieve this effect, will use some <strong>Tweens</strong>. But instead of “tweening” from a position to another, we will tween the points according to the path <strong>indices</strong>.</p><p>0 -&gt; 10 // 1 -&gt; 11 // 2- &gt; 12 // 3 -&gt; 13 // 4 -&gt; 14 // 5 -&gt; 15</p><p>For instance on the drawing above: the point at index <strong>0 </strong>will go to the point at index <strong>10 </strong>by going through all the previous ones. When the tween is updated, we move the current line point accordingly on the curve.</p><p>In <a href="https://github.com/clementchenebault/xps/blob/master/casestudy/line_drawing/app/scripts/views/ViewLine.js">ViewLine.js</a>, we create the Tweens:</p><pre>newPoints(){<br>   for (var i = 0; i &lt; line.vert.length; i++) {<br>      var startIndex = ( (line.vert.length -1 ) - i);<br>      var endIndex =  (this.path.length - 1) - i;</pre><pre>      let obj = { startIndex: startIndex, endIndex: endIndex, currentIndex: startIndex}<br>      let o = Easings.returnVariable(<strong>obj</strong>, 1, { <strong>currentIndex: endIndex</strong>  });<br>      <strong>o.point = startIndex;<br>      this.objectsToTween[index++] = o;</strong><br>   }<br>}</pre><p><em>Note: Read a bit further to see how to update the tweens in an update loop.</em></p><p>The important classes to check out are <a href="https://github.com/clementchenebault/xps/blob/master/casestudy/line_drawing/app/scripts/views/ViewLine.js"><strong>ViewLine.js</strong></a>, <a href="https://github.com/clementchenebault/xps/blob/master/casestudy/line_drawing/app/scripts/views/viewsAnimals/ViewAnimal.js"><strong>ViewAnimal.js</strong></a>, and <a href="https://github.com/clementchenebault/xps/blob/master/casestudy/line_drawing/app/scripts/views/viewsAnimals/ViewAnimal.js"><strong>Animal.js</strong></a></p><figure><a href="http://work.goodboydigital.com/christmas/clement/casestudy/line_drawing/"><img alt="" src="https://cdn-images-1.medium.com/max/480/1*cQHUeGM_K1VKcQYoJsnhiQ.gif" /></a><figcaption>Try it here: <a href="http://work.goodboydigital.com/christmas/clement/casestudy/line_drawing/">http://work.goodboydigital.com/christmas/clement/casestudy/line_drawing/</a></figcaption></figure><ul><li><strong>Leaving the animal shape</strong></li></ul><p>To leave the animal shape, we use two methods:</p><p><em>a) Directly change the state to make the line wander again:</em></p><pre>this.objectsToTween = []; <em>// delete the tweens manually</em><br> for (var i = 0; i &lt; this.line.points.length; i++) {<br>   this.points[this.points.length - 1- i] =      this.line.vert[i*this.sub];<br> }</pre><pre>this._cutExtraPoints(20);<br>this.state = STATES.wandering;</pre><p><em>b) Use the same idea for the drawing, create a path to leave the shape</em></p><p>I’m being very lazy, and that’s almost use the same code as before.</p><p><strong><em>Note: Remove the extra points to avoid animating a line of 200 points! (and luckily, it looks better too)</em></strong></p><pre>_cutExtraPoints(max) {</pre><pre>   let nbPtsToSlice = this.points.length - max;<br>   let offset = Math.ceil(this.points.length / max);</pre><pre>   let arr = [];<br>   let index = 0;<br>   for (var i = 0; i &lt; this.points.length; i+=offset) {<br>      arr[index++] = this.points[i]<br>   }<br>   <br>   this.points = arr;<br>   this.line.points = this.points; <em>// set the new points</em><br>   this.needsUpdate = true; <em>// will need to upload the buffers</em><br> }</pre><h3><strong>2. Final Scene</strong></h3><h3>Clément Chenebault on Twitter</h3><p>Working hard for the #christmasexperiment opening w/ @yiwen_lin @bertrandcarrara Stay tuned ✊ #webgl #WIP https://t.co/RHGdLYx7Hw</p><p>For this line animation, I can’t recommend the great case study from <a href="https://medium.com/u/af2723158b35">David Ronai</a> enough, especially if you’re interested in using lines with a predefined path.</p><p><a href="http://www.makioandfloz.com/article/treeline">Treelines case study</a></p><p>About the different paths, they couldn’t be simpler, they all are <a href="https://en.wikipedia.org/wiki/Helix"><strong>Helix</strong></a><strong>:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/255/1*k_QqIQTuM9-5_JYeG6lIbw.png" /><figcaption><a href="https://en.wikipedia.org/wiki/Helix">https://en.wikipedia.org/wiki/Helix</a></figcaption></figure><pre>// pseudo code<br>x= x + cos(time/20) * radius;<br>z = z + sin(time/20) * radius;  <br>y -= .1; // y is only going up</pre><p>For more information, you can check out the firstLine() and secondLine() method in the following link:</p><p><a href="https://github.com/yiwenl/Christmas_Experiment_2016/blob/master/build/src/js/SubsceneFinale.js">yiwenl/Christmas_Experiment_2016</a></p><p>The owl itself is composed of several lines, same process as before:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ApPUSrZIi0as6sZELfvYGg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*brr2KyfZg1TQ7hmI_MUxTg.png" /></figure><h3>3. Optimisation</h3><p>Drawing lines can be very processor intensive, therefore there are some tips about optimisation when using them:</p><ul><li>the main optimisation: <strong>only upload the buffers we need</strong>!</li></ul><p>In the <a href="https://github.com/clementchenebault/mcgl/blob/master/src/mcgl/geometry/Line.js"><strong>Line.js geometry</strong></a>, there is a needsUpdate property to know if the line has been changed and needs to be updated.</p><p>If the length of the line (its number of points) didn’t change, we don’t need to upload the following buffers: indices, width, directions, uv, counter. We only need to update the positions of the points, and the previous and next attributes.</p><p><em>Note: The </em><em>needsUpdate property is managed from the ViewLine class itself (set to </em><em>truewhen the line as been changed).</em></p><ul><li>Removing the extra points (with the cutExtraPoints(max) method from earlier)</li><li>Because we often modify some big arrays, I tend to avoid using the native array functions to modify them. For example, instead of using the push() method, we can do:</li></ul><pre>let index = 0<br>let myArray = [];</pre><pre>for (let i = 0; i &lt; values.length; i++) {<br>  myArray[index++] = values[i];<br>}</pre><p>Same for the splice() method which can be a bit greedy:</p><pre>splice(arr, index) {<br>   var len=arr.length;<br>   if (!len) { return }<br>   while (index&lt;len) {<br>      arr[index] = arr[index+1];<br>      index++<br>   }</pre><pre>   arr.length--;<br>}</pre><p>For more information, see <a href="https://gamealchemist.wordpress.com/2013/05/01/lets-get-those-javascript-arrays-to-work-fast/">this link</a>!</p><ul><li>Using pool is a great optimisation.</li><li>Manually managing the render loop easing can really help with performance (more on this later).</li><li>Finally, reduce the number of points on mobile.</li></ul><h3>4. How to ease with no libs</h3><p>Following the optimisation tricks, here is some explanation about the easing we use in the experiment.</p><p>Because we don’t need all features that TweenLite.js provides, there is a simple class to manage the tweens (code is a bit dirty for now, brave yourself!) :</p><p>Easings.js</p><p><a href="https://github.com/clementchenebault/mcgl/blob/master/src/mcgl/utils/Easings.js">clementchenebault/mcgl</a></p><p>How to use:</p><pre>import Easings from &quot;Easings&quot;;</pre><pre>window.Easings = new Easings(); <em>// boouh global variable, ikr...</em></pre><pre>let myObj = {<br>   x: 0<br>}</pre><pre>Easings.to(myObj, duration || 1, {<br>   x: whatever || 1,<br>   delay: 1,<br>   ease: Easings.easeOutCirc,<br>   onUpdate: ()=&gt;{},<br>   onComplete: ()=&gt;{}<br>});</pre><pre><em>// in an update loop, or if you happen to have a ticker manager it would even be better</em><br>Easings.update();</pre><p>While this class already exists, I didn’t use it really (<em>AND I CAN’T REMEMBER WHY…</em>) but still, there is the code to update the Tweens (the ones we set earlier when <strong>we draw into an animal shape)</strong>:</p><pre><em>// in the update loop</em><br>for (var i = 0; i &lt; this.objectsToTween.length; i++) {<br>     let o = this.objectsToTween[i]</pre><pre>     for (var k = 0; k &lt; o.props.length; k++) {<br>        var e = o.props[k];<br>        o.obj[e.var] = <strong>Easings.easeOutCirc(o.currentIteration, e.value, e.toValue - e.value, o.duration)</strong>;</pre><pre><em>        // move the point on the curve (by flooring the index)</em><br>        let indexFloor = Math.floor(o.obj[e.var]);<br>        this.line.vert[o.point][0] = this.path[indexFloor][0];<br>        this.line.vert[o.point][1] = this.path[indexFloor][1];<br>        this.line.vert[o.point][2] = this.path[indexFloor][2];<br>     }</pre><pre><strong>     o.currentIteration += 1;</strong> <em>// increment the iteration</em><br>     <strong>if(o.currentIteration &gt; o.duration){<br>        o.delete = true;<br>     }</strong><br>    }<br>    else {<br>       this.splice(this.objectsToTween, i); <em>// splice method for optimisation<br></em>       i--;<br>    }<br>   }</pre><pre><em>// and here is the easing.</em>easeOutCirc<em> function<br></em><strong>easeOutCirc</strong>(t, b, c, d) {    <br>   return c * Math.sqrt(1 - (t=t/d-1)*t) + b;    <br>}</pre><p>In <a href="https://github.com/clementchenebault/xps/blob/master/casestudy/line_drawing/app/scripts/views/ViewLine.js">ViewLine.js</a>, you can find this code in therender() method.</p><h3>Conclusion</h3><p>If you have any questions (or feedback), you can contact me on twitter <a href="https://twitter.com/mad_clem">@mad_clem</a> :)</p><p>I’ll pass on the obvious (how great it was to work on this experience, etc.) to dispatch some huge thanks to:</p><ul><li><a href="https://twitter.com/yiwen_lin">Wen</a> and <a href="https://twitter.com/bertrandcarrara">Bertrand Carrara</a>.</li><li><a href="https://medium.com/u/af2723158b35">David Ronai</a>, for inviting us once again!</li><li><a href="https://medium.com/u/669eb6f85385">Tom Jennings</a> for fixing my typos, even though he doesn’t understand anything about code!</li><li>to all people contributing of making the WebGL world a better place by sharing knowledges:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*rUpolQpJkgYGs1FOtn8jwA.gif" /></figure><p>Last but not least, it’s been great to work on this experience, <em>etc..</em>. :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=19eb54068379" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>