Neural Network SDFs

calendar_today

01.01.2024

label

Programming

mouse

Houdini 20

Description

Shadertoy artist Blackle Mori modified a neural network to convert meshes to signed distances stored as matrices. How to use this neural network is explained in the video below.

1 Code

The relatively compact code below multiplies matrices and sine waves in order to shape the famous Stanford Bunny, effectively making it resolution-independent.

VEX
        // (c) by blackle mori
// https://www.shadertoy.com/view/wtVyWK

function float scene(vector p){
    if(length(p) > 1.0) {
        return length(p) - 0.8;
    }
    
    vector4 f00 = sin(p.z * set(-3.02,1.95,-3.42,-.60)   + p.y * set(3.08,.85,-2.25,-.24)   - p.x * set(-.29,1.16,-3.74,2.89) + set(-.71,4.50,-3.24,-3.50));
    vector4 f01 = sin(p.z * set(-.40,-3.61,3.23,-.14)    + p.y * set(-.36,3.64,-3.91,2.66)  - p.x * set(2.90,-.54,-2.75,2.71) + set(7.02,-5.41,-1.12,-7.41));
    vector4 f02 = sin(p.z * set(-1.77,-1.28,-4.29,-3.20) + p.y * set(-3.49,-2.81,-.64,2.79) - p.x * set(3.15,2.14,-3.85,1.83) + set(-2.07,4.49,5.33,-2.17));
    vector4 f03 = sin(p.z * set(-.49,.68,3.05,.42)       + p.y * set(-2.87,.78,3.78,-3.41)  - p.x * set(-2.65,.33,.07,-.64)   + set(-3.24,-5.90,1.14,-4.71));
    
    vector4 f10 = sin(set(-.34,.06,-.59,-.76,.10,-.19,-.12,.44,.64,-.02,-.26,.15,-.16,.21,.91,.15)      * f00 +
                      set(.01,.54,-.77,.11,.06,-.14,.43,.51,-.18,.08,.39,.20,.33,-.49,-.10,.19)         * f01 +
                      set(.27,.22,.43,.53,.18,-.17,.23,-.64,-.14,.02,-.10,.16,-.13,-.06,-.04,-.36)      * f02 +
                      set(-.13,.29,-.29,.08,1.13,.02,-.83,.32,-.32,.04,-.31,-.16,.14,-.03,-.20,.39)     * f03 +
                      set(.73,-4.28,-1.56,-1.80))/1.0                                                   + f00;
        
    vector4 f11 = sin(set(-1.11,.55,-.12,-1.00,.16,.15,-.30,.31,-.01,.01,.31,-.42,-.29,.38,-.04,.71)    * f00 +
                      set(.96,-.02,.86,.52,-.14,.60,.44,.43,.02,-.15,-.49,-.05,-.06,-.25,-.03,-.22)     * f01 +
                      set(.52,.44,-.05,-.11,-.56,-.10,-.61,-.40,-.04,.55,.32,-.07,-.02,.28,.26,-.49)    * f02 +
                      set(.02,-.32,.06,-.17,-.59,.00,-.24,.60,-.06,.13,-.21,-.27,-.12,-.14,.58,-.55)    * f03 +
                      set(-2.24,-3.48,-.80,1.41))/1.0                                                   + f01;
                    
    vector4 f12 = sin(set(.44,-.06,-.79,-.46,.05,-.60,.30,.36,.35,.12,.02,.12,.40,-.26,.63,-.21)        * f00 +
                      set(-.48,.43,-.73,-.40,.11,-.01,.71,.05,-.25,.25,-.28,-.20,.32,-.02,-.84,.16)     * f01 +
                      set(.39,-.07,.90,.36,-.38,-.27,-1.86,-.39,.48,-.20,-.05,.10,-.00,-.21,.29,.63)    * f02 +
                      set(.46,-.32,.06,.09,.72,-.47,.81,.78,.90,.02,-.21,.08,-.16,.22,.32,-.13)         * f03 +
                      set(3.38,1.20,.84,1.41))/1.0                                                      + f02;
        
    vector4 f13 = sin(set(-.41,-.24,-.71,-.25,-.24,-.75,-.09,.02,-.27,-.42,.02,.03,-.01,.51,-.12,-1.24) * f00 +
                      set(.64,.31,-1.36,.61,-.34,.11,.14,.79,.22,-.16,-.29,-.70,.02,-.37,.49,.39)       * f01 +
                      set(.79,.47,.54,-.47,-1.13,-.35,-1.03,-.22,-.67,-.26,.10,.21,-.07,-.73,-.11,.72)  * f02 +
                      set(.43,-.23,.13,.09,1.38,-.63,1.57,-.20,.39,-.14,.42,.13,-.57,-.08,-.21,.21)     * f03 +
                      set(-.34,-3.28,.43,-.52))/1.0                                                     + f03;
        
    f00 = sin(set(-.72,.23,-.89,.52,.38,.19,-.16,-.88,.26,-.37,.09,.63,.29,-.72,.30,-.95)               * f10 +
              set(-.22,-.51,-.42,-.73,-.32,.00,-1.03,1.17,-.20,-.03,-.13,-.16,-.41,.09,.36,-.84)        * f11 +
              set(-.21,.01,.33,.47,.05,.20,-.44,-1.04,.13,.12,-.13,.31,.01,-.34,.41,-.34)               * f12 +
              set(-.13,-.06,-.39,-.22,.48,.25,.24,-.97,-.34,.14,.42,-.00,-.44,.05,.09,-.95)             * f13 +
              set(.48,.87,-.87,-2.06))/1.4                                                              + f10;
        
    f01 = sin(set(-.27,.29,-.21,.15,.34,-.23,.85,-.09,-1.15,-.24,-.05,-.25,-.12,-.73,-.17,-.37)         * f10 +
              set(-1.11,.35,-.93,-.06,-.79,-.03,-.46,-.37,.60,-.37,-.14,.45,-.03,-.21,.02,.59)          * f11 +
              set(-.92,-.17,-.58,-.18,.58,.60,.83,-1.04,-.80,-.16,.23,-.11,.08,.16,.76,.61)             * f12 +
              set(.29,.45,.30,.39,-.91,.66,-.35,-.35,.21,.16,-.54,-.63,1.10,-.38,.20,.15)               * f13 +
              set(-1.72,-.14,1.92,2.08))/1.4                                                            + f11;
        
    f02 = sin(set(1.00,.66,1.30,-.51,.88,.25,-.67,.03,-.68,-.08,-.12,-.14,.46,1.15,.38,-.10)            * f10 +
              set(.51,-.57,.41,-.09,.68,-.50,-.04,-1.01,.20,.44,-.60,.46,-.09,-.37,-1.30,.04)           * f11 +
              set(.14,.29,-.45,-.06,-.65,.33,-.37,-.95,.71,-.07,1.00,-.60,-1.68,-.20,-.00,-.70)         * f12 +
              set(-.31,.69,.56,.13,.95,.36,.56,.59,-.63,.52,-.30,.17,1.23,.72,.95,.75)                  * f13 +
              set(-.90,-3.26,-.44,-3.11))/1.4                                                           + f12;
        
    f03 = sin(set(.51,-.98,-.28,.16,-.22,-.17,-1.03,.22,.70,-.15,.12,.43,.78,.67,-.85,-.25)             * f10 +
              set(.81,.60,-.89,.61,-1.03,-.33,.60,-.11,-.06,.01,-.02,-.44,.73,.69,1.02,.62)             * f11 +
              set(-.10,.52,.80,-.65,.40,-.75,.47,1.56,.03,.05,.08,.31,-.03,.22,-1.63,.07)               * f12 +
              set(-.18,-.07,-1.22,.48,-.01,.56,.07,.15,.24,.25,-.09,-.54,.23,-.08,.20,.36)              * f13 +
              set(-1.11,-4.28,1.02,-.23))/1.4                                                           + f13;
        
    return dot(f00, set(.09,.12,-.07,-.03)) + dot(f01, set(-.04,.07,-.08,.05)) +
           dot(f02, set(-.01,.06,-.02,.07)) + dot(f03, set(-.05,.07,.03,.04)) - 0.16;
        
}

f@d = scene(v@P);
    

2 Sources

download

Downloads

smart_display

Videos

A Haphazard Tutorial for making Neural SDFs in Shadertoy

link

Related articles

favorite

210

label

Programming

Clustering Attribute Values

favorite

538

label

Programming

Fitting Planes to Point Clouds

favorite

225

label

Programming

Genetic Algorithm for Optimization

favorite

33

label

Programming

Invoking Blocks and Node Graphs

favorite

92

label

Programming

Sorting Classes by Attribute Values

favorite

470

label

AutomationPythonProgramming

Batch processing files with Python