Skip to content
MaterialInstance

Gameplay / MaterialInstance

MaterialInstance Class ​

Material instance class

Usage example: create a script named "MaterialExample", attach the script to a model object, open the script, enter the following code to save, run the game, and you will see the effect of dynamically setting material in the scene. The code is as follows:

ts
@Component
export default class MaterialExample extends Script {
    protected onStart(): void {
        if (this.gameObject instanceof mw.Model){
            // Get the material on the current Mesh
            const mis = this.gameObject.getMaterialInstance();
            mis.forEach(mi => {
            // Get the floating point parameters of the current material
                let aspns = mi.getAllScalarParameterName();
                aspns.forEach(aspn => {
                    // Set floating point parameters for the current material
                    mi.setScalarParameterValue(aspn, 1);
                });
                // Get the color parameters of the current material
                let avpns = mi.getAllVectorParameterName();
                avpns.forEach(avpn => {
                    // Set the color parameters of the current material
                    mi.setVectorParameterValue(avpn, LinearColor.red);
                });
                // Get the mapping parameters of the current material
                let atpns = mi.getAllTextureParameterName();
                atpns.forEach(atpn => {
                    // Set the texture parameters of the current material to ensure that the set asset are loaded
                    mi.setTextureParameterValue(atpn, "26512");
                });
            });
        }
    }
}
@Component
export default class MaterialExample extends Script {
    protected onStart(): void {
        if (this.gameObject instanceof mw.Model){
            // Get the material on the current Mesh
            const mis = this.gameObject.getMaterialInstance();
            mis.forEach(mi => {
            // Get the floating point parameters of the current material
                let aspns = mi.getAllScalarParameterName();
                aspns.forEach(aspn => {
                    // Set floating point parameters for the current material
                    mi.setScalarParameterValue(aspn, 1);
                });
                // Get the color parameters of the current material
                let avpns = mi.getAllVectorParameterName();
                avpns.forEach(avpn => {
                    // Set the color parameters of the current material
                    mi.setVectorParameterValue(avpn, LinearColor.red);
                });
                // Get the mapping parameters of the current material
                let atpns = mi.getAllTextureParameterName();
                atpns.forEach(atpn => {
                    // Set the texture parameters of the current material to ensure that the set asset are loaded
                    mi.setTextureParameterValue(atpn, "26512");
                });
            });
        }
    }
}

Table of contents ​

Methods ​

getAllScalarParameterName(): string[] other
Return all floating point material parameters
getAllTextureParameterName(): string[] other
Return all texture material parameters
getAllVectorParameterName(): string[] other
Return all color material parameters
getProperty(propertyName: string): any other
Get material assigned name property
getPropertyNames(): string[] other
Get all property names of material
getScalarParameterValue(parameterName: string): number other
Get floating-point parameter values
getTextureParameterValue(parameterName: string): string other
Get texture parameter values
getVectorParameterValue(parameterName: string): LinearColor other
Obtain color parameter values
setProperty(propertyName: string, value: any): void other
Set material properties
setScalarParameterValue(parameterName: string, value: number): void other
Set floating-point parameter values.
setTextureParameterValue(parameterName: string, value: string): void other
Set texture parameter values
setVectorParameterValue(parameterName: string, value: LinearColor): void other
Set color parameter values

Methods ​

getAllScalarParameterName ​

• getAllScalarParameterName(): string[] other

Return all floating point material parameters

Returns ​

string[]Material parameter array

getAllTextureParameterName ​

• getAllTextureParameterName(): string[] other

Return all texture material parameters

Returns ​

string[]Material parameter array

getAllVectorParameterName ​

• getAllVectorParameterName(): string[] other

Return all color material parameters

Returns ​

string[]Material parameter array

getProperty ​

• getProperty(propertyName): any other

Get material assigned name property

Parameters ​

propertyNamestring

Returns ​

any

getPropertyNames ​

• getPropertyNames(): string[] other

Get all property names of material

Returns ​

string[]Return a list of attribute names

getScalarParameterValue ​

• getScalarParameterValue(parameterName): number other

Get floating-point parameter values

Parameters ​

parameterName stringIncoming material scalar parameter name range: parameter name

Returns ​

numberParameter values

Scalar parameter is an adjustable value in material, which can be used to control various property of material, such as color, intensity, transparency, etc.

By obtaining the value of the scalar parameter, you can dynamically adjust and use it in the game as needed.


getTextureParameterValue ​

• getTextureParameterValue(parameterName): string other

Get texture parameter values

Parameters ​

parameterName stringParameter name range:

Returns ​

stringParameter values

getVectorParameterValue ​

• getVectorParameterValue(parameterName): LinearColor other

Obtain color parameter values

Parameters ​

parameterName stringParameter name range:

Returns ​

LinearColorParameter values

setProperty ​

• setProperty(propertyName, value): void other

Set material properties

Parameters ​

propertyNamestring
valueany

setScalarParameterValue ​

• setScalarParameterValue(parameterName, value): void other

Set floating-point parameter values.

Parameters ​

parameterName stringParameter name range:
value numberThe parameter value setting can adjust the numerical value of the parameter. Type: Floating point range: No restrictions.

setTextureParameterValue ​

• setTextureParameterValue(parameterName, value): void other

Set texture parameter values

Parameters ​

parameterName stringParameter name range:
value stringParameter value range: The set value, without any restrictions, is reasonable

setVectorParameterValue ​

• setVectorParameterValue(parameterName, value): void other

Set color parameter values

Parameters ​

parameterName stringParameter name range:
value LinearColorParameter value type: Floating point range: Unrestricted.