Mostly Harmless

Legacy:Making A Static Mesh Translucent

From Unreal Wiki, The Unreal Engine Documentation Site
Jump to: navigation, search

This page describes how to dynamically make Static mesh's appear translucent (similar to the way Style=STY_Translucent could do the same with actors in Unreal Engine1), what that means is you should be able to modify the textures on any existing Static mesh during runtime and make them appear translucent using the Alpha drawtype.

In fact, you can do pretty much anything that can be done with the entire Material/Shader system as this method relies on replacing a Static mesh's original textures.

The process of making existing texture's translucent on a Static mesh is not actually too difficult (albeit there are problems described later), but the hard part is Dynamicly Accessing Original Static Mesh Textures but thankfully there is a page to describe how that's done :) (and you will need the code from that page, it is not included here because I intend to update these pages seperately AND this is a slightly different subject)

Here is the code I have used for making certain Static mesh's appear translucent, due to my limited knowledge of shaders I have had to limit this code in some respects (i.e. if some particular shader options are used it can interfere in such a way as to make the translucency effect not work) if you can improve on my workarounds for some shaders then please do as the code in it's current form is somewhat limited.

NOTE:You will find the latest FindStaticMeshSkins function code on the Dynamicly Accessing Original Static Mesh Textures page.

// This function is used to create a new shader with an opacity channel at a certain Alpha (to create the translucency effect)
 
simulated function Shader CreateNewTShader()
 
{
 
	local Shader ShaderObj;
 
	ShaderObj = New Class'Engine.Shader';
 
	// Creates translucency
 
	ShaderObj.Opacity = New Class'Engine.ConstantColor';
 
	ConstantColor(ShaderObj.Opacity).Color.A = 128.0;
 
	return ShaderObj;
 
}
 
// This is the main function where most texture/shader changes are made
 
simulated function MakeActorTranslucent(actor A)
 
{
 
	local Array<Material> NewSkins;
 
	local Material TempMat;
 
	local int i;
 
	// First of all retrieve the list of existing skins
 
	NewSkins = FindStaticMeshSkins(A.StaticMesh);
 
	// Then iterate that list and try to make the skin translucent (using a ConstantColor modifier to change the Alpha)
 
	for (i=0; i<NewSkins.Length; i++)
 
	{
 
		// If the skin is already a shader this creates some difficulty, the code will 'try' to compensate but you will notice that this will not work on a lot of things
 
		if (Shader(NewSkins[i]) != None)
 
		{
 
			// If SelfIllumination is set then your really screwed...This code tries to compensate with FallbackMaterial's...Open to suggestions here
 
			if (Shader(NewSkins[i]).SelfIllumination != None)
 
			{
 
				// Look for a useable FallbackMaterial, if you can't find one then get rid of the SelfIllumination (last resort)
 
				if (NewSkins[i].FallbackMaterial != None && (Shader(NewSkins[i].FallbackMaterial) == none || Shader(Newskins[i].FallbackMaterial).SelfIllumination == none))
 
				{
 
					// If the FallbackMaterial is a shader itself then attempt to use it
 
					if (Shader(NewSkins[i].FallbackMaterial) != None)
 
						NewSkins[i] = NewSkins[i].FallbackMaterial;
 
					else
 
					{
 
						TempMat = NewSkins[i].FallbackMaterial;
 
						NewSkins[i] = CreateNewTShader();
 
						Shader(NewSkins[i]).Diffuse = TempMat;
 
					}
 
				}
 
				else if (NewSkins[i].FallbackMaterial == None)
 
					Shader(NewSkins[i]).SelfIllumination = None;
 
			}
 
			// Create translucency
 
			Shader(NewSkins[i]).Opacity = New Class'Engine.ConstantColor';
 
			ConstantColor(Shader(NewSkins[i]).Opacity).Color.A = 128;
 
		}
 
		// If this is called then the translucency changes should take place perfectly
 
		else
 
		{
 
			TempMat = CreateNewTShader();
 
			Shader(TempMat).Diffuse = NewSkins[i];
 
			NewSkins[i] = TempMat;
 
		}
 
		// Setup the actor's Skins array
 
		A.Skins[i] = NewSkins[i];
 
	}
 
}

Related Topics[edit]

Comments[edit]

Shambler: When I created this code for my own purposes I didn't have to account for every single possible combination of shaders and modifiers, thus if people know a good way of improving this code to work with more combinations of modifers etc. then please UPDATE this page (and preferably the code too) thanks :)