forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddComponentsExample.cs
More file actions
121 lines (102 loc) · 3.98 KB
/
Copy pathAddComponentsExample.cs
File metadata and controls
121 lines (102 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System.Collections.Generic;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Rendering;
using Unity.Transforms;
using UnityEngine;
using UnityEngine.Rendering;
public class AddComponentsExample : MonoBehaviour
{
public Mesh Mesh;
public Material m_material;
public bool m_differentMaterial = false;
public int m_w = 30;
public int m_h = 30;
// Example Burst job that creates many entities
[GenerateTestsForBurstCompatibility]
public struct SpawnJob : IJobParallelFor
{
public Entity Prototype;
public int w;
public int h;
public bool singleMat;
public EntityCommandBuffer.ParallelWriter Ecb;
public void Execute(int index)
{
// Clone the Prototype entity to create a new entity.
var e = Ecb.Instantiate(index, Prototype);
// Prototype has all correct components up front, can use SetComponent to
// set values unique to the newly created entity, such as the transform.
int matIndex = singleMat ? 0 : index;
Ecb.SetComponent(index, e, MaterialMeshInfo.FromRenderMeshArrayIndices(matIndex, 0));
Ecb.SetComponent(index, e, new LocalToWorld {Value = ComputeTransform(index)});
}
public float4x4 ComputeTransform(int index)
{
int y = index / w;
int x = index % h;
float3 pos = new float3(x - (float)w * 0.5f, 0, y - (float)h * 0.5f);
return float4x4.Translate(pos);
}
}
void Start()
{
var world = World.DefaultGameObjectInjectionWorld;
var entityManager = world.EntityManager;
EntityCommandBuffer ecb = new EntityCommandBuffer(Allocator.TempJob);
// Create a RenderMeshDescription using the convenience constructor
// with named parameters.
var desc = new RenderMeshDescription(
shadowCastingMode: ShadowCastingMode.Off,
receiveShadows: false);
int objCount = m_w * m_h;
var matList = new List<Material>();
if ( m_differentMaterial )
{
for (int i=0;i<objCount;i++)
{
var mat = new Material(m_material);
Color col = Color.HSVToRGB(((float)(i * 10) / (float)objCount) % 1.0f, 0.7f, 1.0f);
// Color col = Color.HSVToRGB(Random.Range(0.0f,1.0f), 1.0f, 1.0f);
mat.SetColor("_Color", col); // set for LW
mat.SetColor("_BaseColor", col); // set for HD
matList.Add(mat);
}
}
else
{
matList.Add(m_material);
}
var meshArray = new RenderMeshArray() { Materials = matList.ToArray(), Meshes = new[] { Mesh } };
meshArray.ResetHash128();
// Create empty base entity
var prototype = entityManager.CreateEntity();
// Call AddComponents to populate base entity with the components required
// by Hybrid Renderer
RenderMeshUtility.AddComponents(
prototype,
entityManager,
desc,
meshArray,
MaterialMeshInfo.FromRenderMeshArrayIndices(0, 0));
entityManager.AddComponentData(prototype, new LocalToWorld());
// Spawn most of the entities in a Burst job by cloning a pre-created prototype entity,
// which can be either a Prefab or an entity created at run time like in this sample.
// This is the fastest and most efficient way to create entities at run time.
var spawnJob = new SpawnJob
{
Prototype = prototype,
Ecb = ecb.AsParallelWriter(),
w = m_w,
h = m_h,
singleMat = !m_differentMaterial
};
var spawnHandle = spawnJob.Schedule(m_h*m_w,128);
spawnHandle.Complete();
ecb.Playback(entityManager);
ecb.Dispose();
entityManager.DestroyEntity(prototype);
}
}