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

    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.

    #2
    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.

    Comment


      #3
      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.

      Comment


        #4
        What is problem with your current solution? What you want to achieve?

        Comment


          #5
          I think the PerInstanceRandom material node would work

          Comment


            #6
            Originally posted by Jenny Gore View Post
            What is problem with your current solution? What you want to achieve?
            I want to use the same material in all of the objects but each of them with different colours. Now, if I change its base colour in one of them, all of them will have the same colour.

            Comment


              #7
              Add a param to the main material with the color, and then create a material instance. You'll need to duplicate the material instance for each color you want, but it will be much more optimized than using a separate material with only the colors changed. I also do this for UV scaling for various meshes. I'll post some screenshots if you need once I get off of work tonight.
              Last edited by SaviorNT; 10-16-2015, 04:03 PM.
              WIP: Science Project - A collection of middle school through advanced college level science theory and formula-based functions for use in your own projects
              World Machine to UE4 Export Macro
              WM Folder Generator - Creates a folder that you name with HeightMap, NormalMap, SplatMap, and Tile sub-folders

              Comment


                #8
                If they are all material instances, you can set their scalar/vector parameters in blueprint with the "set scalar/vector parameter" nodes and change their values to whatever you want when they spawn.

                Comment


                  #9
                  Originally posted by ViaCognita View Post
                  I want to use the same material in all of the objects but each of them with different colours. Now, if I change its base colour in one of them, all of them will have the same colour.
                  What is problem using one material instance per color?

                  Comment


                    #10
                    Even using an instance isn't going to cut it, you need to convert them to Dynamic Material Instances. then each one can have unique colour.

                    Comment


                      #11
                      Originally posted by Anadin View Post
                      Even using an instance isn't going to cut it, you need to convert them to Dynamic Material Instances. then each one can have unique colour.
                      They are UMaterialInstanceDynamic and they have the same colour.

                      Comment


                        #12
                        Originally posted by Jenny Gore View Post
                        What is problem using one material instance per color?
                        I don't know. I'm new in Unreal Development.

                        Comment


                          #13
                          I have created another material instance, with a blue colour. I get this:

                          Before start the game:
                          Click image for larger version

Name:	InitStar.png
Views:	1
Size:	70.8 KB
ID:	1091385

                          Running the game:
                          Click image for larger version

Name:	Running.png
Views:	1
Size:	122.3 KB
ID:	1091386

                          The two yellow balls have a yellow material instance, and in the game I create balls with white-blue material. As you can see, all the Actors in game change their material instance.

                          Comment


                            #14
                            In the first screenshot, I show how to 1) Create a UV Scaling parameter and 2) Using a Switch Param to add a color to the texture. This is my main project, and I only needed a normal texture, or a red one. In the second picture, I made up a section that will allow you to specify any color you want.

                            Once one of these are in your main material, right click the material, and create a material instance. Name this instance MainMatName_Color_Inst. Create as many instances as you need colors for. Change each individual instance to whatever color, and add that instance to the mesh's material slot.

                            PS: In the comment node, I say to connect the blue pin to an alpha mask.. it's actually the alpha channel that needs connected to an alpha mask (if you have one).. it's the bottom pin.


                            Click image for larger version

Name:	RedDoorMat01.PNG
Views:	1
Size:	194.8 KB
ID:	1091396

                            Click image for larger version

Name:	PaintAnyColor.PNG
Views:	1
Size:	94.3 KB
ID:	1091397
                            Last edited by SaviorNT; 10-17-2015, 11:00 AM.
                            WIP: Science Project - A collection of middle school through advanced college level science theory and formula-based functions for use in your own projects
                            World Machine to UE4 Export Macro
                            WM Folder Generator - Creates a folder that you name with HeightMap, NormalMap, SplatMap, and Tile sub-folders

                            Comment


                              #15
                              I have created this git repository if you want to check all the project: https://github.com/ViaCognita/Estrellas. You can also download Estrellas.sdf 7zipped here: https://onedrive.live.com/redir?resi...hint=file%2c7z

                              I don't know why, but the repository is about 1Gb. Maybe because I'm using starter content.
                              Last edited by ViaCognita; 10-18-2015, 10:41 AM.

                              Comment

                              Working...
                              X