perlin noise

Algemene discussies en vragen omtrent Liberty BASIC programma's. Stuur zoveel mogelijk delen van listings (snippets) in als je hulp wilt.

Moderators: anthonio, Abcott

perlin noise

Berichtdoor bluatigro » vr okt 18, 2013 1:07 pm

deze code creert een regenboog gekleurd texture
Code: Selecteer alles
WindowWidth = DisplayWidth
WindowHeight = DisplayHeight
nomainwin
global black , white , pi
black = rgb( 0 , 0 , 0 )
white = rgb( 255 , 255 , 255 )
pi = atn( 1 ) * 4
open "perlin noise 1.0" for graphics as #m
  #m "trapclose [quit]"
''i made the texture smal : it takes minutes to draw
  for x = 0 to 100
    for y = 0 to 100
      ''get a noise number -1 ... 1
      p = PerlinNoise( x , y , 1/16 , 4 , 0.5 )
      ''get a colorobject
      kl = rainbow( p * 180 )
      ''set draw color
      call setcolor kl
      ''draw a point
      #m "goto " ; x ; " " ; y
      #m "down"
      #m "set " ; x ; " " ; y
      #m "up"
    next y
  next x

wait
[quit]
  close #m
end

function rad( deg )
  rad = deg * pi / 180
end function

''======================================================================
''some code for manupulating colors
''======================================================================

function rainbow( deg )
''create a colorobject out of the rainbow
  r = sin( rad( deg ) ) * 127 + 128
  g = sin( rad( deg - 120 ) ) * 127 + 128
  b = sin( rad( deg + 120 ) ) * 127 + 128
  rainbow = rgb( r , g , b )
end function

sub setcolor kl
''use colorobject kl to change drawcolor
    r = int( kl and 255 )
    g = int( kl / 256 ) and 255
    b = int( kl / 256 / 256 ) and 255
    #m "backcolor " ; r ;" "; g ;" "; b
    #m "color " ; r ; " " ; g ; " " ; b
end sub

function mix( colora , f , colorb )
''for a colorobject that is inbetween 2 colorobjects
''get propertys of the colorobjects
    r1 = int( colora and 255 )
    g1 = int( colora / 256 ) and 255
    b1 = int( colora / 256 / 256 ) and 255
    r2 = int( colorb and 255 )
    g2 = int( colorb / 256 ) and 255
    b2 = int( colorb / 256 / 256 ) and 255
''clamp the mixfactor
  if f < 0 then f = 0
  if f > 1 then f = 1
''get mixed propertys
  r = r1 + ( r2 - r1 ) * f
  g = g1 + ( g2 - g1 ) * f
  b = b1 + ( b2 - b1 ) * f
''use constructor for new colorobject
  mix = rgb( r , g , b )
end function

function rgb( r , g , b )
''constructor colorobject
  r = r and 255
  g = g and 255
  b = b and 255
  rgb = r + g * 256 + b * 256 ^ 2
end function

''========================================================================================
''some code to create noise
''========================================================================================

function PerlinNoise( x , y , f , octaves , persistence )
  total = 0
  p = persistence
  n = octaves - 1
  for i = 0 to n
    frequency = f * 2 ^ i
    amplitude = p ^ i
    total = total + InterpolatedNoise( x * frequency , y * frequency ) * amplitude
  next i
  PerlinNoise = total
end function

function Noise( x , y )
''random number generator
  n = x + y * 57
  n = ( n * 2 ^ 13 ) xor n
  Noise = ( 1.0 - ( ( n * ( n * n * 15731 + 789221 ) + 1376312589 ) and (2^31-1)) / 1073741824.0 )
end function

function InterpolatedNoise( x , y )
  integerX    = int( x )
  fractionalX = x - integerX
  integerY    = int( y )
  fractionalY = y - integerY
  v1 = SmoothedNoise( integerX,     integerY )
  v2 = SmoothedNoise( integerX + 1, integerY )
  v3 = SmoothedNoise( integerX,     integerY + 1 )
  v4 = SmoothedNoise( integerX + 1, integerY + 1 )
  i1 = Interpolate( v1 , v2 , fractionalX )
  i2 = Interpolate( v3 , v4 , fractionalX )
  InterpolatedNoise = Interpolate( i1 , i2 , fractionalY )
