Example #1
0
        public virtual void Transform(IUnitTransformation transform, TransformFlags flags)
        {
            foreach (var t in Vertices)
            {
                t.Location = transform.Transform(t.Location);
            }
            Plane  = new Plane(Vertices[0].Location, Vertices[1].Location, Vertices[2].Location);
            Colour = Colour;
            if (flags.HasFlag(TransformFlags.TextureScalingLock) && Texture.Texture != null)
            {
                // Make a best-effort guess of retaining scaling. All bets are off during skew operations.
                // Transform the current texture axes
                var origin = transform.Transform(Coordinate.Zero);
                var ua     = transform.Transform(Texture.UAxis) - origin;
                var va     = transform.Transform(Texture.VAxis) - origin;
                // Multiply the scales by the magnitudes (they were normals before the transform operation)
                Texture.XScale *= ua.VectorMagnitude();
                Texture.YScale *= va.VectorMagnitude();
            }
            {
                // Transform the texture axes and move them back to the origin
                var origin = transform.Transform(Coordinate.Zero);
                var ua     = transform.Transform(Texture.UAxis) - origin;
                var va     = transform.Transform(Texture.VAxis) - origin;

                // Only do the transform if the axes end up being not perpendicular
                // Otherwise just make a best-effort guess, same as the scaling lock
                if (Math.Abs(ua.Dot(va)) < 0.0001m && DMath.Abs(Plane.Normal.Dot(ua.Cross(va).Normalise())) > 0.0001m)
                {
                    Texture.UAxis = ua;
                    Texture.VAxis = va;
                }
                else
                {
                    AlignTextureToFace();
                }

                if (flags.HasFlag(TransformFlags.TextureLock) && Texture.Texture != null)
                {
                    // Check some original reference points to see how the transform mutates them
                    var scaled   = (transform.Transform(Coordinate.One) - transform.Transform(Coordinate.Zero)).VectorMagnitude();
                    var original = (Coordinate.One - Coordinate.Zero).VectorMagnitude();

                    // Ignore texture lock when the transformation contains a scale
                    if (DMath.Abs(scaled - original) <= 0.01m)
                    {
                        // Calculate the new shift values based on the UV values of the vertices
                        var vtx = Vertices[0];
                        Texture.XShift = Texture.Texture.Width * vtx.TextureU - (vtx.Location.Dot(Texture.UAxis)) / Texture.XScale;
                        Texture.YShift = Texture.Texture.Height * vtx.TextureV - (vtx.Location.Dot(Texture.VAxis)) / Texture.YScale;
                    }
                }
            }
            CalculateTextureCoordinates(true);
            UpdateBoundingBox();
        }
 private bool DrawElasticConfiguration <T>(
     string name,
     bool expanded,
     SerializedProperty elasticProperty,
     SerializedProperty extentProperty,
     TransformFlags requiredFlag,
     TransformFlags providedFlags) where T : ElasticConfiguration
 {
     if (providedFlags.HasFlag(requiredFlag))
     {
         bool result = false;
         using (new EditorGUI.IndentLevelScope())
         {
             result = InspectorUIUtility.DrawScriptableFoldout <T>(
                 elasticProperty,
                 name,
                 expanded);
             EditorGUILayout.PropertyField(extentProperty, includeChildren: true);
         }
         return(result);
     }
     else
     {
         return(false);
     }
 }
