GodotComponentTest/scenes/tests/HexTile3DMaterialAssign.cs

65 lines
2.7 KiB
C#
Raw Normal View History

using System.Diagnostics;
2023-11-18 22:32:57 +01:00
using Godot;
2023-11-18 22:32:57 +01:00
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;
2023-11-18 22:32:57 +01:00
// Called when the node enters the scene tree for the first time.
2023-11-18 22:32:57 +01:00
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));
2023-11-18 22:32:57 +01:00
_hexTile = (HexTile3D)FindNode("HexTile3D");
Debug.Assert(_hexTile != null);
2023-11-18 22:32:57 +01:00
_customTileMaterial = GD.Load<ShaderMaterial>("materials/HexTileTextureLookup.tres");
_blackWhitePatternTexture = new ImageTexture();
2023-11-18 22:32:57 +01:00
Image image = new();
image.Load("assets/4x4checker.png");
2023-11-18 22:32:57 +01:00
_blackWhitePatternTexture.CreateFromImage(image, (uint)(Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
_colorPatternTexture = new ImageTexture();
image.Load("assets/4x4checkerColor.png");
2023-11-18 22:32:57 +01:00
_colorPatternTexture.CreateFromImage(image, (uint)(Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
}
2023-11-18 22:32:57 +01:00
public void OnBlackWhitePatternButton() {
GD.Print("Apply Black White Pattern!");
_customTileMaterial.SetShaderParam("MapAlbedoTexture", _blackWhitePatternTexture);
}
2023-11-18 22:32:57 +01:00
public void OnColorPatternButton() {
GD.Print("Apply Collor Pattern!");
//currentMaterial.SetShaderParam("MapAlbedoTexture", _colorPatternTexture);
_customTileMaterial.SetShaderParam("MapAlbedoTexture", _colorPatternTexture);
2023-11-18 22:32:57 +01:00
// _customTileMaterial.SetShaderParam("MapAlbedoTexture", _imageTexture);
2023-11-18 22:32:57 +01:00
// _hexTile.Mesh.SetSurfaceMaterial(0, _customTileMaterial);
}
2023-11-18 22:32:57 +01:00
public void OnTextureSizeChanged(float value) {
_customTileMaterial.SetShaderParam("TextureSize", (int)value);
GD.Print("Texture size: " + _customTileMaterial.GetShaderParam("TextureSize"));
}
2023-11-18 22:32:57 +01:00
}