playMaker

Author Topic: Get Pixel  (Read 2206 times)

Arnoob

  • Junior Playmaker
  • **
  • Posts: 63
Get Pixel
« on: April 14, 2017, 05:30:03 AM »
Hello every one! I am currently trying to make a custom action that permits to sample the pixel color of a texture at a given position (basically, an action for this : https://docs.unity3d.com/ScriptReference/Texture2D.GetPixel.html).

Here is what I wrote so far, but it seems to make Unity freeze. Does anyone have an idea of what I did wrong?

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Material)]
[Tooltip("Get pixel color from a readable texture from a normalized vector2 position")]
public class Texture2DGetPixel : FsmStateAction
{

[ActionSection("Input")]
public FsmTexture SampledTexture;

[ActionSection("Position")]
[Tooltip("The floats to get the normalized position from, from 0 to 1")]
public FsmFloat xPos;
public FsmFloat yPos;

[ActionSection("Result")]
[Tooltip("Store the sampled color in a Color variable.")]
public FsmColor StoredColor;

[Tooltip("Repeat every frame while the state is active.")]
public bool everyFrame;

public override void Reset()
{

SampledTexture = null;
xPos = 0;
yPos = 0;

}

public override void OnEnter()
{
DoGetPixelCOlor();

if (!everyFrame)
Finish();
}

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

void DoGetPixelCOlor()
{
if (SampledTexture == null) return;

{
Texture2D SourceTexture = SampledTexture.Value as Texture2D;
int x = Mathf.FloorToInt (xPos.Value * SourceTexture.width);
int y = Mathf.FloorToInt (yPos.Value * SourceTexture.height);
Color StoredColor = SourceTexture.GetPixel (x, y);
}
}

}
}

elusiven

  • Full Member
  • ***
  • Posts: 233
  • Debt we all must pay...
Re: Get Pixel
« Reply #1 on: April 15, 2017, 02:36:00 PM »
From what I can see, you are invoking the method in OnEnter, which launches when the state goes active. Also, you are constantly invoking the method in the OnUpdate.

OnUpdate should only be triggered if the bool is true, otherwise, do the OnEnter, and then finish it.

Also, you can set bool to be false by default.

See if this works, I've not tested this.

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Material)]
[Tooltip("Get pixel color from a readable texture from a normalized vector2 position")]
public class Texture2DGetPixel : FsmStateAction
{

[ActionSection("Input")]
public FsmTexture SampledTexture;

[ActionSection("Position")]
[Tooltip("The floats to get the normalized position from, from 0 to 1")]
public FsmFloat xPos;
public FsmFloat yPos;

[ActionSection("Result")]
[Tooltip("Store the sampled color in a Color variable.")]
public FsmColor StoredColor;

[Tooltip("Repeat every frame while the state is active.")]
public FsmBool everyFrame;

public override void Reset()
{

SampledTexture = null;
xPos = null;
yPos = null;
                        everyFrame = null;

}

public override void OnEnter()
{
DoGetPixelCOlor();
            Finish();
}

public override void OnUpdate()
{
if(everyFrame.Value) {
                DoGetPixelCOlor();
            }
}

void DoGetPixelCOlor()
{
if (SampledTexture == null) return;

{
Texture2D SourceTexture = SampledTexture.Value as Texture2D;
int x = Mathf.FloorToInt (xPos.Value * SourceTexture.width);
int y = Mathf.FloorToInt (yPos.Value * SourceTexture.height);
Color StoredColor = SourceTexture.GetPixel (x, y);
}
}

}
}
« Last Edit: April 15, 2017, 02:40:36 PM by elusiven »

Arnoob

  • Junior Playmaker
  • **
  • Posts: 63
Re: Get Pixel
« Reply #2 on: April 15, 2017, 03:21:20 PM »
Thank you for your answer!

However, I realized that the freeze was coming from somewhere else. The action comples without problems and doesn't hang the editor.

That said, it doesn't returns the sampled color (the color always stays the same).
Does anyone knows where I did a mistake in the function itself?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Get Pixel
« Reply #3 on: April 19, 2017, 06:31:45 AM »
Hi,

 Have you checked GetPixelColor on the Ecosystem? I think it does the same thing you could compare.

also, one of your issue is that you should ONLY call finish inside OnEnter if Everyframe is false, check out other actions on how they handle everyframe and finish().


 Bye,

 Jean

Arnoob

  • Junior Playmaker
  • **
  • Posts: 63
Re: Get Pixel
« Reply #4 on: April 20, 2017, 01:57:48 PM »
Hi Jean!

That's strange, I can't find it in the ecosystem (using version 4.9)?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Get Pixel
« Reply #5 on: April 21, 2017, 03:05:02 AM »
Hi,

 ah yes, it wasn't not listed properly, now it is :)

 Bye,

 Jean