quick scaling on X Y Z Axes fix

This post has 2 Replies | 2 Followers

Top 500 Contributor
Posts 5
Ion_21 Posted: 02-22-2011 2:35 PM

This is a quick fix to allow a user to scale models along the X, Y, and Z axes. That said Do not expect pretty code, for it to be error handled, or for it to work perfectly. 

How to use the it:

The code creates components that can be added to existing models.

ChangeRelativeModelScale

When added to the model it changes the model from static to dynamic; and you will see 3 text boxes in the editor labeled with the axis they handle and containing the value 1.0000. In general it works like the scale in the Apply relative transform dialog. The model is scaled based on the values entered tin the text boxes. So, values above 1 scale the model up along the specified axis; and values below 1 scale the model down along the specified axis.

ScaleValueManipulation

When added to the model it changes the model from static to dynamic; and you will see 3 text boxes in the editor labeled with the axis they handle and containing the actual scale values extracted from the model's matrix. changing these values will actually replace the scale of the matrix with the values in the 3 text boxes. 

The reason they change the model to dynamic is so that the onUpdate method will get called so that the changes in the scale will be made visible in the editor. 

After the components are used they can be removed and the scaling changes will persist , because the scaling info is saved in the matrix and not the component it self.

trouble shooting 

If you make a change to the scale of a model and you do not see any changes.

  • Try moving the model to kick start the OnUpdate method.
  • Make sure the model is dynamic.
  • if the values are to low it can result in errors that create in a scale of zero along an axis so flat models

The model is still there but moved.

  • it just moved I do not know how to fix that 

 

by posting it here i am looking for ways to better the components (maybe so they do not have to be dynamic to show changes).

Thank you for your time 

Ion_21

the code ------------------------------------------------------------

 

//ScaleValueManipulation---------------------------------------------------------------

using System;

using Microsoft.Xna.Framework;

using SynapseGaming.LightingSystem.Core;

using SynapseGaming.LightingSystem.Editor;

using SynapseGaming.LightingSystem.Rendering;

using SynapseGaming.LightingSystem.Serialization;

 

 

namespace greatness.Components

{

    [Serializable]

    class ScaleValueManipulation : BaseComponentAutoSerialization<ISceneEntity>

    {

        /// <summary>

        /// The scale of the model.

        /// The EditorProperty and EditorNumberPadOptions attributes define

        /// how the editor control for this property will behave.

        /// The reason the max value you can put in the ere in 0.9999999 is 

        /// past that pion things start to happen that i do not know about.

        /// This alco changes the ObjectType to dynamic, because if it is not 

        /// the OnUpdate will not be called and changes you want to make will 

        /// not be done. - Travis

        /// </summary>

        [SerializeMember]

        [EditorProperty(true, Description = "Model X Scale", ToolTipText = "Model's X scale in relation to the world.")]

        [EditorNumberPadOptions(8, 0.00000001, 0.9999999, 0.00000001)]

        public float f_XmodelScale { get; set; }

        [SerializeMember]

        [EditorProperty(true, Description = "Model Y Scale", ToolTipText = "Model's Y scale in relation to the world.")]

        [EditorNumberPadOptions(8, 0.00000001, 0.9999999, 0.00000001)]

        public float f_YmodelScale { get; set; }

        [SerializeMember]

        [EditorProperty(true, Description = "Model Z Scale", ToolTipText = "Model's Z scale in relation to the world.")]

        [EditorNumberPadOptions(8, 0.00000001, 0.9999999, 0.00000001)]

        public float f_ZmodelScale { get; set; }

 

 

        /// <summary>

        /// Initializes the component to have the scale from the matrix and sets it to dynamic. 

        /// (gets the current values of the model's matrix and from that gets the curent scales along the 3 asixs)

        /// </summary>

        public override void OnInitialize()

        {

            if (!(ParentObject is ISceneObject))

                return;

 

            Vector3 v3_currentScale = new Vector3(1f);

            Vector3 v3_currentTanslation;

            Quaternion quat_currentRotation;

            ISceneObject obj = ParentObject as ISceneObject;

 

            if (obj.World.Decompose(out v3_currentScale, out quat_currentRotation, out v3_currentTanslation))

            {

                f_XmodelScale = v3_currentScale.X;

                f_YmodelScale = v3_currentScale.Y;

                f_ZmodelScale = v3_currentScale.Z;

            }

            obj.ObjectType = ObjectType.Dynamic;

 

        }

 

        /// <summary>

        /// ovreides the update method and calls the DoScaling() method to do the calculations 

        /// </summary>

        public override void OnUpdate(GameTime gametime)

        {

            if (!(ParentObject is ISceneObject))

                return;

 

            Vector3 v3_currentScale = new Vector3(1f);

            Vector3 v3_currentTanslation;

            Quaternion quat_currentRotation;

 

            ISceneObject obj = ParentObject as ISceneObject;

 

            if (obj.World.Decompose(out v3_currentScale, out quat_currentRotation, out v3_currentTanslation))

            {

                if (!v3_currentScale.Equals(new Vector3(f_XmodelScale, f_YmodelScale, f_ZmodelScale)))

                {

                    DoScaling();

                }

 

            }

 

        }

 

