playMaker

Author Topic: Mesh UV offset  (Read 4900 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15316
  • Official Playmaker Support
    • View Profile
Mesh UV offset
« on: September 16, 2012, 04:26:29 AM »
Hi,

 Following a request,
http://hutonggames.com/playmakerforum/index.php?topic=2257.0

Please find an action that will offset the mesh UV ( NOT THE MATERIAL UV).

it works as well everyframe if you want to animate the UV.

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2012. All rights reserved.

using UnityEngine;
using System;
using HutongGames.PlayMaker;

[ActionCategory("Meshes")]
[Tooltip("Offset Mesh UV")]
public class OffsetUV : FsmStateAction
{
[RequiredField]
[CheckForComponent(typeof(MeshFilter))]
public FsmOwnerDefault gameObject;

public FsmVector2 offset;

public FsmFloat offsetX;
public FsmFloat offsetY;


public bool everyFrame;

Mesh _mesh;
Vector2[] _uvs;



public override void Reset()
{
gameObject = null;
offset = null;
offsetX = null;
offsetY = null;
everyFrame = false;
}

public override void OnEnter ()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
Finish();
return;
}

_mesh = (go.GetComponent<MeshFilter>() as MeshFilter).mesh;


if (_mesh == null)
{
LogError("Missing Mesh!");
Finish();
return;
}

  // Build the uvs
    _uvs = new Vector2[_mesh.vertices.Length];
    _uvs = _mesh.uv; // Take the existing UVs



DoOffsetUV();

if (!everyFrame)
{
Finish();
}
}

public override void OnUpdate ()
{
DoOffsetUV();
}

private float _xOffset = 0f;
private float _yOffset = 0f;

void DoOffsetUV()
{
if (_mesh == null)
{
return;
}

Vector2[] _new_uvs = new Vector2[_uvs.Length];

float xOffset = offsetX.Value;
float yOffset = offsetY.Value;
if (! offset.IsNone)
{
xOffset += offset.Value.x;
yOffset += offset.Value.y;
}

if (xOffset.Equals(_xOffset) && yOffset.Equals(_yOffset))
{
// no change.
return;
}
_xOffset = xOffset;
_yOffset = yOffset;

    for(int i = 0; i < _uvs.Length; i++)
{
_new_uvs[i] = new Vector2 (_uvs[i].x + _xOffset, _uvs[i].y + _yOffset); // Offset all the UV's
}

   // Apply the modded UV's
   _mesh.uv = _new_uvs;
}
}


bye,

 Jean

Groo Gadgets

  • Full Member
  • ***
  • Posts: 139
    • View Profile
Re: Mesh UV offset
« Reply #1 on: September 19, 2012, 04:23:05 PM »
Hey Jean,

Thank you very much for this custom action, I just tested it out and it works perfectly!

You're a lifesaver and a real credit to the PlayMaker comunity  :-)

Cheers,

Simon

clandestine

  • Sr. Member
  • ****
  • Posts: 254
    • View Profile
    • ЯKD.ZONE
Re: Mesh UV offset
« Reply #2 on: October 20, 2014, 07:00:40 AM »
Hey guys, i know this is old, but i need this action so i downloaded and it gives me the following error (i'm using U4.5):

Assets/PlayMaker/Actions/OffsetUV.cs(8,2): error CS0246: The type or namespace name `Tooltip' could not be found. Are you missing a using directive or an assembly reference?

Assets/PlayMaker/Actions/OffsetUV.cs(8,2): error CS0104: `TooltipAttribute' is an ambiguous reference between `UnityEngine.TooltipAttribute' and `HutongGames.PlayMaker.TooltipAttribute'


Is there a workaround or a fix for that?
Do you have an updated action or an alternative? (for scrolling the texture or normal map)

Thanks a lot for your time!

clandestine

  • Sr. Member
  • ****
  • Posts: 254
    • View Profile
    • ЯKD.ZONE
Re: Mesh UV offset
« Reply #3 on: October 28, 2014, 12:23:36 AM »
Please.

mdotstrange

  • Hero Member
  • *****
  • Posts: 551
    • View Profile
    • Can't code? Who cares! Make games anyway!
Re: Mesh UV offset
« Reply #4 on: November 01, 2014, 11:52:47 PM »
After seeing your post I was curious about this action- it is available through Ecosystem and it generates the same error when you download/install it from there as well.
« Last Edit: November 02, 2014, 02:04:25 AM by mdotstrange »
Indie game dev and instructor at the Strange School. Learn Playmaker at the Strange School!

terri

  • Sr. Member
  • ****
  • Posts: 375
    • View Profile
    • terrivellmann.tumblr.com
Re: Mesh UV offset
« Reply #5 on: January 18, 2015, 08:33:49 AM »
Hey guys, i know this is old, but i need this action so i downloaded and it gives me the following error (i'm using U4.5):

Assets/PlayMaker/Actions/OffsetUV.cs(8,2): error CS0246: The type or namespace name `Tooltip' could not be found. Are you missing a using directive or an assembly reference?

Assets/PlayMaker/Actions/OffsetUV.cs(8,2): error CS0104: `TooltipAttribute' is an ambiguous reference between `UnityEngine.TooltipAttribute' and `HutongGames.PlayMaker.TooltipAttribute'


Is there a workaround or a fix for that?
Do you have an updated action or an alternative? (for scrolling the texture or normal map)

Thanks a lot for your time!

check this out:
http://hutonggames.com/playmakerforum/index.php?topic=7454.msg36892

clandestine

  • Sr. Member
  • ****
  • Posts: 254
    • View Profile
    • ЯKD.ZONE
Re: Mesh UV offset
« Reply #6 on: January 18, 2015, 09:15:04 AM »
Quote from: terri
check this out:
http://hutonggames.com/playmakerforum/index.php?topic=7454.msg36892

Hey thanks Terri, i didnt understand much, but will give it a shot and see what happens.

clandestine

  • Sr. Member
  • ****
  • Posts: 254
    • View Profile
    • ЯKD.ZONE
Re: Mesh UV offset
« Reply #7 on: March 05, 2015, 07:29:29 AM »
I had the chance to check out the solution and its extremely simple to fix.
But how do i use the action, nothing seems to happen?

pandachilli

  • Playmaker Newbie
  • *
  • Posts: 9
    • View Profile
Re: Mesh UV offset
« Reply #8 on: October 01, 2015, 03:13:42 AM »
As a Playmaker / Unity N00b and at the risk of stating the obvious the way it seems to work for me was to:
1. Add a preceding Animated Float(?) action + setting the Anim Curve to loop (adjusted by clicking the cog icons in the mini-curve editor)
2. Create & assign a (float) variable as the 'Float Variable'.
3. Passing that into the UV Offset by using that new variable on the required axis.

Hope this helps someone. Great action BTW..thanks!
« Last Edit: October 01, 2015, 04:21:37 AM by pandachilli »