- Home /
Create thumbnail of 3d model for Mobile platform
Hi All,
My scenario is as follows:
I am working on an app targeted for mobile platforms like Android. I host some 3d models as assets in a webserver. These models are fbx files. I present a grid view of the available assets in webserver to the user, where each grid item represents a 3d model as a thumbnail image.
I can download the assets at runtime using UnityWebRequest and create thumbnails using the following code:
Texture2D texture = null;
texture = AssetPreview.GetAssetPreview(myModel);
The problem is that this code works only inside Unity editor. As these APIs are not available for other platforms, the problem of creating thumbnails for 3d models looks hard to address.
On searching the forums, I found the following approach:
Create a texture out of the 3d model
Use the texture to display in the grid item
However, creating a texture out of 3d model looks tricky. I tried something on the lines of:
Renderer renderer = myModel.GetComponent<Renderer>();
if(renderer != null) {
Material material = renderer.material;
Debug.Log("material = " + material);
Texture texture = material.GetTexture("_MainTex");
}
Renderer is null for the top level component of the 3d model.
As the 3d model is composed of several child components, the material exists only for those child components. Iterating through the child components and creating texture out of them doesn't look right.
I would like to know:
Is there any easy approach to create thumbnail image out of 3d model
Is there any other way to solve this issue
Answer by Remy_Unity · Mar 29, 2018 at 10:24 AM
The method you mention will get you the texture currently used by the 3D model, not a thumbnail of your model.
To create a thumbnail you will have to render the model to a texture. The easiest way to do this is to add your model to the scene, use a camera to look at it, and render what the camera sees to a render texture : https://docs.unity3d.com/ScriptReference/RenderTexture.html
You can later on use this render texture on a quad or anything you want.
Also, I don't know if your app is meant to dowload a lot of models, but a better approach would be to generate the thumbnails offline and store them on the webserver alongside the fbx.
Doing this you can fetch all the thumbnails first, and only download the full models when you want to display them.
Thanks for the good suggestion. The idea of RenderTexture would definitely help for cases where the thumbnail has to be generated from a model rendered in a scene.
However, in my case, I thought that I would require the thumbnail to be generated without rendering it to a scene. As you had indicated, the better approach would be to generate the thumbnails externally and host them in webserver which in turn could be downloaded into the app. $$anonymous$$y initial idea was to avoid redundant network requests to fetch the thumbnail and the 3d model separately but after consideration, I agree that it is fine for my usecase. Thanks for your answer which would help someone in the future too.