Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: PerlinNoise

  1. #1
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    61
    Posts
    3,869
    Rep Power
    398

    Working Improved Perlin Noise

    This is a translation of Ken Perlin's Improved Noise Function written in Java.

    https://github.com/kryton9/thinBasic-PerlinNoise.git

    There are two functions for the programmer:

    1. PerlinNoiseSeed( seedValue )
    seedValue is an integer number, it will affect the noise returned from:

    2. PerlinNoise( x, y, z )
    x, y, z are double values

    You generally use PerlinNoiseSeed( seedValue ) once in the beginning of your code.
    It gives you the same controlled noise results
    each time your program runs.

    As more examples are made it will be more apparent the value of these two functions.

    Click image for larger version. 

Name:	noiseCosoleTest.jpg 
Views:	23 
Size:	36.6 KB 
ID:	9684

    You don't need Git to use these files. Just follow the link and click on the following to download a zip file of all the needed code:
    Click image for larger version. 

Name:	gitUse.jpg 
Views:	24 
Size:	43.8 KB 
ID:	9686
    Last edited by kryton9; 13-05-2017 at 22:07.
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  2. #2
    Thank you kryton9 for the noise function, i haven't focused on this subject before, now i find it interesting. but what confuse me is that for any 3 numbers the result is the same as before ie it is not like the RND function. which gives different results , i will read the explanation here about perlin noise http://web.archive.org/web/201602271...s/m_perlin.htm
    since he said "If you pass it the same parameter twice, it produces the same number twice. It is very important that it behaves in this way, otherwise the Prelim function will simply produce nonsense"

    i have tried your function noise to plot x, y, 0 by plotting the noise output as if it is y. the result is like a chaotic heart pulses
    Click image for larger version. 

