GodotComponentTest/scenes/tests/HexTile3DMaterialAssign.cs

65 lines
2.7 KiB
C#

using System.Diagnostics;
using Godot;
public class HexTile3DMaterialAssign : Spatial {
// Declare member variables here. Examples:
// private int a = 2;
// private string b = "text";
private Button _blackWhitePatternButton;
private Button _colorPatternButton;
private SpinBox _textureSizeSpinBox;
private HexTile3D _hexTile;
private ShaderMaterial _customTileMaterial;
private ImageTexture _blackWhitePatternTexture;
private ImageTexture _colorPatternTexture;
// Called when the node enters the scene tree for the first time.
public override void _Ready() {
_blackWhitePatternButton = (Button)FindNode("BlackWhitePatternButton");
Debug.Assert(_blackWhitePatternButton != null);
_blackWhitePatternButton.Connect("pressed", this, nameof(OnBlackWhitePatternButton));
_colorPatternButton = (Button)FindNode("ColorPatternButton");
Debug.Assert(_colorPatternButton != null);
_colorPatternButton.Connect("pressed", this, nameof(OnColorPatternButton));
_textureSizeSpinBox = (SpinBox)FindNode("TextureSizeSpinBox");
Debug.Assert(_textureSizeSpinBox != null);
_textureSizeSpinBox.Connect("value_changed", this, nameof(OnTextureSizeChanged));
_hexTile = (HexTile3D)FindNode("HexTile3D");
Debug.Assert(_hexTile != null);
_customTileMaterial = GD.Load<ShaderMaterial>("materials/HexTileTextureLookup.tres");
_blackWhitePatternTexture = new ImageTexture();
Image image = new();
image.Load("assets/4x4checker.png");
_blackWhitePatternTexture.CreateFromImage(image, (uint)(Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
_colorPatternTexture = new ImageTexture();
image.Load("assets/4x4checkerColor.png");
_colorPatternTexture.CreateFromImage(image, (uint)(Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
}
public void OnBlackWhitePatternButton() {
GD.Print("Apply Black White Pattern!");
_customTileMaterial.SetShaderParam("MapAlbedoTexture", _blackWhitePatternTexture);
}
public void OnColorPatternButton() {
GD.Print("Apply Collor Pattern!");
//currentMaterial.SetShaderParam("MapAlbedoTexture", _colorPatternTexture);
_customTileMaterial.SetShaderParam("MapAlbedoTexture", _colorPatternTexture);
// _customTileMaterial.SetShaderParam("MapAlbedoTexture", _imageTexture);
// _hexTile.Mesh.SetSurfaceMaterial(0, _customTileMaterial);
}
public void OnTextureSizeChanged(float value) {
_customTileMaterial.SetShaderParam("TextureSize", (int)value);
GD.Print("Texture size: " + _customTileMaterial.GetShaderParam("TextureSize"));
}
}