- Home /
ECS How can I pass a variable into rendermesh closure?
I've been following a couple videos, one on procedural generation and another on the ecs system. I've got pretty much everything figured out in this test but when I set the shared component data rendermesh the variable I try to pass only returns as the last value that was set. Similar to a lambda expression.
This is the code and below I'll go more into depth on where the problem exactly is.
var rnd = new Unity.Mathematics.Random((uint)System.DateTime.UtcNow.Ticks);
// Creating Randomness.
float[,] noiseMap = Noise.GenerateNoiseMap(mapSize.x, mapSize.y, seed, noiseScale, octaves, persistence, lacunarity, offset);
System.Random prng = new System.Random(seed);
// Looping over entities
for (int x = 0; x < mapSize.x; x++)
{
for(int y = 0; y < mapSize.y; y++)
{
float tileHeight = noiseMap[x, y];
TerrainType tileTerrain;
for (int i = 0; i < regions.Length; i++)
{
if (tileHeight <= regions[i].terrainHeight)
{
Vector2 tilePosition = CoordToPosition(x, y);
float evaluatedHeight = tileHeightCurve.Evaluate(tileHeight) * 10;
int z = 0;
for (z = 0; z < Mathf.FloorToInt(evaluatedHeight); z++)
{
var spriteEntity = spriteEntities[x * mapSize.y + y];
// Assigning values to the renderer.
entityManager.SetSharedComponentData(spriteEntity, new RenderMesh { mesh = spriteMesh, material = spriteMaterials[regions[i].spriteListIndex] });
// Assigning random position.
entityManager.SetComponentData(spriteEntity, new Translation { Value = new float3(tilePosition.x, tilePosition.y + (heightMultiplier * (z-1)), 0) });
}
}
}
}
}
The following line
entityManager.SetSharedComponentData(spriteEntity, new RenderMesh { mesh = spriteMesh, material = spriteMaterials[regions[i].spriteListIndex] });
Since the RenderMesh values are enclosed I don't know how to pass the updated spriteListIndex for that variable.
When the line is changed to the following
entityManager.SetSharedComponentData(spriteEntity, new RenderMesh { mesh = spriteMesh, material = spriteMaterials[rnd.NextInt(spriteMaterials.Count)] });
This actually loads additional materials although they are random and not desired this still confuses me. Here are two screenshots, second image is the random sprite example. https://gyazo.com/3c62e608c22373d4cfe74650593d2eb2 https://gyazo.com/be744c12606424811a0f8097cdc807c8 Any explanation or assistance would be greatly appreciated.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Class Scope Variable 1 Answer