Files
Prism/ExampleApp/Src/MapGenerator.cs

99 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Prism;
namespace Example
{
public class MapGenerator : Entity
{
// [EditorSlider("MapWidth Custom Name", 2, 0, 1024)]
public int MapWidth = 128;
public int MapHeight = 128;
public int Octaves = 4;
public float Persistance = 0.74f;
public int Seed = 21;
public float Lacunarity = 3.0f;
public Vec2 Offset = new Vec2(13.4f, 6.26f);
public float NoiseScale = 0.5f;
public float speed = 0.0f;
public void GenerateMap()
{
// float[,] noiseMap = Noise.GenerateNoiseMap(MapWidth, MapHeight, NoiseScale);
float[,] noiseMap;
try
{
noiseMap = Noise.GenerateNoiseMap(MapWidth, MapHeight, Seed, NoiseScale, Octaves, Persistance, Lacunarity, Offset);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
uint width = (uint)noiseMap.GetLength(0);
uint height = (uint)noiseMap.GetLength(1);
Texture2D texture = new Texture2D(width, height);
Vec4[] colorMap = new Vec4[width * height];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
colorMap[x + y * width] = Vec4.Lerp(Color.Black, Color.White, noiseMap[x, y]);
}
}
texture.SetData(colorMap);
Console.WriteLine("HasComponent - TransformComponent = {0}", HasComponent<TransformComponent>());
Console.WriteLine("HasComponent - ScriptComponent = {0}", HasComponent<ScriptComponent>());
Console.WriteLine("HasComponent - MeshComponent = {0}", HasComponent<MeshComponent>());
MeshComponent meshComponent = GetComponent<MeshComponent>();
if (meshComponent == null)
{
Console.WriteLine("MeshComponent is null!");
meshComponent = CreateComponent<MeshComponent>();
}
meshComponent.Mesh = MeshFactory.CreatePlane(1.0f, 1.0f);
Console.WriteLine("Mesh has {0} materials!", meshComponent.Mesh.GetMaterialCount());
MaterialInstance material = meshComponent.Mesh.GetMaterial(1);
material.Set("u_AlbedoTexToggle", 1.0f);
material.Set("u_AlbedoTexture", texture);
}
void OnCreate()
{
GenerateMap();
}
void OnUpdate(float ts)
{
Mat4 transform = GetTransform();
Vec3 translation = transform.Translation;
translation.Y += ts * speed;
if (Input.IsKeyPressed(KeyCode.Space))
{
Console.WriteLine("Space Pressed");
translation.Y -= 10.0f;
}
transform.Translation = translation;
SetTransform(transform);
}
}
}