end function

function SmoothedNoise( x , y )
  corners = ( Noise(x-1, y-1)+Noise(x+1, y-1)+Noise(x-1, y+1)+Noise(x+1, y+1) ) / 16
  sides   = ( Noise(x-1, y)  +Noise(x+1, y)  +Noise(x, y-1)  +Noise(x, y+1) ) /  8
  center  =  Noise(x, y) / 4
  SmoothedNoise = corners + sides + center
end function

function Interpolate( a , b , x )
    ft = x * 3.1415927
    f = ( 1 - cos( ft ) ) * .5
    Interpolate = a * ( 1 - f ) + b * f
end function

bluatigro
 
Berichten: 306
Geregistreerd: za sep 27, 2008 6:01 pm

Re: perlin noise

Berichtdoor bluatigro » wo okt 23, 2013 1:24 pm

update :
- nu met vuur , marmer en explosie voorbeeld

tekenen druurt n paar minuten

Code: Selecteer alles
WindowWidth = DisplayWidth
WindowHeight = DisplayHeight
global black , red , green , yellow
global blue , magenta , cyan , white , pi
global gray , pink , orange , purple
black   = rgb(   0 ,   0 ,   0 )
red     = rgb( 255 ,   0 ,   0 )
green   = rgb(   0 , 255 ,   0 )
yellow  = rgb( 255 , 255 ,   0 )
blue    = rgb(   0 ,   0 , 255 )
magenta = rgb( 255 ,   0 , 255 )
cyan    = rgb(   0 , 255 , 255 )
white   = rgb( 255 , 255 , 255 )
gray    = rgb( 127 , 127 , 127 )
pink    = rgb( 255 , 127 , 127 )
orange  = rgb( 255 , 127 ,   0 )
purple  = rgb( 127 ,   0 , 127 )
pi = atn( 1 ) * 4
nomainwin
notice chr$(13) + "Bluatigro presents :" + chr$( 13 ) _
+ chr$( 13 ) _
+ "Perlin noise example textures ." + chr$( 13 ) _
+ "0 = marble ." + chr$( 13 ) _
+ "1 = fire ." + chr$( 13 ) _
+ "2 = pure noise ." + chr$( 13 ) _
+ "3 = explosoin ."
prompt "type a number [ 0 ... 4 ]" ; a$
state = val( a$ )

open "perlin noise 1.0" for graphics as #m
  #m "trapclose [quit]"
''i made the texture smal : it takes minutes to draw
  for x = 0 to 200
    for y = 0 to 200
      ''get a noise number -1 ... 1
      p = PerlinNoise( x , y , 1/8 , 3 , 0.5 )
      select case state
        case 0     ''marble
          ''add the noise to a wave .
          p = sin( x / 16 + y / 16 + p * 8 )
          ''get a colorobject
          kl = mix( black , ( p + 1 ) / 2 , white )
        case 1     ''fire
          ''change noise to 0 ... 1
          p = ( p + 1 ) / 2
          ''create a sloping plane to reshape the noise .
          height = y / 200
          ''get a colorobject .
          kl = color4( p * height , black , red , yellow , white )
        case 2     ''rainbow chaos
          ''get a colorobject
          kl = rainbow( p * 180 )
        case else  ''explosoin
          ''change noise to 0 ... 1
          p = ( p + 1 ) / 2
          ''create a cone to reshape the noise .
          height = 1 - sqr( ( x - 100 ) ^ 2 + ( y - 100 ) ^ 2 ) / 100
          ''get a colorobject .
          kl = color4( p * height , black , red , yellow , white )
      end select
      ''use colorobject to chane drawing color .
      call setcolor kl
      ''draw a point
      #m "goto " ; x ; " " ; y
      #m "down"
      #m "set " ; x ; " " ; y
      #m "up"
    next y
  next x
  notice "ready"
wait
[quit]
  close #m
end

function rad( deg )
  rad = deg * pi / 180
end function

''======================================================================
''some code for manupulating colors
''======================================================================

