Announcement

Collapse
No announcement yet.

Using the same material with all of objects but each of them with different colours

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • replied
    Originally posted by JamesEmory View Post
    It's quite easy to set this up in the material editor and blueprints, but I don't know enough C++ to help you out.
    The problem is that all the meshes that have the same material instance have the same base colour.

    Leave a comment:


  • replied
    It's quite easy to set this up in the material editor and blueprints, but I don't know enough C++ to help you out.

    Leave a comment:


  • Using the same material with all of objects but each of them with different colours

    I'm new in Unreal development and I have created a material with a vector parameter for its base colour. I have also a material instance for this material.

    I want to use this material in many objects, but all of these objects will have different colours. And these objects will be created at runtime: I have a spawner class to spawn them. For testing purposes I have two spheres with the material instance applied.

    In header I have:
    Code:
    UPROPERTY(EditAnywhere)
    USphereComponent* SphereComponent;
    
    UPROPERTY(EditAnywhere)
    UStaticMeshComponent* SphereVisual;
    On Spawn class constructor I have this:

    Code:
    AStarActor::AStarActor()
    {
     	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    	PrimaryActorTick.bCanEverTick = true;
    
    	// Our root component will be a sphere that reacts to physics
    	SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
    	RootComponent = SphereComponent;
    	SphereComponent->InitSphereRadius(1.0f);
    	SphereComponent->SetCollisionProfileName(TEXT("SphereStart"));
    
    	// Create and position a mesh component so we can see where our sphere is
    	SphereVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
    	SphereVisual->AttachTo(RootComponent);
    	static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
    	if (SphereVisualAsset.Succeeded())
    	{
    		UStaticMesh* mesh = SphereVisualAsset.Object;
    		static ConstructorHelpers::FObjectFinder<UMaterial> MatFinder(TEXT("Material'/Game/Geometry/Materials/Star.Star'"));  // "MaterialInstanceConstant'/Game/Geometry/Materials/Star_Inst.Star_Inst'"
    		if (MatFinder.Succeeded())
    		{
    			StarMaterial = MatFinder.Object;
    		}
    
    		SphereVisual->SetStaticMesh(mesh);
    		SphereVisual->SetRelativeLocation(FVector(0.0f, 0.0f, -40.0f));
    		SphereVisual->SetWorldScale3D(FVector(0.8f));
    	}
    }
    And the PostInitializeComponents method:
    Code:
    void AStarActor::PostInitializeComponents()
    {
    	Super::PostInitializeComponents();
    
    	UMaterialInstanceDynamic* matInstance = UMaterialInstanceDynamic::Create(StarMaterial, this);
    
    	matInstance->SetVectorParameterValue(FName(TEXT("BaseColor")), FLinearColor(255.0, 255.0, 255.0, 1.0));
    	SphereVisual->SetMaterial(0, matInstance);
    }
    Is it possible to have two or more objects with the same material but with different colours?
    Last edited by ViaCognita; 10-16-2015, 01:34 PM.
Working...
X