        /// <summary>

        ///replaces the origional scale with the scale from the editor' UI

        /// </summary>

        public void DoScaling()

        {

            if (!(ParentObject is ISceneObject))

                return;

 

            Vector3 v3_currentScale = new Vector3(1f);

            Vector3 v3_currentTanslation;

            Quaternion quat_currentRotation;

            ISceneObject obj = ParentObject as ISceneObject;

 

            if (obj.World.Decompose(out v3_currentScale, out quat_currentRotation, out v3_currentTanslation))

            {

                obj.World = Matrix.CreateScale(f_XmodelScale, f_YmodelScale, f_ZmodelScale) * Matrix.CreateFromQuaternion(quat_currentRotation) * Matrix.CreateTranslation(v3_currentTanslation);

            }

 

        }

    }

}

//ChangeRelativeModelScale------------------------------------------------------
using System;
using Microsoft.Xna.Framework;
using SynapseGaming.LightingSystem.Core;
using SynapseGaming.LightingSystem.Editor;
using SynapseGaming.LightingSystem.Rendering;
using SynapseGaming.LightingSystem.Serialization;
namespace greatness.Components
{
    [Serializable]
    class ChangeRelativeModelScale : BaseComponentAutoSerialization<ISceneEntity>
    {
        /// <summary>
        /// The scale the model no indviduall axises.
        /// The EditorProperty and EditorNumberPadOptions attributes define
        /// how the editor control for this property will behave.
        /// multiplies the scale of the model by the value in on the 
        /// specified axis by the amount in the NumberPad control.
        /// (should work similar to the "Apply relative transform dialog" but 
        /// values shoumd presist)
        /// MAY Need to move the target to force an upDate (don't know why yet)
        /// - Travis
        /// </summary>
        [SerializeMember]
        [EditorProperty(true, Description = "Relative X Scale", ToolTipText = "Model's X scale in relation to the itself.")]
        [EditorNumberPadOptions(7, 0.0000001, 100, 0.0000001)]
        public float f_XmodelScale { get; set; }
        [SerializeMember]
        [EditorProperty(true, Description = "Relative Y Scale", ToolTipText = "Model's Y scale in relation to the itself.")]
        [EditorNumberPadOptions(7, 0.0000001, 100, 0.0000001)]
        public float f_YmodelScale { get; set; }
        [SerializeMember]
        [EditorProperty(true, Description = "Relative Z Scale", ToolTipText = "Model's Z scale in relation to the itself.")]
        [EditorNumberPadOptions(7, 0.0000001, 100, 0.0000001)]
        public float f_ZmodelScale { get; set; }
        /// <summary>
        /// Initializes the component to have the scale of 1.0f and sets it to dynamic. 
        /// </summary>
        public override void OnInitialize()
        {
            if (!(ParentObject is ISceneObject))
                return;
            ISceneObject obj = ParentObject as ISceneObject;
            Vector3 v3_currentScale = new Vector3(1f);
            f_XmodelScale = v3_currentScale.X;
            f_YmodelScale = v3_currentScale.Y;
            f_ZmodelScale = v3_currentScale.Z;
            obj.ObjectType = ObjectType.Dynamic;
        }
        /// <summary>
        /// ovreides the update method and calls the DoScaling() method to do the calculations 
        /// </summary>
        public override void OnUpdate(GameTime gametime)
        {
            if (!(ParentObject is ISceneObject))
                return;
            Vector3 v3_scaleTestValue = new Vector3(1f);
            ISceneObject obj = ParentObject as ISceneObject;
            Vector3 v3_scaleToMultiplyBy = new Vector3(f_XmodelScale, f_YmodelScale, f_ZmodelScale);
            if (!v3_scaleTestValue.Equals(v3_scaleToMultiplyBy))
            {
                ScaleTheModel(v3_scaleToMultiplyBy);
            }
            f_XmodelScale = 1f;
            f_YmodelScale = 1f;
            f_ZmodelScale = 1f;
        }
        /// <summary>
        ///replaces the origional scale with the scale from the editor' UI
        /// </summary>
        public void ScaleTheModel(Vector3 v3_scaleToMultiplyBy)
        {
            if (!(ParentObject is ISceneObject))
                return;
            ISceneObject obj = ParentObject as ISceneObject;
            obj.World = Matrix.CreateScale(v3_scaleToMultiplyBy) * obj.World;
        }
    }
}

Top 10 Contributor
Posts 5,368
Employee
SunBurn_Studio_Licensee

Hi Ion,

Nice work, that looks great.  I'm sure a lot of people will find this very helpful, thanks for contributing it to the community!

 

Follow me on Twitter – development and personal tweets
Awesome XNA Videos – Lighting, Rendering, and game videos

Top 10 Contributor
Posts 1,289
SunBurn_Community_Licensee
SunBurn_Contributor
SunBurn_Pro_Licensee

That's a great idea: I can imagine adding features as hacks to the SunBurn Editor this way :p

Page 1 of 1 (3 items) | RSS