Example #3
0
File: Face.cs Project: silky/sledge
        public virtual void Transform(IUnitTransformation transform, TransformFlags flags)
        {
            foreach (var t in Vertices)
            {
                t.Location = transform.Transform(t.Location);
            }
            Plane = new Plane(Vertices[0].Location, Vertices[1].Location, Vertices[2].Location);
            Colour = Colour;
            if (flags.HasFlag(TransformFlags.TextureScalingLock) && Texture.Texture != null)
            {
                // Make a best-effort guess of retaining scaling. All bets are off during skew operations.
                // Transform the current texture axes
                var origin = transform.Transform(Coordinate.Zero);
                var ua = transform.Transform(Texture.UAxis) - origin;
                var va = transform.Transform(Texture.VAxis) - origin;
                // Multiply the scales by the magnitudes (they were normals before the transform operation)
                Texture.XScale *= ua.VectorMagnitude();
                Texture.YScale *= va.VectorMagnitude();
            }
            {
                // Transform the texture axes and move them back to the origin
                var origin = transform.Transform(Coordinate.Zero);
                var ua = transform.Transform(Texture.UAxis) - origin;
                var va = transform.Transform(Texture.VAxis) - origin;

                // Only do the transform if the axes end up being not perpendicular
                // Otherwise just make a best-effort guess, same as the scaling lock
                if (Math.Abs(ua.Dot(va)) < 0.0001m && DMath.Abs(Plane.Normal.Dot(ua.Cross(va).Normalise())) > 0.0001m)
                {
                    Texture.UAxis = ua;
                    Texture.VAxis = va;
                }
                else
                {
                    AlignTextureToFace();
                }

                if (flags.HasFlag(TransformFlags.TextureLock) && Texture.Texture != null)
                {
                    // Check some original reference points to see how the transform mutates them
                    var scaled = (transform.Transform(Coordinate.One) - transform.Transform(Coordinate.Zero)).VectorMagnitude();
                    var original = (Coordinate.One - Coordinate.Zero).VectorMagnitude();

                    // Ignore texture lock when the transformation contains a scale
                    if (DMath.Abs(scaled - original) <= 0.01m)
                    {
                        // Calculate the new shift values based on the UV values of the vertices
                        var vtx = Vertices[0];
                        Texture.XShift = Texture.Texture.Width * vtx.TextureU - (vtx.Location.Dot(Texture.UAxis)) / Texture.XScale;
                        Texture.YShift = Texture.Texture.Height * vtx.TextureV - (vtx.Location.Dot(Texture.VAxis)) / Texture.YScale;
                    }
                }
            }
            CalculateTextureCoordinates(true);
            UpdateBoundingBox();
        }
Example #4
0
 public bool IsDirected() => flags.HasFlag(TransformFlags.Directed);
Example #5
0
 public virtual void Transform(IUnitTransformation transform, TransformFlags flags)
 {
     foreach (var t in Vertices)
     {
         t.Location = transform.Transform(t.Location);
     }
     Plane = new Plane(Vertices[0].Location, Vertices[1].Location, Vertices[2].Location);
     Colour = Colour;
     if (flags.HasFlag(TransformFlags.TextureScalingLock) && Texture.Texture != null)
     {
         // Make a best-effort guess of retaining scaling. All bets are off during skew operations.
         // Transform the current texture axes
         var origin = transform.Transform(Coordinate.Zero);
         var ua = transform.Transform(Texture.UAxis) - origin;
         var va = transform.Transform(Texture.VAxis) - origin;
         // Multiply the scales by the magnitudes (they were normals before the transform operation)
         Texture.XScale *= ua.VectorMagnitude();
         Texture.YScale *= va.VectorMagnitude();
     }
     if (flags.HasFlag(TransformFlags.TextureLock) && Texture.Texture != null)
     {
         // Transform the texture axes and move them back to the origin
         var origin = transform.Transform(Coordinate.Zero);
         var ua = transform.Transform(Texture.UAxis) - origin;
         var va = transform.Transform(Texture.VAxis) - origin;
         // Only do the transform if the axes end up being not perpendicular
         // Otherwise just make a best-effort guess, same as the scaling lock
         if (Math.Abs(ua.Dot(va)) < 0.0001m)
         {
             Texture.UAxis = ua;
             Texture.VAxis = va;
         }
         // Calculate the new shift values based on the UV values of the vertices
         var vtx = Vertices[0];
         Texture.XShift = Texture.Texture.Width * vtx.TextureU - (vtx.Location.Dot(Texture.UAxis)) / Texture.XScale;
         Texture.YShift = Texture.Texture.Height * vtx.TextureV - (vtx.Location.Dot(Texture.VAxis)) / Texture.YScale;
     }
     else
     {
         // During rotate/skew operations we'll mess up the texture axes, just reset them.
         AlignTextureToFace();
     }
     CalculateTextureCoordinates();
     UpdateBoundingBox();
 }
    public void OnReplayRecord(IReplayWriter stream, ReplayInfo info)
    {
        _transformFlags = TransformFlags.None;
        if (info.ReplayStateMode == ReplayStateMode.Snapshot)
        {
            _snapshotPosition = transform.position;
            _snapshotRotation = transform.rotation;

            stream.Write(_snapshotPosition);
            stream.Write(_snapshotRotation);

            if (_rigidbody && trackRigid)
            {
                _snapshotVelocity        = _rigidbody.velocity;
                _snapshotAngularVelocity = _rigidbody.angularVelocity;
                stream.Write(_snapshotVelocity);
                stream.Write(_snapshotAngularVelocity);
            }
        }
        else if (info.ReplayStateMode == ReplayStateMode.Delta)
        {
            if (!ReplayMath.IsApproximatelyEqual(transform.position, _snapshotPosition))
            {
                _transformFlags |= TransformFlags.Transform;
            }

            if (!ReplayMath.IsApproximatelyEqual(transform.rotation, _snapshotRotation))
            {
                _transformFlags |= TransformFlags.Rotation;
            }

            if (_rigidbody && trackRigid)
            {
                if (!ReplayMath.IsApproximatelyEqual(_rigidbody.velocity, _snapshotVelocity))
                {
                    _transformFlags |= TransformFlags.Velocity;
                }

                if (!ReplayMath.IsApproximatelyEqual(_rigidbody.angularVelocity, _snapshotAngularVelocity))
                {
                    _transformFlags |= TransformFlags.AngularVelocity;
                }
            }

            stream.Write((byte)_transformFlags);
            if (_transformFlags.HasFlag(TransformFlags.Transform))
            {
                if (info.ReplayDeltaMode == ReplayDeltaMode.DeltaCompression)
                {
                    stream.Write(transform.position - _snapshotPosition);
                    _snapshotPosition = transform.position;
                }
                else if (info.ReplayDeltaMode == ReplayDeltaMode.SubFrame)
                {
                    stream.Write(transform.position);
                }
            }

            if (_transformFlags.HasFlag(TransformFlags.Rotation))
            {
                if (info.ReplayDeltaMode == ReplayDeltaMode.DeltaCompression)
                {
                    stream.Write(transform.rotation * Quaternion.Inverse(_snapshotRotation));
                    _snapshotRotation = transform.rotation;
                }
                else if (info.ReplayDeltaMode == ReplayDeltaMode.SubFrame)
                {
                    stream.Write(transform.rotation);
                }
            }

            if (_transformFlags.HasFlag(TransformFlags.Velocity))
            {
                if (info.ReplayDeltaMode == ReplayDeltaMode.DeltaCompression)
                {
                    stream.Write(_rigidbody.velocity - _snapshotVelocity);
                    _snapshotVelocity = _rigidbody.velocity;
                }
                else if (info.ReplayDeltaMode == ReplayDeltaMode.SubFrame)
                {
                    stream.Write(_rigidbody.velocity);
                }
            }

            if (_transformFlags.HasFlag(TransformFlags.AngularVelocity))
            {
                if (info.ReplayDeltaMode == ReplayDeltaMode.DeltaCompression)
                {
                    stream.Write(_rigidbody.angularVelocity - _snapshotAngularVelocity);
                    _snapshotAngularVelocity = _rigidbody.angularVelocity;
                }
                else if (info.ReplayDeltaMode == ReplayDeltaMode.SubFrame)
                {
                    stream.Write(_rigidbody.angularVelocity);
                }
            }
        }
    }
