playMaker

Author Topic: Get Pixel  (Read 87 times)

Arnoob

  • Playmaker Newbie
  • *
  • Posts: 30
    • View Profile
Get Pixel
« on: April 14, 2017, 02: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: 204
  • Debt we all must pay...
    • View Profile
Re: Get Pixel
« Reply #1 on: April 15, 2017, 11:36:00 AM »
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, 11:40:36 AM by elusiven »

Arnoob

  • Playmaker Newbie
  • *
  • Posts: 30
    • View Profile
Re: Get Pixel
« Reply #2 on: April 15, 2017, 12: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: 11713
    • View Profile
Re: Get Pixel
« Reply #3 on: April 19, 2017, 03: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

  • Playmaker Newbie
  • *
  • Posts: 30
    • View Profile
Re: Get Pixel
« Reply #4 on: April 20, 2017, 10:57:48 AM »
Hi Jean!

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

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 11713
    • View Profile
Re: Get Pixel
« Reply #5 on: April 21, 2017, 12:05:02 AM »
Hi,

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

 Bye,

 Jean