Monogame - 缩放时在网格上重复纹理

问题描述 投票:0回答:0

目前我正在开发一款第一人称射击游戏,它使用 3D 网格构建我的世界。我的网格可以在每一侧(前、后、左、右、上、下)都有纹理。这很好用。这是代码:

    public Block()
    {
        vertexData = new List<VertexPositionNormalTexture>();

        //front
        vertexData.Add(new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, 0.5f), new Vector3(0, 0, 1), new Vector2(0.0f, 1.0f)));
        vertexData.Add(new VertexPositionNormalTexture(new Vector3(-0.5f, 0.5f, 0.5f), new Vector3(0, 0, 1), new Vector2(0.0f, 0.0f)));
        vertexData.Add(new VertexPositionNormalTexture(new Vector3(0.5f, -0.5f, 0.5f), new Vector3(0, 0, 1), new Vector2(1.0f, 1.0f)));

        vertexData.Add(new VertexPositionNormalTexture(new Vector3(0.5f, -0.5f, 0.5f), new Vector3(0, 0, 1), new Vector2(1.0f, 1.0f)));
        vertexData.Add(new VertexPositionNormalTexture(new Vector3(-0.5f, 0.5f, 0.5f), new Vector3(0, 0, 1), new Vector2(0.0f, 0.0f)));
        vertexData.Add(new VertexPositionNormalTexture(new Vector3(0.5f, 0.5f, 0.5f), new Vector3(0, 0, 1), new Vector2(1.0f, 0.0f)));

        SetUp(TexType.front);
        vertexData = new List<VertexPositionNormalTexture>();

        //right
        vertexData.Add(new VertexPositionNormalTexture(new Vector3(0.5f, -0.5f, 0.5f), new Vector3(1, 0, 0), new Vector2(0.0f, 1.0f)));
        vertexData.Add(new VertexPositionNormalTexture(new Vector3(0.5f, 0.5f, 0.5f), new Vector3(1, 0, 0), new Vector2(0.0f, 0.0f)));
        vertexData.Add(new VertexPositionNormalTexture(new Vector3(0.5f, -0.5f, -0.5f), new Vector3(1, 0, 0), new Vector2(1.0f, 1.0f)));

        vertexData.Add(new VertexPositionNormalTexture(new Vector3(0.5f, -0.5f, -0.5f), new Vector3(1, 0, 0), new Vector2(1.0f, 1.0f)));
        vertexData.Add(new VertexPositionNormalTexture(new Vector3(0.5f, 0.5f, 0.5f), new Vector3(1, 0, 0), new Vector2(0.0f, 0.0f)));
        vertexData.Add(new VertexPositionNormalTexture(new Vector3(0.5f, 0.5f, -0.5f), new Vector3(1, 0, 0), new Vector2(1.0f, 0.0f)));

        SetUp(TexType.right);
        vertexData = new List<VertexPositionNormalTexture>();

  //and so on for the remaining faces...
  }


    public void SetUp(TexType texType)
    {
        vertexBuffer = new VertexBuffer(Basic.gDevice, typeof(VertexPositionNormalTexture), vertexData.Count, BufferUsage.WriteOnly);
        vertexBuffer.SetData(vertexData.ToArray());

        buffers.Add(new Tuple<VertexBuffer, TexType>(vertexBuffer, texType));
    }

    public void Draw(Matrix world)
    {
            foreach (var buffer in buffers)
            {
                string texName = textures[buffer.Item2.ToString()];
                GameScreen.effect.Texture = Textures.textures[texName];

                SetEffectsAndDraw(world, buffer.Item1, buffer.Item1.VertexCount);
            }
    }
    private void SetEffectsAndDraw(Matrix world, VertexBuffer buffer, int primitiveCount)
    {
        GameScreen.effect.View = GameScreen.camera.view;
        GameScreen.effect.Projection = GameScreen.camera.projection;
        GameScreen.effect.EnableDefaultLighting();
        GameScreen.effect.World = world;

        GameScreen.effect.FogEnabled = true;
        GameScreen.effect.FogColor = Color.Black.ToVector3();
        GameScreen.effect.FogStart = 4.0f;
        GameScreen.effect.FogEnd = 12.0f;

        //GameScreen.effect.TextureEnabled = true;

        GameScreen.effect.CurrentTechnique.Passes[0].Apply();

        Basic.gDevice.SetVertexBuffer(buffer);
        Basic.gDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, primitiveCount / 3);
    }

现在我可以在每个轴上缩放我的网格。我的问题是,网格上的纹理被拉伸了。 缩放网格时如何在网格面上重复纹理?例如,当我使用 scale.Y = 10 缩放网格时,纹理看起来非常难看。 是否有可能在拉伸和重复纹理之间切换? 我搜索了很多但找不到解决方案。

提前谢谢你!

xna textures scale mesh monogame
© www.soinside.com 2019 - 2024. All rights reserved.