Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Captain_Pineapple · Jul 30, 2019 at 03:25 PM · componentsystemsharedmanaged

[ECS] Set data to a SharedComponent of Type RenderMesh from EntityCommandBuffer

Hello there,

i currently try to create a Cube while in a Job. This Cube is an entity and needs a RenderMesh component to be able to get rendered.


Problem here is the following. While i can add a MeshRender component i am not able to actually feed the needed data (Mesh, Material) in the component to get it to render. Only Entities that i created in the main thread are rendered. If i use the EntityCommandBuffer to create an entity in a job the sharedComponent RenderMesh has no data. (Which kind of makes sense) I tried feeding the Mesh/Material data directly to the job to set it there but since reference type variables are not allowed in jobs this is not possible.


Does anyone here have insight on this? If you need more info on the current setup let me know.

Cheers

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image OcarinaBoy2049 · Sep 28, 2020 at 10:52 AM 0
Share

I know this is old, but I have the same problem. Still looking for a solution

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by andrew-lukasik · Sep 28, 2020 at 04:10 PM

Entities Version 0.14.0-preview.19 - September 10, 2020


One answer to this is instantiating from pre-defined pool of entity prefabs and not touching reference types after that at all. This system below, for example, will destroy & instantiate 1000 mesh entities every frame < 1ms (Burst enabled) while sourcing prefab entities from world entities (differentiated by id/hash calculated as "specific string name id".GetHashcode()).

Note: filter_prefabs_job can be removed once you figure out a way to create a persistent NativeArray<Entity> prefabs, NativeHashMap<int,Entity> prefabs_lookup or even just a single valid Entity prefab for this system to use.

preview gif


PrefabPoolTestSystem.cs

 using Debug = UnityEngine.Debug;
 using Unity.Collections;
 using Unity.Entities;
 using Unity.Jobs;
 using Unity.Mathematics;
 using Unity.Transforms;
 using Unity.Rendering;
 using Random = Unity.Mathematics.Random;
 
 public class PrefabPoolTestSystem : SystemBase
 {
     int _poolId = "grey capsule".GetHashCode();
 
     protected override void OnCreate ()
     {
         Debug.Log($"{GetType().Name} will be looking for {nameof(EntityMeshPrefab)} hash: '{_poolId}'");
     }
     
     protected override void OnUpdate ()
     {
         var ecb = new EntityCommandBuffer( Allocator.TempJob );
         var ecbpw = ecb.AsParallelWriter();
         
         Entities
             .WithName("delete_existing_instances_job")
             .WithNone<Prefab>()// do not delete prefabs
             .WithAll<EntityMeshPrefab>()
             .ForEach( ( in int entityInQueryIndex , in Entity entity ) =>
             {
                 ecbpw.DestroyEntity( entityInQueryIndex , entity );
             } )
             .WithBurst().ScheduleParallel();
         
         NativeList<Entity> prefabs = new  NativeList<Entity>( 100 , Allocator.TempJob );
         NativeList<Entity>.ParallelWriter prefabs_writer = prefabs.AsParallelWriter();
         int poolId = _poolId;
         Entities
             .WithName("filter_prefabs_job")
             .WithAll<Prefab>()
             .ForEach( ( in int entityInQueryIndex , in Entity entity , in EntityMeshPrefab entityMeshPrefab ) =>
             {
                 if( entityMeshPrefab.id==poolId )
                     prefabs_writer.AddNoResize( entity );
             } )
             .WithBurst().ScheduleParallel();
 
         uint seed = (uint) math.pow(Time.ElapsedTime+1,2).GetHashCode();
         Job
             .WithName("instantiate_job")
             .WithReadOnly( prefabs )
             .WithCode( () =>
             {
                 int numPrefabs = prefabs.Length;
                 if( numPrefabs!=0 )
                 {
                     var rnd = new Random( seed );
                     for( int i=0 ; i<1000 ; i++ )
                     {
                         Entity prefab = prefabs[ rnd.NextInt(0,numPrefabs) ];
                         ecb.Instantiate( prefab );
                         ecb.SetComponent( prefab , new Translation{
                             Value = rnd.NextFloat3( new float3{ x=-10 , y=-10 , z=-10 } , new float3{ x=10 , y=10 , z=10 } )
                         } );
                     }
                 }
             } )
             .WithBurst().Schedule();
 
         Job
             .WithName("disposal_job")
             .WithCode( () =>
             {
                 prefabs.Dispose();
             })
             .WithoutBurst().Run();
 
         Job
             .WithName("playback_commands")
             .WithCode( () =>
             {
                 ecb.Playback( EntityManager );
                 ecb.Dispose();
             })
             .WithoutBurst().Run();
     }
 }
 

EntityMeshPrefab.cs

 using Unity.Entities;
 public struct EntityMeshPrefab : IComponentData
 {
     public int id;
 }


This component allows you to create entity prefabs from scene gameobjects in, so called, hybrid approach (GameObject to Entity workflow):

EntityMeshPrefabComponent.cs

 using UnityEngine;
 using Unity.Entities;
 
 [DisallowMultipleComponent]
 [RequireComponent( typeof(MeshFilter) , typeof(MeshRenderer) )]
 [RequiresEntityConversion]
 public class EntityMeshPrefabComponent : MonoBehaviour, IConvertGameObjectToEntity
 {
     [SerializeField] string _id = "grey capsule";
     void IConvertGameObjectToEntity.Convert ( Entity entity , EntityManager dstManager , GameObjectConversionSystem conversionSystem )
     {
         dstManager.AddComponent<Prefab>( entity );
         
         int hash = _id.GetHashCode();
         dstManager.AddComponentData<EntityMeshPrefab>( entity , new EntityMeshPrefab{
             id = hash
         } );
         Debug.Log($"{name} created an {nameof(EntityMeshPrefab)} with id '{hash}'");
     }
 }

inspector

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

110 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Question about new Component system 2 Answers

Legacy particle system doesn't work on Unity 4.0 6 Answers

2D Animation does not start 1 Answer

Entity-Component-System 2 Answers

[ECS] Create Entities from Job ( IJobForEachWithEntity) using an EntityCommandBuffer 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges