Stanford UE4 + C++ Course Study Record 16: Material - Flickering Dissolution Effect

Table of contents

1. Create the material

2. Code pass value

3. Dissolving effect


        This section will implement the flickering effect of an object after being hit.

1. Create the material

        First, create a new material M_HitFlashDemo under the Materials folder, and set any basic color. In the course, a very clever method is used to set the "self-illumination color" property, so as to realize the instantaneous flashing of the material. As shown in Figure 16-1, there is a time in the material. When the object is hit, TimeToHit is set as the current time through C++. At this moment, the result of the subtraction between the two is 0; but when the hit is triggered, the object needs to emit light. The "self-illumination color" attribute needs to be set greater than 0, so change 0 to 1 through the 1-x node. Finally, since the Time node in the material will be continuously updated, the difference of the subtraction will become larger and larger, so use Clamp to limit its upper and lower bounds to 1 and 0.

Figure 16-1 Flashing when hit

        As shown in Figure 16-1, we can check the "End" of the "Material Expression Time" of the Time node and set it to 3, so that we can preview the effect of material flickering:

Figure 16-2 Flashing effect

2. Code pass value

        Next, transfer to VS and use C++ to realize the logic of passing in time. At this time, be sure to uncheck the "End" of the Time node .

        First, derive a SurTargetDummy class from Actor in UE to write related codes. This part of the code is very simple. Create a static mesh component and the SurAttributeComp component implemented in Section 13, and bind the custom OnHealthChanged event to set the TimeToHit variable to the current time.

SurTargetDummy.h

#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "SurTargetDummy.generated.h"

class USurAttributeComponent;

UCLASS()
class SURKEAUE_API ASurTargetDummy : public AActor
{
	GENERATED_BODY()
	
protected:

	UPROPERTY(VisibleAnywhere)
	USurAttributeComponent* AttributeComp;

	UPROPERTY(VisibleAnywhere)
	UStaticMeshComponent* MeshComp;
	
	UFUNCTION()
	void OnHealthChanged(AActor* InstigatorActor, USurAttributeComponent* OwningComp, float NewHealth, float Delta);

public:	
	ASurTargetDummy();
};

SurTargetDummy.cpp

#include "SurTargetDummy.h"
#include "SurAttributeComponent.h"

ASurTargetDummy::ASurTargetDummy()
{
	MeshComp = CreateDefaultSubobject<UStaticMeshComponent>("MeshComp");
	RootComponent = MeshComp;

	AttributeComp = CreateDefaultSubobject<USurAttributeComponent>("AttributeComp");
	AttributeComp->OnHealthChanged.AddDynamic(this, &ASurTargetDummy::OnHealthChanged);
}

void ASurTargetDummy::OnHealthChanged(AActor* InstigatorActor, USurAttributeComponent* OwningComp, float NewHealth, float Delta)
{
	// 治愈(>0)或免疫(=0)时不触发受击特效
	if (Delta < 0) {
		MeshComp->SetScalarParameterValueOnMaterials("TimeToHit", GetWorld()->TimeSeconds);
	}
}

        Then go back to UE, create the TargetDummyBP blueprint class under Content, and inherit the above TargetDummy class. Set any geometry for it, and set our flashing material M_HitFlashDemo. Put it in the scene and run:

Figure 16-3 Running the test

        It can be seen that the flickering speed at this time is still relatively slow. If you want to speed up the flickering speed, you only need to increase the change speed of the "self-illumination color" value, so add multiplication after the subtraction, and the multiplier can be adjusted by yourself:

Figure 16-4 Add multiplication
Figure 16-5 Increase speed

        Further, we can expose the multiplier of the multiplication as a parameter, create it as the HitFlashSpeed ​​parameter, and set the default value to be other than 0. After doing this, we can create a material instance as in the previous section to adjust the animation speed in real time; further, we can also implement in the code the function of determining the blinking speed according to the amount of damage, or any other effect you can think of.


3. Dissolving effect

        We'll use a similar approach and create another material with a dissolve effect called M_DissolveEffect. First, set the "blending mode" of the material to "mask" in the details panel (a general concept, put it on the original object like a cover to control whether it is displayed), so that the content on the other side can be rendered after the material is transparent , you can check "Double Sided".

        Create a Texture Sample and set it to any texture with the name "cloud" or "noise". Here I set T_TilingClouds_01 in the Gideon file. Then, connect it to the "Opacity Mask" pin, so that you can see the partial transparency effect:

Figure 16-6 Blueprint Settings
Figure 16-7 Partial transparency effect

        In order to display the dynamic dissolution effect normally later, remember to check "Real-time" as shown in the picture above.

        Now, we can try to adjust the "Opacity Mask Clipping Value" of the material to observe the effect on the display effect. We can find that the larger the value, the more transparent parts of the material. According to the introduction of the course, the alpha value of each point of the texture we use is different (the channel that determines whether it is transparent), the "opacity mask clipping value" is the threshold that determines whether to display or not, and the alpha value of the texture exceeds this threshold. can be displayed. Therefore, we can control the dynamic dissolution effect by setting the Alpha value:

Figure 16-8 Dissolving effect

        Set the Period of the Time node to 1, and the Opacity Mask Clipping Value to 1. If you do not understand the above principles, you can analyze the value of each node, or use the method of printing strings learned in the previous section. After saving, apply it to the cube in the scene to see the effect:

Figure 16-9 Running the test

Guess you like

Origin blog.csdn.net/surkea/article/details/127266725