Jump to content

Something wrong with my mesh


Recommended Posts

I'm creating a mesh from scratch but when I add a texture, I get nothing. The plane is simply white. Are my UV coords or normals not set up correctly for Babylonjs or am I missing something else?

 

import { Mesh, VertexBuffer, StandardMaterial, Texture } from 'babylonjs'

export default function CreateMesh(height, width, scene) {
  const mesh = new Mesh('TerrainMesh', scene)
  const indices = []
  const vertices = []
  const normals = []
  const uvs = []
  const startX = -width / 2
  const startY = -height / 2
  let index = 0

  for (let y = 0; y < height; y++) {
    for (let x = 0; x < width; x++) {
      vertices.push(startX + x, 0, startY + y)
      uvs.push(x / (width), y / (width - 1))
      normals.push(0, 1, 0)
      if (x < width - 1 && y < height - 1) {
        indices.push(index, index + width + 1, index + width)
        indices.push(index + width + 1, index, index + 1)
      }
      index++
    }
  }

  mesh.setVerticesData(VertexBuffer.PositionKind, vertices)
  mesh.setVerticesData(uvs, VertexBuffer.UVKind)
  mesh.setVerticesData(normals, VertexBuffer.NormalKind)
  mesh.setIndices(indices)

  let groundMaterial = new StandardMaterial("ground", scene)
  groundMaterial.diffuseTexture = new Texture("textures/earth.jpg", scene)
  mesh.material = groundMaterial

  return mesh
}

Link to comment
Share on other sites

Hi guys!  First time posters... my treat (sometimes).  :)  (I have no life/purpose)  heh

https://www.babylonjs-playground.com/#RGZPJH#7

I found problem, too...

https://www.babylonjs-playground.com/#RGZPJH#8

  mesh.setVerticesData(VertexBuffer.PositionKind, vertices)  // correct params order
  mesh.setVerticesData(uvs, VertexBuffer.UVKind)  // reversed params
  mesh.setVerticesData(normals, VertexBuffer.NormalKind)  // reversed params

I've made this mistake a few HUNDRED times, myself.  :)  Party-on!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...