AGENT SKILL

wave5-post-transform-matrix-non-uniform-scale

Apply non-uniform scale to entities using PostTransformMatrix, which is multiplied into LocalToWorld after the standard TRS computation.

License 许可
MIT
最近更新
2026-07-25
Stars
★ 8
Forks
0

01 前言 Front Matter

name
wave5-post-transform-matrix-non-uniform-scale
tags
[ "transforms" ]
metadata
{ "tier": 3, "internal-only": true }
description
Apply non-uniform scale to entities using PostTransformMatrix, which is multiplied into LocalToWorld after the standard TRS computation.

03 SKILL.md 正文

PostTransformMatrix — Non-Uniform Scale

Intent

Apply non-uniform scale to entities using PostTransformMatrix, which is multiplied into LocalToWorld after the standard TRS computation.

Use When

  • Non-uniform scale needed (e.g., (1, 2, 1) — taller but same width)
  • Squash-and-stretch animation, terrain tile variations, procedurally scaled geometry

Avoid When

  • Uniform scale is sufficient — use LocalTransform.Scale
  • Non-uniform scale is baked and static — bake into mesh vertices or baked matrix directly

Senior Pattern

// In a custom LocalToWorld system (IJobChunk):
[BurstCompile]
public struct ApplyPostTransformJob : IJobChunk
{
    [ReadOnly]  public ComponentTypeHandle<LocalTransform> LocalTransformHandle;
    public ComponentTypeHandle<LocalToWorld> LocalToWorldHandle;
    // Optional — null when absent from chunk
    [ReadOnly]  public ComponentTypeHandle<PostTransformMatrix> PostTransformMatrixHandle;

    [BurstCompile]
    public void Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex,
        bool useEnableMask, in v128 chunkEnabledMask)
    {
        var transforms = chunk.GetNativeArray(ref LocalTransformHandle);
        var localToWorlds = chunk.GetNativeArray(ref LocalToWorldHandle);

        // One null check per chunk, not per entity:
        bool hasPost = chunk.Has(ref PostTransformMatrixHandle);
        NativeArray<PostTransformMatrix> postArr = hasPost
            ? chunk.GetNativeArray(ref PostTransformMatrixHandle)
            : default;

        var enumerator = new ChunkEntityEnumerator(useEnableMask, chunkEnabledMask, chunk.Count);
        while (enumerator.NextEntityIndex(out var i))
        {
            float4x4 trs = transforms[i].ToMatrix();
            localToWorlds[i] = new LocalToWorld
            {
                Value = hasPost ? math.mul(trs, postArr[i].Value) : trs
            };
        }
    }
}

// Add PostTransformMatrix in Baker for authoring-time non-uniform scale:
public override void Bake(MyAuthoring authoring)
{
    var entity = GetEntity(TransformUsageFlags.Dynamic);
    AddComponent(entity, new PostTransformMatrix
    {
        Value = float4x4.Scale(authoring.NonUniformScale)
    });
}

Anti-Patterns

  • Storing non-uniform scale in LocalTransform.Scale — it is a single float; non-uniform values silently truncated to uniform.
  • Ignoring PostTransformMatrix in a custom LocalToWorld system — non-uniform scale silently discarded.
  • Checking HasComponent<PostTransformMatrix> per-entity in hot path — use chunk.Has() which is one check per chunk, not per entity.
  • Wrong multiplication order: math.mul(postTransform, trs) — correct order is math.mul(trs, postTransform).

Runtime Risks

  • GetNativeArray returns default when component absent from chunk — always null-check before indexing.
  • Writing PostTransformMatrix from a parallel job that also writes LocalTransform on same entities — both type handles must be correctly marked (ref vs ReadOnly).

Performance Notes

  • Optional component check via chunk.Has(): one boolean per chunk, not per entity.
  • Avoids storing full 4x4 matrix in LocalTransform for all entities when only a minority need non-uniform scale.

Architecture Guidance

  • Correct multiplication order: LocalToWorld.Value = math.mul(standardTRS, postTransform[i].Value).
  • Standard Unity transform system already handles PostTransformMatrix — only custom transform systems need to implement this manually.
  • Prefer PostTransformMatrix over modifying mesh vertices at runtime for dynamic non-uniform scale.

Related Skills

[[local-transform-write-pattern]], [[local-to-world-read-only-contract]], [[write-group-custom-transform]], [[wave4-ijobchunk-full-anatomy]]

04 同仓库更多 Skills

dyCuong03
dyCuong03/unity-agent-team

dots-update-groups

How to choose the right ECS system group and ordering so per-frame and per-fixed-step work is deterministic and never races its own readers. Covers InitializationSystemGroup / SimulationSystemGroup / PresentationSystemGroup / FixedStepSimulationSystemGroup / VariableRateSimulationSystemGroup, UpdateBefore / UpdateAfter peer ordering, OrderFirst / OrderLast group-boundary placement, RateManager, and the cross-group ordering trap. Use when adding an ISystem, fixing "value visible one frame late" bugs, or coupling simulation to a fixed timestep.

★ 8
dyCuong03
dyCuong03/unity-agent-team

dots-transform-patterns

The Entities 1.x transform model — LocalTransform (writable input) vs LocalToWorld (system-owned matrix), Parent/Child hierarchy, reparenting cost, uniform vs non-uniform scale (PostTransformMatrix), and when to opt out with custom transform systems. Use when designing parent/child hierarchies, runtime reparenting (mount/dismount/socket), or choosing transform components at the Baker boundary.

★ 8
dyCuong03
dyCuong03/unity-agent-team

dots-singleton-patterns

Model cross-system shared configuration and game state as a single-entity component with a strict single-writer contract. Covers RequireForUpdate gating, SystemAPI.GetSingleton / GetSingletonRW / HasSingleton / TryGetSingleton, bake-time vs runtime-created vs ECB-system singletons, the "exactly one entity" runtime invariant, and the single-writer rule. Use when designing frame-stable global config, shared mutable state, or any component intended to exist on exactly one entity.

律师与法务
★ 8
dyCuong03
dyCuong03/unity-agent-team

dots-hybrid-bridge

The one-way bridge between DOTS-owned runtime state and Unity-owned presentation. Covers UnityObjectRef for managed asset refs in components, the DOTS-writes / Unity-reads ownership rule, when to use managed IComponentData vs UnityObjectRef vs CompanionComponent, baker-bridge vs runtime-bridge, and SubScene boundary rules. Use when an entity must reference Unity assets (Mesh, Material, AudioClip, ScriptableObject), a MonoBehaviour view must read entity state, or input/UI must trigger ECS work.

★ 8
dyCuong03
dyCuong03/unity-agent-team

dots-event-driven-ecs

Model one-frame events and external requests in ECS without dual ownership, polling, or persistent state leaks. Covers request-tag pattern with ECB, enableable command components (no structural change), event buffers (IBufferElementData), explicit consumer ownership, one-frame latency rules, and the banned C# event / UnityEvent / static bus anti-patterns. Use when input/UI/network must trigger ECS work, when a one-frame signal fans out to N consumers (damage, hit, collision), or when state changes must be processed exactly once.

★ 8
dyCuong03
dyCuong03/unity-agent-team

valid_skill

A complete, well-formed test fixture skill for SkillHub validation testing. Covers basic task routing and code review for Unity projects.

代码审查测试与质量
★ 8