====== 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 | {{:scripts:HF_SineDemo.zs|HF_SineDemo.zs}} | ===== About ===== The 'HF_SineDemo' is a complex script that generates sinusoidal heightfields like this: {{:scripts:hf_sinedemo:hf_sinedemo.png|}} 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 > project.InitProject endif // // get map handle, and check size // set hMap assert hMap "Cannot get heightfield!" // getting size (nx = width, ny = height) set nx set ny // if heightfield is initialised, store backup if >> // 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 > return 0 endif set nx templist.Width_px set ny templist.Height_px set f templist.HorizScale_m // trying to initialise heightfield assert "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 > return 0 endif // // prepare some variables for the loop // float fx fy phase freq Angle_rad set freq
Settings.Period> set Angle_rad 3.14159> set Angle_rad 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 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 i> // fx = i * cos(angle) set fy j> // fy = j * sin(angle) set phase set f > // f = sin (freq * phase) set f // f = f * amplitude map.SetPixel hMap i j &f while nx> while 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