Godot Version
4.2
Question
I’m trying to create irregular shaped tiles using polygons, and I want to use the same texture for each tile. I have the minimum code below that creates a number of polygons in a row.
The texture for the first polygon is displayed properly, but for the remaining polygon, only a plain green texture is shown. I suspect the issue might be something to do with either UVs (which I’ve left empty), or how I’ve imported the texture resource.
Does anyone know what I’m doing wrong? I’ve linked below the texture I’m using, plus the test result.
CODE:
extends Node2D
# VARS
# the texture for each polygon
@onready var grass_texture : Texture2D = preload("res://grass_texture.png")
# the shape of the polygon - 64 x 64 square
@onready var polygon_corners : PackedVector2Array = [
Vector2(0, 0),
Vector2(64, 0),
Vector2(64, 64),
Vector2(0, 64),
]
# the number of polygons in a row
@onready var number_of_polygons : int = 3
# the spacing between the top-left corner of each polygon
@onready var polygon_spacing : Vector2 = Vector2(128, 0)
# FUNCS
func _ready():
# for each polygon
for polygon_number in number_of_polygons:
# create a variable to hold the updated coordinates for each corner
var corner_coordinates : PackedVector2Array
# for each corner, take the original position, and create world coordinates based on which polygon it is
for corner in polygon_corners:
var corner_coordinate : Vector2 = corner + (polygon_spacing * polygon_number)
corner_coordinates.append(corner_coordinate) # add coordinates to the holding variable
# now we have the coordinates, create a polygon
var new_polygon = Polygon2D.new()
# add the coordinates data, the texture, and add it to the tree
new_polygon.polygon = corner_coordinates
new_polygon.texture = grass_texture
add_child(new_polygon)