You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Godot 4.1程序化网格生成无显示问题求助

Godot 4.1程序化网格生成无法显示问题

我在Godot 4.1中进行程序化网格生成时遇到了困难。以下是我的测试代码(仍处于实验阶段,代码较为杂乱,敬请谅解):

using Godot;
using Godot.Collections;
using System;
using System.Collections.Generic;

[GlobalClass]
public partial class ChunkMesh : ArrayMesh
{
    private Vector3 downLeft = Vector3.Left + Vector3.Down;
    private Vector3 downRight = Vector3.Right + Vector3.Down;
    private Vector3 upLeft = Vector3.Up + Vector3.Left;
    private Vector3 upRight = Vector3.Up + Vector3.Right;
    private Godot.Collections.Array arrays = new Godot.Collections.Array();
    private List<Vector3> vertex = new List<Vector3>();
    private List<Vector3> normal = new List<Vector3>();
    private List<Color> color = new List<Color>();
    private List<int> indices = new List<int>();

    private void Begin()
    {
        ClearSurfaces();
        vertex.Clear();
        normal.Clear();
        color.Clear();
        indices.Clear();
    }

    private void End()
    {
        arrays.Resize((int)ArrayType.Max);
        arrays[(int)ArrayType.Vertex] = vertex.ToArray();
        arrays[(int)ArrayType.Normal] = normal.ToArray();
        arrays[(int)ArrayType.Color] = color.ToArray();
        arrays[(int)ArrayType.Index] = indices.ToArray();
        AddSurfaceFromArrays(PrimitiveType.Triangles, arrays);
    }

    private Vector3 Rotate(Vector3 toRotate, Vector3 axis, float angle)
    {
        if (axis.Length() == 0)
            return toRotate;
        return toRotate.Rotated(axis, angle);
    }

    private void DrawPoint(Vector3 position, Vector3 direction, Color color)
    {
        this.normal.Add(direction);
        this.color.Add(color);
        this.vertex.Add(position);
    }

    public void DrawQuad(Vector3 position, Vector3 direction, float scale, Color color)
    {
        direction = direction.Normalized();
        Vector3 dirDiff = Vector3.Back - direction;
        Vector3 rotateAxis = Vector3.Back.Cross(dirDiff).Normalized();
        float angle = Vector3.Back.SignedAngleTo(dirDiff, rotateAxis);
        scale /= 2;
        Begin();
        DrawPoint(position + Rotate(downLeft * scale, rotateAxis, angle), direction, color);
        DrawPoint(position + Rotate(upLeft * scale, rotateAxis, angle), direction, color);
        DrawPoint(position + Rotate(upRight * scale, rotateAxis, angle), direction, color);
        DrawPoint(position + Rotate(downRight * scale, rotateAxis, angle), direction, color);
        indices.Add(0);
        indices.Add(1);
        indices.Add(2);
        indices.Add(2);
        indices.Add(3);
        indices.Add(0);
        End();
    }
}
using Godot;
using System;

[GlobalClass]
public partial class Chunk : MeshInstance3D
{
    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {

    }

    // Called every frame. 'delta' is the elapsed time since the previous frame.
    public override void _Process(double delta)
    {
        float scale = Time.GetTicksMsec() / 1000f % (Mathf.Pi * 2);
        ((ChunkMesh)Mesh).DrawQuad(Vector3.Zero, Vector3.Back.Rotated(Vector3.Up, scale), 1.0F, new Color("red"));
    }
}

我先尝试了ImmediateMesh,现在改用ArrayMesh,但无论如何操作,网格都无法显示,即使切换到编辑器的线框视图也看不到任何内容。请问我哪里出错了?

编辑1

我发现如果通过以下代码保存Mesh:

ResourceSaver.Save(Mesh, "res://quad.tres", ResourceSaver.SaverFlags.Compress);

保存后的网格资源是正常的,所以问题可能出在赋值、提交或显示环节?

编辑2

我在Godot 3.5中运行几乎相同的代码,它可以正常工作……所以这可能是一个bug,我已提交相关Issue。


内容的提问来源于stack exchange,提问作者Robert Ostrowski-Jagoda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 20:04:59