rename/update materials_and_textures sample project
@@ -1,80 +0,0 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:thermion_flutter/thermion_flutter.dart';
|
||||
import 'package:vector_math/vector_math_64.dart' hide Colors;
|
||||
|
||||
void main() {
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Flutter Demo',
|
||||
theme: ThemeData(
|
||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
||||
useMaterial3: true,
|
||||
),
|
||||
home: const MyHomePage(title: 'Flutter Demo Home Page'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MyHomePage extends StatefulWidget {
|
||||
const MyHomePage({super.key, required this.title});
|
||||
|
||||
final String title;
|
||||
|
||||
@override
|
||||
State<MyHomePage> createState() => _MyHomePageState();
|
||||
}
|
||||
|
||||
class _MyHomePageState extends State<MyHomePage> {
|
||||
ThermionViewer? _viewer;
|
||||
late ThermionAsset _asset;
|
||||
late MaterialInstance _materialInstance;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
ThermionFlutterPlugin.createViewer().then((viewer) async {
|
||||
_viewer = viewer;
|
||||
await _viewer!.setPostProcessing(true);
|
||||
_materialInstance = await _viewer!.createUnlitMaterialInstance();
|
||||
_asset = await _viewer!.createGeometry(
|
||||
GeometryHelper.cube(normals: false, uvs: false),
|
||||
materialInstances: [_materialInstance]);
|
||||
|
||||
await _viewer!
|
||||
.setTransform(_asset.entity, Matrix4.translation(Vector3.all(2)));
|
||||
await _materialInstance.setParameterFloat4(
|
||||
"baseColorFactor", 0.0, 1.0, 0.0, 1.0);
|
||||
await _viewer!.setCameraPosition(0, 0, 10);
|
||||
await _viewer!.setRendering(true);
|
||||
setState(() {});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Stack(children: [
|
||||
if (_viewer == null) CircularProgressIndicator(),
|
||||
if (_viewer != null) ...[
|
||||
Positioned.fill(child: ThermionWidget(viewer: _viewer!)),
|
||||
Align(
|
||||
alignment: Alignment.bottomCenter,
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(12),
|
||||
child: ElevatedButton(
|
||||
onPressed: () async {
|
||||
await _materialInstance.setParameterFloat4(
|
||||
"baseColorFactor", 1.0, 0.0, 0.0, 1.0);
|
||||
},
|
||||
child: Text("Set material property (baseColorFactor)"))))
|
||||
]
|
||||
]));
|
||||
}
|
||||
}
|
||||
1
examples/flutter/materials_and_textures/assets
Symbolic link
@@ -0,0 +1 @@
|
||||
../../assets
|
||||
|
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 11 KiB |
|
Before Width: | Height: | Size: 295 B After Width: | Height: | Size: 295 B |
|
Before Width: | Height: | Size: 406 B After Width: | Height: | Size: 406 B |
|
Before Width: | Height: | Size: 450 B After Width: | Height: | Size: 450 B |
|
Before Width: | Height: | Size: 282 B After Width: | Height: | Size: 282 B |
|
Before Width: | Height: | Size: 462 B After Width: | Height: | Size: 462 B |
|
Before Width: | Height: | Size: 704 B After Width: | Height: | Size: 704 B |
|
Before Width: | Height: | Size: 406 B After Width: | Height: | Size: 406 B |
|
Before Width: | Height: | Size: 586 B After Width: | Height: | Size: 586 B |
|
Before Width: | Height: | Size: 862 B After Width: | Height: | Size: 862 B |
|
Before Width: | Height: | Size: 862 B After Width: | Height: | Size: 862 B |
|
Before Width: | Height: | Size: 1.6 KiB After Width: | Height: | Size: 1.6 KiB |
|
Before Width: | Height: | Size: 762 B After Width: | Height: | Size: 762 B |
|
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.2 KiB |
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.4 KiB |
|
Before Width: | Height: | Size: 68 B After Width: | Height: | Size: 68 B |
|
Before Width: | Height: | Size: 68 B After Width: | Height: | Size: 68 B |
|
Before Width: | Height: | Size: 68 B After Width: | Height: | Size: 68 B |
186
examples/flutter/materials_and_textures/lib/main.dart
Normal file
@@ -0,0 +1,186 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:thermion_flutter/thermion_flutter.dart';
|
||||
import 'package:thermion_flutter/thermion_flutter.dart' as t;
|
||||
import 'package:vector_math/vector_math_64.dart' hide Colors;
|
||||
|
||||
void main() {
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Flutter Demo',
|
||||
theme: ThemeData(
|
||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
||||
useMaterial3: true,
|
||||
),
|
||||
home: const MyHomePage(title: 'Flutter Demo Home Page'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MyHomePage extends StatefulWidget {
|
||||
const MyHomePage({super.key, required this.title});
|
||||
|
||||
final String title;
|
||||
|
||||
@override
|
||||
State<MyHomePage> createState() => _MyHomePageState();
|
||||
}
|
||||
|
||||
class _MyHomePageState extends State<MyHomePage> {
|
||||
ThermionViewer? _viewer;
|
||||
late ThermionAsset _asset;
|
||||
bool green = true;
|
||||
bool unlit = true;
|
||||
|
||||
t.Texture? _texture;
|
||||
t.Texture? _textureSampler;
|
||||
t.LinearImage? _image;
|
||||
late MaterialInstance _unlitMaterial;
|
||||
late MaterialInstance _litMaterial;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
ThermionFlutterPlugin.createViewer().then((viewer) async {
|
||||
_viewer = viewer;
|
||||
await _viewer!.setPostProcessing(true);
|
||||
|
||||
_unlitMaterial = await _viewer!.createUnlitMaterialInstance();
|
||||
_litMaterial = await _viewer!.createUbershaderMaterialInstance();
|
||||
await _viewer!.addDirectLight(
|
||||
DirectLight.sun(
|
||||
intensity: 50000,
|
||||
direction: Vector3(1, -1, -1).normalized(),
|
||||
),
|
||||
);
|
||||
await _viewer!.loadSkybox("assets/default_env_skybox.ktx");
|
||||
await _viewer!.loadIbl("assets/default_env_ibl.ktx");
|
||||
|
||||
for (var material in [_unlitMaterial, _litMaterial]) {
|
||||
await material.setParameterInt("baseColorIndex", -1);
|
||||
await material.setParameterFloat4(
|
||||
"baseColorFactor",
|
||||
0.0,
|
||||
1.0,
|
||||
0.0,
|
||||
1.0,
|
||||
);
|
||||
}
|
||||
|
||||
_asset = await _viewer!.createGeometry(
|
||||
GeometryHelper.cube(),
|
||||
materialInstances: [_unlitMaterial],
|
||||
);
|
||||
|
||||
await _viewer!.setCameraPosition(0, 0, 5);
|
||||
await _viewer!.setRendering(true);
|
||||
setState(() {});
|
||||
});
|
||||
}
|
||||
|
||||
Future _setMaterialTexture(MaterialInstance materialInstance) async {
|
||||
await _textureSampler?.dispose();
|
||||
await _texture?.dispose();
|
||||
await _image?.destroy();
|
||||
|
||||
await materialInstance.setParameterInt("baseColorIndex", 0);
|
||||
var imageBuffer = await rootBundle.load("assets/background.png");
|
||||
var imageData = imageBuffer.buffer.asUint8List(imageBuffer.offsetInBytes);
|
||||
_image = await _viewer!.decodeImage(imageData);
|
||||
var width = await _image!.getWidth();
|
||||
var height = await _image!.getHeight();
|
||||
|
||||
_texture = await _viewer!.createTexture(width, height);
|
||||
await _texture!.setLinearImage(
|
||||
_image!,
|
||||
PixelDataFormat.RGBA,
|
||||
PixelDataType.FLOAT,
|
||||
);
|
||||
|
||||
final textureSampler = await _viewer!.createTextureSampler();
|
||||
await materialInstance.setParameterTexture(
|
||||
"baseColorMap",
|
||||
_texture!,
|
||||
textureSampler,
|
||||
);
|
||||
}
|
||||
|
||||
Future _setActiveMaterialColor() async {
|
||||
var active = unlit ? _unlitMaterial : _litMaterial;
|
||||
await active.setParameterFloat4(
|
||||
"baseColorFactor",
|
||||
green ? 0.0 : 1.0,
|
||||
green ? 1.0 : 0.0,
|
||||
0.0,
|
||||
1.0,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Stack(
|
||||
children: [
|
||||
if (_viewer == null) CircularProgressIndicator(),
|
||||
if (_viewer != null) ...[
|
||||
Positioned.fill(
|
||||
child: ThermionListenerWidget(
|
||||
inputHandler: DelegateInputHandler.fixedOrbit(_viewer!),
|
||||
child: ThermionWidget(viewer: _viewer!),
|
||||
),
|
||||
),
|
||||
Align(
|
||||
alignment: Alignment.bottomCenter,
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(12),
|
||||
child: Row(
|
||||
children: [
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
green = !green;
|
||||
_setActiveMaterialColor();
|
||||
},
|
||||
child: Text(
|
||||
"Set baseColorFactor to ${green ? "red" : "green"}",
|
||||
),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
unlit = !unlit;
|
||||
setState(() {
|
||||
|
||||
});
|
||||
await _asset.setMaterialInstanceAt(
|
||||
unlit ? _unlitMaterial : _litMaterial,
|
||||
);
|
||||
_setActiveMaterialColor();
|
||||
},
|
||||
child: Text("Use ${unlit ? "lit" : "unlit"} material"),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
var materialInstance = await _viewer!
|
||||
.getMaterialInstanceAt(_asset.entity, 0);
|
||||
await _setMaterialTexture(materialInstance);
|
||||
},
|
||||
child: Text("Apply texture"),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -59,6 +59,7 @@
|
||||
ignoresPersistentStateOnLaunch = "NO"
|
||||
debugDocumentVersioning = "YES"
|
||||
debugServiceExtension = "internal"
|
||||
enableGPUValidationMode = "1"
|
||||
allowLocationSimulation = "YES">
|
||||
<BuildableProductRunnable
|
||||
runnableDebuggingMode = "0">
|
||||
|
Before Width: | Height: | Size: 101 KiB After Width: | Height: | Size: 101 KiB |
|
Before Width: | Height: | Size: 5.5 KiB After Width: | Height: | Size: 5.5 KiB |
|
Before Width: | Height: | Size: 520 B After Width: | Height: | Size: 520 B |
|
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 14 KiB |
|
Before Width: | Height: | Size: 1.0 KiB After Width: | Height: | Size: 1.0 KiB |
|
Before Width: | Height: | Size: 36 KiB After Width: | Height: | Size: 36 KiB |
|
Before Width: | Height: | Size: 2.2 KiB After Width: | Height: | Size: 2.2 KiB |
@@ -1,4 +1,4 @@
|
||||
name: materials
|
||||
name: materials_and_textures
|
||||
description: "A new Flutter project."
|
||||
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
|
||||
|
||||
@@ -38,3 +38,5 @@ dependency_overrides:
|
||||
|
||||
flutter:
|
||||
uses-material-design: true
|
||||
assets:
|
||||
- assets/
|
||||