首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Godot中,UV映射是如何与surfacetool一起工作的?

在Godot中,UV映射是如何与surfacetool一起工作的?
EN

Stack Overflow用户
提问于 2019-03-27 19:55:16
回答 1查看 1.3K关注 0票数 1

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

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

代码语言:javascript
复制
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)

我希望每个三角形只覆盖纹理中相应的坐标。看起来好像每个三角形都被看作是一个单独的表面。

EN

回答 1

Stack Overflow用户

发布于 2019-05-03 02:20:17

你有很多整数除法。有很多方法可以解决这个问题。这里有一个快速的方法:只需在导出的变量的默认值后添加.0:

代码语言:javascript
复制
export var terrain_width  = 16.0
export var terrain_depth  = 16.0
export var terrain_height = 1.0
export var terrain_scale  = 1.0

这里有一种更明确的方式:

代码语言:javascript
复制
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,这应该可以解决这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55376601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档