Example #7
0
        public virtual void Transform(IUnitTransformation transform, TransformFlags flags)
        {
            foreach (var t in Vertices)
            {
                t.Location = transform.Transform(t.Location);
            }
            Plane  = new Plane(Vertices[0].Location, Vertices[1].Location, Vertices[2].Location);
            Colour = Colour;

            var origin = transform.Transform(Coordinate.Zero);
            var ua     = transform.Transform(Texture.UAxis) - origin;
            var va     = transform.Transform(Texture.VAxis) - origin;

            if (flags.HasFlag(TransformFlags.TextureScalingLock) && Texture.Texture != null)
            {
                // Make a best-effort guess of retaining scaling. All bets are off during skew operations.
                // Transform the current texture axes
                // Multiply the scales by the magnitudes (they were normals before the transform operation)
                Texture.XScale *= ua.VectorMagnitude();
                Texture.YScale *= va.VectorMagnitude();
            }
            {
                //NOTE(SVK):Only edit solids not entities. //do entities have faces??
                if (this.Parent.Parent.GetType().Name == "World" && this.Parent.GetType().Name == "Solid")
                {
                    if (Texture.Flags.HasFlag(FaceFlags.TextureLocked))
                    {
                        Coordinate     Center = this.Parent.Parent.SelCenter;
                        Matrix         Xfrm   = transform.GetMatrix();
                        TransformFlags f      = (TransformFlags)0;
                        if (Xfrm != Matrix.Zero)
                        {
                            f = GetTransformFlags(Xfrm);
                        }
                        else
                        {
                            f = flags;
                        }


                        if (f.HasFlag(TransformFlags.Translate))
                        {
                            Texture.PositionRF = transform.Transform(Texture.PositionRF);
                        }

                        if (f.HasFlag(TransformFlags.Rotation))
                        {
                            Xfrm = ToRF(Xfrm);

                            Texture.TransformAngleRF = Xfrm * Texture.TransformAngleRF;
                            Texture.PositionRF       = Texture.PositionRF - Center;
                            Texture.PositionRF       = RotateRF(Xfrm, ToRF(Texture.PositionRF));
                            Texture.PositionRF       = ToChisel(Texture.PositionRF);
                            Texture.PositionRF       = Texture.PositionRF + Center;
                        }
                    }
                }
                else //entities etc...
                {
                    // Transform the texture axes and move them back to the origin
                    // Only do the transform if the axes end up being not perpendicular
                    // Otherwise just make a best-effort guess, same as the scaling lock

                    if (Math.Abs(ua.Dot(va)) < 0.0001m && DMath.Abs(Plane.Normal.Dot(ua.Cross(va).Normalise())) > 0.0001m)
                    {
                        Texture.UAxis = ua;
                        Texture.VAxis = va;
                    }
                    else
                    {
                        AlignTextureToWorld();
                    }
                    if (flags.HasFlag(TransformFlags.TextureLock) && Texture.Texture != null)
                    {
                        // Check some original reference points to see how the transform mutates them
                        var scaled   = (transform.Transform(Coordinate.One) - transform.Transform(Coordinate.Zero)).VectorMagnitude();
                        var original = (Coordinate.One - Coordinate.Zero).VectorMagnitude();
                        // Ignore texture lock when the transformation contains a scale
                        if (DMath.Abs(scaled - original) <= 0.01m)
                        {
                            // Calculate the new shift values based on the UV values of the vertices
                            var vtx = Vertices[0];
                            Texture.XShift = Texture.Texture.Width * vtx.TextureU - (vtx.Location.Dot(Texture.UAxis)) / Texture.XScale;
                            Texture.YShift = Texture.Texture.Height * vtx.TextureV - (vtx.Location.Dot(Texture.VAxis)) / Texture.YScale;
                        }
                    }
                }
            }

            AlignTexture();
            UpdateBoundingBox();
        }