Missing Mesh Filter

Michael ChristiansenMichael Christiansen asked 5 years ago

On page 5 of the Maker document, it says that new Curve Poly object contain both a Curve Poly Behavior,  Mesh Renderer, and Mesh Filter. I do not see the Filter in the generated object. When I try to add the filter, I get an error saying that the Filter already exists. Is the CurvePoly component a (extend) MeshFilter?

3 Answers
Best Answer
Alessandro MartinelliAlessandro Martinelli Staff answered 5 years ago

The Mesh Filter is there, but it’s hidden in the inspector, since version 1.1 of the Shape Editor and 1.0 of Maker.
It was not reported on the docs though (in version 1.0 of the Shape Editor it was still visible).
 
To make it clear, I report here the code hiding the Filter (this code is re-executed on Awake, on Start and everytime the model is modified by the editor or the lod is changed):
 

MeshFilter filter = gameObject.GetComponent<MeshFilter>();
if (filter == null)
{
      filter = gameObject.AddComponent<MeshFilter>();
}
filter.hideFlags |= HideFlags.HideAndDontSave | HideFlags.HideInInspector | HideFlags.DontSaveInBuild;

Unity HideFlags are documented here: https://docs.unity3d.com/ScriptReference/HideFlags.html
 
This was done because CurvedPoly keeps full control of it and therefore there is no reason to allow the user to manually interact with the filter from the inspector. You can still access it by code with a C# script if you need:

MeshFilter filter = gameObject.GetComponent<MeshFilter>();

Always by script, if it is the Mesh you are looking for you can retrieve it directly from the curvedPoly behaviour:

CurvedPoly curvedPoly = gameObject.GetComponent<CurvedPoly>();
Mesh mesh = curvedPoly.GetMesh();

One last thing: the mesh generated by curvedPoly is a virtual mesh. It is stored in memory and it’s destroyed every time the editor is closed; same is happening at runtime.  It’s never saved within the scene (HideFlags.HideAndDontSave) and no asset is generated in the project. It is always regenerated by curvedpoly: only the curved poly asset is saved and stored, this granting a significant reduction in the amount of data required to store the model in the app.
 

Michael ChristiansenMichael Christiansen answered 5 years ago

Thanks for the clarification. Another question I have is when I use “Remove Curved Poly” in the Mesh section of the component, I strips the Curve Poly component and makes the Mesh Filter visible (although the Mesh Filter is empty). However, if I make a prefab from this stripped game object, the mesh disappears. I guess this has something to do with the above. How do I retain the mesh generated by a CP object? To be honest, I am not entirely clear on how Unity stores and manages mesh assets in all cases. 

Alessandro MartinelliAlessandro Martinelli Staff replied 5 years ago

To store the mesh Unity needs to generate a Mesh Asset. Usually Mesh Assets are imported from external sources like obj, fbx or blender files.

I think, indeed, that the only way to generate a mesh asset is to export the mesh using one of the supported mesh file extensions, then unity will generate a mesh asset from the file.

I don’t know if there is another way: by scripts, Assets are generated and managed with the Editor Asset Database, https://docs.unity3d.com/ScriptReference/AssetDatabase.html, but the Mesh type is a special type in Unity and I don’t know if the Asset Database gives a complete support to store meshes.

So, there are a set of free tools on the asset store you can use save the mesh as a file, probably the best to use in combination with Curved Poly is ProBuilder.

Here some links.

ProBuilder
https://unity3d.com/unity/features/worldbuilding/probuilder
https://docs.unity3d.com/Packages/com.unity.probuilder@4.0/manual/workflow-exporting.html
https://assetstore.unity.com/packages/tools/modeling/probuilder-2-x-111418

Other Asset on the Store
https://assetstore.unity.com/packages/essentials/fbx-exporter-101408
https://assetstore.unity.com/packages/tools/utilities/scene-obj-exporter-22250
https://assetstore.unity.com/packages/tools/utilities/masa-life-s-obj-exporter-26608

Michael ChristiansenMichael Christiansen answered 5 years ago

Back to the MeshFilter. I am using a second plugin called Deform to manipulate the mesh generated by PC. Deform works very well at deforming the mesh in various way dynamically. 
Deform gets its mesh from a renderer or skinned renderer. The funny thing is the two work perfectly in edit mode. But in play mode, Deform can no longer see the renderer.
Is there a way to access the PC renderer from the API. Or someway to disable making invisible? 
Thanks
Mike

Michael ChristiansenMichael Christiansen replied 5 years ago

Never mind. I see in you message above where you give an example of accessing the renderer. Thanks.

Alessandro MartinelliAlessandro Martinelli Staff replied 5 years ago

You say ‘Renderer’, I assume you’re talking about the MeshFilter (Curved Poly doesn’t interact with the Mesh Renderer – the only exception is the Geometry Operator which access the Renderer to change materials)

I guess deform needs a reference to the mesh filter to get the mesh. The Mesh Filter on Curved Poly is not stored when you go in play mode (because of HideFlags.DontSaveInBuild), and it’s regenerated at runtime (with the code above).

So i think (i don’t know the plugin, so i’m not sure) you can retrieve what you need at runtime and assign it to Deform with a dedicated script of few lines of code . At runtime CurvedPoly will have its new MeshFIlter instance regenerated on Awake (https://docs.unity3d.com/ScriptReference/MonoBehaviour.Awake.html), after that it will not change any more and you can retrieve it safely.

Michael ChristiansenMichael Christiansen replied 5 years ago

Yes I meant filter. In case you are wondering, I can turn off the deform component by default and turn on at Start, and the component successfully deforms the mesh generated by PC. Deform can generate interesting dynamic shapes which is what I’m looking for. https://assetstore.unity.com/packages/tools/modeling/deform-148425 but there are several other plugins with similar functionality in the asset store.