Search Unity

[SOLVED] SetComponentData(Entities, new Rotation { Value = r}); cause entities to randomly rotate .

Discussion in 'Data Oriented Technology Stack' started by Quatum1000, Jul 26, 2018.

  1. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    724
    Hi,

    on creating an entity by
    .SetComponentData(Entities, new Rotation { Value = GameObject.transform.rotation}); and without starting a job, cause all visible entities start to rotate randomly on play.

    Code (CSharp):
    1. using Unity.Entities;
    2. using UnityEngine;
    3. using Unity.Transforms;
    4. using Unity.Rendering;
    5.  
    6. public class ECS_StaticObjectManager : MonoBehaviour {
    7.    public GameObject GameObject;
    8.  
    9.    private void Awake() {
    10.        var entityManager = World.Active.GetOrCreateManager<EntityManager>();
    11.  
    12.        Entity[] Entities = new Entity[1];
    13.  
    14.        Entities[0] = entityManager.CreateEntity(
    15.                ComponentType.Create<Position>(),
    16.                ComponentType.Create<Rotation>(),
    17.                ComponentType.Create<TransformMatrix>(),
    18.                ComponentType.Create<MeshInstanceRenderer>());
    19.  
    20.        var r = GameObject.transform.rotation;
    21.  
    22.        Rotation ftm = new Rotation();
    23.        ftm = new Rotation { Value = GameObject.transform.rotation };
    24.        entityManager.SetComponentData(Entities[0], new Rotation { Value = ftm.Value });
    25.        entityManager.SetComponentData(Entities[0], new Position { Value = GameObject.transform.position});
    26.      
    27.        var mat = GameObject.gameObject.GetComponent<MeshRenderer>().sharedMaterial;
    28.        mat.enableInstancing = true;
    29.        var mesh = GameObject.gameObject.GetComponent<MeshFilter>().sharedMesh;
    30.              
    31.        entityManager.SetSharedComponentData(Entities[0], new MeshInstanceRenderer {
    32.            mesh = mesh,
    33.            material = mat,
    34.        });
    35.    }
    36. }
    37.  
    SetComponentData(Entities, new Rotation (Value = Quaternion.identity}) stops the randomly false rotation after play. (..., new LocalRotation) doesn't work also.

    I want to use the entity manager to simply display some game object meshes, without rotation or moving. Any idea what this strange rotation cause and how to init with a correct rotation?

    Thank you
     
  2. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    724
    The ECS was broken or out of tune. Now its works for any reason.

    With some small acknowledge it's possible to render all static gameobjects in the scene by pure ECS. No idea what happen to the GI in this case.

    Code (CSharp):
    1.     using Unity.Entities;
    2.     using UnityEngine;
    3.     using Unity.Transforms;
    4.     using Unity.Rendering;
    5.    
    6.     public class ECS_StaticObjectManager : MonoBehaviour {
    7.  
    8.        public GameObject GameObject;
    9.    
    10.        private void Awake() {
    11.            var entityManager = World.Active.GetOrCreateManager<EntityManager>();
    12.    
    13.            Entity[] Entities = new Entity[1];
    14.    
    15.            Entities[0] = entityManager.CreateEntity(
    16.                    ComponentType.Create<Position>(),
    17.                    ComponentType.Create<Rotation>(),
    18.                    ComponentType.Create<TransformMatrix>(),
    19.                    ComponentType.Create<MeshInstanceRenderer>());
    20.    
    21.            entityManager.SetComponentData(Entities[0], new Rotation { Value = GameObject.transform.rotation });
    22.            entityManager.SetComponentData(Entities[0], new Position { Value = GameObject.transform.position});
    23.      
    24.            var mat = GameObject.GetComponent<MeshRenderer>().sharedMaterial;
    25.            mat.enableInstancing = true;
    26.            var mesh = GameObject.GetComponent<MeshFilter>().sharedMesh;
    27.              
    28.            entityManager.SetSharedComponentData(Entities[0], new MeshInstanceRenderer {
    29.                mesh = mesh,
    30.                material = mat,
    31.            });
    32.        }
    33.     }
    34.    
    35.  
     
    Last edited: Jul 28, 2018
    sngdan likes this.