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:
@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
propertyName | string |
---|
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 string | Incoming material scalar parameter name range: parameter name |
---|
Returns
number | Parameter 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 string | Parameter name range: |
---|
Returns
string | Parameter values |
---|
getVectorParameterValue
• getVectorParameterValue(parameterName
): LinearColor
other
Obtain color parameter values
Parameters
parameterName string | Parameter name range: |
---|
Returns
LinearColor | Parameter values |
---|
setProperty
• setProperty(propertyName
, value
): void
other
Set material properties
Parameters
propertyName | string |
---|---|
value | any |
setScalarParameterValue
• setScalarParameterValue(parameterName
, value
): void
other
Set floating-point parameter values.
Parameters
parameterName string | Parameter name range: |
---|---|
value number | The 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 string | Parameter name range: |
---|---|
value string | Parameter value range: The set value, without any restrictions, is reasonable |
setVectorParameterValue
• setVectorParameterValue(parameterName
, value
): void
other
Set color parameter values
Parameters
parameterName string | Parameter name range: |
---|---|
value LinearColor | Parameter value type: Floating point range: Unrestricted. |