|
Table of Contents
Scripts > HF_SineDemo
AboutThe 'HF_SineDemo' is a complex script that generates sinusoidal heightfields like this: This script demonstrates the following concepts:
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
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Share Alike 3.0 Unported
|