Hi everyone,
I am creating a procedural terrain script in ECS, but having trouble altering render components. This code has no compile/unity errors.
Any help is greatly appreciated
using Unity.Burst;
using Unity.Entities;
using UnityEngine;
using Unity.Entities.Graphics;
using Unity.Rendering;
using Unity.Collections;
using Unity.Transforms;
using Unity.Mathematics;
using NUnit.Framework.Interfaces;
public class HeightMapper : MonoBehaviour
{
public int ChunkSize;
public Color col;
public GameObject prefab;
}
class HeightMapperBaker : Baker<HeightMapper>
{
public override void Bake(HeightMapper authoring)
{
Entity Spawner = GetEntity(TransformUsageFlags.None);
AddComponent(Spawner, new ChunkSpawnerData
{
Test = GetEntity(authoring.prefab, TransformUsageFlags.Dynamic),
ChunkSize = authoring.ChunkSize,
Colour = authoring.col
}); ;
Debug.Log("Baked");
}
}
public struct ChunkSpawnerData : IComponentData
{
public int ChunkSize;
public Color Colour;
public Entity Test;
}
public struct ChunkData : IComponentData
{
public Vector2 Position;
}
[BurstCompile]
public partial class GridSpawner : SystemBase
{
int ChunkSize;
bool Setup;
public EntityManager EM;
EntityArchetype Arc;
public Material Mat;
RenderMeshArray RMA;
[BurstCompile]
protected override void OnCreate()
{
base.OnCreate();
EM = EntityManager.World.EntityManager;
Arc = EM.CreateArchetype(typeof(ChunkData), typeof(RenderMesh), typeof(LocalTransform), typeof(RenderBounds), typeof(LocalToWorld));
}
[BurstCompile]
protected override void OnUpdate()
{
if (!SystemAPI.TryGetSingletonEntity<ChunkSpawnerData>(out Entity Spawner)) return;
if (!Setup) SetupVariables2(Spawner);
Log("Updated with singleton");
}
[BurstCompile]
void SetupVariables(Entity SpawnerEntity)
{
Setup = true;
EM = EntityManager.World.EntityManager;
ChunkSpawnerData Spawner = SystemAPI.GetComponent<ChunkSpawnerData>(SpawnerEntity);
ChunkSize = Spawner.ChunkSize;
Log(ChunkSize.ToString());
EntityCommandBuffer ECB = new EntityCommandBuffer(Allocator.TempJob);
Entity Chunk = ECB.CreateEntity(Arc);
Material ObjMat = BaseMat(Spawner);
Mesh PlaneMesh = CreatePlaneMesh();
RenderMesh ObjectRenderer = new RenderMesh()
{
mesh = PlaneMesh,
material = ObjMat
};
RenderBounds ObjectRendererBounds = new RenderBounds()
{
Value = PlaneMesh.bounds.ToAABB()
};
LocalTransform Transform = new LocalTransform()
{
Position = float3.zero,
Rotation = quaternion.EulerXYZ(0, 0, 0),
Scale = 1
};
ChunkData ThisChunkData = new ChunkData()
{
Position = Vector2.zero
};
LocalToWorld LTW = new LocalToWorld()
{
Value = Transform.ToMatrix()
};
ECB.SetSharedComponentManaged(Chunk, ObjectRenderer);
ECB.SetComponent(Chunk, ObjectRendererBounds);
ECB.SetComponent(Chunk, Transform);
ECB.SetComponent(Chunk, ThisChunkData);
ECB.SetComponent(Chunk, LTW);
ECB.AddComponent(Chunk, new Simulate());
ECB.SetName(Chunk, "NEWCHUNK");
ECB.Playback(EM);
ECB.Dispose();
}
[BurstCompile]
void SetupVariables2(Entity SpawnerEntity)
{
Setup = true;
ChunkSpawnerData Spawner = SystemAPI.GetComponent<ChunkSpawnerData>(SpawnerEntity);
ChunkSize = Spawner.ChunkSize;
Log(ChunkSize.ToString());
Entity Sample = EM.Instantiate(Spawner.Test);
RMA = EM.GetSharedComponentManaged<RenderMeshArray>(Sample);
EntityCommandBuffer ECB = new EntityCommandBuffer(Allocator.TempJob);
Material Mat = BaseMat(Spawner);
Mesh ObjectMesh = CreatePlaneMesh();
Entity Chunk = ECB.Instantiate(Spawner.Test);
RMA.Meshes = new Mesh[] { ObjectMesh };
ECB.SetName(Chunk, "NEWCHUNK");
ECB.SetSharedComponentManaged(Chunk, RMA);
ECB.Playback(EM);
ECB.Dispose();
}
[BurstCompile]
Mesh CreatePlaneMesh()
{
Mesh Result = new Mesh();
float PlaneSize = 10;
Vector3[] vertices = {
new Vector3(-0.5f, 0, -0.5f) * PlaneSize,
new Vector3(0.5f, 0, -0.5f) * PlaneSize,
new Vector3(-0.5f, 0, 0.5f) * PlaneSize,
new Vector3(0.5f, 0, 0.5f) * PlaneSize
};
int[] triangles = {
0, 1, 2,
1, 3, 2
};
Vector2[] uvs = new Vector2[vertices.Length];
for (int i = 0; i < uvs.Length; i++)
{
uvs[i] = new Vector2(vertices[i].x + 0.5f, vertices[i].z + 0.5f);
}
Result.vertices = vertices;
Result.triangles = triangles;
Result.uv = uvs;
Result.RecalculateNormals();
return Result;
}
Material BaseMat(ChunkSpawnerData Spawner)
{
Material DefaultMat = new Material(Shader.Find("HDRP/Lit"));
DefaultMat.SetFloat("_CullMode", (float)UnityEngine.Rendering.CullMode.Off);
DefaultMat.color = Color.red;
return DefaultMat;
}
void Log(string msg)
{
Debug.Log(msg);
}
}