add mono c# (using dotnet9.0 from https://github.com/dotnet/runtime), add ECS (entt), add some c# code, add fastNoise
This commit is contained in:
84
ExampleApp/Src/MapGenerator.cs
Normal file
84
ExampleApp/Src/MapGenerator.cs
Normal file
@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Prism;
|
||||
|
||||
namespace Example
|
||||
{
|
||||
public class MapGenerator : Entity
|
||||
{
|
||||
// TODO: [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 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)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
112
ExampleApp/Src/Noise.cs
Normal file
112
ExampleApp/Src/Noise.cs
Normal file
@ -0,0 +1,112 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Prism;
|
||||
|
||||
namespace Example
|
||||
{
|
||||
public static class Noise
|
||||
{
|
||||
public static float[,] GenerateNoiseMap(int mapWidth, int mapHeight, float scale)
|
||||
{
|
||||
float[,] noiseMap = new float[mapWidth, mapHeight];
|
||||
|
||||
if (scale <= 0)
|
||||
scale = 0.0001f;
|
||||
|
||||
for (int y = 0; y < mapHeight; y++)
|
||||
{
|
||||
for (int x = 0; x < mapWidth; x++)
|
||||
{
|
||||
float sampleX = x / scale;
|
||||
float sampleY = y / scale;
|
||||
|
||||
float perlinValue = Prism.Noise.PerlinNoise(sampleX, sampleY);
|
||||
noiseMap[x, y] = perlinValue;
|
||||
}
|
||||
}
|
||||
|
||||
return noiseMap;
|
||||
}
|
||||
|
||||
public static float InverseLerp(float min, float max, float value)
|
||||
{
|
||||
if (Math.Abs(max - min) < 0.000001f) return min;
|
||||
return (value - min) / (max - min);
|
||||
}
|
||||
|
||||
public static float[,] GenerateNoiseMap(int mapWidth, int mapHeight, int seed, float scale, int octaves, float persistance, float lacunarity, Vec2 offset)
|
||||
{
|
||||
float[,] noiseMap = new float[mapWidth, mapHeight];
|
||||
|
||||
System.Random prng = new System.Random(seed);
|
||||
|
||||
Vec2[] octaveOffsets = new Vec2[octaves];
|
||||
for (int i = 0; i < octaves; i++)
|
||||
{
|
||||
float offsetX = prng.Next(-100000, 100000) + offset.X;
|
||||
float offsetY = prng.Next(-100000, 100000) + offset.Y;
|
||||
octaveOffsets[i] = new Vec2(offsetX, offsetY);
|
||||
}
|
||||
|
||||
if (scale <= 0)
|
||||
{
|
||||
scale = 0.0001f;
|
||||
}
|
||||
|
||||
float maxNoiseHeight = float.MinValue;
|
||||
float minNoiseHeight = float.MaxValue;
|
||||
|
||||
float halfWidth = mapWidth / 2f;
|
||||
float halfHeight = mapHeight / 2f;
|
||||
|
||||
|
||||
for (int y = 0; y < mapHeight; y++)
|
||||
{
|
||||
for (int x = 0; x < mapWidth; x++)
|
||||
{
|
||||
|
||||
float amplitude = 1;
|
||||
float frequency = 1;
|
||||
float noiseHeight = 0;
|
||||
|
||||
for (int i = 0; i < octaves; i++)
|
||||
{
|
||||
float sampleX = (x - halfWidth) / scale * frequency + octaveOffsets[i].X;
|
||||
float sampleY = (y - halfHeight) / scale * frequency + octaveOffsets[i].Y;
|
||||
|
||||
float perlinValue = Prism.Noise.PerlinNoise(sampleX, sampleY);// * 2 - 1; // 0->1 // -1 -> 1
|
||||
noiseHeight += perlinValue * amplitude;
|
||||
|
||||
amplitude *= persistance;
|
||||
frequency *= lacunarity;
|
||||
}
|
||||
|
||||
if (noiseHeight > maxNoiseHeight)
|
||||
{
|
||||
maxNoiseHeight = noiseHeight;
|
||||
}
|
||||
else if (noiseHeight < minNoiseHeight)
|
||||
{
|
||||
minNoiseHeight = noiseHeight;
|
||||
}
|
||||
noiseMap[x, y] = noiseHeight;
|
||||
}
|
||||
}
|
||||
|
||||
for (int y = 0; y < mapHeight; y++)
|
||||
{
|
||||
for (int x = 0; x < mapWidth; x++)
|
||||
{
|
||||
noiseMap[x, y] = InverseLerp(minNoiseHeight, maxNoiseHeight, noiseMap[x, y]);
|
||||
}
|
||||
}
|
||||
|
||||
return noiseMap;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
38
ExampleApp/Src/Script.cs
Normal file
38
ExampleApp/Src/Script.cs
Normal file
@ -0,0 +1,38 @@
|
||||
using System;
|
||||
|
||||
using Prism;
|
||||
|
||||
namespace Example
|
||||
{
|
||||
public class Script : Entity
|
||||
{
|
||||
public float Speed = 5.0f;
|
||||
|
||||
public void OnCreate()
|
||||
{
|
||||
Console.WriteLine("Script.OnCreate");
|
||||
Console.WriteLine("this is c# function OnCreate, this will be called by cpp");
|
||||
}
|
||||
|
||||
public void OnUpdate(float ts)
|
||||
{
|
||||
Mat4 transform = GetTransform();
|
||||
Vec3 translation = transform.Translation;
|
||||
|
||||
float speed = Speed * ts;
|
||||
|
||||
if (Input.IsKeyPressed(KeyCode.Up))
|
||||
translation.Y += speed;
|
||||
else if (Input.IsKeyPressed(KeyCode.Down))
|
||||
translation.Y -= speed;
|
||||
if (Input.IsKeyPressed(KeyCode.Right))
|
||||
translation.X += speed;
|
||||
else if (Input.IsKeyPressed(KeyCode.Left))
|
||||
translation.X -= speed;
|
||||
|
||||
transform.Translation = translation;
|
||||
SetTransform(transform);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user