function color4( x , a , b , c , d )
  uit = a
  if x < 1 / 3 then uit = mix( a , x * 3 , b )
  if x >= 1 / 3 and x < 2 / 3 then uit = mix( b , x * 3 - 1 , c )
  if x >= 2 / 3 and x < 3 then uit = mix( c , x * 3 - 2 , d )
  if x >= 1 then uit = d
  color4 = uit
end function

function rainbow( deg )
''create a colorobject out of the rainbow
  r = sin( rad( deg ) ) * 127 + 128
  g = sin( rad( deg - 120 ) ) * 127 + 128
  b = sin( rad( deg + 120 ) ) * 127 + 128
  rainbow = rgb( r , g , b )
end function

sub setcolor kl
''use colorobject kl to change drawcolor
    r = int( kl and 255 )
    g = int( kl / 256 ) and 255
    b = int( kl / 256 / 256 ) and 255
    #m "backcolor " ; r ;" "; g ;" "; b
    #m "color " ; r ; " " ; g ; " " ; b
end sub

function mix( colora , f , colorb )
''for a colorobject that is inbetween 2 colorobjects
''get propertys of the colorobjects
    r1 = int( colora and 255 )
    g1 = int( colora / 256 ) and 255
    b1 = int( colora / 256 / 256 ) and 255
    r2 = int( colorb and 255 )
    g2 = int( colorb / 256 ) and 255
    b2 = int( colorb / 256 / 256 ) and 255
''clamp the mixfactor
  if f < 0 then f = 0
  if f > 1 then f = 1
''get mixed propertys
  r = r1 + ( r2 - r1 ) * f
  g = g1 + ( g2 - g1 ) * f
  b = b1 + ( b2 - b1 ) * f
''use constructor for new colorobject
  mix = rgb( r , g , b )
end function

function rgb( r , g , b )
''constructor colorobject
  r = r and 255
  g = g and 255
  b = b and 255
  rgb = r + g * 256 + b * 256 ^ 2
end function

''========================================================================================
''some code to create noise
''========================================================================================

function PerlinNoise( x , y , f , octaves , persistence )
  total = 0
  p = persistence
  n = octaves - 1
  for i = 0 to n
    frequency = f * 2 ^ i
    amplitude = p ^ i
    total = total + InterpolatedNoise( x * frequency , y * frequency ) * amplitude
  next i
  PerlinNoise = total
end function

function Noise( x , y )
''random number generator
  n = x + y * 57
  n = ( n * 2 ^ 13 ) xor n
  Noise = ( 1.0 - ( ( n * ( n * n * 15731 + 789221 ) + 1376312589 ) and (2^31-1)) / 1073741824.0 )
end function

function InterpolatedNoise( x , y )
  integerX    = int( x )
  fractionalX = x - integerX
  integerY    = int( y )
  fractionalY = y - integerY
  v1 = SmoothedNoise( integerX,     integerY )
  v2 = SmoothedNoise( integerX + 1, integerY )
  v3 = SmoothedNoise( integerX,     integerY + 1 )
  v4 = SmoothedNoise( integerX + 1, integerY + 1 )
  i1 = Interpolate( v1 , v2 , fractionalX )
  i2 = Interpolate( v3 , v4 , fractionalX )
  InterpolatedNoise = Interpolate( i1 , i2 , fractionalY )
end function

function SmoothedNoise( x , y )
  corners = ( Noise(x-1, y-1)+Noise(x+1, y-1)+Noise(x-1, y+1)+Noise(x+1, y+1) ) / 16
  sides   = ( Noise(x-1, y)  +Noise(x+1, y)  +Noise(x, y-1)  +Noise(x, y+1) ) /  8
  center  =  Noise(x, y) / 4
  SmoothedNoise = corners + sides + center
end function

function Interpolate( a , b , x )
    ft = x * 3.1415927
    f = ( 1 - cos( ft ) ) * .5
    Interpolate = a * ( 1 - f ) + b * f
end function

bluatigro
 
Berichten: 306
Geregistreerd: za sep 27, 2008 6:01 pm


Keer terug naar Liberty BASIC Code

Wie is er online

Gebruikers op dit forum: Geen geregistreerde gebruikers. en 1 gast

cron