Browse Source

Noise Toy

Added a Noise Toy to demo the new NoiseGenerator!
Peter Robinson 3 years ago
parent
commit
1b214352e2
3 changed files with 237 additions and 0 deletions
  1. 97 0
      toybox/NoiseToy/main.cs
  2. 11 0
      toybox/NoiseToy/module.taml
  3. 129 0
      toybox/NoiseToy/scripts/terrainMap.cs

+ 97 - 0
toybox/NoiseToy/main.cs

@@ -0,0 +1,97 @@
+//-----------------------------------------------------------------------------
+// Copyright (c) 2013 GarageGames, LLC
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to
+// deal in the Software without restriction, including without limitation the
+// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+// sell copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+// IN THE SOFTWARE.
+//-----------------------------------------------------------------------------
+
+function NoiseToy::create( %this )
+{
+    // Load scripts.
+    exec( "./scripts/terrainMap.cs" );
+
+    // Set the sandbox drag mode availability.
+    Sandbox.allowManipulation( pan );
+    Sandbox.allowManipulation( pull );
+
+    // Set the manipulation mode.
+    Sandbox.useManipulation( pan );
+
+    // Configure the toy.
+    NoiseToy.Seed = getRandom(1, 100);
+	NoiseToy.OctaveCount = 4;
+	NoiseToy.Persistence = 0.5;
+
+	SandboxScene.clear();
+
+	//Create our noise generator
+	NoiseToy.generator = new NoiseGenerator();
+
+	//Create our map
+	NoiseToy.terrain = new CompositeSprite()
+	{
+		class = "TerrainMap";
+	};
+
+    // Add the configuration options.
+    addSelectionOption( "Earth,Moon,Mars,PlanetX", "Color Mode", 4, "setColorMode", false, "Changes the colors used for different values." );
+    addNumericOption("Seed", 1, 999999, 1, "setNoiseSeed", NoiseToy.Seed, false, "A given seed always prouduces the same result." );
+    addNumericOption("Octave Count", 1, 8, 1, "setOctaveCount", NoiseToy.OctaveCount, false, "Each octave produces a smaller layer of noise added to the result." );
+    addNumericOption("Persistence", 0.05, 0.95, 0.05, "setPersistence", NoiseToy.persistence, false, "Higher values of persistence allow each octave to have a greater impact on the result." );
+
+    // Reset the toy.
+    %this.reset();
+}
+
+function NoiseToy::destroy( %this )
+{
+}
+
+function NoiseToy::reset(%this)
+{
+	%this.generator.setSeed(%this.seed);
+
+	%this.terrain.generate(%this.generator, %this.octaveCount, %this.persistence);
+}
+
+function NoiseToy::setColorMode(%this, %value)
+{
+	%this.terrain.colorMode = %value;
+
+	%this.terrain.generate(%this.generator, %this.octaveCount, %this.persistence);
+}
+
+function NoiseToy::setNoiseSeed( %this, %value )
+{
+    NoiseToy.Seed = %value;
+	%this.reset();
+}
+
+function NoiseToy::setOctaveCount( %this, %value )
+{
+    NoiseToy.OctaveCount = %value;
+
+	%this.terrain.generate(%this.generator, %this.octaveCount, %this.persistence);
+}
+
+function NoiseToy::setPersistence( %this, %value )
+{
+    NoiseToy.Persistence = %value;
+
+	%this.terrain.generate(%this.generator, %this.octaveCount, %this.persistence);
+}

+ 11 - 0
toybox/NoiseToy/module.taml

@@ -0,0 +1,11 @@
+<ModuleDefinition
+    ModuleId="NoiseToy"
+	VersionId="1"
+	BuildId="1"
+	Description="Generate terrain using the Perlin Noise function!"
+    Dependencies="ToyAssets=*"
+	Type="toy"
+	ToyCategoryIndex="3"
+	ScriptFile="main.cs"
+	CreateFunction="create"
+	DestroyFunction="destroy" />

+ 129 - 0
toybox/NoiseToy/scripts/terrainMap.cs

