Hi,

First I'm new to godot, I have a Unity background.

I'm using Immediate Geometry to generate procedural mesh geometry on the fly and I would like to know how to display it in the editor viewport when playing the game. Adding the C# [tool] attribute is actually not working as expected, the geometry stays after quitting the game. Also, it looks hard to manage with logs...it seems close to [Execute in Editor] from Unity to me.

Moreover how can I turn on wireframe on this generated geometry ?

Thank you in advance !

@panicq said: I'm using Immediate Geometry to generate procedural mesh geometry on the fly and I would like to know how to display it in the editor viewport when playing the game. Adding the C# [tool] attribute is actually not working as expected, the geometry stays after quitting the game. Also, it looks hard to manage with logs...it seems close to [Execute in Editor] from Unity to me.

Unfortunately, that is really the only way I know of to run scripts in the Godot editor. I think I remember reading you can manually tell a script to run, but I cannot find the option now.

I will admit, for some tasks using tool is not very elegant. It is a fairly powerful system to use and most everything is doable with tool, but it can take quite a bit of boiler plate code to get things working in an expected manner.

You can make things a little easier in the log by checking Engine.editor_hint to see if the script is running in the Godot editor or not, and then printing to the log based on whether it is in the editor.

Moreover how can I turn on wireframe on this generated geometry ?

You can turn a wireframe in the Godot editor by clicking the perspective dropdown and selecting Display Wireframe. This will make the entire scene wireframe though. I'm fairly certain there isn't a way to make just a single node display a wireframe in Godot right now, though I could be wrong.

Thanks for your answer but I would like a bit more help on this one. I've created a simple test script but nothing is showing up in the editor :/ How can I do this in a clean way, meaning displaying the mesh and deleting it when closing the game ?

	using Godot;
	using System;
	
	[Tool]
	public class MeshGenerator : MeshInstance
	{
		SurfaceTool st;
	
		public override void _Ready()
		{
			if(Engine.EditorHint)
	        {
	            GD.Print("Editor on");
	            GenerateMesh();
			}
		}
	
		public override void _Notification(int what)
		{
			base._Notification(what);
			if(what == MainLoop.NotificationWmQuitRequest)
			{
				GD.Print("END");
				Mesh = null;
			}
		}
	
		private void GenerateMesh()
		{
			st = new SurfaceTool();
	
			st.Begin(Mesh.PrimitiveType.Triangles);
	
			st.AddVertex(new Vector3(-1, 0, -1));
			st.AddVertex(new Vector3(1, 0, -1));
			st.AddVertex(new Vector3(0, 0, 1));
	
			st.GenerateNormals();
	
			Mesh mesh = st.Commit();
	
			Mesh = mesh;
		}
	}
4 months later

Thanks for your answer :) Since last time I've been exploring a few things. @Zylann I've been looking at your plugin, this is quite interesting thanks :) Do you have any tips on managing the generation of dense meshes in Godot without implementing a module ? For now I've been using ArrayMesh and generating colliders using CreateTrimeshCollision (for simple marching cubes) but I always have FPS drops ... Moreover, i'm using OpenSimplexNoise, does it cost a lot performance wise ? In Godot (using C# or Cpp) it's really hard to profile as there is no profiler so it's a bit of guess...

Thanks in advance :)

ArrayMesh and trimesh collision is pretty much what I'm doing too. Collision can get a speed up if you generate it before producing an ArrayMesh: https://github.com/Zylann/godot_voxel/issues/54 In C# you'd probably need to pool your data structures to avoid garbage collection. Basically, during the game, new should never happen more than once (even implicit new called from inside C# libs).

Also, you don't need to "delete" the mesh at the end of the game. Meshes inherit Resource, and Resource inherit Reference, so it will be automatically freed as soon as no other objects use it.

OpenSimplexNoise is relatively slow, especially in 3D with multiple octaves. You can speed it up depending on what you use it for. If it's voxels, you can look at the first octave and check if it's too high or too low so you can skip all other octaves since they won't have a high enough effect to affect the result. Another way is verify if a box is empty by looking at the corner values and check if the value can possibly ramp fast enough to become visible within that box (using derivatives). If it's a heightmap, you can switch to shaders (and use a GLSL implementation of noise) to generate your terrain and grab the data from GPU using a Viewport. You can probably do that too for voxels but in Godot 3 it's tricky due to the absence of a 3D image data type, it would be better with compute in Godot 4.

I've created a simple test script but nothing is showing up in the editor

Maybe you don't see the triange because of backface culling? Turn it around, maybe you'll see it

@Zylann Thank you for your answer ".. grab the data from GPU using a Viewport" -> could you elaborate a bit more ? Are you saying for getting back info for colliders, if so how is this doable without compute shaders ?

Yes compute shaders are really lacking in Godot 3.

Also I'm curious about your profiling workflow, what tools are you using to monitor the calls ? And finally am I missing a way to display the generated geometry in the viewport of the editor (like unity would do) ?

Thanks again :)

".. grab the data from GPU using a Viewport" -> could you elaborate a bit more ? Are you saying for getting back info for colliders, if so how is this doable without compute shaders ?

I'm not saying Viewports can give you colliders, but if you need to generate a heightmap (which is basically an image), then rendering a quad and screenshotting the result using an invisible Viewport node is a pretty fast alternative.

Also I'm curious about your profiling workflow, what tools are you using to monitor the calls ?

I use a profiler I made myself, which is able to provide temporal information. Its WIP code can be found here: https://github.com/Zylann/godot_voxel/tree/profiler/profiler

And finally am I missing a way to display the generated geometry in the viewport of the editor (like unity would do) ?

[Tool] it is. Which is really the same as [ExecuteInEditor].

I'm not saying Viewports can give you colliders, but if you need to generate a heightmap (which is basically an image), then rendering a quad and screenshotting the result using an invisible Viewport node is a pretty fast alternative.

This is what I thought, but at this point it's better to use a seamless noise texture and to store this data in a flat array cpu side :D

I use a profiler I made myself, which is able to provide temporal information. Its WIP code can be found here: https://github.com/Zylann/godot_voxel/tree/profiler/profiler

Nice ! Is this working using C#?

[Tool] it is. Which is really the same as [ExecuteInEditor].

This is kind of not manageable (if I understood well) as everything is then being executed in editor. I would like to be able to see my mesh in the editor when I start the scene, when the mesh appears. In Unity when you generate a mesh procedurally you see it in the viewport, as everything else btw...

Thanks :)

Nice ! Is this working using C#?

It was originally a C++ one but recent changes I made may allow to bind it to the script API, so yes C# would be able to use it. I'm just not sure how performant the API is when StringName is expected, when in C# there is only System.String. If that means allocating a new string everytime then profiling might have some overhead"

This is kind of not manageable (if I understood well) as everything is then being executed in editor. I would like to be able to see my mesh in the editor when I start the scene, when the mesh appears. In Unity when you generate a mesh procedurally you see it in the viewport, as everything else btw...

Godot is designed a lot differently. In Unity, your game does run inside of the editor's process, with all the niceties it might have, but also all the dangers of freezing or crashing the editor. In Godot the game runs in another process, so not only crashes and freezes are safe to the editor, it also allows to debug without suspending it (although for now, it doesnt mean much for C#), or even spawn multiple instances of the game (which might be planned). So yeah, if you want to see your mesh, either you see it in game, or you make it run in editor.

Also, not everything has to run in the editor. You can use the editor hint to see if the code is running in editor or in game and if in editor only run a code path that you want and only run the update function when you want it to run.

2 years later