我正在用Godot编写一个地形生成器。我使用SurfaceTool和三角形条带生成网格。几何体工作正常-但UV贴图不工作,因为每个三角形重复纹理,而不是覆盖纹理的完整网格一次:

规格化UV坐标以适应0,0 - 1,1维应该是可行的,但不起作用,因为它只使一对三角形具有完整的纹理,其余的为空:

extends MeshInstance
export var terrain_width = 16
export var terrain_depth = 16
export var terrain_height = 1
export var terrain_scale = 1
#Noise Generation
var terrain_noise = OpenSimplexNoise.new()
var terrain_heightmap = ImageTexture.new()
var terrain_texture = Texture.new()
var terrain_material = SpatialMaterial.new()
var terrain_mesh = Mesh.new()
var surfacetool = SurfaceTool.new()
func _ready():
generate_heightmap()
generate_mesh()
func generate_heightmap():
#Setup the noise
terrain_noise.seed = randi()
terrain_noise.octaves = 4
terrain_noise.period = 20.0
terrain_noise.persistence = 0.8
#Make the texture
terrain_heightmap.create_from_image(terrain_noise.get_image(terrain_width+1, terrain_depth+1))
terrain_texture = terrain_heightmap
func generate_mesh():
#Set the material texture to the heightmap
terrain_material.albedo_texture = terrain_texture
# This is where the uv-mapping occurs, via the add_uv function
for z in range(0,terrain_depth):
surfacetool.begin(Mesh.PRIMITIVE_TRIANGLE_STRIP)
for x in range(0,terrain_width):
surfacetool.add_uv(Vector2((terrain_width-x)/terrain_width,z/terrain_depth))
surfacetool.add_vertex(Vector3((terrain_width-x)*terrain_scale, terrain_noise.get_noise_2d(x,z)*terrain_height*terrain_scale, z*terrain_scale))
surfacetool.add_uv(Vector2((terrain_width-x)/terrain_width,(z+1)/terrain_depth))
surfacetool.add_vertex(Vector3((terrain_width-x)*terrain_scale, terrain_noise.get_noise_2d(x,z+1)*terrain_height*terrain_scale, (z+1)*terrain_scale))
surfacetool.generate_normals()
surfacetool.index()
surfacetool.commit(terrain_mesh)
#Set the texture and mesh on the MeshInstance
self.material_override = terrain_material
self.set_mesh(terrain_mesh)
self.set_surface_material(0, terrain_texture)我希望每个三角形只覆盖纹理中相应的坐标。看起来好像每个三角形都被看作是一个单独的表面。
发布于 2019-05-03 02:20:17
你有很多整数除法。有很多方法可以解决这个问题。这里有一个快速的方法:只需在导出的变量的默认值后添加.0:
export var terrain_width = 16.0
export var terrain_depth = 16.0
export var terrain_height = 1.0
export var terrain_scale = 1.0这里有一种更明确的方式:
export(float) var terrain_width = 16
export(float) var terrain_depth = 16
export(float) var terrain_height = 1
export(float) var terrain_scale = 1假设你没有任何其他的bug,这应该可以解决这个问题。
https://stackoverflow.com/questions/55376601
复制相似问题