Search Unity

Resolved How to scale Entity?

Discussion in 'Entity Component System' started by heu3becteh, Dec 2, 2022.

  1. heu3becteh

    heu3becteh

    Joined:
    Aug 6, 2020
    Posts:
    25
    SetComponentData<LocalToWorld> does nothing.

    Here is some example code...
    Code (CSharp):
    1. boundingBoxCamera = GetSingletonEntity<BoundingBoxCamera>();
    2. localToWorld = entityManager.GetComponentData<LocalToWorld>(boundingBoxCamera);
    3. entityManager.SetComponentData<Translation>(boundingBoxCamera, new Translation { Value = new float3(3f, 2f, 1f) });
    4. entityManager.SetComponentData<LocalToWorld>(boundingBoxCamera, new LocalToWorld
    5. {
    6.     Value = float4x4.TRS(
    7.         translation: new float3(0f, SC_Initialize.height / 2, 0f),
    8.         rotation: localToWorld.Rotation,
    9.         scale: new float3(SC_Initialize.width + 10f, SC_Initialize.height + 10f, SC_Initialize.width + 10f)
    10.     )
    11. });
    In this case nothing set by SetComponentData<LocalToWorld> works, Entity has the same Rotation and Scale as before, and Translation is only changed by SetComponentData<Translation>.

    LocalToWorld was useless to me before too when I did try to rotate Entities, but there was a solution of using Rotation component...
    Is there some similar solution for scale?
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,770
    Local to world is overriten internally by DOTS systems.
    If you use translation, rotation components, there is also scale/none unifor scale components.
    Do not try to change local to world manually. Won't work, unless you disable Unity system.
     
    heu3becteh likes this.
  3. heu3becteh

    heu3becteh

    Joined:
    Aug 6, 2020
    Posts:
    25
    Thank you for the reply.

    Code (CSharp):
    1. boundingBoxCamera = GetSingletonEntity<BoundingBoxCamera>();
    2. entityManager.SetComponentData<Translation>(boundingBoxCamera, new Translation { Value = new float3(0f, SC_Initialize.height / 2, 0f) });
    3. entityManager.AddComponent<NonUniformScale>(boundingBoxCamera);
    4. entityManager.SetComponentData<NonUniformScale>(boundingBoxCamera, new NonUniformScale { Value = new float3(SC_Initialize.width + 10f, SC_Initialize.height + 10f, SC_Initialize.width + 10f) });
    Does the trick.

    I have seen an example of code somewhere with writing to LocalToWorld directly, and that seems to be wrong, as you have mentioned.

    The problem was that my Entity somehow had no NonUniformScale component, even though it had non-uniform scale. It seems that this component is not added by default, and you have to add it if you plan to change the scale.

    By the way, when I did try to change this component (without adding it first) together with LocalToWorld, I've got A component with type:Unity.Transforms.NonUniformScale has not been added to the entity error, but the changes to LocalToWorld had been applied. And that was strange: Entity had the previous Translation, no NonUniformScale component, but in fact was rendered according to different values from LocalToWorld matrix.
     
    Antypodish likes this.
  4. Fribur

    Fribur

    Joined:
    Jan 5, 2019
    Posts:
    136
    Antypodish likes this.
  5. h_kardan

    h_kardan

    Joined:
    Dec 24, 2022
    Posts:
    2
    I just noticed that you cant change LocalToWorld scale value in the moment that you create an entity. my solution to this was :
    1- create entities with a system
    2- reach those entities with another system and then create a LocalToWorld value by urself with help of Matrix4x4.TRS(position,rotation,Scale);
    there you can manipulate the Scale as float3 with what value you want . (you can deform the entity)

    new LocalToWorld { value = Matrix4x4.TRS(Get Position of entity, Get rotation Of entity, Use New Scale that you want)}
     
    bb8_1 likes this.
  6. ufo_zj

    ufo_zj

    Joined:
    Nov 7, 2018
    Posts:
    6
    Dainofel, pingliu0811 and bb8_1 like this.