Search Unity

Runtime Preview (Thumbnail) Generator (Open Source)

Discussion in 'Assets and Asset Store' started by yasirkula, Oct 14, 2017.

  1. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Hello there,

    I've just posted a script that lets you generate thumbnails for your GameObject's and/or materials during gameplay and wanted to share it here, as well. The generated thumbnail is in Texture2D format and supports transparent background.

    Asset Store: https://assetstore.unity.com/packages/tools/camera/runtime-preview-generator-112860
    Also available at: https://github.com/yasirkula/UnityRuntimePreviewGenerator
    Discord: https://discord.gg/UJJt549AaV
    GitHub Sponsors ☕

    Should work on pretty much any platform that Unity supports.

    example1.png example2.png

    FAQ
    • Rendered thumbnail is empty, why?
    If shouldIgnoreParticleSystems parameter is set to false, child particle systems of the model may force the camera to zoom out a lot. Or, on SRPs, your Forward Renderer asset's Opaque Layer Mask and/or Transparent Layer Mask might be excluding layer 22 (preview objects are temporarily assigned to this layer).
    • Rendered thumbnail doesn't look correct, why?
    If there are lighting issues and you're generating thumbnails in Awake, try Start instead. Otherwise, try disabling post-processing effects and see if it makes any difference. If it does, then you should temporarily disable post-processing while capturing thumbnails.

    Enjoy!
     
    Last edited: May 31, 2023
    elmerirusi, r137, Fressbrett and 7 others like this.
  2. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Now available on Asset Store!
     
  3. Darkirby3

    Darkirby3

    Joined:
    Apr 24, 2017
    Posts:
    2
    This is perfect ! You saved me a big headache. This is exactly what I was searching for my inventory system.

    If you are interested I would like to offer you a special account into my video game. You can find my game at www.citywars.ca. We are still far away from having a playable game but piece by piece we get further from a prototype. Your system is now part of our inventory system. :D
     
    yasirkula likes this.
  4. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    It's nice to hear :) Thank you for your offer. I don't have free time for games nowadays but thanks again for your kindness, I appreciate it.
     
  5. Darkirby3

    Darkirby3

    Joined:
    Apr 24, 2017
    Posts:
    2
    Yeah i understand. Well Thanks again. If you need anything lets us know !

    Also if you change your mind latter you're still welcome ;P
    Have a nice day !
     
  6. Play_Edu

    Play_Edu

    Joined:
    Jun 10, 2012
    Posts:
    722
  7. tapawafo

    tapawafo

    Joined:
    Jul 25, 2016
    Posts:
    170
    Hello! First of all, thank you so much for sharing this asset, it's wonderful and exactly what I needed.

    I am having an issue however - has anybody figured the issue with this asset and Unity 2018? After upgrading, I recall seeing an error or warning about it, and since seeing that it's stopped working - annoyingly, there has been no error/warning since then, (I really wish I addressed it immediately - my next step is to make a new project and import to see if I can catch the error a second time) so I'm having trouble troubleshooting the issue.

    I tried finding an upgrade guide for 2018 with changes/deprecation but couldn't - if anyone could help I'd be very thankful! And I'll post here if I find the solution myself of course.
     
  8. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I was able to test the following code with success on a new scene with no post-processing applied on Unity 2018.1.3f1:

    Code (CSharp):
    1. public RawImage img;
    2. public Transform obj;
    3. private void Start()
    4. {
    5.     img.texture = RuntimePreviewGenerator.GenerateModelPreview( obj );
    6. }
    There were no error or warning messages for me while importing the plugin.
     
    tapawafo likes this.
  9. tapawafo

    tapawafo

    Joined:
    Jul 25, 2016
    Posts:
    170
    Thanks for the help. I found that it actually no longer occurs on 2018.2.0f1. We've moved builds a few times in the last few weeks, so it must have been some temporary issue that resolved itself - works just fine after testing again (should've done that before posting. My apologies!)
     
    yasirkula likes this.
  10. mathias_unity633

    mathias_unity633

    Joined:
    Jul 17, 2018
    Posts:
    35
    Any idea about soft shadows but transparent backgrounds? My best solution so far is just having a fake shadow, a sprite, that is resized to the bounding box of the object you're rendering, and repositioned to right below the bounding box. You can probably do it with 'real' shadows if you use some special shader on a plane below the object (?)
     
  11. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Yes you could use such a special shader (this could be a good starting point). However, as an object's shadow may overflow its bounding box, parts of the shadow may not be visible in the generated thumbnail.
     
  12. mathias_unity633

    mathias_unity633

    Joined:
    Jul 17, 2018
    Posts:
    35
    Yeah, depends on which direction you cast the shadow. I think a way to go around that issue would be to feather the thumbnail near the edges :D
     
  13. dcam_unity

    dcam_unity

    Joined:
    Dec 1, 2018
    Posts:
    1
    Hi there, I just had a question regarding this thumbnail generator. Right now I have a video player, and I save the texture of the video player to a RawImage. I want to generate a thumbnail based on the texture of the RawImage, but every time I try calling RuntimePreviewGenerator.GenerateModelPreview I get a null object.

    Code (CSharp):
    1. image.texture = videoPlayer.texture;
    2. thumbnail = RuntimePreviewGenerator.GenerateModelPreview(image.transform, 64, 64, true);
    3.  
    Am I calling runtime preview generator right or am I missing something?
     
  14. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    RuntimePreviewGenerator can only work on objects with Renderer components. For a thumbnail of an existing texture, you may want to use the TextureOps.Scale function here: https://github.com/yasirkula/UnityTextureOps
     
  15. Firlefanz73

    Firlefanz73

    Joined:
    Apr 2, 2015
    Posts:
    1,316
    Hello,

    thanks a lot for this asset :)

    Is there a way to get the grey background completly transparent (Alpha)?

    Thanks!
     
  16. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    RuntimePreviewGenerator.TransparentBackground = true;
    should do it.
     
    Firlefanz73 likes this.
  17. klhurley

    klhurley

    Joined:
    Dec 11, 2014
    Posts:
    2
    yasirkula likes this.
  18. Firlefanz73

    Firlefanz73

    Joined:
    Apr 2, 2015
    Posts:
    1,316
    Perfect! Thanks :)
     
  19. iddqd

    iddqd

    Joined:
    Apr 14, 2012
    Posts:
    501
    Hi, this tool is great and I see you have a ton of other clever content available. Thank you for that!

    I do have a question though, do you think there's a way for GenerateMaterialPreview to ignore the Ambient Lighting so it always looks the same, similar to the Unity Inspector Material Preview?

    Thanks
     
  20. iddqd

    iddqd

    Joined:
    Apr 14, 2012
    Posts:
    501
    Ok so I seem to be successful by changing the ambient mode and then changing it back again:

    Code (CSharp):
    1.  
    2. RenderSettings.ambientMode = UnityEngine.Rendering.AmbientMode.Flat;
    3. float oldIntensity = RenderSettings.ambientIntensity;
    4. RenderSettings.ambientIntensity = 1f;
    5.  
    6. sourceTexture = RuntimePreviewGenerator.GenerateMaterialPreview(prefab, PrimitiveType.Quad, 512, 512);
    7.  
    8. RenderSettings.ambientMode = UnityEngine.Rendering.AmbientMode.Skybox;
    9. RenderSettings.ambientIntensity = oldIntensity;
    10.  
    I've also added a Layer to the GameObject, so I can exclude it from the Directional Light.

    Perfect I would say.
    Thanks!
     
    yasirkula likes this.
  21. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I was about to suggest the same, good thinking!
     
    Crossway and iddqd like this.
  22. Crossway

    Crossway

    Joined:
    May 24, 2016
    Posts:
    507
    Can you please post an example code? I read the readme file but still can't make it to work.
     
  23. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    This code generates a thumbnail for the Target object and stores it inside the Result texture:

    Code (CSharp):
    1. public Transform target;
    2. public Texture2D result;
    3. public int thumbnailSize = 128;
    4. void Start()
    5. {
    6.     RuntimePreviewGenerator.BackgroundColor = new Color( 0, 0, 0, 0 );
    7.     result = RuntimePreviewGenerator.GenerateModelPreview( target, thumbnailSize, thumbnailSize );
    8. }
     
    Last edited: Sep 21, 2019
    Crossway likes this.
  24. Crossway

    Crossway

    Joined:
    May 24, 2016
    Posts:
    507
    Thanks a lot it works.
    Just when I put "RuntimePreviewGenerator.TransparentBackground = true;" line to get transparent background I get error of does not contain a definition for 'TransparentBackground'
     
  25. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    With the latest update, transparency now comes from the alpha value of BackgroundColor.
     
    Crossway likes this.
  26. Crossway

    Crossway

    Joined:
    May 24, 2016
    Posts:
    507
    S*** I'm too noob in coding :D Can you please edit previous sample you just post and add this line too? :oops:
     
  27. Crossway

    Crossway

    Joined:
    May 24, 2016
    Posts:
    507
    Why it stores the result as an Object.texture2D? And not just a texture variable?
    Inside PlayMaker there is no way to get and store a texture2D from an Object.
     
  28. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Texture2D can be stored inside a Texture variable. I think I couldn't understand the question fully.
     
    Crossway likes this.
  29. Crossway

    Crossway

    Joined:
    May 24, 2016
    Posts:
    507
    Can you please create a PlayMaker Action for this for people using PlayMaker? There is no action inside PLAY Maker to store a Texture2D inside a texture variable.
     
  30. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Will see what I can do.
     
    Crossway likes this.
  31. Crossway

    Crossway

    Joined:
    May 24, 2016
    Posts:
    507
    Or please add the C# code that you're using to store Texture2D inside a Texture variable. for people like me who doesn't know much about C# it's hard to use this cool asset.
     
  32. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Texture2D class derives from Texture. So the following C# code is valid:

    Code (CSharp):
    1. Texture2D myTexture2DObject; // Texture2D variable
    2. Texture texture = myTexture2DObject; // Can be assigned to Texture variable directly
     
    Crossway likes this.
  33. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @Crossway I've created PlayMaker actions for you.
     

    Attached Files:

    Crossway likes this.
  34. Crossway

    Crossway

    Joined:
    May 24, 2016
    Posts:
    507
    Thank you million times :) Very useful.
     
    yasirkula likes this.
  35. Crossway

    Crossway

    Joined:
    May 24, 2016
    Posts:
    507
    This is the best free asset I have ever seen :) but today after I build my game I mentioned icons are too dark after game build! It looks good and normal inside Unity editor but too dark after game build (almost black) Can you please check this?
     
  36. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    If the icons are created in Awake, try using Start instead. Or, try adding a 1-frame delay before the icons are generated. I've also encountered dark images when creating icons immediately after the scene is loaded. This has to be something with lights not being fully initialized at first frame.
     
  37. abhi3188

    abhi3188

    Joined:
    Dec 13, 2015
    Posts:
    31
    Thanks for the great asset! I have it all working but my model thumbnails are pretty dark. I'm not calling the icon generation on scene load, I feel it has something to do with lights. Any ideas on how I could ensure even lighting or ideally get a flat unshaded thumbnail?
     
  38. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    If you are generating the thumbnails in Awake or Start, try waiting for at least 1 frame in a coroutine via
    yield return null;
    . Also, you can call the GenerateModelPreviewWithShader function with
    Shader.Find("Unlit/Texture")
    shader and
    string.Empty
    replacementTag to use unlit shader for the thumbnail.
     
  39. abhi3188

    abhi3188

    Joined:
    Dec 13, 2015
    Posts:
    31
    Thanks for the reply. Replacing the shader does work but I realized I do need some slight shadows to show perspective. Is there any way to attach a temporary directional light to the preview render camera so I can have a light pointing directly at the object when the thumbnail is captured?

    Currently I'm readjusting the scene light to a rotation I've manually determined to light up the models nicely and then revert it back. ideally I want to programmatically determine the rotation for the directional light instead of setting it manually.


    Code (CSharp):
    1. directionalLight.rotation = Quaternion.Euler(50f, -140f, 0); // set light to point to object for proper illumination
    2.         thumbnail = RuntimePreviewGenerator.GenerateModelPreview(modelToCapture);
    3.         directionalLight.rotation = originalRotation; //reset light
     
    Last edited: Jun 22, 2020
  40. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I can see 3 alternatives here:

    1. With your current approach, simply call
    directionalLight.rotation = Quaternion.LookRotation(RuntimePreviewGenerator.PreviewDirection);

    2. Use an unlit shader that supports shadows (you should be able to find such shaders on wiki/forums/answers)
    3. You can add a Light component to RuntimePreviewGenerator's camera here and set its culling mask accordingly. But this light could have a performance impact even though it has a culling mask, I can't say for sure
     
    abhi3188 likes this.
  41. PatrickKa

    PatrickKa

    Joined:
    Apr 30, 2014
    Posts:
    245
    This asset is absolutely stunning. Thank you for that!

    I was wondering if you know why sometimes theres still a bit of space at the edges? Do you think theres a way to improve on that without making use of the padding option (and potentially cutting away parts of the image)?

    It's not really that big of a deal though it makes my OCD kick in especially because one edge might have more space than the other haha
     
  42. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Enabling OrthographicMode can help with this issue. Other than that, I'm calculating the bounds of the input object and trying to position the camera correctly but I might be missing something. If you can spot the issue, I'd be happy to resolve it.
     
  43. grizzlycorv

    grizzlycorv

    Joined:
    Nov 9, 2018
    Posts:
    19
    Absolutely one of my favorite assets, I already use it in several places.

    Just one thing: Is there any way to make thumbnails of meshes that are generated at runtime? Like passing the transform of a mesh-renderer?
     
  44. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    You can't pass a Mesh to the plugin, so you should have a dummy GameObject, change its MeshFilter with the procedural mesh and pass that GameObject to the plugin.
     
  45. Quentin_Arragon

    Quentin_Arragon

    Joined:
    Aug 30, 2015
    Posts:
    13
    Hello,
    Thank you for this plugin ! I used it in multiples projects, it is very useful !

    But currently the generated texture contains only the background color, would you be aware of a situation like that ?
    The plugin worked without problem before, and I must have changed something in my project that cause a problem but I don't know what

    When I uncomment the #define DEBUG_BOUNDS line I still have nothing on texture, except the background color
     
  46. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Are you using a Scriptable Render Pipeline? Are you trying to generate thumbnail for a Canvas object or a static object? If the target object is a procedural mesh, do you call its RecalculateBounds function?
     
  47. Quentin_Arragon

    Quentin_Arragon

    Joined:
    Aug 30, 2015
    Posts:
    13
    I use the Universal Render Pipeline in Unity 2019.4.15f1

    I generate the thumbnails in the editor when the application is not playing, but the result is the same when the application is playing

    The object is not static, and it is not a canvas
     
  48. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Hmm, was it working fine with URP before? If so, can you guess which options you've changed might've caused this issue? I want to pinpoint the issue as much as possible.
     
  49. Quentin_Arragon

    Quentin_Arragon

    Joined:
    Aug 30, 2015
    Posts:
    13
    Oh sorry for the inconvenience, it is working now
    I created a new ForwardRenderer asset and assigned it to the URP asset, and now the objet and the bounds debug appears on the thumbnail

    In the past I added a feature to the ForwardRenderer asset that came from a plugin, then deleted the plugin without removing the feature from the ForwardRenderer, and I think that prevented the thumbnail generator to work properly somehow
     
    yasirkula likes this.
  50. pandron

    pandron

    Joined:
    Mar 13, 2019
    Posts:
    3
    I just found this asset and it solved a big problem for me! It's great!

    I am having an issue though where the generated thumbnails have a transparent background when I run it in the editor (as intended), but when I run the game on an Android device the background of the thumbnails is opaque. The color of the background is correct, it just doesn't appear to honor the alpha channel when run on the device.

    I am applying the thumbnail texture to a RawImage.

    EDIT: I also tested on iOS, and the background was transparent as intended. It just doesn't work on Android.

    Any ideas why this may be?
     
    Last edited: Dec 12, 2020