#distance-field #sdf #signed #operator #primitive #circle #transform

signed_distance_fields

A implementation of SDFs (signed distance fields) and some operators to play with these SDFs

7 stable releases

1.0.6 Oct 10, 2022
1.0.5 Oct 8, 2022

#1469 in Rust patterns

MIT license

250KB
299 lines

Signed Distance Fields

github crates.io docs.rs

You can combine, transform and even develop completly new SDFs that can be rendered with one of this libraries multiple renderes

Examples

Primitives

$ cargo run --example primitives
Circle:
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                              ++++++++                                              
                                     ++++++++++++++++++++++++++                                     
                                +++++++----------------------+++++++                                
                             +++++-------                  -------+++++                             
                          ++++-----                              -----++++                          
                       ++++----                                      ----++++                       
                     ++++----                                          ----++++                     
                   ++++---                                                ---++++                   
                  +++---                                                    ---+++                  
                 +++---                                                      ---+++                 
               +++---                                                          ---+++               
              +++---                                                            ---+++              
             +++---                                                              ---+++             
             ++---                                                                ---++             
            +++--                                                                  --+++            
           +++---                                                                  ---+++           
           +++--                                                                    --+++           
           ++---                                                                    ---++           
           ++--                                                                      --++           
          +++--                                                                      --+++          
          +++--                                                                      --+++          
           ++--                                                                      --++           
           ++---                                                                    ---++           
           +++--                                                                    --+++           
           +++---                                                                  ---+++           
            +++--                                                                  --+++            
             ++---                                                                ---++             
             +++---                                                              ---+++             
              +++---                                                            ---+++              
               +++---                                                          ---+++               
                 +++---                                                      ---+++                 
                  +++---                                                    ---+++                  
                   ++++---                                                ---++++                   
                     ++++----                                          ----++++                     
                       ++++----                                      ----++++                       
                          ++++-----                              -----++++                          
                             +++++-------                  -------+++++                             
                                +++++++----------------------+++++++                                
                                     ++++++++++++++++++++++++++                                     
                                              ++++++++                                              
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    

Rectangle:
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                              ++++++++++++++++++++++++++++++++++++++++                              
                              +--------------------------------------+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +--------------------------------------+                              
                              ++++++++++++++++++++++++++++++++++++++++                              
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    

Line:
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
             ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++             
             ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++             
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    

Straight:
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    

Plane:
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************
****************************************************************************************************

Operators

Transform

$ cargo run --example operators_transform
None:
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                              ++++++++++++++++++++++++++++++++++++++++                              
                              +--------------------------------------+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +---                                ---+                              
                              +--------------------------------------+                              
                              ++++++++++++++++++++++++++++++++++++++++                              
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    

Translate:
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
      +++++++++++++++++++++++++++++++++++++++                                                       
     ++-------------------------------------+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       
     ++--                                ---+                                                       

Rotate:
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                               ++                   
                                                                        +++++----+                  
                                                                  ++++-----------++                 
                                                           ++++---------       ---+                 
                                                    ++++---------               --++                
                                             ++++---------                      ---+                
                                      +++++--------                              ---+               
                               +++++---------                                     --++              
                         ++++---------                                            ---+              
                  ++++---------                                                    --++             
           ++++---------                                                           ---+             
        +--------                                                                   ---+            
         +--                                                                         --++           
         +---                                                                        ---+           
          +---                                                                        --++          
          ++--                                                                        ---+          
           +---                                                                        ---+         
           ++--                                                                         --+         
            +---                                                                   --------+        
             +---                                                           ---------++++           
             ++--                                                    ---------++++                  
              +---                                            ---------++++                         
              ++--                                     ---------+++++                               
               +---                              --------+++++                                      
                +---                      ---------++++                                             
                ++--               ---------++++                                                    
                 +---       ---------++++                                                           
                 ++-----------++++                                                                  
                  +----+++++                                                                        
                   ++                                                                               
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    

Scale:
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
          +++-----                                                                -----+++          
          +++-----                                                                -----+++          
          +++-----                                                                -----+++          
          +++-----                                                                -----+++          
          +++-----                                                                -----+++          
          +++-----                                                                -----+++          
          +++-----                                                                -----+++          
          +++-----                                                                -----+++          
          +++-----                                                                -----+++          
          +++-----                                                                -----+++          
          +++-----                                                                -----+++          
          +++-----                                                                -----+++          
          +++-----                                                                -----+++          
          +++-----                                                                -----+++          
          +++-----                                                                -----+++          
          +++-----                                                                -----+++          
          +++-----                                                                -----+++          
          +++-----                                                                -----+++          
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    

