Jump to content

Image mipmaps copy


photo

Recommended Posts

Hi! I'm trying to create texture atlas - so, I need to copy 4 images into one of higher resolution. How can I copy mipmaps in the same way? There is no 'level' argument in copy function. Generate new mipmaps for the atlas image is not a good idea because of possible wrong filtering (for the heightmaps mostly)

Link to comment

Hi,

The answer will depend on what your are actually want to achieve.
If you want to copy mip maps from one texture to another's mip maps you want to use bindColorTexture2D since it provides a way to specify to which mip level you want to write.
Something along the lines like so:

void copyMipLevel(TexturePtr src, TexturePtr dest, int mip_src, int mip_dest)
{
	MaterialPtr copy_material = Materials::findManualMaterial("copy_texture_mip");
	if (!copy_material)
		return;

	RenderTargetPtr render_target = Render::getTemporaryRenderTarget();
	render_target->bindColorTexture2D(0, dest, mip_dest);
	render_target->enable();
	{
		copy_material->setParameterFloat("mip_src", mip_src);
		copy_material->setTexture("source", src);

		copy_material->renderScreen(Render::PASS_POST);

		copy_material->setTexture("source", nullptr);
	}
	render_target->disable();
	Render::releaseTemporaryRenderTarget(render_target);
}


On the other hand if you want to write to 0th mip, you can remove mip_dest param or set it to 0 by default.
The rest can remain the same. If you'd need to copy specific region you could extend shader or replace renderScreen with Ffp calls and setting shader to RenderState manually.

Here an example material:

// More information on ULON format - https://developer.unigine.com/docs/code/formats/ulon_format
// More information on scriptable materials - https://developer.unigine.com/docs/content/materials/scriptable
BaseMaterial <preview_hidden=1 var_prefix=var texture_prefix=tex manual=true hidden=true>
{
	// Texture to be used in a shader (see Fragment shader below). Will be displayed in the Editor UI.
	Texture2D source <source=procedural internal=true>
	
	Float mip_src = 0
	
	Pass post
	{
		Fragment =
		#{
			#include <core/materials/shaders/api/common.h>
			
			STRUCT_FRAG_BEGIN
				INIT_COLOR(float4)
			STRUCT_FRAG_END
			
			MAIN_FRAG_BEGIN(FRAGMENT_IN)
				
				OUT_COLOR = TEXTURE_BIAS(tex_source, IN_UV, var_mip_src);
				
			MAIN_FRAG_END
		#}
	}
}



Please note that this sample will work only in 2.15+, for earlier versions you have to fix a few things. Also note that bindColorTexture2D is available since 2.15
 

Link to comment
×
×
  • Create New...