bdero / flutter_scene

3D library for Flutter.
MIT License
175 stars 12 forks source link

[Question] Changing the materials, textures #23

Closed MarlonJD closed 2 months ago

MarlonJD commented 2 months ago

Hey there 👋. Is there any way to change textures of the material and material's metallic, roughness, alpha values in runtime?

bdero commented 2 months ago

Hi! 👋

Modifying materials

Yes, you can navigate to a MeshPrimitive that has the Material you wish to modify and change any properties:

final dashModel = await Node.fromAsset('build/models/dash.model');

final visibleNode = modelNode.getChildByNamePath(['DashArmature', 'Dash'])!;
final material = visibleNode.mesh!.primitives[0].material as PhysicallyBasedMaterial;

material.metallicRoughnessTexture = null;
material.metallicFactor = 0;
material.roughnessFactor = 0;

material.emissiveFactor = const Color.fromARGB(255, 100, 255, 0);

scene.add(modelNode);
image

Keep in mind that the same Material instance may be referenced by many different meshes across the scene.

The "factor" parameters are multiplied with the corresponding texture channel(s). But if the texture is null, then the factor directly/fully controls the property.

Translucency

The alpha channel of the baseColorFactor controls translucency:

material.baseColorFactor = material.baseColorFactor.withAlpha(100);
image

Replacing textures

Textures are explicitly uploaded to the GPU, and there are a couple of helpers to create them from asset strings and ui.Images in package:flutter_scene/asset_helpers.dart:

// Upload a texture directly from an image asset string.
material.baseColorTexture = await gpuTextureFromAsset('mytexture.png');

// Upload a texture from a ui.Image.
material.normalTexture = await gpuTextureFromImage(myimage);
MarlonJD commented 2 months ago

Wow, it was so much detailed answer, thank you so much