Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Animate UV Offset in Unity Animation Timeline
#1
Hi is it possible to animate the UV Offset for the X or Y property. I have tried to do this by clicking on the record button in Unity's Animation Window, but it doesn't seem to pick it up as a property that can be changed over time.

   
Reply
#2
Hi
All the material parameters are internally stored in an array. It seems that the animator does not show members of type Array.
The solution would be then to create a script that will act as a middle man between the animation and the module:
The script will for example have a float V offset value, editable by an animation. The script will every frame apply that value to yourModule.MaterialSettings[yourMaterialIndex].UVOffset.y
Did this help?
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#3
(05-25-2023, 09:45 AM)_Aka_ Wrote: Hi
All the material parameters are internally stored in an array. It seems that the animator does not show members of type Array.
The solution would be then to create a script that will act as a middle man between the animation and the module:
The script will for example have a float V offset value, editable by an animation. The script will every frame apply that value to yourModule.MaterialSettings[yourMaterialIndex].UVOffset.y
Did this help?
I wrote the following script to try to control the value, but I seem to be getting an error saying that MissingComponentException, there is no Meshrender attached to the Game Object. 

Code:
using UnityEngine;

public class MaterialOffsetController : MonoBehaviour
{
    public float vOffset;
    public GameObject river;
    public int materialIndex;

    private MeshRenderer meshRenderer;

    private void Start()
    {
        // Get the MeshRenderer component from the specified GameObject
        meshRenderer = river.GetComponent<MeshRenderer>();
    }

    private void Update()
    {
        // Update the material's UVOffset.y value based on the vOffset value
        Material[] materials = meshRenderer.materials;
        if (materialIndex >= 0 && materialIndex < materials.Length)
        {
            Material material = materials[materialIndex];
            Vector2 offset = material.GetTextureOffset("_MainTex");
            offset.y = vOffset;
            material.SetTextureOffset("_MainTex", offset);
        }
    }
}

I am setting the script on a empty parent game object and selecting the Curvy Spline Generator as the GameObject. I am pretty sure I am not access the property value correctly.
Reply
#4
Not sure why some of the generators are creating references to the materials that I need to control the vOffset.

   

And other Generators will only create these Material references in Game Play mode, so I can't control the vOffset.

   
Reply
#5
(05-25-2023, 10:05 PM)sam_bond Wrote: I wrote the following script to try to control the value, but I seem to be getting an error saying that MissingComponentException, there is no Meshrender attached to the Game Object. 

Code:
using UnityEngine;

public class MaterialOffsetController : MonoBehaviour
{
    public float vOffset;
    public GameObject river;
    public int materialIndex;

    private MeshRenderer meshRenderer;

    private void Start()
    {
        // Get the MeshRenderer component from the specified GameObject
        meshRenderer = river.GetComponent<MeshRenderer>();
    }

    private void Update()
    {
        // Update the material's UVOffset.y value based on the vOffset value
        Material[] materials = meshRenderer.materials;
        if (materialIndex >= 0 && materialIndex < materials.Length)
        {
            Material material = materials[materialIndex];
            Vector2 offset = material.GetTextureOffset("_MainTex");
            offset.y = vOffset;
            material.SetTextureOffset("_MainTex", offset);
        }
    }
}

I am setting the script on a empty parent game object and selecting the Curvy Spline Generator as the GameObject. I am pretty sure I am not access the property value correctly.

You are accessing the wrong things. The script needs to modify the module, which will then be used to generate the objects. Your script tries to modify the  generated objects. Here is an example of such script

Code:
using FluffyUnderware.Curvy.Generator;
using FluffyUnderware.Curvy.Generator.Modules;
using UnityEngine;

[ExecuteAlways]
public class ApplyUVOffset : MonoBehaviour
{
    public BuildVolumeMesh VolumeMesh;
    public int MaterialIndex;
    public float UvOffsetY;

    private void Update()
    {
        if (VolumeMesh != null && VolumeMesh.MaterialSettings.Count > MaterialIndex)
        {
            CGMaterialSettingsEx materialSettings = VolumeMesh.MaterialSettings[MaterialIndex];
            materialSettings.UVOffset = new Vector2(
                materialSettings.UVOffset.x,
                UvOffsetY
            );
        }
    }
}

This script should be attached to a game object in your Unity scene. You'll need to set the VolumeMesh (your module), MaterialIndex (0 for the default material), and UvOffsetY properties in the Inspector. The script will then adjust the UV offset of the specified material every frame.

Did this help?
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#6
(05-26-2023, 09:50 AM)_Aka_ Wrote:
(05-25-2023, 10:05 PM)sam_bond Wrote: I wrote the following script to try to control the value, but I seem to be getting an error saying that MissingComponentException, there is no Meshrender attached to the Game Object. 

Code:
using UnityEngine;

public class MaterialOffsetController : MonoBehaviour
{
    public float vOffset;
    public GameObject river;
    public int materialIndex;

    private MeshRenderer meshRenderer;

    private void Start()
    {
        // Get the MeshRenderer component from the specified GameObject
        meshRenderer = river.GetComponent<MeshRenderer>();
    }

    private void Update()
    {
        // Update the material's UVOffset.y value based on the vOffset value
        Material[] materials = meshRenderer.materials;
        if (materialIndex >= 0 && materialIndex < materials.Length)
        {
            Material material = materials[materialIndex];
            Vector2 offset = material.GetTextureOffset("_MainTex");
            offset.y = vOffset;
            material.SetTextureOffset("_MainTex", offset);
        }
    }
}

I am setting the script on a empty parent game object and selecting the Curvy Spline Generator as the GameObject. I am pretty sure I am not access the property value correctly.

You are accessing the wrong things. The script needs to modify the module, which will then be used to generate the objects. Your script tries to modify the  generated objects. Here is an example of such script

Code:
using FluffyUnderware.Curvy.Generator;
using FluffyUnderware.Curvy.Generator.Modules;
using UnityEngine;

[ExecuteAlways]
public class ApplyUVOffset : MonoBehaviour
{
    public BuildVolumeMesh VolumeMesh;
    public int MaterialIndex;
    public float UvOffsetY;

    private void Update()
    {
        if (VolumeMesh != null && VolumeMesh.MaterialSettings.Count > MaterialIndex)
        {
            CGMaterialSettingsEx materialSettings = VolumeMesh.MaterialSettings[MaterialIndex];
            materialSettings.UVOffset = new Vector2(
                materialSettings.UVOffset.x,
                UvOffsetY
            );
        }
    }
}

This script should be attached to a game object in your Unity scene. You'll need to set the VolumeMesh (your module), MaterialIndex (0 for the default material), and UvOffsetY properties in the Inspector. The script will then adjust the UV offset of the specified material every frame.

Did this help?

This worked perfectly, thank you so much for pointing me in the right direction.
Reply
#7
You are welcome.
If and when you feel like it, please leave a review for the asset, that helps a lot.
Have a nice day
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Information New free asset: Converter For Unity Splines _Aka_ 17 48 06-03-2024, 03:31 PM
Last Post: _Aka_
  Question about Timeline support GhuaGhua 3 4 05-21-2024, 01:33 PM
Last Post: _Aka_
  Additional offset to Follow behaviour Ayyappa 3 11 04-30-2024, 01:35 PM
Last Post: _Aka_
  Using Unity's SplineContainer in Curvy Splines dlees9191 3 15 02-26-2024, 09:49 AM
Last Post: _Aka_

Forum Jump: