protected override void OnUpdate() { // add matrices component if needed Entities.WithNone <LightMatrices>().WithAll <Light>().ForEach((Entity e) => { EntityManager.AddComponent <LightMatrices>(e); }); // add frustum component if needed Entities.WithNone <Frustum>().WithAll <Light>().ForEach((Entity e) => { EntityManager.AddComponent <Frustum>(e); }); // update Entities.ForEach((ref Light c, ref LocalToWorld tx, ref LightMatrices m, ref SpotLight sl, ref Frustum f) => { // spot light m.projection = ProjectionHelper.ProjectionMatrixPerspective(c.clipZNear, c.clipZFar, sl.fov, 1.0f); m.view = math.inverse(tx.Value); m.mvp = math.mul(m.projection, m.view); ProjectionHelper.FrustumFromMatrices(m.projection, m.view, ref f); }); Entities.ForEach((ref Light c, ref LocalToWorld tx, ref LightMatrices m, ref DirectionalLight dr, ref Frustum f) => { // directional m.projection = ProjectionHelper.ProjectionMatrixOrtho(c.clipZNear, c.clipZFar, dr.size, 1.0f); m.view = math.inverse(tx.Value); m.mvp = math.mul(m.projection, m.view); ProjectionHelper.FrustumFromMatrices(m.projection, m.view, ref f); }); Entities.WithNone <DirectionalLight, SpotLight>().ForEach((ref Light c, ref LocalToWorld tx, ref LightMatrices m, ref Frustum f) => { // point m.projection = float4x4.identity; m.view = math.inverse(tx.Value); m.mvp = math.mul(m.projection, m.view); // build furstum from bounds ProjectionHelper.FrustumFromCube(tx.Value.c3.xyz, c.clipZFar, ref f); }); }
static void FrustumFromCamera(ref CameraMatrices cm, ref Frustum dest) { ProjectionHelper.FrustumFromMatrices(cm.projection, cm.view, ref dest); }