@@ -0,0 +1,129 @@
+//-----------------------------------------------------------------------------
+// Copyright (c) 2013 GarageGames, LLC
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to
+// deal in the Software without restriction, including without limitation the
+// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+// sell copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+// IN THE SOFTWARE.
+//-----------------------------------------------------------------------------
+
+function TerrainMap::onAdd(%this)
+{
+	%tileSize = 1.4;
+	%this.mapSize = 75;
+	%center = (%tileSize / 2) - ((%this.mapSize * %tileSize) / 2);
+	%this.BatchCulling = true;
+	%this.BatchLayout = "rect";
+	%this.BatchIsolated = false;
+	%this.BatchSortMode = "Batch";
+	%this.DefaultSpriteSize = %tileSize;
+	%this.DefaultSpriteStride = %tileSize;
+	%this.Position = %center SPC %center;
+	%this.BodyType = static;
+	%this.Enabled = true;
+
+	//Create the tile
+	for(%y = 0; %y < %this.mapSize; %y++)
+	{
+		for(%x = 0; %x < %this.mapSize; %x++)
+		{
+			%this.addSprite(%x SPC %y);
+			%this.setSpriteImage("ToyAssets:Blank");
+		}
+	}
+
+	SandboxScene.add(%this);
+	%this.colorMode = "Earth";
+}
+
+function TerrainMap::generate(%this, %generator, %octaveCount, %persistence)
+{
+	for(%y = 0; %y < %this.mapSize; %y++)
+	{
+		for(%x = 0; %x < %this.mapSize; %x++)
+		{
+			%this.selectSprite(%x SPC %y);
+			%this.setSpriteBlendColor(%this.getColor(%x, %y, %generator, %octaveCount, %persistence));
+		}
+	}
+}
+
+function TerrainMap::getColor(%this, %x, %y, %generator, %octaveCount, %persistence)
+{
+	%zoomFactor = 0.1;
+	%value = %generator.getComplexNoise(%x * %zoomFactor, %y * %zoomFactor, %octaveCount, %persistence);
+
+	return %this.call("get" @ %this.colorMode @ "Color", %value);
+}
+
+function TerrainMap::getMoonColor(%this, %value)
+{
+	return %value SPC %value SPC %value SPC "1";
+}
+
+function TerrainMap::getEarthColor(%this, %value)
+{
+	if(%value > 0.7)
+	{
+		return (%value + 0.2) SPC (%value + 0.2) SPC (%value + 0.2) SPC "1";
+	}
+	else if(%value > 0.6)
+	{
+		%value = 0.4 + (%value / 10);
+		return %value SPC %value SPC (%value + 0.05) SPC "1";
+	}
+	else if(%value > 0.5)
+	{
+		return (%value / 5) SPC (%value * 1.2) SPC (%value / 5) SPC "1";
+	}
+	else if(%value > 0.48)
+	{
+		return "1" SPC "0.9" SPC %value SPC "1";
+	}
+	else
+	{
+		%factor = %value / 0.48;
+		%red = 0.1 * %factor;
+		%green = 0.1 + (0.2 * %factor);
+		%blue = 0.4 + (0.5 * %factor);
+		return %red SPC %green SPC %blue SPC "1";
+	}
+}
+
+function TerrainMap::getMarsColor(%this, %value)
+{
+	return %value SPC (%value / 4) SPC "0" SPC "1";
+}
+
+function TerrainMap::getPlanetXColor(%this, %value)
+{
+	if(%value > 0.45)
+	{
+		%factor = ((%value - 0.45) / 0.55);
+		%red = 0.15 + (0.29 * %factor);
+		%green = 0.1 - (0.05 * (1 - %factor));
+		%blue = 0.23 + (0.45 * %factor);
+		return %red SPC %green SPC %blue SPC "1";
+	}
+	else
+	{
+		%factor = %value / 0.45;
+		%red = 0.1 * %factor;
+		%green = 0.2 + (0.3 * %factor);
+		%blue = 0.05 + (0.1 * %factor);
+		return %red SPC %green SPC %blue SPC "1";
+	}
+}