Example #1
0
        public async Task Register(WorkerConfig workerConfig)
        {
            RenderMeshDescription rend = await workerConfig.GetDescription();

            workerConfig.Color = rend.RenderMesh.material.color;
            cachedMeshDescriptions[workerConfig] = rend;
        }
Example #2
0
    // 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)
        });
    }
        protected override async void OnCreate()
        {
            commandBufferSystem = World.GetExistingSystem <EntityCommandBufferSystem>();
            query       = GetEntityQuery(typeof(Mark), typeof(Translation), typeof(Depth));
            drillConfig = await Addressables.LoadAssetAsync <DrillConfig>("configs/drill").Task;

            effectMesh = drillConfig.hitEffect.GetDescription();
            rotation   = drillConfig.hitEffect.transform.rotation;
        }
Example #4
0
 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]
                 });
             }
         }
     }
 }
Example #5
0
    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);
    }
Example #6
0
    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,
            });
        }
    }
Example #7
0
    // 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);
    }