Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CSGMesh not working with procedural ArrayMesh #23364

Closed
wants to merge 1 commit into from
Closed

Fix CSGMesh not working with procedural ArrayMesh #23364

wants to merge 1 commit into from

Conversation

ghost
Copy link

@ghost ghost commented Oct 28, 2018

I was trying to make some procedural CSG geometry using a CSGMesh and the triangles were not visible at all. To test, I made an empty scene with a camera at the origin (translation [0,0,0], rotation [0,0,0]), and a spatial node with the following script:

extends Spatial

func _ready():
	var vertices = PoolVector3Array()
	
	var mesh = ArrayMesh.new()
	var arrays = []
	arrays.resize(ArrayMesh.ARRAY_MAX)
	vertices.push_back(Vector3(0,1,-2))
	vertices.push_back(Vector3(1,1,-2))
	vertices.push_back(Vector3(1,0,-2))
	arrays[ArrayMesh.ARRAY_VERTEX] = vertices
	
	mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arrays)
	
	var body = CSGMesh.new()
	body.mesh = mesh
	add_child(body)

This should show a single triangle, but currently it doesn't. After some inspection, it looks like CSGMesh::_build_brush() had some typos with the input vertex and output vertex count switched around, and was not merging the vertices correctly. With my changes, the above code runs correctly and shows a triangle.

@akien-mga akien-mga added this to the 3.1 milestone Oct 29, 2018
@NewNodeGames
Copy link

NewNodeGames commented Oct 29, 2018

Seems to work well with the example script but now when I add a CSGMesh node and select a shape nothing appears (GLES2)

servers/visual_server.cpp:969 - Condition ' array_len == 0 ' is true.
scene/resources/mesh.cpp:796 - Condition ' len == 0 ' is true.
drivers/gles2/rasterizer_storage_gles2.cpp:2004 - Index p_surface=0 out of size (mesh->surfaces.size()=0)
modules/csg/csg_shape.cpp:171 - Condition ' !n ' is true.
servers/visual_server.cpp:969 - Condition ' array_len == 0 ' is true.
scene/resources/mesh.cpp:796 - Condition ' len == 0 ' is true.
drivers/gles2/rasterizer_storage_gles2.cpp:2004 - Index p_surface=0 out of size (mesh->surfaces.size()=0)

Related: 7aea850#commitcomment-31031330

@Zireael07
Copy link
Contributor

I'm not sure CSG works with GLES2...

@NewNodeGames
Copy link

I'm not sure CSG works with GLES2...

It works but have some performance problems and errors with CSGPolygon and CSGMesh.

@ghost
Copy link
Author

ghost commented Oct 29, 2018

I was able to see it with GLES2. This patch does not touch the renderer.

@ghost
Copy link
Author

ghost commented Oct 29, 2018

This should be tested with an index array before merging, I will be able to try that out later this evening.

@ghost
Copy link
Author

ghost commented Oct 30, 2018

Tested again with the following script, it seems to work as expected:

extends Spatial

func _ready():
	var vertices = PoolVector3Array()
	var indices = PoolIntArray()
	
	var mesh = ArrayMesh.new()
	var arrays = []
	arrays.resize(ArrayMesh.ARRAY_MAX)
	vertices.push_back(Vector3(0,1,-2))
	vertices.push_back(Vector3(1,1,-2))
	vertices.push_back(Vector3(1,0,-2))
	indices.push_back(0)
	indices.push_back(1)
	indices.push_back(2)
	arrays[ArrayMesh.ARRAY_VERTEX] = vertices
	arrays[ArrayMesh.ARRAY_INDEX] = indices
	
	mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arrays)
	
	var body = CSGMesh.new()
	body.mesh = mesh
	add_child(body)

@ghost
Copy link
Author

ghost commented Oct 30, 2018

@NewNodeGames The shape not updating in the editor appears to be a separate editor bug. I noticed if you change to CubeMesh/SphereMesh/etc and no mesh is visible, you can get it to show up by changing another property on the CSGMesh. I may get some time to look more into that tomorrow.

@NewNodeGames
Copy link

Tested again with the following script, it seems to work as expected:

extends Spatial

func _ready():
	var vertices = PoolVector3Array()
	var indices = PoolIntArray()
	
	var mesh = ArrayMesh.new()
	var arrays = []
	arrays.resize(ArrayMesh.ARRAY_MAX)
	vertices.push_back(Vector3(0,1,-2))
	vertices.push_back(Vector3(1,1,-2))
	vertices.push_back(Vector3(1,0,-2))
	indices.push_back(0)
	indices.push_back(1)
	indices.push_back(2)
	arrays[ArrayMesh.ARRAY_VERTEX] = vertices
	arrays[ArrayMesh.ARRAY_INDEX] = indices
	
	mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arrays)
	
	var body = CSGMesh.new()
	body.mesh = mesh
	add_child(body)

With this script appears the triangle without apply your patch and csg mesh node shows the primitives again.

@reduz
Copy link
Member

reduz commented Nov 1, 2018

I did a fix in a bit less intrusive way, but thanks for finding out about this

@reduz reduz closed this in b9dd095 Nov 1, 2018
@akien-mga
Copy link
Member

Those variables could really use more explicit names... :)

@ghost ghost deleted the csgmesh-arraymesh-fix branch November 1, 2018 18:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants