mlhaufe

Perlin Terrain

May 9th, 2011
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 4.27 KB | None | 0 0
  1. <!doctype html>
  2. <html>
  3.     <head>
  4.         <title>Div Fractal Mountains</title>
  5.         <hta:application
  6.         maximizebutton = "No"
  7.         minimizebutton = "No"
  8.         border = "Thin"
  9.         selection ="No" />
  10.  
  11.         <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  12.         <meta name="author" content="Alan Bond" />
  13.        
  14.         <style type="text/css">
  15.             html{
  16.                 overflow:hidden;
  17.             }
  18.             body{
  19.                 border:0;
  20.                 margin:0;
  21.                 background-color:black;
  22.             }
  23.             #btnRender, #txtSeed{
  24.                 width:80px;
  25.             }
  26.             #divViewPort{
  27.                 position:absolute;
  28.                 display:none;
  29.                 left:0px;
  30.             }
  31.             .pixel{
  32.                 position:absolute;
  33.                 /*overflow:hidden;*/
  34.                 width:4px;
  35.                 height:20px;
  36.             }
  37.         </style>
  38.  
  39.         <script type="text/vbscript">
  40.             Option Explicit
  41.  
  42.             Dim sngPerlinMap, objPixel
  43.  
  44.             Const MAP_SIZE = 128
  45.             Const MAP_SIZE_1 = 127
  46.             Const ENTER_KEY = 13
  47.            
  48.             Sub Window_OnLoad()
  49.                 SetupView
  50.             End Sub
  51.            
  52.             Sub SetupView()
  53.                 Dim X,Y, Div
  54.                 Dim SX : SX = MAP_SIZE * 4
  55.                 Dim SY : SY = 60
  56.                 Dim PX : PX = SX
  57.                 Dim PY : PY = SY
  58.                 Dim Template : Set Template = Document.CreateElement("div")
  59.                 Template.className = "pixel"
  60.                 Redim objPixel(MAP_SIZE, MAP_SIZE)
  61.                 Self.ResizeTo (SX * 2) + 8, SX * 1.4
  62.                 Self.MoveTo (Screen.Width/2) - SX,(Screen.Height/2) - SX * 0.7
  63.  
  64.                 For Y = 0 to MAP_SIZE_1
  65.                     For X = 0 to MAP_SIZE_1
  66.                         Set Div = divViewPort.AppendChild( Template.cloneNode(False) )
  67.                         With Div.Style
  68.                             .PixelLeft = PX
  69.                             .PixelTop = PY
  70.                             .BackgroundColor = RGB(X*3, Y*2, X*2)
  71.                         End With
  72.                         Set objPixel(X,Y) = Div
  73.                         PX = PX + 4 : PY = PY + 2
  74.                     Next
  75.                     SX = SX - 4 : PX = SX : SY = SY + 2 : PY = SY
  76.                 Next
  77.                 divViewPort.style.display = "block"
  78.             End Sub
  79.  
  80.             Sub btnRender_OnClick()
  81.                 divViewPort.style.display = "none"
  82.                 Dim X,Y,SY,PY,intColour,sngRed,sngGreen,sngBlue
  83.  
  84.                 If IsNumeric(txtSeed.Value) Then
  85.                     Rnd(-1) : Randomize ABS(txtSeed.Value)
  86.                 Else
  87.                     Randomize
  88.                 End If
  89.  
  90.                 Perlin 0.45, 2
  91.  
  92.                 SY=MAP_SIZE*2 : PY=SY
  93.                 sngRed=Rnd(1) : sngGreen=Rnd(1) : sngBlue=Rnd(1)
  94.  
  95.                 For Y = 0 to MAP_SIZE_1
  96.                     For X = 0 to MAP_SIZE_1
  97.                         intColour = sngPerlinMap(X,Y)*5
  98.                         With objPixel(X,Y).Style
  99.                             .PixelTop = PY - sngPerlinMap(X,Y)*6
  100.                             .BackgroundColor = RGB(intColour*sngBlue,intColour*sngGreen,intColour*sngRed)
  101.                         End With
  102.                         PY=PY+2
  103.                     Next
  104.                     SY=SY+2 : PY=SY
  105.                 Next
  106.                 divViewPort.style.display = "block"
  107.             End Sub
  108.  
  109.             Sub txtSeed_OnKeyPress()
  110.                 If Window.Event.KeyCode = ENTER_KEY Then btnRender_OnClick
  111.             End Sub
  112.  
  113.             Sub Perlin(sngScale, intMultiply)
  114.                 ReDim sngPerlinMap(MAP_SIZE, MAP_SIZE)
  115.                 Dim sngRandNoise : ReDim sngRandNoise(MAP_SIZE, MAP_SIZE)
  116.                 Dim sngCeiling : sngCeiling = sngScale * intMultiply
  117.                 Dim intNoiseSize : intNoiseSize = MAP_SIZE/2
  118.                
  119.                 While intNoiseSize > 1
  120.                     Dim NY, NX
  121.                     For NY = 0 To intNoiseSize
  122.                         For NX = 0 To intNoiseSize
  123.                             sngRandNoise(NX, NY) = Rnd(1) * sngCeiling
  124.                         Next
  125.                     Next
  126.                     Dim intScaleRem : intScaleRem = MAP_SIZE/intNoiseSize
  127.                     Dim ISR_1 : ISR_1 = intScaleRem - 1
  128.                     Dim sngStep : sngStep = 1/intScaleRem
  129.                     Dim intNS_1 : intNS_1 = intNoiseSize-1
  130.                     For NY = 0 To intNS_1
  131.                         For NX = 0 To intNS_1
  132.                             Dim IY: IY = 0
  133.                             Dim NX1 : NX1 = NX+1
  134.                             Dim NY1 : NY1 = NY+1
  135.                             Dim N1 : N1 = sngRandNoise(NX, NY)
  136.                             Dim N2 : N2 = sngRandNoise(NX1, NY)
  137.                             Dim N3 : N3 = sngRandNoise(NX, NY1)
  138.                             Dim N4 : N4 = sngRandNoise(NX1, NY1)
  139.                             Dim HX : HX = NX*intScaleRem
  140.                             Dim HY : HY = NY*intScaleRem
  141.                             Dim HTY
  142.                             For HTY = 0 To ISR_1
  143.                                 Dim IX : IX = 0
  144.                                 Dim HYHTY : HYHTY = HY+HTY
  145.                                 Dim HTX
  146.                                 For HTX = 0 To ISR_1
  147.                                     Dim IX_1 : IX_1 = 1-IX
  148.                                     Dim HXHTX : HXHTX = HX+HTX                                 
  149.                                     sngPerlinMap(HXHTX, HYHTY) = sngPerlinMap(HXHTX, HYHTY) + ((N1*IX_1 + N2*IX) * (1-IY) + (N3*IX_1 + N4*IX) * IY)
  150.                                     IX = IX + sngStep
  151.                                 Next
  152.                                 IY = IY+sngStep
  153.                             Next
  154.                         Next
  155.                     Next
  156.                     intNoiseSize = intNoiseSize/2
  157.                     sngCeiling = sngCeiling*intMultiply
  158.                 Wend
  159.             End Sub
  160.         </script>
  161.     </head>
  162.     <body>
  163.         <button id="btnRender">Render</button><br />
  164.         <input type="text" id="txtSeed" value="23455"/>
  165.         <div id="divViewPort"></div>
  166.     </body>
  167. </html>
Advertisement
Add Comment
Please, Sign In to add comment