Skip to content

Instantly share code, notes, and snippets.

@JoeCoo7
Last active January 20, 2020 19:45
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save JoeCoo7/f497af9b1ba2ab5babae3060635a9c6a to your computer and use it in GitHub Desktop.
Save JoeCoo7/f497af9b1ba2ab5babae3060635a9c6a to your computer and use it in GitHub Desktop.
Component Transform with Scale
using Unity.Entities;
using Unity.Mathematics;
//----------------------------------------------------------------------------------------
[System.Serializable]
public struct ModelMatrix : IComponentData
{
public float4x4 Value
}
public class ModelMatrixComponent : ComponentDataWrapper<ModelMatrix> { }
//----------------------------------------------------------------------------------------
[System.Serializable]
public struct Pos : IComponentData
{
public float3 Value;
}
public class PosComponent : ComponentDataWrapper<Pos> { }
//----------------------------------------------------------------------------------------
[System.Serializable]
public struct Rot : IComponentData
{
public quaternion Value;
}
public class RotComponent : ComponentDataWrapper<Rot> { }
//----------------------------------------------------------------------------------------
[System.Serializable]
public struct Scl : IComponentData
{
public float3 Value;
}
public class SclComponent : ComponentDataWrapper<Scl> { }
//----------------------------------------------------------------------------------------
public struct Active: IComponentData{}
public class ActiveComponent : ComponentDataWrapper<Active> { }
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEditor;
using UnityEngine;
using WT.ECS;
[UpdateInGroup(typeof(RenderingGroup))]
[UpdateBefore(typeof(RendererSystem))]
//----------------------------------------------------------------------------------------
public class ModelMatrixSystem : JobComponentSystem
{
#pragma warning disable 649
//----------------------------------------------------------------------------------------
struct TransGroup
{
public ComponentDataArray<ModelMatrix> Matrices;
[ReadOnly] public ComponentDataArray<Pos> Positions;
public int Length;
}
[Inject] TransGroup m_transGroup;
//----------------------------------------------------------------------------------------
struct RotTransGroup
{
public ComponentDataArray<ModelMatrix> Matrices;
[ReadOnly] public ComponentDataArray<Pos> Positions;
[ReadOnly] public ComponentDataArray<Rot> Rotation;
public int Length;
}
[Inject] RotTransGroup m_rotTransGroup;
//----------------------------------------------------------------------------------------
struct ScaleRotTransGroup
{
public ComponentDataArray<ModelMatrix> Matrices;
[ReadOnly] public ComponentDataArray<Pos> Positions;
[ReadOnly] public ComponentDataArray<Rot> Rotation;
[ReadOnly] public ComponentDataArray<Scl> Scales;
public int Length;
}
[Inject] ScaleRotTransGroup m_scaleRotTransGroup;
#pragma warning restore 649
//----------------------------------------------------------------------------------------
[ComputeJobOptimization]
struct TransToMatrix : IJobParallelFor
{
[ReadOnly] public ComponentDataArray<Pos> Positions;
public ComponentDataArray<ModelMatrix> Matrices;
public void Execute(int _index)
{
float3 position = Positions[_index].Value;
Matrices[_index]= new ModelMatrix {Value = math.translate(position)};
}
}
//----------------------------------------------------------------------------------------
[ComputeJobOptimization]
struct RotTransToMatrix : IJobParallelFor
{
[ReadOnly] public ComponentDataArray<Pos> Positions;
[ReadOnly] public ComponentDataArray<Rot> Rotations;
public ComponentDataArray<ModelMatrix> Matrices;
public void Execute(int _index)
{
float3 position = Positions[_index].Value;
quaternion quat = Rotations[_index].Value;
Matrices[_index]= new ModelMatrix {Value = math.rottrans(quat, position)};
}
}
//----------------------------------------------------------------------------------------
[ComputeJobOptimization]
struct ScaleRotTransToMatrix : IJobParallelFor
{
[ReadOnly] public ComponentDataArray<Pos> Positions;
[ReadOnly] public ComponentDataArray<Rot> Rotations;
[ReadOnly] public ComponentDataArray<Scl> Scales;
public ComponentDataArray<ModelMatrix> Matrices;
public void Execute(int _index)
{
float3 position = Positions[_index].Value;
quaternion quat = Rotations[_index].Value;
float3 scale = Scales[_index].Value;
float4x4 matrix = math.mul(math.scale(scale), math.rottrans(quat, MathExt.Float3Zero()));
matrix = math.mul(math.translate(position), matrix);
Matrices[_index]= new ModelMatrix {Value = matrix };
}
}
//----------------------------------------------------------------------------------------
protected override JobHandle OnUpdate(JobHandle _inputDeps)
{
var transToMatrixJob = new TransToMatrix
{
Positions = m_transGroup.Positions,
Matrices = m_transGroup.Matrices
};
var transToMatrixJobHandle = transToMatrixJob.Schedule(m_transGroup.Length, 64, _inputDeps);
var rotTransToMatrixJob = new RotTransToMatrix
{
Positions = m_rotTransGroup.Positions,
Rotations = m_rotTransGroup.Rotation,
Matrices = m_rotTransGroup.Matrices,
};
var rotTransMatrixJobHandle = rotTransToMatrixJob.Schedule(m_rotTransGroup.Length, 64, transToMatrixJobHandle);
var scaleRotTransToMatrixJob = new ScaleRotTransToMatrix
{
Positions = m_scaleRotTransGroup.Positions,
Rotations = m_scaleRotTransGroup.Rotation,
Scales = m_scaleRotTransGroup.Scales,
Matrices = m_scaleRotTransGroup.Matrices
};
return scaleRotTransToMatrixJob.Schedule(m_scaleRotTransGroup.Length, 64, rotTransMatrixJobHandle);
}
}
using System.Collections.Generic;
using RSGLib.ECS.Transforms;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Rendering;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.Experimental.PlayerLoop;
[UpdateInGroup(typeof(RenderingGroup))]
[UpdateAfter(typeof(PreLateUpdate.ParticleSystemBeginUpdateAll))]
//----------------------------------------------------------------------------------------
public class RendererSystem : ComponentSystem
{
// Instance renderer takes only batches of 1023
Matrix4x4[] m_matricesArray = new Matrix4x4[1023];
List<MeshInstanceRenderer> m_cacheduniqueRendererTypes = new List<MeshInstanceRenderer>(10);
ComponentGroup m_instanceRendererGroup;
//----------------------------------------------------------------------------------------
public unsafe static void CopyMatrices(ComponentDataArray<ModelMatrix> _transforms, int _beginIndex, int _length, Matrix4x4[] _outMatrices)
{
fixed (Matrix4x4* matricesPtr = _outMatrices)
{
Assert.AreEqual(sizeof(Matrix4x4), sizeof(ModelMatrix));
var matricesSlice = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice<ModelMatrix>(matricesPtr, sizeof(Matrix4x4), _length);
#if ENABLE_UNITY_COLLECTIONS_CHECKS
NativeSliceUnsafeUtility.SetAtomicSafetyHandle(ref matricesSlice, AtomicSafetyHandle.GetTempUnsafePtrSliceHandle());
#endif
_transforms.CopyTo(matricesSlice, _beginIndex);
}
}
//----------------------------------------------------------------------------------------
protected override void OnCreateManager(int _capacity)
{
m_instanceRendererGroup = GetComponentGroup(typeof(MeshInstanceRenderer), typeof(ModelMatrix), typeof(Active));
}
//----------------------------------------------------------------------------------------
protected override void OnUpdate()
{
EntityManager.GetAllUniqueSharedComponentDatas(m_cacheduniqueRendererTypes);
for (int index = 0;index != m_cacheduniqueRendererTypes.Count;index++)
{
var renderer = m_cacheduniqueRendererTypes[index];
m_instanceRendererGroup.SetFilter(renderer);
var transforms = m_instanceRendererGroup.GetComponentDataArray<ModelMatrix>();
int beginIndex = 0;
while (beginIndex < transforms.Length)
{
int length = math.min(m_matricesArray.Length, transforms.Length - beginIndex);
CopyMatrices(transforms, beginIndex, length, m_matricesArray);
if (renderer.mesh != null)
Graphics.DrawMeshInstanced(renderer.mesh, 0, renderer.material, m_matricesArray, length, null, renderer.castShadows, renderer.receiveShadows);
beginIndex += length;
}
}
m_cacheduniqueRendererTypes.Clear();
}
@alvinwan
Copy link

For anyone else that lands here, I believe the newest ecs will allow you set scale more easily CompositeScale.Value (float4x4), https://forum.unity.com/threads/transformmatrixcomponent-and-scaling.524054/#post-5390964.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment