internal bool UpdateWithRawValue(float value, ulong updateTick, float deltaTime)
    {
        PrepareForUpdate(updateTick);

        if (Utility.Abs(value) > Utility.Abs(nextState.RawValue))
        {
            nextState.RawValue = value;
            nextState.Set(value, stateThreshold);
            return(true);
        }

        return(false);
    }
Example #2
0
    void UpdateBindings(ulong updateTick, float deltaTime)
    {
        var lastInputType            = LastInputType;
        var lastInputTypeChangedTick = LastInputTypeChangedTick;
        var lastUpdateTick           = UpdateTick;

        var bindingCount = regularBindings.Count;

        for (var i = bindingCount - 1; i >= 0; i--)
        {
            var binding = regularBindings[i];

            if (binding.BoundTo != this)
            {
                regularBindings.RemoveAt(i);
                visibleBindings.Remove(binding);
            }
            else
            {
                var value = binding.GetValue(Device);
                if (UpdateWithValue(value, updateTick, deltaTime))
                {
                    lastInputType            = binding.BindingSourceType;
                    lastInputTypeChangedTick = updateTick;
                }
            }
        }

        if (bindingCount == 0)
        {
            UpdateWithValue(0.0f, updateTick, deltaTime);
        }

        Commit();

        if (lastInputTypeChangedTick > LastInputTypeChangedTick)
        {
            if (lastInputType != BindingSourceType.MouseBindingSource ||
                Utility.Abs(LastValue - Value) >= MouseBindingSource.JitterThreshold)
            {
                var triggerEvent = lastInputType != LastInputType;

                LastInputType            = lastInputType;
                LastInputTypeChangedTick = lastInputTypeChangedTick;
            }
        }
    }
Example #3
0
        protected bool CalculateForDirectionalLight(Plane plane, Vector3[] vertices, out Vector2[] texCoors,
                                                    out ColorEx[] colors)
        {
            texCoors = new Vector2[vertices.Length];
            colors   = new ColorEx[vertices.Length];

            float angle = Utility.Abs(plane.Normal.Dot(DerivedDirection));

            var lightCol = new ColorEx(this.textureColor.a * angle, this.textureColor.r, this.textureColor.g, this.textureColor.b);

            for (int i = 0; i < vertices.Length; i++)
            {
                colors[i] = lightCol;
            }

            return(true);
        }
Example #4
0
        protected bool CalculateForPointLight(Plane plane, Vector3[] vertices, out Vector2[] texCoors, out ColorEx[] colors)
        {
            texCoors = new Vector2[vertices.Length];
            colors   = new ColorEx[vertices.Length];

            Vector3 lightPos, faceLightPos;

            lightPos = GetDerivedPosition();

            float dist = plane.GetDistance(lightPos);

            if (Utility.Abs(dist) < range)
            {
                // light is visible

                //light pos on face
                faceLightPos = lightPos - plane.Normal * dist;

                Vector3 verAxis  = plane.Normal.Perpendicular();
                Vector3 horAxis  = verAxis.Cross(plane.Normal);
                var     verPlane = new Plane(verAxis, faceLightPos);
                var     horPlane = new Plane(horAxis, faceLightPos);

                float lightRadiusSqr = range * range;
                float relRadiusSqr   = lightRadiusSqr - dist * dist;
                float relRadius      = Utility.Sqrt(relRadiusSqr);
                float scale          = 0.5f / relRadius;

                float brightness = relRadiusSqr / lightRadiusSqr;
                var   lightCol   = new ColorEx(brightness * this.textureColor.a, this.textureColor.r, this.textureColor.g,
                                               this.textureColor.b);

                for (int i = 0; i < vertices.Length; i++)
                {
                    texCoors[i].x = horPlane.GetDistance(vertices[i]) * scale + 0.5f;
                    texCoors[i].y = verPlane.GetDistance(vertices[i]) * scale + 0.5f;
                    colors[i]     = lightCol;
                }

                return(true);
            }

            return(false);
        }
Example #5
0
        public void TransformAffine(Matrix4 m)
        {
            Debug.Assert(m.IsAffine);

            // Do nothing if current null or infinite
            if (this.isNull || this.isInfinite)
            {
                return;
            }

            Vector3 centre   = Center;
            Vector3 halfSize = HalfSize;

            Vector3 newCentre   = m.TransformAffine(centre);
            var     newHalfSize =
                new Vector3(
                    Utility.Abs(m[0, 0]) * halfSize.x + Utility.Abs(m[0, 1]) * halfSize.y + Utility.Abs(m[0, 2]) * halfSize.z,
                    Utility.Abs(m[1, 0]) * halfSize.x + Utility.Abs(m[1, 1]) * halfSize.y + Utility.Abs(m[1, 2]) * halfSize.z,
                    Utility.Abs(m[2, 0]) * halfSize.x + Utility.Abs(m[2, 1]) * halfSize.y + Utility.Abs(m[2, 2]) * halfSize.z);

            SetExtents(newCentre - newHalfSize, newCentre + newHalfSize);
        }
        public List <ActionEvent> GetAxisEvents(AxisMapping mapping)
        {
            List <ActionEvent> events = new List <ActionEvent>();

            switch (mapping.axis)
            {
            case AxisEnum.MouseY:
                if (Utility.Abs(mouseMovement.y) > mapping.minimumInput)
                {
                    events.Add(new ActionEvent(mapping.action, mapping.sensitivity * mouseMovement.y));
                }
                break;

            case AxisEnum.MouseX:
                if (Utility.Abs(mouseMovement.x) > mapping.minimumInput)
                {
                    events.Add(new ActionEvent(mapping.action, mapping.sensitivity * mouseMovement.x));
                }
                break;
            }

            return(events);
        }
Example #7
0
 public bool DifferenceLessThan(Vector3i other, int limit)
 {
     return(Utility.Abs(x - other.x) < limit && Utility.Abs(y - other.y) < limit && Utility.Abs(z - other.z) < limit);
 }
Example #8
0
 public bool AllComponentsLessThan(int limit)
 {
     return(Utility.Abs(x) < limit && Utility.Abs(y) < limit && Utility.Abs(z) < limit);
 }
        public void GetShadowCamera(SceneManager sceneManager, Camera camera, Viewport viewport, Light light,
                                    Camera textureCamera, int iteration)
        {
            Vector3    pos, dir;
            Quaternion q;

            // reset custom view / projection matrix in case already set
            textureCamera.SetCustomViewMatrix(false);
            textureCamera.SetCustomProjectionMatrix(false);
            textureCamera.Near = light.DeriveShadowNearClipDistance(camera);
            textureCamera.Far  = light.DeriveShadowFarClipDistance(camera);


            // get the shadow frustum's far distance
            var shadowDist = light.ShadowFarDistance;

            if (shadowDist == 0.0f)
            {
                // need a shadow distance, make one up
                shadowDist = camera.Near * 300;
            }
            var shadowOffset = shadowDist * sceneManager.ShadowDirectionalLightTextureOffset;

            // Directional lights
            if (light.Type == LightType.Directional)
            {
                // set up the shadow texture
                // Set ortho projection
                textureCamera.ProjectionType = Projection.Orthographic;
                // set ortho window so that texture covers far dist
                textureCamera.SetOrthoWindow(shadowDist * 2, shadowDist * 2);

                // Calculate look at position
                // We want to look at a spot shadowOffset away from near plane
                // 0.5 is a litle too close for angles
                var target = camera.DerivedPosition + (camera.DerivedDirection * shadowOffset);

                // Calculate direction, which same as directional light direction
                dir = -light.DerivedDirection; // backwards since point down -z
                dir.Normalize();

                // Calculate position
                // We want to be in the -ve direction of the light direction
                // far enough to project for the dir light extrusion distance
                pos = target + dir * sceneManager.ShadowDirectionalLightExtrusionDistance;

                // Round local x/y position based on a world-space texel; this helps to reduce
                // jittering caused by the projection moving with the camera
                // Viewport is 2 * near clip distance across (90 degree fov)
                //~ Real worldTexelSize = (texCam->getNearClipDistance() * 20) / vp->getActualWidth();
                //~ pos.x -= fmod(pos.x, worldTexelSize);
                //~ pos.y -= fmod(pos.y, worldTexelSize);
                //~ pos.z -= fmod(pos.z, worldTexelSize);
                var worldTexelSize = (shadowDist * 2) / textureCamera.Viewport.ActualWidth;

                //get texCam orientation

                var up = Vector3.UnitY;
                // Check it's not coincident with dir
                if (Utility.Abs(up.Dot(dir)) >= 1.0f)
                {
                    // Use camera up
                    up = Vector3.UnitZ;
                }
                // cross twice to rederive, only direction is unaltered
                var left = dir.Cross(up);
                left.Normalize();
                up = dir.Cross(left);
                up.Normalize();
                // Derive quaternion from axes
                q = Quaternion.FromAxes(left, up, dir);

                //convert world space camera position into light space
                var lightSpacePos = q.Inverse() * pos;

                //snap to nearest texel
                lightSpacePos.x -= lightSpacePos.x % worldTexelSize; //fmod(lightSpacePos.x, worldTexelSize);
                lightSpacePos.y -= lightSpacePos.y % worldTexelSize; //fmod(lightSpacePos.y, worldTexelSize);

                //convert back to world space
                pos = q * lightSpacePos;
            }
            // Spotlight
            else if (light.Type == LightType.Spotlight)
            {
                // Set perspective projection
                textureCamera.ProjectionType = Projection.Perspective;
                // set FOV slightly larger than the spotlight range to ensure coverage
                var fovy = light.SpotlightOuterAngle * 1.2;

                // limit angle
                if (fovy.InDegrees > 175)
                {
                    fovy = (Degree)(175);
                }
                textureCamera.FieldOfView = fovy;

                // Calculate position, which same as spotlight position
                pos = light.GetDerivedPosition();

                // Calculate direction, which same as spotlight direction
                dir = -light.DerivedDirection; // backwards since point down -z
                dir.Normalize();
            }
            // Point light
            else
            {
                // Set perspective projection
                textureCamera.ProjectionType = Projection.Perspective;
                // Use 120 degree FOV for point light to ensure coverage more area
                textureCamera.FieldOfView = 120.0f;

                // Calculate look at position
                // We want to look at a spot shadowOffset away from near plane
                // 0.5 is a litle too close for angles
                var target = camera.DerivedPosition + (camera.DerivedDirection * shadowOffset);

                // Calculate position, which same as point light position
                pos = light.GetDerivedPosition();

                dir = (pos - target); // backwards since point down -z
                dir.Normalize();
            }

            // Finally set position
            textureCamera.Position = pos;

            // Calculate orientation based on direction calculated above

            /*
             * // Next section (camera oriented shadow map) abandoned
             * // Always point in the same direction, if we don't do this then
             * // we get 'shadow swimming' as camera rotates
             * // As it is, we get swimming on moving but this is less noticeable
             *
             * // calculate up vector, we want it aligned with cam direction
             * Vector3 up = cam->getDerivedDirection();
             * // Check it's not coincident with dir
             * if (up.dotProduct(dir) >= 1.0f)
             * {
             * // Use camera up
             * up = cam->getUp();
             * }
             */
            var up2 = Vector3.UnitY;

            // Check it's not coincident with dir
            if (Utility.Abs(up2.Dot(dir)) >= 1.0f)
            {
                // Use camera up
                up2 = Vector3.UnitZ;
            }

            // cross twice to rederive, only direction is unaltered
            var left2 = dir.Cross(up2);

            left2.Normalize();
            up2 = dir.Cross(left2);
            up2.Normalize();

            // Derive quaternion from axes
            q = Quaternion.FromAxes(left2, up2, dir);
            textureCamera.Orientation = q;
        }