Name:	perlin.PNG 
Views:	20 
Size:	7.3 KB 
ID:	9682
    if we change yy = yy + 0.03 to yy = yy + 1 it will be more chaotic
    using a Canvas_SineCurve.tbasuc as a template:
    with your include file "PerlinNoise.tbasici"
      Uses "UI"
      #INCLUDE "PerlinNoise.tbasici" 
      '-----------------------------------------------------------
      '---Global variables
      '-----------------------------------------------------------
      Function TBMain () As Long
      
      Dim ScreenWidth     As Long = 800
      Dim ScreenHeight    As Long = 600
      Dim hWin            As DWord        '---Handle of the canvas window
      Dim x,y,yy             As Double
      Dim thick          As Double
      Long f
    
      '-----------------------------------------------------------
      '---Main program
      '-----------------------------------------------------------
      hWin = Canvas_Window("Perlin Noise: press ESC to exit", 1, 1, ScreenWidth, ScreenHeight)
     
      Canvas_Attach(hWin, 0, %TRUE) ' <- double buffer
     
      '---Init canvas
      Canvas_Clear %BLACK
      Canvas_Font "Arial" , 8, %CANVAS_FONTSTYLE_NORMAL
    
      '---Set personalized canvas scale
      Canvas_Scale(-20, -5, 20, 20)  
      yy = -15
      '---Loop For All steps
      For x = -15 To 15 Step 0.02
       
        yy = yy + 0.03
        y = PerlinNoise( x, yy, 0 )
        
        thick = 3
        Canvas_Width(thick)
        Canvas_Line( (x, y*3), (x, y*3) , Rgb(25 * thick, 255, 0) )
       
        f+1
        If f=6 Then 'to make the animation faster
        Canvas_Redraw
        f=0
        End If
        If GetAsyncKeyState(%VK_ESCAPE) Then Exit For
    
      Next
    
    Do
    
    Loop While All( Asc(Canvas_WaitKey) <> 27, IsWindow(hWin) )
    Canvas_Window End 
      
    End Function
    
    Last edited by primo; 07-05-2017 at 21:09. Reason: edit code

  3. #3
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    61
    Posts
    3,869
    Rep Power
    398
    Hi Primo,

    Thanks for your example, that is really neat! It is fun to watch!!

    Yes the noise function from Ken Perlin will give the same results with smooth randomness where the differences are realistic to Nature. As you see in your fine example what looks like a heartbeat can become in 3D terrain, clouds and almost any type of natural material when used to create procedural textures.

    I need to make some more useful code in include files to start tapping into even more complex uses for Perlin Noise. Hope to have more in the coming days.
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  4. #4
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    6,796
    Blog Entries
    3
    Rep Power
    693
    Kent,

    I prepared proposal for change in your repository, you can accept it or not, please review here:
    https://github.com/kryton9/thinBasic-PerlinNoise/pull/1

    The point is - by default, you have not guarantee, that Git will preserve Windows line endings. This will force it.


    Petr
    Last edited by Petr Schreiber; 06-05-2017 at 23:35.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 7 64bit - Intel Core 2 Duo T6600 @ 2.2GHz - 4 GB RAM - NVIDIA GeForce G210M 512MB
    Windows 8 64bit - Intel Core i5-3350P @ 3.1GHz - 8 GB RAM - NVIDIA GeForce GT640 3GB

  5. #5
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    6,796
    Blog Entries
    3
    Rep Power
    693
    Kent,

    I also found one minor imprecision in your port of the code. I prepared second pull request to correct the data types:
    https://github.com/kryton9/thinBasic-PerlinNoise/pull/2

    As usually, feel free to ignore

    As with all pull requests - to review changes, click the File changes tab, where you see it line by line, side by side. Git rules.


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 7 64bit - Intel Core 2 Duo T6600 @ 2.2GHz - 4 GB RAM - NVIDIA GeForce G210M 512MB
    Windows 8 64bit - Intel Core i5-3350P @ 3.1GHz - 8 GB RAM - NVIDIA GeForce GT640 3GB

  6. #6
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    61
    Posts
    3,869
    Rep Power
    398
    Thanks Petr, you have to bare with me as I learn GitHub. Thanks for your help!

    I downloaded the github desktop program. I tell it to sync, I tell it to upload commits
    and it seems random it works, and it doesn't work. It then merged 2 different copies
    together into one big mess. It does a lot and I understand and appreciate what all it can do. So will
    keep trying to learn it. But I spent most of the day syncing by hand all the things I messed up
    using git's tools
    Last edited by kryton9; 07-05-2017 at 02:48.
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  7. #7
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    6,796
    Blog Entries
    3
    Rep Power
    693
    You are doing great, Kent


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 7 64bit - Intel Core 2 Duo T6600 @ 2.2GHz - 4 GB RAM - NVIDIA GeForce G210M 512MB
    Windows 8 64bit - Intel Core i5-3350P @ 3.1GHz - 8 GB RAM - NVIDIA GeForce GT640 3GB

  8. #8
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    61
    Posts
    3,869
    Rep Power
    398

    Not working as I thought

    I made this demo, but it is not giving me that clouds like look that I expected. I know the PerlinNoise function is returning correctly, but I did notice that Processing must use one of the older noise functions as it gives different results than the Improved Perlin Noise function.

    This takes sometime at first. I was hoping to do animations with the PerlinNoise function, but it is not fast enough with my implementation.

    Unfortunately this demo is nothing to look at. Just something to give some code for others perhaps to come up with something.

    Uses "TBGL"   
    #INCLUDE "%APP_INCLUDEPATH%thinbasic_gl.inc"  ' to use glColor4d function
    #INCLUDE "https://raw.githubusercontent.com/kryton9/thinBasic-PerlinNoise/master/PerlinNoise.tbasicu"
    DWord hWnd = 0
    Long x = 0 
    Long y = 0 
    Double frameRate = 0.0
    Long interval = 0  
    Long width = 250  'larger the longer it takes to calculate
    Long height = 100 'larger the longer it takes to calculate 
    Long numPoints = width * height  
    String winTitle = "Calculating " + numPoints + " Noise Values, This can take Some time."
    Double noise ( width, height )
    Double yOff = 0.0
    Double xOff = 0.0
    Double noiseZoom = 0.1 'good ranges : 0.1 to 0.001
    hWnd = TBGL_CreateWindowEx ( "PerlinNoise 2D GL Demo", width, height, 32, %TBGL_WS_WINDOWED )
        TBGL_ShowWindow   
        TBGL_SetWindowTitle ( hWnd, winTitle)
        For y = 1 To height 
            yOff += noiseZoom 
            winTitle = "Calculating " + Str$ (x * y) + " of " + numPoints + " Noise Values, working away."
            TBGL_SetWindowTitle ( hWnd, winTitle)       
            For x = 1 To width 
                xOff += noiseZoom
                noise(x, y) = PerlinNoise xOff, yOff, 7
            Next
        Next  
        TBGL_BlendFunc %GL_SRC_ALPHA , %GL_ONE
        TBGL_UseBlend %TRUE 
        TBGL_RenderMatrix2D ( 1, height, width, 1 )
        While TBGL_IsWindow ( hWnd )
            FrameRate = TBGL_GetFrameRate
            Incr interval
            If interval > 5 Then
                TBGL_SetWindowTitle ( hWnd, "PerlinNoise 2D GL Demo FPS: " + Str$ ( FrameRate, 4 ) )
                interval = 0
            EndIf
            TBGL_ClearFrame
                TBGL_PushMatrix
                    TBGL_BeginPoly %GL_POINTS  
                        For y = 1 To height
                            For x = 1 To width
                                glColor4d 255, 255, 255, noise ( x, y )
                                TBGL_Vertex x, y
                            Next
                        Next
                    TBGL_EndPoly
                TBGL_PopMatrix
            TBGL_DrawFrame
            If TBGL_GetWindowKeyState( hWnd, %VK_ESCAPE ) Then  Exit While
        Wend
    TBGL_DestroyWindow
    
    Click image for larger version. 

Name:	NoiseGLDemo.png 
Views:	22 
Size:	43.6 KB 
ID:	9693
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  9. #9
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    6,796
    Blog Entries
    3
    Rep Power
    693
    Nothing to look at? You serious, Kent? It looks super high tech! Like some alien scratches!

    I will check this one... do you have link to the original code by hand somewhere?


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 7 64bit - Intel Core 2 Duo T6600 @ 2.2GHz - 4 GB RAM - NVIDIA GeForce G210M 512MB
    Windows 8 64bit - Intel Core i5-3350P @ 3.1GHz - 8 GB RAM - NVIDIA GeForce GT640 3GB

  10. #10
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    61
    Posts
    3,869
    Rep Power
    398
    The full code is posted in the message Petr. One thing it proved to me is that my PerlinNoise is too slow unless you work in dimensions 5,5
    I am excited by this because now I will work on his older noise functions. And then will work on making them faster.
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •