<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="fr">
    <title>La Forge - shader</title>
    <subtitle>Le blog de la Forge</subtitle>
    <link rel="self" type="application/atom+xml" href="https://staging.lafor.ge/tags/shader/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://staging.lafor.ge"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2021-01-29T00:00:00+00:00</updated>
    <id>https://staging.lafor.ge/tags/shader/atom.xml</id>
    <entry xml:lang="fr">
        <title>Initiation aux shaders : Matériel, Ombres et Lumière (Partie 3)</title>
        <published>2021-01-29T00:00:00+00:00</published>
        <updated>2021-01-29T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://staging.lafor.ge/shader-3/"/>
        <id>https://staging.lafor.ge/shader-3/</id>
        
        <content type="html" xml:base="https://staging.lafor.ge/shader-3/">&lt;p&gt;Lors de la précédente &lt;a href=&quot;/shader-2&quot;&gt;partie&lt;/a&gt; nous étions laissés avec un résultat sympa mais qui ne casse pas
3 pattes à un canard!&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/raymarch/scene.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Comme promis dans sa conclusion nous allons nous attaquer à rajouter de la couleur dans tout ça. 🌈&lt;/p&gt;
&lt;h2 id=&quot;le-materiel&quot;&gt;Le Matériel&lt;/h2&gt;
&lt;p&gt;Afin d’appliquer un matériel sur un objet de notre scène on va devoir expliquer au raymarcher ce qu’il doit faire
lorsqu’il touche un objet et de quelle couleur le pixel de l’écran associé doit être coloré.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/material.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Un rayon part de l’oeil traverse l’écran et continue sa course jusqu’à rencontrer un objet de notre scène.
Si le rayon touche l’objet vert, le pixel traversé par le rayon doit être coloré en vert. Et à l’inverse si c’est l’objet
rouge qui est touché, le pixel doit devenir rouge.&lt;/p&gt;
&lt;p&gt;Pour cela il nous faut un moyen de stocker l’information de couleur en fonction de l’objet touché.&lt;/p&gt;
&lt;p&gt;On va donc opérer à quelques modification de notre modèle de données.&lt;/p&gt;
&lt;p&gt;Tout d’abord on créé une structure qui va nous permettre de stocker à la fois la distance de l’objet par rapport à l’oeil, ainsi que
son information de couleur.&lt;/p&gt;
&lt;p&gt;Pour ne pas stocker des données trop lourdes on va uniquement définir un entier qui sera l’index de notre matériel à
appliquer au solide s’il est touché par un rayon venant de l’oeil.&lt;/p&gt;
&lt;h2 id=&quot;modifications-du-code-existant&quot;&gt;Modifications du code existant&lt;/h2&gt;
&lt;h3 id=&quot;implementation-de-la-structure-de-donnees&quot;&gt;Implémentation de la structure de données&lt;/h3&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;struct&lt;/span&gt;&lt;span&gt; Data &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    float&lt;/span&gt;&lt;span&gt; d&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; distance signée entre l&amp;#39;objet touché et l&amp;#39;oeil&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    int&lt;/span&gt;&lt;span&gt; m&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;   //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; index du matériel à appliquer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On va devoir faire aussi faire quelques modifications et ajouts:&lt;/p&gt;
&lt;p&gt;Tout d’abord, on doit définir quelques fonctions permettant de manipuler notre structure.&lt;/p&gt;
&lt;p&gt;Notamment une fonction de minimum entre deux &lt;code&gt;struct Data&lt;/code&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;/**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* Retourne le minimum entre deux structures de données en se basant sur la distance signée.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* d1 : structure 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* d2 : structure 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Data &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;minData&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;Data &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;d1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; Data &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;d2&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;d1&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; d2&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;d&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        return&lt;/span&gt;&lt;span&gt; d1&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    return&lt;/span&gt;&lt;span&gt; d2&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;modification-des-sdf-existantes&quot;&gt;Modification des SDF existantes&lt;/h3&gt;
&lt;p&gt;On doit aussi modifier nos deux fonctions SDF (Signed Distance Field) pour le plan et la sphère.&lt;/p&gt;
&lt;h4 id=&quot;sdf-de-la-sphere&quot;&gt;SDF de la sphère&lt;/h4&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;/**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* Retourne la distance signée d&amp;#39;une sphère par rapport à l&amp;#39;oeil&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* point: Point de la scène 3D touché par le rayon de lumière&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* rayon: Rayon de la sphère&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; sdSphere&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;vec3 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;point&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; float&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; rayon&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; length&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;point&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span&gt; rayon&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;devient&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;/**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* Retourne la distance signée d&amp;#39;une sphère par rapport à l&amp;#39;oeil&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* point    : Point de la scène 3D touché par le rayon de lumière&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* radius   : Rayon de la sphère&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* material : Matériel à appliquer à la sphère &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Data &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;sdSphere&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;vec3 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;point&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; float&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; radius&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; material&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; Data&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;point&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; radius&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; material&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h4 id=&quot;sdf-de-la-plan&quot;&gt;SDF de la plan&lt;/h4&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;/**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* Retourne la distance signée d&amp;#39;un plan horizontal par rapport à l&amp;#39;oeil&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* point    : Point de la scène 3D touché par le rayon de lumière&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* height   : Rayon de la sphère&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; sdHorizontalPlan&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;vec3 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;point&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; float&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; height&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; point&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span&gt; height&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Devient&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;/**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* Retourne la distance signée d&amp;#39;un plan horizontal par rapport à l&amp;#39;oeil&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* point    : Point de la scène 3D touché par le rayon de lumière&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* height   : Rayon de la sphère&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* material : Matériel à appliquer au plan&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Data &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;sdHorizontalPlan&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;vec3 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;point&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; float&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; height&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; material&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; Data&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;point&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; height&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; material&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h4 id=&quot;modification-de-la-fonction-de-generation-de-scene&quot;&gt;Modification de la fonction de génération de scène&lt;/h4&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;/**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* Renvoie la distance signée de l&amp;#39;intégralité des objets de la scène&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* point : Point de la scène qui doit être calculé&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; scene&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;vec3 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;point&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    float&lt;/span&gt;&lt;span&gt; dSphere &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; sphere&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;point &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; .&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    float&lt;/span&gt;&lt;span&gt; dPlan &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; sdHorizontalPlan&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;point&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; min&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;dSphere&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; dPlan&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;devient&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;/**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* Renvoie la distance signée et le matériel associé de l&amp;#39;intégralité des objets de la scène&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* point : Point de la scène qui doit être calculé&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Data &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;scene&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;vec3 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;point&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Data dSphere &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; sdSphere&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;point &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; .&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; MATERIAL_RED&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Data dPlan &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; sdHorizontalPlan&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;point&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; MATERIAL_GREEN&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; minData&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;dSphere&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; dPlan&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; le minimum de distance entre les deux structures&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h4 id=&quot;modification-de-l-algorithme-de-raymarching&quot;&gt;Modification de l’algorithme de raymarching&lt;/h4&gt;
&lt;p&gt;On doit aussi modifier la fonction de &lt;a href=&quot;/shader-2/#implementation&quot;&gt;Raymarching&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Il devient:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;/**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* Calcule la distance de l&amp;#39;objet le plus proche par rapport à l&amp;#39;oeil &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* en fonction d&amp;#39;un rayon directeur&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* rO : origine du rayon&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* rD : vecteur directeur du rayon&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Data &lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;rayMarch&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;vec3 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;rO&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; vec3 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;rD&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    float&lt;/span&gt;&lt;span&gt; d &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    int&lt;/span&gt;&lt;span&gt; m &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    for&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;int&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt; MAX_ITERATIONS&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;++&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        vec3 p &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt;  rO &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+&lt;/span&gt;&lt;span&gt; rD &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;*&lt;/span&gt;&lt;span&gt; d&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        Data ds &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;  scene&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        d &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+=&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; ds&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;d&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        m &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; ds&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;m&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;ds&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span&gt; MIN_DISTANCE &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;||&lt;/span&gt;&lt;span&gt; d &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; MAX_DISTANCE&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; Data&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; m&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;dictionnaire-de-materiel&quot;&gt;Dictionnaire de matériel&lt;/h3&gt;
&lt;p&gt;On définit des constantes qui vont correspondre à&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; MATERIAL_RED&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;   1&lt;/span&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; MATERIAL_BLUE&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;  2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; MATERIAL_GREEN&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; MATERIAL_YELLOW&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 4&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Puis une fonction qui va associer notre index à une couleur sous forme de vec3.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;/**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* Renvoie le matériel associé à l&amp;#39;index ou du noir si l&amp;#39;index est inconnu&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* m : index du matériel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;vec3&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; material&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; index&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    switch&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;index&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        case&lt;/span&gt;&lt;span&gt; MATERIAL_RED&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            return&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        case&lt;/span&gt;&lt;span&gt; MATERIAL_GREEN&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            return&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        case&lt;/span&gt;&lt;span&gt; MATERIAL_BLUE&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            return&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        case&lt;/span&gt;&lt;span&gt; MATERIAL_YELLOW&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            return&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        default&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            return&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;fonction-principale&quot;&gt;Fonction principale&lt;/h3&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;/**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* Calcule la couleur que doit posséder un pixel en fonction de ses coordonnées absolues&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* fragColor : Couleur du pixel à afficher à l&amp;#39;écran&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* fragCoord : Coordonnées du pixel à l&amp;#39;écran&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; mainImage&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt; out vec4 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragColor&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; in vec2 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragCoord&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Calcule des UV de l&amp;#39;écran&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec2 uv &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;fragCoord&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;iResolution&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;xy&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; iResolution&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;y&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; La caméra est au centre du monde&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 eye &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; L&amp;#39;écran est décalé de 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 pixel &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;uv&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Calcul du rayon directeur de l&amp;#39;oeil traversant le pixel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 rayDirector &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; normalize&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;pixel &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; eye&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On applique l&amp;#39;algorithme de raymarching à ce pixel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Data data &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; rayMarch&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;eye&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; rayDirector&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On récupère le matériel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 col &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; material&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Output to screen&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    fragColor &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec4&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ce qui nous donne un très joli levé de soleil dans la plaine 😁&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/colors.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Si vous voulez vous amuser avec voici le &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://www.shadertoy.com/view/WlGyDd&quot;&gt;code&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;le-ciel-les-oiseaux-et-ta-mere&quot;&gt;Le ciel les oiseaux et ta mère!&lt;/h2&gt;
&lt;p&gt;Comme vous pouvez le remarquer ( ou justement pas le remarquer 😋 ) notre sol à disparu !&lt;/p&gt;
&lt;p&gt;Cela est du au fait que notre plan est infini et récouvre l’intégralité de l’espace.&lt;/p&gt;
&lt;p&gt;On va donc devoir modifier un chouia notre code pour le faire apparaître de nouveau.&lt;/p&gt;
&lt;p&gt;On va conditionner l’application de notre matériel si et seulement si la distance de l’objet n’est pas supérieur à notre
distance d’affichage.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; mainImage&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt; out vec4 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragColor&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; in vec2 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragCoord&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On applique l&amp;#39;algorithme de raymarching à ce pixel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Data data &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; rayMarch&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;eye&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; rayDirector&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 col &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; le matériel est appliqué seulement si l&amp;#39;objet n&amp;#39;est pas trop loin&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; data&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span&gt; MAX_DISTANCE&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On récupère le matériel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        col &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; material&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Output to screen&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    fragColor &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec4&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ce qui nous donne:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/ciel1.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;On a bien le sol vert qui découpe du ciel noir, mais il y a une sorte d’aréole verte autour du demi-cercle rouge.&lt;/p&gt;
&lt;p&gt;On peut d’ailleurs amplifier le phénomène en diminuant le nombre d’itérations de notre raymarcher.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; MAX_ITERATIONS&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 20&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/ciel2.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Cela est dû au fait que le raymarcher n’atteint pas réellement la surface de l’objet du fait du faible nombre
d’itérations. Ce qui conduit à des erreurs d’appréciation des distances et donc des problème d’attribution de matériel.&lt;/p&gt;
&lt;p&gt;Pour avoir quelque chose de convenable on va au contraire augmenter ce nombre d’itérations.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; MAX_ITERATIONS&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 200&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/ciel3.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Et voilà on a un ciel !! 🥳&lt;/p&gt;
&lt;p&gt;Bon il est noir mais c’est temporaire on va le rendre bien plus beau par la suite !&lt;/p&gt;
&lt;p&gt;Le &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://www.shadertoy.com/view/3tyyDd&quot;&gt;code&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;les-normales&quot;&gt;Les normales&lt;/h2&gt;
&lt;p&gt;Une partie essentielle d’un rendu réaliste du scène 3D est d’être dans la capacité de déterminer les variations géométriques de la surface que tente de rendre.&lt;/p&gt;
&lt;p&gt;Une normal est un vecteur unitaire qui donne une indication sur la direction de l’extérieure de la surface.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/normal2.png&quot; alt=&quot;&quot; /&gt;
&lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://slideplayer.com/slide/3431756/&quot;&gt;crédit de l’image&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Chacun des vecteurs de l’image partent de la surface selon une certaine direction.&lt;/p&gt;
&lt;p&gt;Le but est donc de déterminer la direction de ce vecteur sur l’ensemble de la scène.&lt;/p&gt;
&lt;p&gt;Pour ceux qui s’en rappellent on va utiliser le même principe que lorsque l’on cherche la pente d’une courbe,
autrement dit sa dérivée.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/dx.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Calculer une dérivée revient à caculer une application en un point, se déplacer d’une petite distance recalculer l’application en ce nouveau point. Et faire la
la différence entre ces deux résultat.&lt;/p&gt;
&lt;p&gt;Dans le cadre d’une f(x), en 1D. &lt;code&gt;dx = f(x + h) - f(x)&lt;/code&gt; où h est le plus petit possible.&lt;/p&gt;
&lt;p&gt;On va généraliser la chose dans la 3ème dimension.&lt;/p&gt;
&lt;p&gt;Et caluler notre dérivé dans les 3 dimensions.&lt;/p&gt;
&lt;p&gt;Notre application dans le cadre du calcul de la normale à une surface en un point est tout simplement notre fonction &lt;code&gt;scene&lt;/code&gt; appliquée en un point.&lt;/p&gt;
&lt;p&gt;Pour se déplacer dans les x, y et z. On va définir h suffisamment petit permettant de nous déplacer sur la surface.&lt;/p&gt;
&lt;p&gt;Nous sommes surtout intéressés par la direction du vecteur et non pas sa norme.&lt;/p&gt;
&lt;p&gt;On normalise donc le vecteur composé des dx, dy, dz calculés.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;/**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* Calcul le vector normal d&amp;#39;un point de la scène&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* point : Point de la scène qui doit être calculé&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;vec3&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; normal&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;vec3 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;point&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    float&lt;/span&gt;&lt;span&gt; dp &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; scene&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;point&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;d&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec2 eps &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;001&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    float&lt;/span&gt;&lt;span&gt; dx &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; scene&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;point &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; eps&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;xyy&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span&gt; dp&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    float&lt;/span&gt;&lt;span&gt; dy &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; scene&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;point &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; eps&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;yxy&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span&gt; dp&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    float&lt;/span&gt;&lt;span&gt; dz &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; scene&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;point &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; eps&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;yyx&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span&gt; dp&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; normalize&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;vec3&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;dx&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; dy&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; dz&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;et modifie notre fonction d’affichage pour montrer le champ des normal:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; mainImage&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt; out vec4 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragColor&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; in vec2 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragCoord&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 col &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; le matériel est appliqué seulement si l&amp;#39;object n&amp;#39;est pas trop loin&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; data&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span&gt; MAX_DISTANCE&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On recalcule le point par rapport à la distance trouvée lors du calcul de raymarch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        vec3 point &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; eye &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+&lt;/span&gt;&lt;span&gt; rayDirector &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; data&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;d&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On récupère le matériel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        col &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; normal&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;point&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Output to screen&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    fragColor &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec4&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ce qui nous permet d’afficher cette image:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/normal.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Si on analyse un peu cette image, on peut tracer les normales que j’ai dessiné ici en violet.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/normal3.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Plus une surface est rouge plus son vecteur normal est orienté dans l’axe des x.&lt;/p&gt;
&lt;p&gt;Plus une surface est verte plus son vecteur normal est orienté dans l’axe des y.&lt;/p&gt;
&lt;p&gt;Il n’y a pas de bleu car dès que l’on quitte la surface on n’affiche pas de couleur.&lt;/p&gt;
&lt;p&gt;Les zones en noir correspondent à des composantes négatives, en effet une composante de couleur est
négative elle ramenée à 0.&lt;/p&gt;
&lt;p&gt;On remarque aussi que le sol est entièrement vert, c’est du au fait qu’il est entièrerement horizontal et donc
sa normal est purement vertical et donc de valeur (0, 1, 0).&lt;/p&gt;
&lt;p&gt;Le &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://www.shadertoy.com/view/3lGcDd&quot;&gt;code&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;et-la-lumiere-fut-sun-with-face&quot;&gt;Et la lumière fut ! 🌞&lt;/h2&gt;
&lt;p&gt;Maintenant que l’on a nos normales en chaque point de notre surface on va pouvoir calculer la contribution d’une
source lumineuse dans notre rendu de scène.&lt;/p&gt;
&lt;p&gt;Pour cela nous allons faire appel à loi de Snell-Descartes sur l’incidence de la lumière réfléchi.&lt;/p&gt;
&lt;p&gt;Ce que l’on désire c’est connaître l’intensité lumineuse reçu en chaque point de la scène.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/lights2.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;On trace un rayon de lumière entre le point de la surface qui doit être calculé et la source lumineuse.&lt;/p&gt;
&lt;p&gt;On normalise ce vecteur.&lt;/p&gt;
&lt;p&gt;Puis on réalise le produit scalaire entre ce vecteur direction et la normale à la surface en ce point.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;/**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* Calcul l&amp;#39;intensité lumineuse renvoyée par la scène en un point à l&amp;#39;oeil&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* point         : Point de la scène qui doit être calculé&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* normal        : Vecteur normal à la surface au point calculé&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* lightPosition : Position de la source de lumière dans l&amp;#39;espace&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; lighting&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;vec3 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;point&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; vec3 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;normal&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; vec3 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;lightPosition&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On calcule le vecteur de la direction de la lumière&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 lightDirection &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; lightPosition &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span&gt; point&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On normalise ce vecteur direction&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 lightNormal &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; normalize&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;lightDirection&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Puis l&amp;#39;intensité lumineuse&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; dot&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;lightNormal&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; normal&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Puis on modifie la fonction d’affichage pour montrer la contribution de la lumière dans la scène.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; mainImage&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt; out vec4 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragColor&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; in vec2 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragCoord&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 col &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; le matériel est appliqué seulement si l&amp;#39;object n&amp;#39;est pas trop loin&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; data&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span&gt; MAX_DISTANCE&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On recalcule le point par rapport à la distance trouvée lors du calcul de raymarch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        vec3 point &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; eye &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+&lt;/span&gt;&lt;span&gt; rayDirector &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; data&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;d&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On récupère le matériel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        vec3 normalVector &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; normal&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;point&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On calcule la lumière en un point de la scène&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        float&lt;/span&gt;&lt;span&gt; lightIntensity &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; lighting&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;point&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; normalVector&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; lightPosition&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Intensité lumineuse en niveau de gris&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        col &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;lightIntensity&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Output to screen&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    fragColor &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec4&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ce qui nous donne:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/lights.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Plus la surface est blanche plus la normale est alignée avec la source de lumière.&lt;/p&gt;
&lt;p&gt;Le &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://www.shadertoy.com/view/WlKcDd&quot;&gt;code&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;On va maintenant rendre notre scène en prenant en compte le matériel de la surface ainsi que la contribution lumineuse.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; mainImage&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt; out vec4 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragColor&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; in vec2 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragCoord&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; le matériel est appliqué seulement si l&amp;#39;object n&amp;#39;est pas trop loin&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; data&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span&gt; MAX_DISTANCE&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On recalcule le point par rapport à la distance trouvée lors du calcul de raymarch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        vec3 point &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; eye &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+&lt;/span&gt;&lt;span&gt; rayDirector &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; data&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;d&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On récupère le matériel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        vec3 normalVector &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; normal&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;point&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On calcule la lumière en un point de la scène&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        float&lt;/span&gt;&lt;span&gt; lightIntensity &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; lighting&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;point&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; normalVector&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; lightPosition&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On pondère le rendu du matériel avec la contribution lumineuse&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        col &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; material&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; *&lt;/span&gt;&lt;span&gt; lightIntensity&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Output to screen&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    fragColor &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec4&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et voici le résultat on a une jolie ombre réaliste pour le côté non exposé.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/lights3.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Le &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://www.shadertoy.com/view/ttVyWt&quot;&gt;code&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;ombre-et-lumiere-les-faces-d-une-meme-piece-first-quarter-moon-with-face&quot;&gt;Ombre et lumière les faces d’une même pièce 🌛&lt;/h2&gt;
&lt;p&gt;Notre scène a bien des ombres mais aucune ombre portée.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://www.pinclipart.com/picdir/big/15-150537_peter-pan-standing-with-his-shadow-peter-pan.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Donnons à notre scène un peu plus de réalisme.&lt;/p&gt;
&lt;p&gt;Pour cela il faut se demander la chose suivante: Qu’est-ce qu’une ombre ?&lt;/p&gt;
&lt;p&gt;La réponse évidente est une absence de lumière.&lt;/p&gt;
&lt;p&gt;Mais qu’est ce qui provoque cette absence de lumière ? La réponse est souvent un obstacle entre l’objet 3D
plongé dans l’ombre et la source de lumière.&lt;/p&gt;
&lt;p&gt;Pour déterminer s’il y a un obstacle entre un point de notre scène et une source lumineuse on va appliquer l’algorithme de
raymarching en utilisant comme paramètres:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;le point de la scène que l’on désire déterminer être dans l’ombre ou non&lt;/li&gt;
&lt;li&gt;le rayon directeur de la lumière calulé précédemment&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/shadow.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Le point rO de la scène est ici séparé de la source de lumière par un autre solide qui bloque la vue
du point rO.&lt;/p&gt;
&lt;p&gt;En appliquant l’algorithme de raymarching on va se retrouver au point C.&lt;/p&gt;
&lt;p&gt;Si la distance rO -&amp;gt; C est inférieure à la distance r0 -&amp;gt; S.&lt;/p&gt;
&lt;p&gt;Alors il y a un obstacle sur la route. L’intensité lumineuse en rO est donc nulle !&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; lighting&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;vec3 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;point&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; vec3 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;normal&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; vec3 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;lightPosition&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On calcule le vecteur de la direction de la lumière&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 lightDirection &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; lightPosition &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span&gt; point&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On normalise ce vecteur direction&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 lightNormal &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; normalize&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;lightDirection&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On caclule la distance qui sépare notre point de &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Data data &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; rayMarch&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;point&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; lightNormal&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Si la distance calculé est inférieure à la longueur du rayon &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; alors l&amp;#39;intensité lumineuse est nulle&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;data&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; length&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;lightDirection&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        return&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On calcule l&amp;#39;intensité lumineuse via le produit scalaire des deux vecteurs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; dot&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;lightNormal&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; normal&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/shadow2.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Oups! c’est pas trop ce qu’on voulait 😠&lt;/p&gt;
&lt;p&gt;Alors qu’est ce qui se passe ?&lt;/p&gt;
&lt;p&gt;Et bien on calcule le raymarching d’un point qui est lui même sur une surface de notre
scène, donc toutes les distances calculées sont nulles !!&lt;/p&gt;
&lt;p&gt;Pour corriger ce problème on va “décoller” notre point de calcul en se déplaçant très légèrement dans
la direction de la normal à notre surface.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Data data &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; rayMarch&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;point &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+&lt;/span&gt;&lt;span&gt; normal&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;01&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; lightNormal&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et finalment on atteint le résultat escompté 😁&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/shadow3.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Et on peut amplifier la chose avec une lumière plus rasante.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Source de lumière&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;vec3&lt;/span&gt;&lt;span&gt; lightPosition &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; vec3&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;5&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/shadow4.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Le &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://www.shadertoy.com/view/WlcBRr&quot;&gt;code&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;correction-colorimetrique&quot;&gt;Correction colorimétrique&lt;/h2&gt;
&lt;p&gt;Tout capteur a ses défaut et l’oeil ne fait pas exception.&lt;/p&gt;
&lt;p&gt;Le sujet est passionnant et je vous redirige vers cet &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://www.benq.eu/fr-fr/knowledge-center/knowledge/gamma-monitor.html&quot;&gt;article&lt;/a&gt; pour en savoir plus.&lt;/p&gt;
&lt;p&gt;Ce qui est à retenir c’est ce tableau-ci:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/gamma.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;La première ligne du tableau représente ce que l’on devrait voir avec un capteur linéaire et donc oeil parfait.&lt;/p&gt;
&lt;p&gt;La seconde ce qu’un oeil distingue réellement. Il a tendance a mieux distinguer les hautes intensités
lumineuses et être moins performant dans des conditions de plus faible luminostité.&lt;/p&gt;
&lt;p&gt;Si on représente c’est valeurs sous la forme d’un graphique:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/gamma2.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Le but va être alors de corriger ce manque précision dans les noirs pour rendre plus réelle notre rendu des couleurs.&lt;/p&gt;
&lt;p&gt;On appelle ce coefficient d’erreur dans la colorimétrie le gamma de l’image. Que l’on note γ&lt;/p&gt;
&lt;p&gt;Les données nous donne une valeur de &lt;code&gt;γ&lt;/code&gt; de 2.2.&lt;/p&gt;
&lt;p&gt;La courbe ainsi tracée à pour formule &lt;code&gt;x^γ&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Pour revenir à une dynamique de constraste linénaire on va réaliser son symétrique par rapport l’axe &lt;code&gt;f(x) = x&lt;/code&gt;, autrement dit
&lt;code&gt;f(x) = x ^ (1/γ)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/gamma3.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Bon maintenant que l’on a la théorie place à la pratique!&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; mainImage&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt; out vec4 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragColor&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; in vec2 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragCoord&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Définition du gamma de l&amp;#39;image&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    float&lt;/span&gt;&lt;span&gt; gamma &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;2&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On applique la correction du gamma de chaque de couleur du pixel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    col &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; pow&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;gamma&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Output to screen&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    fragColor &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec4&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ce qui nous donne ce résultat:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/gamma4.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Comme toujours le &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://www.shadertoy.com/view/wt3Bzr&quot;&gt;code&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;de-meilleures-couleurs&quot;&gt;De meilleures couleurs&lt;/h2&gt;
&lt;p&gt;On a des couleurs, de la lumière des ombres portées et même une correction du gamma de notre image.&lt;/p&gt;
&lt;p&gt;Mais qu’est ce que nos couleurs sont moches ! 🤢&lt;/p&gt;
&lt;p&gt;On va d’abord un peu redéfinir notre dictionnaire de matériaux.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; MATERIAL_SPHERE&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; MATERIAL_FLOOR&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;  2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; MATERIAL_SKY&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;    3&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ensuite il nous faut une palette de couleur cohérente. J’ai découvert très récemment le site &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://coolors.co/0a9dff-ffba49-594236&quot;&gt;coolors&lt;/a&gt; qui m’en fourni une.&lt;/p&gt;
&lt;p&gt;Le petit soucis c’est que notre définition des couleurs de pixel est un peu spéciale.&lt;/p&gt;
&lt;p&gt;En effet les composante de couleur vont de 0 à 1 dans notre système et de 0 à 255 sur coolors.&lt;/p&gt;
&lt;p&gt;Le deuxième soucis est qu’ils ne prennent pas en compte les aberrations dûes au gamma de l’image.&lt;/p&gt;
&lt;p&gt;si on se contente d’écrire:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;vec3&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; material&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; index&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 material&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    switch&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;index&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        case&lt;/span&gt;&lt;span&gt; MATERIAL_SPHERE&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            material &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;89&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 66&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 54&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        case&lt;/span&gt;&lt;span&gt; MATERIAL_FLOOR&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            material &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;255&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 186&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 73&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        case&lt;/span&gt;&lt;span&gt; MATERIAL_SKY&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            material &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;10&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 157&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 255&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        default&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            material &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    return&lt;/span&gt;&lt;span&gt; material&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On va obtenir des couleur qui sont un peu plus fades. Qui sont très belle au demeurant :)&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/dynamic.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Si on veut exactement les couleurs que l’on a dans la palette choisie, il va falloir opérer à des ajustements.&lt;/p&gt;
&lt;p&gt;J’ai écrit un petit &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://glot.io/snippets/fvg3i24qbx&quot;&gt;script&lt;/a&gt; qui permet de “corriger” les couleurs.&lt;/p&gt;
&lt;p&gt;En remplaçant les valeurs trouvée.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;/**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* Renvoie le matériel associé à l&amp;#39;index ou du noir si l&amp;#39;index est inconnu&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;* m : index du matériel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;vec3&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; material&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; index&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 material&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    switch&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;index&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        case&lt;/span&gt;&lt;span&gt; MATERIAL_SPHERE&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            material &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;98&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;69&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 51&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;12&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 32&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;88&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        case&lt;/span&gt;&lt;span&gt; MATERIAL_FLOOR&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            material &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;00&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 499&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;51&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 63&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;81&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        case&lt;/span&gt;&lt;span&gt; MATERIAL_SKY&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            material &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;80&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 344&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;03&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1000&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;00&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        default&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            material &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    return&lt;/span&gt;&lt;span&gt; material &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1000&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On obtient:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/dynamic2.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Et c’est bien plus beau 😎&lt;/p&gt;
&lt;p&gt;Le &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://www.shadertoy.com/view/tl3Bzr&quot;&gt;code&lt;/a&gt; associé au chapitre.&lt;/p&gt;
&lt;h2 id=&quot;un-ciel-legerement-plus-realiste&quot;&gt;Un ciel légèrement plus réaliste&lt;/h2&gt;
&lt;p&gt;Dans la vraie vie le ciel n’est jamais bleu uni comme cela, ça se rapproche plutôt d’un dégradé comme celui
ci:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/distant-horizon.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;On va donc essayer de mettre en place ceci pour notre scène.&lt;/p&gt;
&lt;p&gt;Tout d’abord, l’on va définir non pas une couleur de ciel, mais deux, une pour la couleur sombre du dégradé
et une deuxième pour la couleur claire du dégradé.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; MATERIAL_SPHERE&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;      1&lt;/span&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; MATERIAL_FLOOR&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;       2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; MATERIAL_SKY_LIGHT&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;   3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; MATERIAL_SKY_DARK&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;    4&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ensuite nous allons utiliser une fonction d’interpolation linéaire appelée &lt;code&gt;mix&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Elle prend 3 paramètres:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;le vecteur x     : couleur claire du dégradé&lt;/li&gt;
&lt;li&gt;le vecteur y     : couleur sombre du dégradé&lt;/li&gt;
&lt;li&gt;le coefficient a : coefficient à appliquer&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Et renvoie un vecteur de sortie s.&lt;/p&gt;
&lt;p&gt;La formule appliquée sur chaque composante des vecteurs est &lt;code&gt;s = (1- a) * x + a * y&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Pour générer notre dégradé qui va de notre couleur claire vers notre couleur sombre, on va devoir définir notre
coefficient en fonction des uv.y de notre image.&lt;/p&gt;
&lt;p&gt;Pour rappel:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/shaders/quadrant.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Le centre de l’image à une valeur d’uv de (0,0) et le coin supérieur droit(1, 1).&lt;/p&gt;
&lt;p&gt;Nous allons donner à notre gradient cette courbe de coefficient:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/gradient.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;La formule est &lt;code&gt;a(y) = (y + 0.5) ^ 1.5&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Et maintenant le code!&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; mainImage&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt; out vec4 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragColor&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; in vec2 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragCoord&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Calcule des UV de l&amp;#39;écran&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec2 uv &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;fragCoord&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;iResolution&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;xy&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; iResolution&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;y&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; ciel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 skyColDark  &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; material&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;MATERIAL_SKY_DARK&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 skyColLight &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; material&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;MATERIAL_SKY_LIGHT&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 col &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; mix&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;skyColLight&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; skyColDark&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; pow&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;uv&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ....&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Output to screen&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    fragColor &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec4&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ce qui donne:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/sky.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Beaucoup mieux! 😃&lt;/p&gt;
&lt;p&gt;Il reste une petite chose à traiter. La lumière est réfléchi par les couches de l’atmosphère et redirigé
vers le sol.&lt;/p&gt;
&lt;p&gt;Dans un but de simplification on va considéré que cette lumière venant du ciel est purement vertical (ce qui n’est évidemment pas vrai).&lt;/p&gt;
&lt;p&gt;On va définir une composant de couleur supplémentaire que l’on va additionner au lumière calculées.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Définition de la couleur venant du ciel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;vec3&lt;/span&gt;&lt;span&gt; ambiantSky &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; normalVector.y &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;*&lt;/span&gt;&lt;span&gt; skyColLight&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On pondère le rendu du matériel avec la contribution lumineuse&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;col &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; material&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;data.m&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; *&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;lightIntensity &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+&lt;/span&gt;&lt;span&gt; ambiantSky&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et voici le rendu finale de notre scène!!&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/sky2.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Le &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://www.shadertoy.com/view/3ttBRr&quot;&gt;code&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;On est passé de&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/raymarch/scene.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;à&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/lights/sky2.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Quelle aventure cette article 😁.  On a appris à:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;appliquer un matériel sur un objet de notre scène.&lt;/li&gt;
&lt;li&gt;calculer les lumières de manière réaliste&lt;/li&gt;
&lt;li&gt;calculer les ombres associées&lt;/li&gt;
&lt;li&gt;gérer la colorimétrie et le gamma de notre image&lt;/li&gt;
&lt;li&gt;et enfin faire un ciel digne de ce nom !&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Dans la prochaine partie on verra comment implémenter une caméra dans ce monde 3D un peu plus perfectionnée que maintenant.&lt;/p&gt;
&lt;p&gt;Je vous remercie de votre lecture et je vous dis à la prochaine pour de nouveaux articles, sur les shaders ou un autre sujet. 💗&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="fr">
        <title>Initiation aux shaders : RayMarching (Partie 2)</title>
        <published>2020-12-26T00:00:00+00:00</published>
        <updated>2020-12-26T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://staging.lafor.ge/shader-2/"/>
        <id>https://staging.lafor.ge/shader-2/</id>
        
        <content type="html" xml:base="https://staging.lafor.ge/shader-2/">&lt;p align=&quot;center&quot;&gt;
  &lt;img src=&quot;https://media.giphy.com/media/vzX4OAfFKhpzG/giphy.gif&quot; alt=&quot;&quot;/&gt;
&lt;/p&gt;
&lt;p&gt;On a vu dans la partie &lt;a href=&quot;/shader-1&quot;&gt;précédente&lt;/a&gt; comment manipuler les pixels d’une image grâce au langage de
shader.&lt;/p&gt;
&lt;p&gt;Il est temps de s’attaquer à du bien plus intéressant: la 3D ! 😎&lt;/p&gt;
&lt;h2 id=&quot;l-algorithme-de-raymarching&quot;&gt;L’algorithme de RayMarching&lt;/h2&gt;
&lt;p&gt;Littéralement “marché de rayon”.&lt;/p&gt;
&lt;p&gt;L’idée est très élégante. Elle se base entièrement sur le principe des SDF (Signed Distance Field).&lt;/p&gt;
&lt;p&gt;On a comme situation de départ un oeil situé au centre de l’écran mais reculé d’une certaine distance.&lt;/p&gt;
&lt;p&gt;La situation est représentée vu de haut.&lt;/p&gt;
&lt;p&gt;On veut afficher notre objet en bleu.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/raymarch/etape1.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;On choisi un pixel sur notre écran.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/raymarch/etape2.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;On détermine un rayon directeur entre notre oeil et le pixel.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/raymarch/etape3.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Grâce à la SDF de notre solide on détermine la distance de l’objet par rapport à notre oeil et on trace
un cercle du rayon de la distance nous séparant de l’objet. Autrement dit le point du solide le plus proche de
l’oeil.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/raymarch/etape4.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Puis on trace un rayon infini partant de l’oeil et dans la direction du vecteur. Le point d’intersection &lt;code&gt;M&lt;/code&gt; entre
le cercle que l’on vient de tracer devient notre nouveau point de départ.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/raymarch/etape5.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;On réitère l’opération de ce point &lt;code&gt;M&lt;/code&gt; comme oeil.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/raymarch/etape6.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;On récupère un point &lt;code&gt;O&lt;/code&gt; et rebelote.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/raymarch/etape7.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Lorsque le diamètre du cercle obtenu est suffisamment petit, on considère que l’on a touché
l’objet.&lt;/p&gt;
&lt;p&gt;On somme l’ensemble des distances trouvées précédemment et l’on retourne la somme.&lt;/p&gt;
&lt;p&gt;Il existe le cas ou le rayon ne touche pas l’objet alors le diamètre du cercle va être trop
grand.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/raymarch/etape8.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Si la distance est trop grande on la renvoie la distance maximale.&lt;/p&gt;
&lt;p&gt;Ce qui est important de comprendre c’est que l’on va efectué cette algorithme un très grand nombre
de fois et plusieurs fois par frame. Autant optimiser tout ce qui peut l’être. 😉&lt;/p&gt;
&lt;h2 id=&quot;implementation&quot;&gt;Implémentation&lt;/h2&gt;
&lt;p&gt;Pour nous faciliter la vie on va définir quelques constantes:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; MIN_DISTANCE&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;001&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; MAX_DISTANCE&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 20&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; MAX_ITERATIONS&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 50&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Pui on implémente l’algorithme de raymarching en lui-même.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; rayMarch&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;vec3 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;rayOrigin&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; vec3 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;rayDirector&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Initialisation des distances&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    float&lt;/span&gt;&lt;span&gt; accumulatedDistance &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    float&lt;/span&gt;&lt;span&gt; computedDistance &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 currentPoint&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Boucle de l&amp;#39;algorithme d&amp;#39;au plus MAX_ITERATIONS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    for&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;int&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt; MAX_ITERATIONS&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;++&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Calcul du point courant&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        currentPoint &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; rayOrigin &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+&lt;/span&gt;&lt;span&gt; rayDirector &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;*&lt;/span&gt;&lt;span&gt; accumulatedDistance&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Calcul de la distance du point courant par rapport à une sphère&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; de rayon 0.5 et décalé de 0.5 vers la droite et avancé de 1.8 de l&amp;#39;écran&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        computedDistance &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; length&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;currentPoint &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;5&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On accumule la distance trouvée&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        accumulatedDistance &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+=&lt;/span&gt;&lt;span&gt; computedDistance&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Si la distance trouvé est suffisamment faible on stoppe l&amp;#39;algorithme&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt; computedDistance &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt; MIN_DISTANCE &lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Si la distance trouvée est trop grange on retourne la distance&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        if&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt; accumulatedDistance &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; MAX_DISTANCE &lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            return&lt;/span&gt;&lt;span&gt; MAX_DISTANCE&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    return&lt;/span&gt;&lt;span&gt; accumulatedDistance&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Puis on calcule les paramètres de notre algorithme&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Calcule des UV de l&amp;#39;écran&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    vec2&lt;/span&gt;&lt;span&gt; uv &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;fragCoord&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;*&lt;/span&gt;&lt;span&gt;iResolution.xy&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; /&lt;/span&gt;&lt;span&gt; iResolution.y&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; La caméra est au centre du monde&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    vec3&lt;/span&gt;&lt;span&gt; eye &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; vec3&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; L&amp;#39;écran est décalé de 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    vec3&lt;/span&gt;&lt;span&gt; pixel &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; vec3&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;uv&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Calcul du rayon directeur de l&amp;#39;oeil traversant le pixel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    vec3&lt;/span&gt;&lt;span&gt; rayDirector &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; normalize&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;pixel &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span&gt; eye&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Le rayon directeur doit-être unitaire. D’où l’utilisation de la méthode
&lt;code&gt;normalize&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Pour rappel les UVs sont des coordonnées dont l’origine est au centre
de l’écran et dont les coordonnées sont transformé pour que l’axe des Y
dans un ratio 16:9 soit situés entre [-1, 1].&lt;/p&gt;
&lt;p&gt;On applique en suite l’algorithme de raymarching avec les paramètres calculés.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On applique l&amp;#39;algorithme de raymarching à ce pixel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    float&lt;/span&gt;&lt;span&gt; d &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; rayMarch&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;eye&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; rayDirector&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On désire afficher cette distance sous la forme d’une composante
de couleur. On doit donc compresser les distances pour les faire
rentrer dans l’intervalle &lt;code&gt;[0, 1]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Et on affiche sous forme de niveau de gris notre sphere.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On tasse les distances pour les faire rentrer dans l&amp;#39;intervalles [0, 1]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    d &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;/=&lt;/span&gt;&lt;span&gt; MAX_DISTANCE&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On affiche&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    fragColor &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; vec4&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;d&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; d&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; d&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/raymarch/sphere.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Et cette fois-ci on affiche une sphère et non pas un disque! Bienvenu dans la 3ème dimension !! 😎&lt;/p&gt;
&lt;p&gt;Pour s’en convaincre on peut booster le contraste et la luminosité de l’image.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/raymarch/sphere2.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;On remarque des cercles concentriques décalés vers la gauche. Plus le disque est sombre plus il est proche d’une
distance de 0.&lt;/p&gt;
&lt;p&gt;On est bien en face d’un objet dans la 3ème dimension.&lt;/p&gt;
&lt;h2 id=&quot;plusieurs-objets-dans-notre-scene&quot;&gt;Plusieurs objets dans notre scène&lt;/h2&gt;
&lt;p&gt;Notre monde est un peu vide, il est temps de le remplir.&lt;/p&gt;
&lt;h3 id=&quot;dessiner-un-plan-horizontal&quot;&gt;Dessiner un plan horizontal&lt;/h3&gt;
&lt;p&gt;Un plan horizontal est défini par l’ensemble des points qui sont à la même hauteur.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/raymarch/plan.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;On défini la SDF du plan&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; horizontalPlan&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; height&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; vec3 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;point&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; point&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span&gt; height&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Elle prend en paramètre la hauteur du plan par rapport
au plan XZ de notre monde. Ainsi que le point dont on cherche la
distance par rapport au plan.&lt;/p&gt;
&lt;p&gt;Pour tester notre code on va opérer une petite modification.&lt;/p&gt;
&lt;p&gt;On va définir la SDF de ce plan.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; horizontalPlan&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; height&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; vec3 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;point&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; point&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span&gt; height&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Puis une fonction &lt;code&gt;scene&lt;/code&gt; qui prend en compte le point dont
lequel on désire connaître la distance par rapport à la scène.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; scene&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;vec3 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;point&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Un plan 0.2 unité plus bas que le plan XZ&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; horizontalPlan&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; point&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On va par la même occassion se définir une SDF pour la sphère.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; sphere&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;vec3 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;center&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; vec3 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;point&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; float&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; rayon&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; length&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;point &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; center&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span&gt; rayon&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et on modifie la fonction de raymarching&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; rayMarch&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;vec3 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;rayOrigin&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; vec3 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;rayDirector&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Initialisation des distances&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    float&lt;/span&gt;&lt;span&gt; accumulatedDistance &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    float&lt;/span&gt;&lt;span&gt; computedDistance &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 currentPoint&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Boucle de l&amp;#39;algorithme&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    for&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;int&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt; MAX_ITERATIONS&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;++&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Calcul du point courant&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        currentPoint &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; rayOrigin &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+&lt;/span&gt;&lt;span&gt; rayDirector &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;*&lt;/span&gt;&lt;span&gt; accumulatedDistance&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Calcule de la distance du point courant par rapport à la scene&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        computedDistance &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; scene&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;currentPoint&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; On accumule la distance trouvée&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        accumulatedDistance &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;+=&lt;/span&gt;&lt;span&gt; computedDistance&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Si la distance trouvé est suffisamment faible on stoppe l&amp;#39;algorithme&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt; computedDistance &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt; MIN_DISTANCE &lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Si la distance trouvée est trop grange on retourne la distance&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;        if&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt; accumulatedDistance &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; MAX_DISTANCE &lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;            return&lt;/span&gt;&lt;span&gt; MAX_DISTANCE&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    return&lt;/span&gt;&lt;span&gt; accumulatedDistance&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ce qui nous permet d’afficher ceci 😃&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/raymarch/plan2.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;On obtient bien ce qui est attendu. la ligne d’horizon tend bien vers le plan
car elle se trouve à une distance trop importante.&lt;/p&gt;
&lt;h3 id=&quot;afficher-plus-d-un-element&quot;&gt;Afficher plus d’un élément.&lt;/h3&gt;
&lt;p&gt;Pour afficher plus d’un élément il faut se représenter les choses ainsi&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/raymarch/union.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Partant du point P et suivant ce rayon précis la sphère est plus proche
de P que le plan (orange).&lt;/p&gt;
&lt;p&gt;Autrement dit la SDF de la sphère va renvoyer une distance plus faible que
le plan.&lt;/p&gt;
&lt;p&gt;Mathématiquement ce concept peut-être représenté par le minimum des deux
distances.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; scene&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;vec3 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;point&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    float&lt;/span&gt;&lt;span&gt; dSphere &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; sphere&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;vec3&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; point&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; .&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    float&lt;/span&gt;&lt;span&gt; dPlan &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; horizontalPlan&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; point&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; min&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;dSphere&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; dPlan&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ce qui nous permet d’afficher les deux éléments.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/raymarch/scene.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Mais il manque quelque chose…&lt;/p&gt;
&lt;p&gt;De la couleur ! 🤡&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;On a appris à afficher très sommairement des solides dans un environnement
3D. Et sans bibliothèques complexes.&lt;/p&gt;
&lt;p&gt;La prochaine fois on rajoutera de la couleur, des ombres et de la lumière!&lt;/p&gt;
&lt;p&gt;Je vous donne aussi le lien de mon &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://www.shadertoy.com/view/ttdyW8&quot;&gt;shadertoy&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Je vous remercie de m’avoir lu 😀.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="fr">
        <title>Initiation aux shaders (Partie 1)</title>
        <published>2020-12-24T00:00:00+00:00</published>
        <updated>2020-12-24T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://staging.lafor.ge/shader-1/"/>
        <id>https://staging.lafor.ge/shader-1/</id>
        
        <content type="html" xml:base="https://staging.lafor.ge/shader-1/">&lt;p align=&quot;center&quot;&gt;
  &lt;img src=&quot;https://media.giphy.com/media/3ZZbooUEo4ScjZdOoO/giphy.gif&quot; alt=&quot;la guerre&quot;/&gt;
&lt;/p&gt;
&lt;p&gt;Bon je vais pas vous mentir j’ai à peu près 3 jours d’expérience sur le sujet, voyez plutôt ça comme une sorte de cahier de route de ma découverte du
domaine.&lt;/p&gt;
&lt;p&gt;J’ai trouvé deux outils pour faciliter mon initiation:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://thebookofshaders.com&quot;&gt;The book of shaders&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://www.shadertoy.com/new&quot;&gt;ShaderToy&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Le premier est un peu la bible du débutant, le second permet d’expériencer facilement ce que l’on apprend.&lt;/p&gt;
&lt;p&gt;En route pour l’aventure 🐱‍👓&lt;/p&gt;
&lt;h2 id=&quot;hello-world-sauce-shader&quot;&gt;Hello world sauce shader&lt;/h2&gt;
&lt;p&gt;Ce qui déroute en premier lorsque l’on lance shadertoy, c’est l’absence de ce que l’on connait d’habitude.&lt;/p&gt;
&lt;p&gt;Le &lt;code&gt;main&lt;/code&gt; est déjà très étrange:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; mainImage&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt; out vec4 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragColor&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; in vec2 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragCoord&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Cela ressemble vaguement à du C mais il y des différences. Le main qui s’applle &lt;code&gt;mainImage&lt;/code&gt; prend deux paramètres:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;out vec4 fragColor&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;in vec2 fragCood&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Beaucoup de choses déjà dans ces deux paramètres. D’abord les mots-clefs &lt;code&gt;in/out&lt;/code&gt;. Ils agissent comme des sas
entre le code que l’on écrit et le code qui appelle le code que l’on écrit.&lt;/p&gt;
&lt;h3 id=&quot;operateur-de-liaison&quot;&gt;Opérateur de liaison&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;in&lt;/code&gt; défini une variable qui peut-être modifié par notre code sans pouvoir impacter le code appelant, en forçant la recopie de la variable&lt;/li&gt;
&lt;li&gt;&lt;code&gt;out&lt;/code&gt; défini une variable qui n’est pas initialisée par le code appelant mais qui peut être utilisé par celui-ci à l’issue de l’éxécution de la fonction.
C’est dans les faits une référence.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;les-vecteurs&quot;&gt;Les vecteurs&lt;/h3&gt;
&lt;p&gt;Ensuite vient les termes &lt;code&gt;vec4&lt;/code&gt; et &lt;code&gt;vec2&lt;/code&gt;, ce sont des structures de données semblables à des tableaux de &lt;code&gt;float&lt;/code&gt; (toujours des floats), mais boosté aux stéroïdes.
Il existe en effet tout une collection de facilté d’écriture.&lt;/p&gt;
&lt;p&gt;Il est par exemple possible de déclarer un &lt;code&gt;vec2&lt;/code&gt; ainsi:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;vec2&lt;/span&gt;&lt;span&gt; pos &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; vec2&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et accéder à ses composantes de cette manière:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;float&lt;/span&gt;&lt;span&gt; x &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; pos.x&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; 1.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;float&lt;/span&gt;&lt;span&gt; y &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; pos.y&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; 2.0&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On peut aussi opérer des inversions de composantes&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;vec2&lt;/span&gt;&lt;span&gt; inverted_pos &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; pos.yx&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; (2.0, 1.0) &lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Il est aussi possible de faire de la récupération partielle de composantes&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;vec3&lt;/span&gt;&lt;span&gt; pos &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; vec3&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 3&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;vec2&lt;/span&gt;&lt;span&gt; pos2 &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; pos.xz&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; (2.0, 3.0)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ou même dupliquer des composantes&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;vec3&lt;/span&gt;&lt;span&gt; pos &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; vec3&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 3&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;vec3&lt;/span&gt;&lt;span&gt; pos2 &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; pos.xxx&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; (1.0, 1.0, 1.0)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Dernier outil que je connais, il est possible de ne pas complètement définir un vecteur&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;vec3&lt;/span&gt;&lt;span&gt; pos &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; vec3&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; (0.0, 0.0, 0.0)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;vec3&lt;/span&gt;&lt;span&gt; pos2 &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; vec3&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; (4.0, 4.0, 4.0)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;les-nom-de-variables-du-main&quot;&gt;Les nom de variables du main&lt;/h3&gt;
&lt;p&gt;Avant de parler de tout ça une petite remise en contexte est nécessaire.&lt;/p&gt;
&lt;p&gt;Le &lt;code&gt;mainImage&lt;/code&gt; n’est pas vraiment le &lt;code&gt;main&lt;/code&gt; du programme. Un shader est une routine qui s’éxécute sur
chaque pixels d’une image.&lt;/p&gt;
&lt;p&gt;Pour rappel, un pixel est la plus petite unité qui compose une image, pixel ( picture element ).&lt;/p&gt;
&lt;p&gt;Afin de rendre une image on ne peut décemment pas traiter un à un les pixels ça serait beaucoup trop long et gourmand en ressources,
surtout avec les résolutions d’aujourd’hui qui sont gigantesques comme la 4k et la 8k.&lt;/p&gt;
&lt;p&gt;L’idée qui a été choisi est de paralléliser les traitements de chacun des pixels.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/shaders/tamis.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Cela implique que le traitement entre deux pixels est totalement indépendant car il doit s’effectuer au même
moment.&lt;/p&gt;
&lt;p&gt;Un pixel est composé de 3 lampes, une rouge, une bleue et une verte. En fonction
de la valeur de chaque composante on est capable de générer n’importe quelle couleur du spectre visible.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/shaders/pixels.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;h4 id=&quot;couleur-du-pixel&quot;&gt;Couleur du pixel&lt;/h4&gt;
&lt;p&gt;Le but de notre routine est de définir l’intensité des lampes rouge, verte et bleue.&lt;/p&gt;
&lt;p&gt;Le résultat de notre calcul doit être affecté dans la variable &lt;code&gt;fragColor&lt;/code&gt;. Celle-la est un &lt;code&gt;vec4&lt;/code&gt;, cela signifie
qu’elle possède 4 composantes: (Rouge, Vert, Bleu, ?).&lt;/p&gt;
&lt;p&gt;La dernière est le canal alpha qui gère la transparence. On ne s’en occupera pas dans cette partie.&lt;/p&gt;
&lt;p&gt;Dernière chose à savoir est que chacune de ces composantes se répartissent entre 0 ( pas de lumière du tout ) et 1 ( intensité maximale ).&lt;/p&gt;
&lt;p&gt;Le code ci-dessous explique de renvoyer pour chaque pixel de l’image un vecteur &lt;code&gt;(1.0, 0.0, 0.0, 1.0)&lt;/code&gt; autrement dit
du rouge pur. La 4ème composante est à &lt;code&gt;1.0&lt;/code&gt; pour signifier qu’il n’y a pas de transparence.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; mainImage&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt; out vec4 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragColor&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; in vec2 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragCoord&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Output to screen&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    fragColor &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec4&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/shaders/rouge.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Mais aussi du vert&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;fragColor &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; vec4&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/shaders/vert.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Mais aussi du bleu&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;fragColor &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; vec4&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/shaders/bleu.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Mais aussi du jaune&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;fragColor &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt; vec4&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/shaders/jaune.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Bref vous voyez l’idée. Notre routine contrôle chaque pixel de l’écran qui mis tous ensemble forme l’image finale.&lt;/p&gt;
&lt;h4 id=&quot;la-position-du-pixel&quot;&gt;La position du pixel&lt;/h4&gt;
&lt;p&gt;Bon tout ça c’est très bien mais un peu limité tous les pixels ont la même couleur on est pas près de faire le nouveau Pixar à la mode. 😋&lt;/p&gt;
&lt;p&gt;C’est là qu’intervient notre second paramètre &lt;code&gt;fragCoord&lt;/code&gt; il s’agit là aussi d’un &lt;code&gt;vec2&lt;/code&gt;, la première composante est l’abscisse du
pixel sur l’écran et la deuxième son ordonnée dans la pratique (x, y).&lt;/p&gt;
&lt;p&gt;Essayons de visualiser cela 🧐&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; mainImage&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt; out vec4 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragColor&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; in vec2 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragCoord&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Output to screen&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    fragColor &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec4&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;fragCoord&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; ,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/shaders/rouge.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Rouge, encore! Mais c’est normal quand on y réfléchi, les coordonnées des pixels sont données sous leur forme
cartésiennes, c’est à dire des nombres entiers positifs.&lt;/p&gt;
&lt;p&gt;Or les composantes de couleur ne peuvent pas dépasser la valeur &lt;code&gt;1.0&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Il va donc falloir normaliser les valeurs. Pour cela on va utiliser un autre outils mis à disposition par
le langage de shader qui nous fourni tout une série de variable appelée des &lt;code&gt;uniforms&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/shaders/uniform.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Il faut les voir comme le moyens de communiquer des informations entre les différentes unité de traitement de pixels.
Celle qui va nous intéresser ici est &lt;code&gt;iResolution&lt;/code&gt;. Elle contient la taille en pixels de l’écran et donc le nombre en X et en Y
de pixels.&lt;/p&gt;
&lt;p&gt;Ce qui va nous permettre d’effectuer une règle de trois entre la position du pixel et la résolution.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;iResolution.x       -&amp;gt; rouge pur (1.0)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;fragCoord.x         -&amp;gt; intensité de rouge  (X)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;X = (fragCoord.x * 1.0) / iResolution.x&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;En utilisant les propriétés des vecteurs:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; mainImage&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt; out vec4 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragColor&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; in vec2 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragCoord&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec2 uv &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; fragCoord &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; iResolution&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;xy&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Output to screen&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    fragColor &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec4&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;uv&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; ,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/shaders/gradient_x.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;De même en Y&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; mainImage&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt; out vec4 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragColor&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; in vec2 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragCoord&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec2 uv &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; fragCoord &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; iResolution&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;xy&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Output to screen&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    fragColor &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec4&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;uv&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; ,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/shaders/gradient_y.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;On peut même cartographier les deux composantes&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; mainImage&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt; out vec4 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragColor&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; in vec2 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragCoord&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec2 uv &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; fragCoord &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; iResolution&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;xy&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Output to screen&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    fragColor &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec4&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;uv&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; uv&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; ,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/shaders/gradient_xy.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Le noir est une absence de luminosité autrement dit &lt;code&gt;(0.0, 0.0, 0.0, 1.0)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Le jaune une combinaison de rouge et de vert, le jaune pur représente &lt;code&gt;(1.0, 1.0, 0.0, 1.0)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;En prenant la formule à l’envers on peut déterminer l’origine du repère de notre écran ainsi que le sens du repère.&lt;/p&gt;
&lt;p&gt;Pour une résolution de 800x450, il vient:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/shaders/gradient_xy_coord.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;h2 id=&quot;changement-de-repere&quot;&gt;Changement de repère&lt;/h2&gt;
&lt;p&gt;Bon je sais pas vous mais le repère en bas à gauche n’a jamais été ma tasse de thé ou de café pour les amateurs.&lt;/p&gt;
&lt;p&gt;Je préfère que l’origine de mon repère soit le centre de mon écran, cela va faciliter grandement les calculs qui vont suivre.
(oui il va y avoir un max de maths 🤓).&lt;/p&gt;
&lt;p&gt;Pour mettre le centre au centre on va retrancher la moitié de la résolution sur chacune des composantes.&lt;/p&gt;
&lt;p&gt;Cette soustraction va automatiquement nous amener à obtenir des valeurs négatives. Une composante de couleur étant toujours
positive, une valeur négative sera mise à &lt;code&gt;0.0&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; mainImage&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt; out vec4 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragColor&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; in vec2 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragCoord&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec2 uv &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; fragCoord &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; iResolution&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;xy&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Output to screen&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    fragColor &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec4&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;uv&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; uv&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; ,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On se retrouve avec 4 quadrants:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/shaders/quadrant.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Que l’on peut avec la même technique que tout à l’heure visualiser de manière plus précise sous la forme
d’un continuum.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; mainImage&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt; out vec4 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragColor&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; in vec2 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragCoord&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec2 uv &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;fragCoord &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; iResolution&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;xy&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; /&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;iResolution&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;xy&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Output to screen&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    fragColor &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec4&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;uv&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; uv&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; ,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/shaders/quadrant_continuum.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;h2 id=&quot;dessiner-des-formes-simples&quot;&gt;Dessiner des formes simples&lt;/h2&gt;
&lt;h3 id=&quot;le-disque&quot;&gt;Le disque&lt;/h3&gt;
&lt;p&gt;C’est la forme la plus simple à afficher.&lt;/p&gt;
&lt;p&gt;Son équation dans le plan cartésien est :&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;x² + y² &amp;lt; R²&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Comme on a centré le repère, il n’y a pas besoin de se soucier
d’offset.&lt;/p&gt;
&lt;p&gt;en prenant la racine de chaque côté&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;sqrt(x² + y²) &amp;lt; R&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Le langage fourni une fonction appellée &lt;code&gt;length&lt;/code&gt; qui permet de récupérer la norme d’un vecteur.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; mainImage&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt; out vec4 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragColor&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; in vec2 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragCoord&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec2 uv &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;fragCoord &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; iResolution&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;xy&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; /&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;iResolution&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;xy&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Output to screen&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    fragColor &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec4&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;uv&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; uv&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; ,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;uv&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        fragColor &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec4&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; ,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/shaders/ellipse.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Bon c’est presque ça, on a juste négligé le ratio de notre image. Pour rétablir le disque on peut diviser nos coordonnées par la résolution en Y sur 2.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; mainImage&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt; out vec4 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragColor&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; in vec2 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragCoord&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec2 uv &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;fragCoord &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; iResolution&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;xy&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; /&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;iResolution&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Output to screen&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    fragColor &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec4&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;uv&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; uv&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; ,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;uv&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        fragColor &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec4&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; ,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/shaders/disque.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Et tada 🎉 un disque!&lt;/p&gt;
&lt;h3 id=&quot;un-triangle-equilateral&quot;&gt;Un triangle équilatéral&lt;/h3&gt;
&lt;p&gt;Je vais pas vous mentir, ça n’a pas été très facile de déterminer comment dessiner le triangle.&lt;/p&gt;
&lt;p&gt;Le principe reste le même: déterminer si un point appartient ou non à une surface.&lt;/p&gt;
&lt;p&gt;Le problème ici est que la forme est bien plus complexe qu’un disque.&lt;/p&gt;
&lt;p&gt;Il faut donc ruser pour trouver une &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://www.developpez.net/forums/d3690/general-developpement/algorithme-mathematiques/algorithmes-structures-donnees/point-l-interieur-d-triangle/#post23027&quot;&gt;solution&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Une petite simulation avec &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https://www.geogebra.org/3d/wn6emtpk&quot;&gt;geogebra&lt;/a&gt; m’a permis de me simplifier la représentation du problème.&lt;/p&gt;
&lt;p&gt;Si &lt;code&gt;M&lt;/code&gt; est en dehors de l’angle AB, AC. La valeur de d est négative.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/shaders/exterieur1.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;De même dans l’autre sens&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/shaders/exterieur2.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Par contre si l’on se trouve dans l’angle, la valeur d devient positive.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/shaders/interieur1.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Le problème c’est que si le point est dans l’angle mais en dehors du triangle, la valeur reste positive.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/shaders/interieur2.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;L’idée est d’appliquer l’algorithme sur les 3 angles, pour que le point soit bel et bien à l’intérieur
du triangle. Si chacun des produits scalaires sont positifs alors le point M appartient au triangle.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;glsl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; mainImage&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt; out vec4 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragColor&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; in vec2 &lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;fragCoord&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec2 uv &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;fragCoord &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; iResolution&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;xy&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; /&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;iResolution&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Output to screen&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    fragColor &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec4&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;uv&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; uv&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; ,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Coordonnées des sommets du triangles équilatéral&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    float&lt;/span&gt;&lt;span&gt; k &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;75&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 A &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;57&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; *&lt;/span&gt;&lt;span&gt; k&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 B &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;28&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; *&lt;/span&gt;&lt;span&gt; k&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 C &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;28&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; *&lt;/span&gt;&lt;span&gt; k&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Calcul des coordonnées des vecteurs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 AB &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; B&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span&gt;A&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 AC &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; C&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span&gt;A&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 BA &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; A&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span&gt;B&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 BC &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; C&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span&gt;B&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 CA &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; A&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span&gt;C&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 CB &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; B&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span&gt;C&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; M est le point que l&amp;#39;on cherche à déterminer s&amp;#39;il est en dehors ou non du triangle&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 AM &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;uv &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; A&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;xy&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 BM &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;uv &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; B&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;xy&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec3 CM &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec3&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;uv &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; C&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;xy&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Calcul des déterminants&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    float&lt;/span&gt;&lt;span&gt; d1 &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; dot&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;cross&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;AB&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; AM&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; cross&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;AM&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; AC&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    float&lt;/span&gt;&lt;span&gt; d2 &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; dot&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;cross&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;BA&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; BM&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; cross&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;BM&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; BC&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    float&lt;/span&gt;&lt;span&gt; d3 &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; dot&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;cross&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;CA&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; CM&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; cross&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;CM&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; CB&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Dessin du cercle de rayon 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;uv&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        fragColor &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec4&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; ,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; Si tous les déterminants sont positifs, le point est dans le triangle&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-10&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;d1 &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;span&gt; d2 &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;span&gt; d3 &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        fragColor &lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; vec4&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt; ,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-14&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-4&quot;&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et un triangle pour la table 12 ! Un ! 👨‍🍳&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/shaders/triangle.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;h2 id=&quot;champ-de-distance-signe&quot;&gt;Champ de Distance Signé&lt;/h2&gt;
&lt;p&gt;Les deux algorithmes (pour le disque et pour le triangle) sont appelées des SDF : Signed Distance Field ou Champ de Distance Signé.&lt;/p&gt;
&lt;p&gt;Un champ est une propriété qui recouvre tout un espace ( ici 2D ).
Cela signifie que notre propiété va s’appliquer sur chaque pixel de l’écran.&lt;/p&gt;
&lt;p&gt;La deuxième partie est cette idée de distance. On recheche pour tout point de l’écran si l’on est à l’intérieur ou l’extérieur
d’une surface donnée et surtout à quelle distance on se trouve d’elle.&lt;/p&gt;
&lt;p&gt;Reprenons le disque qui est un peu plus simple. Son champ de distance est donné en tout point de l’écran
par la relation suivante:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Distance du point observé par rapport au centre du disque - rayon du disque&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/shaders/sdf1.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Tout ce que l’on fera par la suite sera de calculer la SDF de nos forme.&lt;/p&gt;
&lt;p&gt;Et pourquoi “signé”, parce que si l’on est à l’intérieur de la du disque, la distance devient
négative.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../assets/images/shaders/sdf2.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;C’est tout pour aujourd’hui!&lt;/p&gt;
&lt;p&gt;La suite sera de se plonger dans le monde merveilleux de la 3D enfin de la projection
de la 3D sur un écran 2D.&lt;/p&gt;
&lt;p&gt;On y verra notamment le RayMarcher.&lt;/p&gt;
&lt;p&gt;Bonne Fêtes à toutes et tous et merci de m’avoir lu. 🤗&lt;/p&gt;
</content>
        
    </entry>
</feed>
