0 votes

Hello,

I am trying to generate a texture using the OpenSimplexNoise and apply it to a material with a script marked as tool, so I can see the changes in the editor.

But I can't figure out how to know when a value inside the OpenSimplexNoise resource is changed, so I can update the texture.

I know about NoiseTexture, but I just want to try to do it myself to see how things works.

Here is my current code:

[Tool]
public class RandomMap : MeshInstance
{
    private int width;
    private int height;

    [Export]
    private OpenSimplexNoise openSimplexNoise;

    [Export]
    public int Width
    {
        get { return width; }
        private set { width = value; UpdateTexture(); }
    }

    [Export]
    public int Height
    {
        get { return height; }
        private set { height = value; UpdateTexture(); }
    }

    private SpatialMaterial material = null;

    public override void _EnterTree()
    {
        Material currentMaterial = GetSurfaceMaterial(0);

        if (currentMaterial == null || !(currentMaterial is SpatialMaterial))
        {
            material = new SpatialMaterial();
            SetSurfaceMaterial(0, material);
        }
        else
        {
            material = currentMaterial as SpatialMaterial;
        }
    }

    public override void _Ready()
    {
        UpdateTexture();
    }

    private void UpdateTexture()
    {
        ImageTexture texture = new ImageTexture();
        texture.CreateFromImage(openSimplexNoise.GetImage(Width, Height));
        material.AlbedoTexture = texture;
    }
}

I tried using openSimplexNoise.Connect("changed", this, "UpdateTexture") but it didn't work. I think because it's code doesn't run on the editor.

Godot version 3.5
in Engine by (12 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.