Example #10
0
 /// <summary>
 /// </summary>
 public bool Equals(Real obj, Real tolerance)
 {
     return(Utility.Abs(obj - this) <= tolerance);
 }
Example #11
0
 /// <summary>
 /// Used to test inequality between two Reals
 /// </summary>
 /// <param name="left"></param>
 /// <param name="right"></param>
 /// <returns></returns>
 /// <remarks>The == operator uses the static Tolerance value to determine equality </remarks>
 public static bool operator !=(Real left, Real right)
 {
     return(Utility.Abs(right._value - left._value) >= Tolerance);
 }
Example #12
0
        /// <summary>
        /// Extends mouseMoved to inject mouse position into tray manager, and checks
        /// for mouse wheel movements to slide the carousel, because we can.
        /// </summary>
        /// <param name="evt"></param>
        /// <returns></returns>
        public override bool MouseMoved(SIS.MouseEventArgs evt)
        {
            if (this.TrayManager.InjectMouseMove(evt))
            {
                return(true);
            }

            if (!(CurrentSample != null && !IsSamplePaused) && this.TitleLabel.TrayLocation != TrayLocation.None &&
                evt.State.Z.Relative != 0 && this.SampleMenu.ItemsCount != 0)
            {
                var newIndex = (int)(this.SampleMenu.SelectionIndex - evt.State.Z.Relative / Utility.Abs(evt.State.Z.Relative));
                this.SampleMenu.SelectItem(Utility.Clamp <int>(newIndex, this.SampleMenu.ItemsCount - 1, 0));
            }
            try
            {
                return(base.MouseMoved(evt));
            }
            catch (Exception ex) // show error and fall back to menu
            {
                RunSample(null);
                string msg = ex.Message + "\nSource: " + ex.InnerException;
                LogManager.Instance.Write("[Samples] Error! " + msg);
                this.TrayManager.ShowOkDialog("Error!", msg);
            }

            return(true);
        }
Example #13
0
 public bool DifferenceLessThan(Vector3 other, Real limit)
 {
     return(Utility.Abs(X - other.X) < limit && Utility.Abs(Y - other.Y) < limit && Utility.Abs(Z - other.Z) < limit);
 }
Example #14
0
 public bool AllComponentsLessThan(Real limit)
 {
     return(Utility.Abs(X) < limit && Utility.Abs(Y) < limit && Utility.Abs(Z) < limit);
 }
Example #15
0
 public bool DifferenceLessThan(Vector3 other, Real limit)
 {
     return(Utility.Abs(this.x - other.x) < limit && Utility.Abs(this.y - other.y) < limit &&
            Utility.Abs(this.z - other.z) < limit);
 }
Example #16
0
 public bool AllComponentsLessThan(Real limit)
 {
     return(Utility.Abs(this.x) < limit && Utility.Abs(this.y) < limit && Utility.Abs(this.z) < limit);
 }