Skip to content

Instantly share code, notes, and snippets.

@QueenOfSquiggles
Created June 10, 2021 15:12
Show Gist options
  • Save QueenOfSquiggles/0ba854e1d208602504338219123b44b5 to your computer and use it in GitHub Desktop.
Save QueenOfSquiggles/0ba854e1d208602504338219123b44b5 to your computer and use it in GitHub Desktop.
A test to see if my procedural mesh can handle materials that include depth and normal maps in Godot
extends Spatial
export (Material) var material : Material
var vertices := []
var uvs := []
func _ready():
generate()
func generate(): # generates the mesh and adds as a child
var st := SurfaceTool.new()
st.begin(Mesh.PRIMITIVE_TRIANGLES) # begin
st.set_material(material) #set material
make_quad(Vector3.ZERO, Vector2.ONE * 10.0) # setup the vertex + uv data
for i in range(vertices.size()): # apply vertices and uvs to SurfaceTool
st.add_uv(uvs[i])
st.add_vertex(vertices[i])
st.generate_normals() # generate normals
var mesh := st.commit() # setup mesh
var meshinst := MeshInstance.new() # create a mesh instance
meshinst.mesh = mesh # set mesh to mesh instance
add_child(meshinst) # add mesh instance to the spatial
func make_quad(position : Vector3, size : Vector2): # makes a simple quad at position with provided size
vertices.push_back(Vector3(position.x, 0, position.z + size.y))
vertices.push_back(Vector3(position.x, 0, position.z))
vertices.push_back(Vector3(position.x + size.x, 0, position.z))
vertices.push_back(Vector3(position.x, 0, position.z + size.y))
vertices.push_back(Vector3(position.x + size.x, 0, position.z))
vertices.push_back(Vector3(position.x + size.x, 0, position.z + size.y))
uvs.push_back(Vector2(0,0))
uvs.push_back(Vector2(0,1))
uvs.push_back(Vector2(1,1))
uvs.push_back(Vector2(0,0))
uvs.push_back(Vector2(1,1))
uvs.push_back(Vector2(1,0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment