public Entity CreateBlock(BlockType type, int2 position) { float scale = config.blockScale; Entity entity = CreateBaseEntity(VisualOrigin + new float3(position.x, 0, position.y) * scale); EntityManager.AddComponentData(entity, new NonUniformScale() { Value = new float3(scale, GroupSize.x * 2, scale) }); EntityManager.AddComponentData(entity, new Block() { Type = type }); EntityManager.AddComponentData(entity, new BlockPoint() { Value = position }); EntityManager.AddComponentData(entity, new Depth() { Value = 0 }); RenderMeshUtility.AddComponents(entity, EntityManager, MeshDescriptions[(int)type]); return(entity); }
// RenderMeshUtility.AddComponents code example as actual code to make sure it compiles. void CodeExample() { var world = World.DefaultGameObjectInjectionWorld; var entityManager = world.EntityManager; var desc = new RenderMeshDescription( Mesh, Material); // RenderMeshUtility can be used to easily create Hybrid Renderer // compatible entities, but it can only be called from the main thread. var entity = entityManager.CreateEntity(); RenderMeshUtility.AddComponents( entity, entityManager, desc); entityManager.AddComponentData(entity, new ExampleComponent()); // If multiple similar entities are to be created, 'entity' can now // be instantiated using Instantiate(), and its component values changed // afterwards. // This can also be done in Burst jobs using EntityCommandBuffer.ParallelWriter. var secondEntity = entityManager.Instantiate(entity); entityManager.SetComponentData(secondEntity, new Translation { Value = new float3(1, 2, 3) }); }
public Entity CreateWorker(WorkerConfig workerConfig, int2 position) { float3 worldPos = blockGroupSystem.VisualOrigin + new float3(position.x, 0, position.y); Entity block = blockGroupSystem.GetBlock(position); Entity entity = CreateBaseEntity(worldPos); Color color = workerConfig.Color; EntityManager.AddComponentData(entity, new Worker() { Ability = workerConfig.ability, CurrentBlock = block, Frequency = workerConfig.frequency, SizeLossPerHit = workerConfig.sizeLossPerHit, Radius = workerConfig.radius, MaxBounces = workerConfig.maxBounces, MarkDuration = workerConfig.markDuration, Timer = 0.75f, Color = new float4(color.r, color.g, color.b, color.a) }); EntityManager.AddComponentData(entity, new DestinationPoint() { Value = position, PreviousPoint = position }); EntityManager.AddComponentData(entity, new VerticalLimit() { FlightHeight = Random.CreateFromIndex((uint)Time.ElapsedTime).NextInt(5, 8) }); EntityManager.AddComponentData(entity, new NonUniformScale() { Value = workerConfig.size }); EntityManager.AddComponentData(entity, new DrillPower() { Amount = workerConfig.power, Frequency = workerConfig.frequency }); EntityManager.AddComponentData(entity, new WorkerAnimations() { Bounce = bounceCurve, Move = moveCurve }); RenderMeshUtility.AddComponents(entity, EntityManager, cachedMeshDescriptions[workerConfig]); EntityManager.SetComponentData(entity, new Translation() { Value = blockGroupSystem.ToWorldPoint(position, 0) }); return(entity); }
public void AddPrimitiveInstanced( uint nodeIndex, string meshName, Mesh mesh, int[] materialIndices, uint instanceCount, NativeArray <Vector3>?positions, NativeArray <Quaternion>?rotations, NativeArray <Vector3>?scales, int primitiveNumeration = 0 ) { if ((settings.mask & ComponentType.Mesh) == 0) { return; } foreach (var materialIndex in materialIndices) { var material = gltf.GetMaterial(materialIndex) ?? gltf.GetDefaultMaterial(); material.enableInstancing = true; var renderMeshDescription = new RenderMeshDescription(mesh, material, subMeshIndex: materialIndex); var prototype = entityManager.CreateEntity(nodeArcheType); RenderMeshUtility.AddComponents(prototype, entityManager, renderMeshDescription); if (scales.HasValue) { entityManager.AddComponent <NonUniformScale>(prototype); } for (var i = 0; i < instanceCount; i++) { var instance = i > 0 ? entityManager.Instantiate(prototype) : prototype; entityManager.SetComponentData(instance, new Translation { Value = positions?[i] ?? Vector3.zero }); entityManager.SetComponentData(instance, new Rotation { Value = rotations?[i] ?? Quaternion.identity }); entityManager.SetComponentData(instance, new Parent { Value = nodes[nodeIndex] }); if (scales.HasValue) { entityManager.SetComponentData(instance, new NonUniformScale() { Value = scales.Value[i] }); } } } }
public static void AddRenderMeshRenderer(EntityManager em, Entity e, Material mat = default) { mat = mat == default ? Resources.Load <Material>("Terminal8x8") : mat; em.AddComponents(e, _meshDataTypes); var mesh = new Mesh(); mesh.MarkDynamic(); RenderMeshUtility.AddComponents(e, em, new RenderMeshDescription { RenderMesh = new RenderMesh { mesh = mesh, material = mat } }); }
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( Mesh, Material, shadowCastingMode: ShadowCastingMode.Off, receiveShadows: false); // 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); 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(), EntityCount = EntityCount, }; var spawnHandle = spawnJob.Schedule(EntityCount, 128); spawnHandle.Complete(); ecb.Playback(entityManager); ecb.Dispose(); entityManager.DestroyEntity(prototype); }
private void SwapRenderMesh(Entity entity, bool isTorso, Mesh torsoMesh, Mesh mesh) { var origMeshData = EntityManager.GetSharedComponentData <RenderMesh>(entity); EntityManager.RemoveComponent <RenderMesh>(entity); var renderMeshDescription = new RenderMeshDescription(isTorso ? torsoMesh : mesh, DynamicMaterial, ShadowCastingMode.On); RenderMeshUtility.AddComponents(entity, EntityManager, renderMeshDescription); EntityManager.AddComponentData(entity, new LocalToWorld()); if (!isTorso) { EntityManager.AddComponentData(entity, new NonUniformScale { Value = origMeshData.mesh.bounds.size, }); } }
// Start is called before the first frame update void Start() { var world = World.DefaultGameObjectInjectionWorld; var entityManager = world.EntityManager; EntityCommandBuffer ecbJob = new EntityCommandBuffer(Allocator.TempJob); EntityCommandBuffer ecbMainThread = new EntityCommandBuffer(Allocator.Temp); var desc = new RenderMeshDescription( Mesh, Material, shadowCastingMode: ShadowCastingMode.Off, receiveShadows: false); var prototype = entityManager.CreateEntity(); RenderMeshUtility.AddComponents( prototype, entityManager, desc); entityManager.AddComponentData(prototype, new MaterialColor()); // 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 = ecbJob.AsParallelWriter(), EntityCount = EntityCount, ObjectScale = ObjectScale, Radius = Radius, Twists = Twists, }; int numJobEntities = EntityCount - MainThreadEntityCount; var spawnHandle = spawnJob.Schedule(numJobEntities, 128); // Spawn a small portion in the main thread to test that the ECB API works. // This is NOT the recommended way, this simply tests that this API works. for (int i = 0; i < MainThreadEntityCount; ++i) { int index = i + numJobEntities; var e = ecbMainThread.CreateEntity(); RenderMeshUtility.AddComponents( e, ecbMainThread, desc); ecbMainThread.SetComponent(e, new LocalToWorld { Value = spawnJob.ComputeTransform(index) }); // Use AddComponent because we didn't clone the prototype here ecbMainThread.AddComponent(e, new MaterialColor { Value = spawnJob.ComputeColor(index) }); } spawnHandle.Complete(); ecbJob.Playback(entityManager); ecbJob.Dispose(); ecbMainThread.Playback(entityManager); entityManager.DestroyEntity(prototype); }
public virtual void AddPrimitive( uint nodeIndex, string meshName, Mesh mesh, int[] materialIndices, uint[] joints = null, uint?rootJoint = null, float[] morphTargetWeights = null, int primitiveNumeration = 0 ) { if ((settings.mask & ComponentType.Mesh) == 0) { return; } Entity node; if (primitiveNumeration == 0) { // Use Node GameObject for first Primitive node = nodes[nodeIndex]; } else { node = entityManager.CreateEntity(nodeArcheType); entityManager.SetComponentData(node, new Translation { Value = new float3(0, 0, 0) }); entityManager.SetComponentData(node, new Rotation { Value = quaternion.identity }); entityManager.SetComponentData(node, new Parent { Value = nodes[nodeIndex] }); } var hasMorphTargets = mesh.blendShapeCount > 0; for (var index = 0; index < materialIndices.Length; index++) { var material = gltf.GetMaterial(materialIndices[index]) ?? gltf.GetDefaultMaterial(); RenderMeshUtility.AddComponents(node, entityManager, new RenderMeshDescription(mesh, material, layer: settings.layer, subMeshIndex: index)); if (joints != null || hasMorphTargets) { if (joints != null) { var bones = new Entity[joints.Length]; for (var j = 0; j < bones.Length; j++) { var jointIndex = joints[j]; bones[j] = nodes[jointIndex]; } // TODO: Store bone entities array somewhere (pendant to SkinnedMeshRenderer.bones) } if (morphTargetWeights != null) { for (var i = 0; i < morphTargetWeights.Length; i++) { var weight = morphTargetWeights[i]; // TODO set blend shape weight in proper component (pendant to SkinnedMeshRenderer.SetBlendShapeWeight(i, weight); ) } } } } }
protected override void OnUpdate() { if (hitSource == null) { hitSource = GameObject.Find("HitAudioSource").GetComponent <AudioSource>(); } EntityCommandBuffer buffer = commandBufferSystem.CreateCommandBuffer(); quaternion rot = rotation; if (query.CalculateEntityCount() == 0) { return; } NativeArray <Entity> result = query.ToEntityArray(Allocator.Temp); NativeArray <Translation> positions = query.ToComponentDataArray <Translation>(Allocator.Temp); NativeArray <Depth> dents = query.ToComponentDataArray <Depth>(Allocator.Temp); NativeArray <Mark> marks = query.ToComponentDataArray <Mark>(Allocator.Temp); for (int i = 0; i < result.Length; i++) { Mark mark = marks[i]; Entity entity = result[i]; float4 c = marks[i].Color; Color color = new Color(c.x, c.y, c.z, c.w); Entity effect = buffer.CreateEntity(); float3 pos = positions[i].Value; pos.y = -dents[i].Value + 0.01f; buffer.AddComponent(effect, new Translation() { Value = pos }); buffer.AddComponent(effect, new LocalToWorld()); buffer.AddComponent(effect, new Rotation() { Value = rot }); buffer.AddComponent(effect, new Effect() { Timer = 0, Duration = mark.Duration, Size = 1f, Block = mark.Block }); buffer.AddComponent(effect, new Scale() { Value = 1 }); buffer.AddComponent(effect, new BlockColor() { Value = new float4(color.r, color.g, color.b, color.a * mark.Power) }); buffer.AddComponent(effect, new BlockGlow() { Value = new float4(color.r, color.g, color.b, color.a * mark.Power) }); RenderMeshUtility.AddComponents(effect, buffer, effectMesh); buffer.SetComponent(entity, mark); buffer.RemoveComponent <Mark>(entity); if (Time.ElapsedTime - lastSoundPlayTime > 0.05f) { lastSoundPlayTime = (float)Time.ElapsedTime; AudioClip sound = drillConfig.hitSounds[math.clamp((int)(dents[i].Value * drillConfig.hitSounds.Length), 0, drillConfig.hitSounds.Length - 1)]; hitSource.PlayOneShot(sound); } } positions.Dispose(); dents.Dispose(); result.Dispose(); marks.Dispose(); }