Skip to main content
Typo & added details
Source Link
DMGregory
  • 136.3k
  • 22
  • 247
  • 373

As I'd mentioned in the comments earlier, a Sprite in Unity isn't really a "type" of texture. It's a slice of texture data.

The "Sprite (2D and UI)" import mode you see is a bit of a shortcut to both import the Texture2D texture with some default settings suitable for sprite use AND create one or more Sprite objects sliced from it, as one bundled asset (hence the expansion arrow in the Project folder).

So, we just need to figure out which of those "Sprite (2D and UI)" default settings is different from what you're getting when you load your texture by code.

tex = new Texture2D(2, 2);

This creates a texture with mipmaps by default. When you import a Sprite into Unity, it defaults to not including mipmaps. So, this is a likely source of the difference you're seeing.

Without mipmaps, when we draw the texture at less than native size, we'll tend to see aliasing artifacts - edges will look sharper than normal, and in high detail areas we might get noisy speckling. Mipmaps solve this by blurring together adjacent pixels of the texture when rendering at reduced size, but for your case that doesn't seem to be the effect you want. In your second image, slight mipmap aliasing could be what's giving you sharper detail and more speckling in the hair.

So, try loading your image without mipmaps:

public static Texture2D LoadPNG(string filePath, bool generateMipMaps = false)
{

// ... 

    tex = new Texture2D(2, 2, TextureFormat.RGBA32, generateMipsgenerateMipMaps);

Does that make the dynamically loaded texture behave more like your Sprite asset?

Just beware that turning off mipmaps can produce some very distracting artifacts if you're scaling the image down significantly, or if it's going to move or be displayed in perspective. You might instead want to adjust your image filtering code to give it a sharper response even when working on mipmap filtered input.

As I'd mentioned in the comments earlier, a Sprite in Unity isn't really a "type" of texture. It's a slice of texture data.

The "Sprite (2D and UI)" import mode you see is a bit of a shortcut to both import the Texture2D texture with some default settings suitable for sprite use AND create one or more Sprite objects sliced from it, as one bundled asset (hence the expansion arrow in the Project folder).

So, we just need to figure out which of those "Sprite (2D and UI)" default settings is different from what you're getting when you load your texture by code.

tex = new Texture2D(2, 2);

This creates a texture with mipmaps by default. When you import a Sprite into Unity, it defaults to not including mipmaps. So, this is a likely source of the difference you're seeing.

Without mipmaps, when we draw the texture at less than native size, we'll tend to see aliasing artifacts - edges will look sharper than normal, and in high detail areas we might get noisy speckling. Mipmaps solve this by blurring together adjacent pixels of the texture when rendering at reduced size, but for your case that doesn't seem to be the effect you want.

So, try loading your image without mipmaps:

public static Texture2D LoadPNG(string filePath, bool generateMipMaps = false)
{

// ... 

    tex = new Texture2D(2, 2, TextureFormat.RGBA32, generateMips);

Does that make the dynamically loaded texture behave more like your Sprite asset?

As I'd mentioned in the comments earlier, a Sprite in Unity isn't really a "type" of texture. It's a slice of texture data.

The "Sprite (2D and UI)" import mode you see is a bit of a shortcut to both import the Texture2D texture with some default settings suitable for sprite use AND create one or more Sprite objects sliced from it, as one bundled asset (hence the expansion arrow in the Project folder).

So, we just need to figure out which of those "Sprite (2D and UI)" default settings is different from what you're getting when you load your texture by code.

tex = new Texture2D(2, 2);

This creates a texture with mipmaps by default. When you import a Sprite into Unity, it defaults to not including mipmaps. So, this is a likely source of the difference you're seeing.

Without mipmaps, when we draw the texture at less than native size, we'll tend to see aliasing artifacts - edges will look sharper than normal, and in high detail areas we might get noisy speckling. Mipmaps solve this by blurring together adjacent pixels of the texture when rendering at reduced size, but for your case that doesn't seem to be the effect you want. In your second image, slight mipmap aliasing could be what's giving you sharper detail and more speckling in the hair.

So, try loading your image without mipmaps:

public static Texture2D LoadPNG(string filePath, bool generateMipMaps = false)
{

// ... 

    tex = new Texture2D(2, 2, TextureFormat.RGBA32, generateMipMaps);

Does that make the dynamically loaded texture behave more like your Sprite asset?

Just beware that turning off mipmaps can produce some very distracting artifacts if you're scaling the image down significantly, or if it's going to move or be displayed in perspective. You might instead want to adjust your image filtering code to give it a sharper response even when working on mipmap filtered input.

Source Link
DMGregory
  • 136.3k
  • 22
  • 247
  • 373

As I'd mentioned in the comments earlier, a Sprite in Unity isn't really a "type" of texture. It's a slice of texture data.

The "Sprite (2D and UI)" import mode you see is a bit of a shortcut to both import the Texture2D texture with some default settings suitable for sprite use AND create one or more Sprite objects sliced from it, as one bundled asset (hence the expansion arrow in the Project folder).

So, we just need to figure out which of those "Sprite (2D and UI)" default settings is different from what you're getting when you load your texture by code.

tex = new Texture2D(2, 2);

This creates a texture with mipmaps by default. When you import a Sprite into Unity, it defaults to not including mipmaps. So, this is a likely source of the difference you're seeing.

Without mipmaps, when we draw the texture at less than native size, we'll tend to see aliasing artifacts - edges will look sharper than normal, and in high detail areas we might get noisy speckling. Mipmaps solve this by blurring together adjacent pixels of the texture when rendering at reduced size, but for your case that doesn't seem to be the effect you want.

So, try loading your image without mipmaps:

public static Texture2D LoadPNG(string filePath, bool generateMipMaps = false)
{

// ... 

    tex = new Texture2D(2, 2, TextureFormat.RGBA32, generateMips);

Does that make the dynamically loaded texture behave more like your Sprite asset?