// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. namespace FlaxEngine { partial struct RenderView { /// /// Initializes this view with default options. /// public void Init() { MaxShadowsQuality = Quality.Ultra; ModelLODDistanceFactor = 1.0f; ModelLODDistanceFactorSqrt = 1.0f; #pragma warning disable 0612 ShadowModelLODDistanceFactor = 1.0f; #pragma warning restore 0612 Flags = ViewFlags.DefaultGame; Mode = ViewMode.Default; } /// /// Updates the cached data for the view (inverse matrices, etc.). /// public void UpdateCachedData() { Matrix.Invert(ref View, out IV); Matrix.Invert(ref Projection, out IP); Matrix.Multiply(ref View, ref Projection, out var viewProjection); Frustum = new BoundingFrustum(viewProjection); Matrix.Invert(ref viewProjection, out IVP); CullingFrustum = Frustum; } /// /// Initializes render view data. /// /// The view. /// The projection. public void SetUp(ref Matrix view, ref Matrix projection) { Projection = projection; NonJitteredProjection = projection; View = view; UpdateCachedData(); } /// /// Set up view for projector rendering. /// /// Near plane /// Far plane /// Camera's position /// Camera's direction vector /// Camera's up vector /// Camera's FOV angle (in degrees) public void SetProjector(float nearPlane, float farPlane, Float3 position, Float3 direction, Float3 up, float angle) { // Copy data Near = nearPlane; Far = farPlane; Position = position; // Create projection matrix Matrix.PerspectiveFov(angle * Mathf.DegreesToRadians, 1.0f, nearPlane, farPlane, out Projection); NonJitteredProjection = Projection; TemporalAAJitter = Float4.Zero; // Create view matrix Direction = direction; var target = Position + Direction; Matrix.LookAt(ref Position, ref target, ref up, out View); UpdateCachedData(); } /// /// Copies render view data from the camera. /// /// The camera. public void CopyFrom(Camera camera) { var viewport = camera.Viewport; CopyFrom(camera, ref viewport); } /// /// Copies render view data from the camera. /// /// The camera. /// The custom viewport to use for view/projeection matrices override. public void CopyFrom(Camera camera, ref Viewport viewport) { Vector3 cameraPos = camera.Position; LargeWorlds.UpdateOrigin(ref Origin, cameraPos); Position = cameraPos - Origin; Direction = camera.Direction; Near = camera.NearPlane; Far = camera.FarPlane; camera.GetMatrices(out View, out Projection, ref viewport, ref Origin); NonJitteredProjection = Projection; TemporalAAJitter = Float4.Zero; RenderLayersMask = camera.RenderLayersMask; UpdateCachedData(); } /// /// Calculates the world matrix for the given transformation instance rendering. /// /// The object transformation. /// The output matrix. public void GetWorldMatrix(ref Transform transform, out Matrix world) { Float3 translation = transform.Translation - Origin; Matrix.Transformation(ref transform.Scale, ref transform.Orientation, ref translation, out world); } } }