How to scale Entity?

SetComponentData<LocalToWorld> does nothing.

Here is some example code…

boundingBoxCamera = GetSingletonEntity<BoundingBoxCamera>();
localToWorld = entityManager.GetComponentData<LocalToWorld>(boundingBoxCamera);
entityManager.SetComponentData<Translation>(boundingBoxCamera, new Translation { Value = new float3(3f, 2f, 1f) });
entityManager.SetComponentData<LocalToWorld>(boundingBoxCamera, new LocalToWorld
{
    Value = float4x4.TRS(
        translation: new float3(0f, SC_Initialize.height / 2, 0f),
        rotation: localToWorld.Rotation,
        scale: new float3(SC_Initialize.width + 10f, SC_Initialize.height + 10f, SC_Initialize.width + 10f)
    )
});

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?

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.

1 Like

Thank you for the reply.

boundingBoxCamera = GetSingletonEntity<BoundingBoxCamera>();
entityManager.SetComponentData<Translation>(boundingBoxCamera, new Translation { Value = new float3(0f, SC_Initialize.height / 2, 0f) });
entityManager.AddComponent<NonUniformScale>(boundingBoxCamera);
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.

1 Like

Note that Entities 1.0 comes with a new approach to transforms: Transforms in Entities | Entities | 1.0.16

1 Like

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)}

1 Like
AddComponent(entity, new PostTransformMatrix
            {
                Value = float4x4.Scale(newScale)
            });

The newScale can be either a float or float3.
https://docs.unity3d.com/Packages/com.unity.entities@1.0/api/Unity.Transforms.PostTransformMatrix.html

3 Likes