Matrix:
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
  ++-+++++                                                                                          
     +--   --------++++                                                                             
       +---             --------++++                                                                
         +---                         --------+++                                                   
           ++--                                ---+                                                 
              +--                                ---+                                               
                +--                                 --+                                             
                  +---                                --++                                          
                    +---                                ---+                                        
                      ++--                                ---+                                      
                         +--                                 --+                                    
                           +---                                --+                                  
                             +---                                --++                               
                               ++--                                ---+                             
                                  +--                                ---+                           
                                    +--                                 --+                         
                                      +---                                --++                      
                                        +---                                ---+                    
                                          ++--                                ---+                  
                                             +--                                 --+                
                                               +---                                --+              
                                                 +---                                --++           
                                                   +++--------                         ---+         
                                                                ++++--------             ---+       
                                                                             ++++--------   --+     
                                                                                          +++++-++  
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    

Boolean

$ cargo run --example operators_boolean
a:
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                        ++++++++++++++++++++                                        
                                    +++++------------------+++++                                    
                                 ++++------              ------++++                                 
                               +++-----                      -----+++                               
                             +++----                            ----+++                             
                           +++----                                ----+++                           
                          +++---                                    ---+++                          
                         +++---                                      ---+++                         
                        +++---                                        ---+++                        
                        ++---                                          ---++                        
                       +++--                                            --+++                       
                       ++---                                            ---++                       
                       ++---                                            ---++                       
                       ++---                                            ---++                       
                       ++---                                            ---++                       
                       +++--                                            --+++                       
                        ++---                                          ---++                        
                        +++---                                        ---+++                        
                         +++---                                      ---+++                         
                          +++---                                    ---+++                          
                           +++----                                ----+++                           
                             +++----                            ----+++                             
                               +++-----                      -----+++                               
                                 ++++------              ------++++                                 
                                    +++++------------------+++++                                    
                                        ++++++++++++++++++++                                        
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    

b:
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
            ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++            
            +--------------------------------------------------------------------------+            
            +--                                                                      --+            
            +--                                                                      --+            
            +--                                                                      --+            
            +--                                                                      --+            
            +--                                                                      --+            
            +--                                                                      --+            
            +--                                                                      --+            
            +--                                                                      --+            
            +--                                                                      --+            
            +--                                                                      --+            
            +--------------------------------------------------------------------------+            
            ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++            
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    

a ∪ b:
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                        ++++++++++++++++++++                                        
                                    +++++------------------+++++                                    
                                 ++++------              ------++++                                 
                               +++-----                      -----+++                               
                             +++----                            ----+++                             
                           +++----                                ----+++                           
            +++++++++++++++++---                                    ---+++++++++++++++++            
            +------------------                                      ------------------+            
            +--                                                                      --+            
            +--                                                                      --+            
            +--                                                                      --+            
            +--                                                                      --+            
            +--                                                                      --+            
            +--                                                                      --+            
            +--                                                                      --+            
            +--                                                                      --+            
            +--                                                                      --+            
            +--                                                                      --+            
            +------------------                                      ------------------+            
            +++++++++++++++++---                                    ---+++++++++++++++++            
                           +++----                                ----+++                           
                             +++----                            ----+++                             
                               +++-----                      -----+++                               
                                 ++++------              ------++++                                 
                                    +++++------------------+++++                                    
                                        ++++++++++++++++++++                                        
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    

a ∩ b:
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                          ++++++++++++++++++++++++++++++++++++++++++++++++                          
                         +++--------------------------------------------+++                         
                        +++---                                        ---+++                        
                        ++---                                          ---++                        
                       +++--                                            --+++                       
                       ++---                                            ---++                       
                       ++---                                            ---++                       
                       ++---                                            ---++                       
                       ++---                                            ---++                       
                       +++--                                            --+++                       
                        ++---                                          ---++                        
                        +++---                                        ---+++                        
                         +++--------------------------------------------+++                         
                          ++++++++++++++++++++++++++++++++++++++++++++++++                          
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    

a\b:
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
            --------------------                                    --------------------            
            -+++++++++++++++---                                      ---+++++++++++++++-            
            -++         +++---                                        ---+++         ++-            
            -++         ++---                                          ---++         ++-            
            -++        +++--                                            --+++        ++-            
            -++        ++---                                            ---++        ++-            
            -++        ++---                                            ---++        ++-            
            -++        ++---                                            ---++        ++-            
            -++        ++---                                            ---++        ++-            
            -++        +++--                                            --+++        ++-            
            -++         ++---                                          ---++         ++-            
            -++         +++---                                        ---+++         ++-            
            -+++++++++++++++---                                      ---+++++++++++++++-            
            --------------------                                    --------------------            
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    

Dependencies

~16MB
~129K SLoC