Example

Creating an image series of an animated terrain

using System;
using System.Collections.Generic;
using InstantTerraApi;

namespace MyAnimationProject
{
    //*****************************************************************************
    //*****************************************************************************
    class Program
    {
        //*****************************************************************************
        //*****************************************************************************
        static void Main()
        {
            // Prepare some variables for our animation
            int framePerSecond = 10;
            float duration = 3.0f; // Duration of the translation
            float speed = 1.0f;    // Distance per second

            int terrainWidth = 256; // Width of the terrain
            int terrainHeight = 256; // Height of the terrain

            // Name of output pictures: pic_001.tif, pic_002.tif...
            string outputPath     = @"C:\tmp\";
            string exportFilename = "pic_{0:000}.tif";

            // Create an instance of InstantTerra
            InstantTerra instantTerra = new InstantTerra();

            // Get project, graph
            Project project = instantTerra.GetProject();
            Graph graph = project.GetGraph();

            /*
             * To get the perlin noise, we just have to get the first node of the list.
             * To get the export node, we just have to get the last node of the list.
             * The default project is composed of [Perlin noise, Apply curve, Export terrain]
             */
            List<Node> nodes = graph.GetAllNodes();

            Node perlinNoiseNode = nodes[0];
            Node exportNode      = nodes[2];

            // Set the width and height of the terrain
            perlinNoiseNode.SetParameterValue("width",  terrainWidth.ToString());
            perlinNoiseNode.SetParameterValue("height", terrainHeight.ToString());

            // We have to know how many frame will compose our animation
            int totalFrames = (int)(framePerSecond * duration);

            // We also have to know the translation step of each frame
            float translationStep = speed * framePerSecond;

            // We can now start to compute the animation
            for (int frameIndex = 0; frameIndex < totalFrames; ++frameIndex)
            {
                float nextOffset = frameIndex * translationStep; // Compute the next offset
                perlinNoiseNode.SetParameterValue("offset_x", nextOffset.ToString()); // Apply the offset

                // We have to set the output filename and export it
                string filename = string.Format(exportFilename, frameIndex);
                exportNode.SetParameterValue("file_name", $"{outputPath}{filename}");

                project.ExportAll();

                // Just print a message to show the progress
                Console.Write($"Frame {frameIndex + 1} of {totalFrames} \r");
            }

            // Print a message when it is over
            Console.WriteLine("Done!");

            // Close InstantTerra
            instantTerra.Close();
        }
    }
}