flutter_cube
flutter_cube copied to clipboard
Quick question, what have I done wrong, for some reason the texture is glitching.
So I tried having a cube that rotates according to the mpu6050 which data is read and send via a Arduino Uno. It works perfectly expect for the cube glitching in textures (I tried using different cubes/objects) but the same thing happened.
Also feel free to let me know if I could have done anything else better. Thanks in advance.
import 'dart:convert';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_cube/flutter_cube.dart';
import 'package:funduinoblecontroller/bluetooth/bluetoothmanager.dart';
class CubeScreen extends StatefulWidget {
BluetoothManager blemanager;
CubeScreen({Key? key, required this.blemanager}) : super(key: key);
@override
_CubeScreenState createState() => _CubeScreenState();
}
class _CubeScreenState extends State<CubeScreen> with SingleTickerProviderStateMixin {
late Scene _scene;
Object? _cube;
bool run = true;
double xAngle = 0;
double yAngle = 0;
double zAngle = 0;
double prevXAngle = 0;
double prevYAngle = 0;
double prevZAngle = 0;
double defaultvalueX = 0;
double defaultvalueY = 0;
double defaultvalueZ = 0;
bool isDefaultSet = false;
late AnimationController _controller;
void _onSceneCreated(Scene scene) {
_scene = scene;
scene.camera.position.z = 27;
// set light
_cube = Object(scale: Vector3(8.0, 8.0, 8.0), backfaceCulling: false , isAsset: true,
lighting: true,
fileName: 'assets/cube/cube.obj');
// enable lighting
scene.world.add(_cube!);
// position the light behind the camera
scene.light.position.z = -1;
}
// dispose remove the listener and reset the scene cube etc
@override
void dispose(){
run = false;
_controller.dispose();
_cube = null;
_scene = Scene();
super.dispose();
}
Future<void> getMessages() async {
while (run) {
await Future.delayed(Duration(milliseconds: 20));
try {
// Replace with your actual bluetooth reading logic
await Future.delayed(Duration(milliseconds: 20));
try {
List<int> readValue = await widget.blemanager.selectedCharacteristic!.read();
if(!utf8.decode(readValue).contains(widget.blemanager.messagebuffertemp)){
widget.blemanager.messagebuffertemp = utf8.decode(readValue);
await widget.blemanager.updatebuffer();
}
} catch (e) { }
String message = widget.blemanager.messagebuffer;
RegExp exp = RegExp(r'([XYZ])([+-]?\d+(\.\d+)?)');
Iterable<RegExpMatch> matches = exp.allMatches(message);
Map<String, double> values = {};
for (var match in matches) {
String? value = match.group(1); // X, Y, or Z
String? count = match.group(2); // value
values[value!] = double.parse(count!);
}
if (values.containsKey("X")) {
xAngle = values["X"]!;
}
if (values.containsKey("Y")) {
yAngle = values["Y"]!;
}
if (values.containsKey("Z")) {
zAngle = values["Z"]!;
}
// If default values are not set, set them and take them as reference
if (!isDefaultSet) {
defaultvalueX = xAngle;
defaultvalueY = yAngle;
defaultvalueZ = zAngle;
isDefaultSet = true;
} else {
xAngle -= defaultvalueX;
yAngle -= defaultvalueY;
zAngle -= defaultvalueZ;
}
if (xAngle != prevXAngle || yAngle != prevYAngle || zAngle != prevZAngle) {
try{
setState(() {
if (_cube != null) {
var x = xAngle;
var y = yAngle;
var z = zAngle;
_cube!.position.x = 5.0;
_cube!.position.y = 5.0;
_cube!.position.z = 5.0;
// The X,Y,Z are not assigned to themselfs due to the mpu6050 sensor
// not being mounted in the same orientation as the cube
_cube!.rotation.x = -y;
_cube!.rotation.y = z;
_cube!.rotation.z = -x;
_cube!.position.x = 0.0;
_cube!.position.y = 0.0;
_cube!.position.z = 0.0;
_cube!.updateTransform();
_scene.update();
}
});
}catch (e){
run = false;
}
}
// Store the current values for the next comparison
prevXAngle = xAngle;
prevYAngle = yAngle;
prevZAngle = zAngle;
} catch (e) {
run = false;
}
}
}
@override
void initState() {
super.initState();
_controller = AnimationController(duration: Duration(milliseconds: 600), vsync: this)
..addListener(() {
if (_cube != null) {
_cube!.lighting = true;
_cube!.updateTransform();
_scene.update();
}
})
..repeat();
getMessages();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("MPU Cube"),
),
body: Stack(
children: [
Center(
child: Cube(
interactive: false,
onSceneCreated: _onSceneCreated,
),
),
],
),
);
}
}
Here are some images of the problem:
https://imgur.com/a/XfW59jC
This is the main issue with the package. I've experience this with any type of shape that had thin "walls" to put it simply. If you make a full solid cube you might avoid it. But if you put a texture on top, it will still happen.
And the package seems to not be mantained anymore. I would recommend to use impeler in IOS and in android we just need to wait till they get it running.
If you find a fix please share.
Probably same issue https://github.com/dariocavada/panorama_viewer/issues/13