Beispiel #1
0
        /// <inheritdoc/>
        protected override bool IsToRender(PositionableRenderable body)
        {
            #region Sanity checks
            if (body == null)
            {
                throw new ArgumentNullException(nameof(body));
            }
            #endregion

            switch (body.RenderIn)
            {
            case ViewType.All:
            case ViewType.SupportOnly:
                return(true);

            case ViewType.GlowOnly:
                return(Name.EndsWith(" Glow", StringComparison.OrdinalIgnoreCase));

            case ViewType.NormalOnly:
                return(false);

            default:
                throw new ArgumentException("Invalid ViewType!", nameof(body));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Pick an <see cref="PositionableRenderable"/> in 3D-space using the mouse
        /// </summary>
        /// <param name="location">The screen space location to start the ray from (usually mouse coordinates)</param>
        /// <param name="position">Returns the position of the vertex closest to the intersection in entity space</param>
        /// <returns>The picked <see cref="PositionableRenderable"/> or <c>null</c>.</returns>
        public PositionableRenderable Pick(Point location, out DoubleVector3 position)
        {
            Ray pickingRay = PickingRay(location);

            PositionableRenderable closestBody = null;
            float closestDistance = float.MaxValue;

            foreach (PositionableRenderable body in _sortedBodies)
            {
                // Find bodies that intersect the ray
                float distance;
                if (body.Pickable && body.Intersects(pickingRay, out distance))
                {
                    // Check if new hit is closer that the previous best
                    if (distance < closestDistance)
                    {
                        closestBody     = body;
                        closestDistance = distance;
                    }
                }
            }

            if (closestBody == null)
            {
                position = default(DoubleVector3);
            }
            else
            {
                // Calculate position along the ray and compensate for position offset
                position = (pickingRay.Position + closestDistance * pickingRay.Direction) - ((IPositionableOffset)closestBody).Offset;
            }
            return(closestBody);
        }
Beispiel #3
0
 private void ConfigureParticleSystem(Entity entity, PositionableRenderable particleSystem, ParticleSystem component)
 {
     particleSystem.Name               = entity.Name;
     particleSystem.PreTransform       = Matrix.Translation(component.Shift);
     particleSystem.VisibilityDistance = component.VisibilityDistance;
     particleSystem.Wireframe          = WireframeEntities;
     particleSystem.DrawBoundingSphere = BoundingSphereEntities;
     particleSystem.DrawBoundingBox    = BoundingBoxEntities;
 }
        private readonly List <PositionableRenderable> _sortedOpaqueBodies = new List <PositionableRenderable>(30); // Use educated guess for list capacity
        #endregion

        #region Sorting algorithms
        /// <summary>
        /// The difference between the distance of <paramref name="body1"/> and <paramref name="body2"/> to the camera
        /// </summary>
        /// <returns>The inverted difference value usable for sorting</returns>
        private static int CameraDistSort(PositionableRenderable body1, PositionableRenderable body2)
        {
            double distance = body1.CurrentCameraDistance - body2.CurrentCameraDistance;

            if (distance > int.MaxValue - 1)
            {
                return(int.MaxValue);
            }
            if (distance < int.MinValue + 2)
            {
                return(int.MinValue + 1);
            }
            return((int)distance);
        }
Beispiel #5
0
        /// <summary>
        /// Renders a <see cref="PositionableRenderable"/> from the <see cref="Scene"/>
        /// </summary>
        /// <param name="body">The <see cref="PositionableRenderable"/> to render</param>
        protected virtual void RenderBody(PositionableRenderable body)
        {
            #region Sanity checks
            if (body == null)
            {
                throw new ArgumentNullException(nameof(body));
            }
            #endregion

            // Apply the camera offset to make sure positioning is right
            ApplyCameraBase(body);

            using (new ProfilerEvent(() => "Render " + body))
                body.Render(Camera, Scene.GetEffectiveLights);
        }
Beispiel #6
0
        /// <summary>
        /// Applies the position and rotation of a Model element to a View representation.
        /// </summary>
        protected void UpdateRepresentation(Entity element, PositionableRenderable representation)
        {
            #region Sanity checks
            if (element == null)
            {
                throw new ArgumentNullException(nameof(element));
            }
            if (representation == null)
            {
                throw new ArgumentNullException(nameof(representation));
            }
            #endregion

            representation.Position = Universe.Terrain.ToEngineCoords(element.Position);
            representation.Rotation = Quaternion.RotationYawPitchRoll(element.Rotation.DegreeToRadian(), 0, 0);
        }
Beispiel #7
0
        /// <summary>
        /// Swings the camera to look at a specifc <see cref="PositionableRenderable"/>.
        /// </summary>
        public void SwingCameraTo(PositionableRenderable target)
        {
            #region Sanity checks
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }
            #endregion

            SwingCameraTo(new CameraState <Vector2>
            {
                Name     = View.Camera.Name,
                Position = target.Position.Flatten(),
                Radius   = target.WorldBoundingSphere.HasValue ? target.WorldBoundingSphere.Value.Radius * 2.5f : 50,
            });
        }
Beispiel #8
0
        //--------------------//

        #region Render body
        /// <inheritdoc/>
        protected override void RenderBody(PositionableRenderable body)
        {
            #region Sanity checks
            if (body == null)
            {
                throw new ArgumentNullException(nameof(body));
            }
            #endregion

            // Backup the current surface effect and replace it by a special one for depth
            SurfaceEffect surfaceEffect = body.SurfaceEffect;
            body.SurfaceEffect = SurfaceEffect.Depth;

            base.RenderBody(body);

            // Restore the original surface effect
            body.SurfaceEffect = surfaceEffect;
        }
Beispiel #9
0
        //--------------------//

        #region Visibility check
        /// <inheritdoc/>
        protected override bool IsToRender(PositionableRenderable body)
        {
            #region Sanity checks
            if (body == null)
            {
                throw new ArgumentNullException(nameof(body));
            }
            #endregion

            // Filter bodies if this is a terrain-only reflection
            if (Reflection && Engine.Effects.WaterEffects <= WaterEffectsType.ReflectTerrain && !(body is Terrain))
            {
                return(false);
            }

            // Perform the the more generic tests
            return(base.IsToRender(body));
        }
Beispiel #10
0
 private void ConfigureModel(PositionableRenderable model, Mesh component)
 {
     model.PreTransform = Matrix.Scaling(component.Scale, component.Scale, component.Scale) *
                          Matrix.RotationYawPitchRoll(
         component.RotationY.DegreeToRadian(),
         component.RotationX.DegreeToRadian(),
         component.RotationZ.DegreeToRadian()) *
                          Matrix.Translation(component.Shift);
     model.Alpha    = component.Alpha;
     model.Pickable = component.Pickable;
     model.RenderIn = (ViewType)component.RenderIn;
     if (Lighting)
     {
         model.SurfaceEffect = SurfaceEffect.Shader;
     }
     model.Wireframe          = WireframeEntities;
     model.DrawBoundingSphere = BoundingSphereEntities;
     model.DrawBoundingBox    = BoundingBoxEntities;
 }
Beispiel #11
0
        /// <summary>
        /// Checks if a <see cref="PositionableRenderable"/> is supposed to be rendered in this type of <see cref="View"/>
        /// </summary>
        /// <param name="body">The <see cref="PositionableRenderable"/> to check</param>
        /// <returns><c>true</c> if the <see cref="PositionableRenderable"/> is supposed to be rendered</returns>
        protected virtual bool IsToRender(PositionableRenderable body)
        {
            #region Sanity checks
            if (body == null)
            {
                throw new ArgumentNullException(nameof(body));
            }
            #endregion

            switch (body.RenderIn)
            {
            case ViewType.All:
            case ViewType.NormalOnly:
                return(true);

            case ViewType.SupportOnly:
            case ViewType.GlowOnly:
                return(false);

            default:
                throw new ArgumentException("Invalid ViewType!", nameof(body));
            }
        }