L3DT users' wiki
Large 3D terrain generator

 

Scripts > HF_SineDemo

Description A script to generate a sinusoidal heightfield, demonstrating how to set map pixel values and perform algebra in ZeoScript.
Author Aaron
Created 2016/05/12 04:31
Requires L3DT v16.05
Download HF_SineDemo.zs

About

The 'HF_SineDemo' is a complex script that generates sinusoidal heightfields like this:

This script demonstrates the following concepts:

  • Initialising a map using map.Init.
  • Storing a backup (undo) point for a map using L3DTio_Backup.BackupMap
  • Getting user input using EditUI.
  • Performing basic mathematics using mul (mutiply), div (division), add (addition), sub (subtraction), sin (sine) and cos (cosine).
  • Displaying progress boxes via progbox objects.
  • Iterating through map pixels using do/while loops.
  • Setting pixel values using map.SetPixel.

Script contents

// Author:  A. Torpy
// Updated: 12 May 2016

//
// declare variables
//

int i j nx ny
float f
hvar hMap
string MapName

set MapName "HF" // we're going to get the project's heightfield map

//
// ensure project is initialised
//
if <not <project.IsInit>>
  project.InitProject
endif


//
// get map handle, and check size
//

set hMap <GetMap MapName>
assert hMap "Cannot get heightfield!"

// getting size (nx = width, ny = height)
set nx <map.GetWidth hMap>
set ny <map.GetHeight hMap>

// if heightfield is initialised, store backup
if <not <iseq 0 <map.GetWidth hMap>>>
  // heightfield is initialised, so store a backup
  L3DTio_Backup.BackupMap "HF" "HF_SineDemo" 0 "view.ShowMap \"HF\""
else
  // heightfield not initialised, so ask for size and initialise
  
  int templist.Width_px
  int templist.Height_px
  float templist.HorizScale_m
  
  set templist.Width_px 50
  set templist.Height_px 50
  set templist.HorizScale_m 10
  
  // asking for user to enter settings
  if <not <EditUI &templist "Enter heightfield size">>
    return 0
  endif
  
  set nx templist.Width_px
  set ny templist.Height_px
  set f templist.HorizScale_m
  
  // trying to initialise heightfield
  assert <map.Init hMap nx ny 20 f false> "Cannot initalise heightfield!" // nb: "20" is map type code for heightfield in L3DT
  
endif // done initialising heightfield

//
// Ask user for sinusoid settings
//

float Settings.Amplitude
float Settings.Period
float Settings.Angle

set Settings.Amplitude 10.0
set Settings.Period 20
set Settings.Angle 45

if <not <EditUI &Settings "Enter sinusoid settings">>
  return 0
endif

//
// prepare some variables for the loop
//

float fx fy phase freq Angle_rad
set freq <div <mul 3.14159 2> Settings.Period>
set Angle_rad <mul <div Settings.Angle 180.0> 3.14159>
set Angle_rad <sub <div 3.14149 2> Angle_rad> // makes angles clockwise, with 0 at north

//
// prepare progress box
//

progbox ProgBox
int64 p pmax

set p 0
set pmax ny

progbox.ShowWnd2 &ProgBox 0x5 // 0x1 = percent, 0x2 = indef, 0x4 = allow cancel
progbox.SetTitle &ProgBox "Calculating"

//
// here we go!
//
set j 0
do // iterate over j (y-axis)

  // set progress every line
  if <not <progbox.SetProgress &ProgBox <incr p> pmax>>
    // if you got here, user has cancelled calculation
    progbox.HideWnd &ProgBox
    return 0
  endif

  set i 0
  do // iterate over i (x-axis)

    set fx <mul <cos Angle_rad> i> // fx = i * cos(angle)
    set fy <mul <sin Angle_rad> j> // fy = j * sin(angle)
    set phase <add fx fy>
    
    set f <sin <mul freq phase>> // f = sin (freq * phase)

    set f <mul f Settings.Amplitude> // f = f * amplitude
    
    map.SetPixel hMap i j &f
        
  while <islt <incr i> nx>
while <islt <incr j> ny>

progbox.HideWnd &ProgBox

//
// flag map as being ready and modified
//
map.SetFlag hMap 1 true // MAPFLAG_ISREADY
map.SetFlag hMap 5 true // MAPFLAG_ISMODIFIED

//
// refresh map display
//
view.ShowMap MapName
 
scripts/hf_sinedemo.txt · Last modified: 2017/08/31 07:10 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki