Created
February 19, 2019 13:50
-
-
Save nothke/de0d15af18e1975c89548aa520bcccf1 to your computer and use it in GitHub Desktop.
ECS LOD test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using Unity.Entities; | |
using Unity.Mathematics; | |
using Unity.Collections; | |
using Unity.Transforms; | |
using Unity.Rendering; | |
public class LODTest : MonoBehaviour | |
{ | |
public Mesh mesh; | |
public Mesh meshLod0; | |
public Material material; | |
NativeArray<Entity> entities; | |
public float separation = 1; | |
EntityManager manager; | |
private void Start() | |
{ | |
manager = World.Active.GetOrCreateManager<EntityManager>(); | |
int size = 100; | |
entities = new NativeArray<Entity>(size * size, Allocator.Persistent); | |
for (int y = 0; y < size; y++) | |
{ | |
for (int x = 0; x < size; x++) | |
{ | |
var position = new float3( | |
x * separation, 0, | |
y * separation); | |
SpawnWithLOD(position); | |
} | |
} | |
} | |
void SpawnWithLOD(float3 position) | |
{ | |
// LOD GROUP | |
Entity lodGroup = manager.CreateEntity( | |
typeof(MeshLODGroupComponent), | |
typeof(ActiveLODGroupMask), | |
typeof(Position), | |
typeof(RenderMesh), | |
typeof(MeshLODComponent)); | |
var mlogc = new MeshLODGroupComponent() | |
{ | |
LODDistances0 = new float4(50, 100, 200, 400), | |
WorldReferencePoint = position | |
}; | |
var pos = new Position() { Value = position }; | |
var rm = new RenderMesh() | |
{ | |
material = material, | |
mesh = mesh, | |
}; | |
var mloc = new MeshLODComponent() | |
{ | |
Group = lodGroup, | |
LODMask = 1 | |
}; | |
manager.SetComponentData(lodGroup, mlogc); | |
manager.SetComponentData(lodGroup, pos); | |
manager.SetSharedComponentData(lodGroup, rm); | |
manager.SetComponentData(lodGroup, mloc); | |
// LOD 1 | |
Entity lod2Entity = manager.CreateEntity( | |
typeof(Position), | |
typeof(RenderMesh), | |
typeof(MeshLODComponent)); | |
var rm2 = new RenderMesh() | |
{ | |
material = material, | |
mesh = meshLod0, | |
}; | |
var mloc2 = new MeshLODComponent() | |
{ | |
Group = lodGroup, | |
LODMask = 2 | |
}; | |
manager.SetComponentData(lod2Entity, pos); | |
manager.SetSharedComponentData(lod2Entity, rm2); | |
manager.SetComponentData(lod2Entity, mloc2); | |
} | |
private void OnDestroy() | |
{ | |
entities.Dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment