O que eu fiz de errado, na esfera, texturização, ou transformação. Para que no eixo X,…
- User
O que eu fiz de errado, na esfera, texturização, ou transformação. Para que no eixo X, em determinado ponto da rotação, a esfera comece a girar para o lado oposto em que começou a rotação?
import 'dart:async'; import 'dart:typed_data'; import 'dart:ui'; import 'package:flutter/material.dart'; import 'dart:math' as math; import 'dart:ui' as ui; import 'package:vector_math/vector_math_64.dart' hide Colors;
void main() { runApp(MyApp()); }
class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Esfera 3D com Textura Fixa')), body: Center(child: SphereWidget()), backgroundColor: const Color.fromARGB(255, 52, 13, 245), ), ); } }
class SphereWidget extends StatefulWidget { @override _SphereWidgetState createState() => _SphereWidgetState(); }
class _SphereWidgetState extends State { double angle = 0; double rotation = 0; ui.Image? texture;
@override void initState() { super.initState(); loadImageFromAsset('assets/02.jpg'); rotate(); }
void rotate() { Timer.periodic(Duration(milliseconds: 16), (timer) { setState(() { angle = (angle + 0.008) % (2 * math.pi); // Controla a velocidade e mantém o ângulo no intervalo de 0 a 2π rotation += 0.5; }); }); }
Future loadImageFromAsset(String path) async { final completer = Completer<ui.Image>(); final image = AssetImage(path); image.resolve(ImageConfiguration()).addListener( ImageStreamListener((info, _) { completer.complete(info.image); }), ); texture = await completer.future; setState(() {}); }
@override Widget build(BuildContext context) { if (texture == null) { return Container(); }
/* return Stack( children: [ for (int i = 0; i < 15; i++) Positioned( left: (i % 5) * 200.0, // Distância entre esferas horizontalmente top: (i ~/ 5) * 200.0, // Distância entre esferas verticalmente child: CustomPaint( size: Size(10, 10), painter: SpherePainter(texture!, angle, rotation), ), ), ], ); */ return CustomPaint( size: const Size(100, 100), painter: SpherePainter(texture!, angle, rotation), );} }
class SpherePainter extends CustomPainter { final ui.Image texture; final double angle; final Matrix4 transform = Matrix4.identity(); final Vector3 position = Vector3.zero(); final Vector3 rotation = Vector3.zero(); final Vector3 scale = Vector3(1, 1, 1); double rotacao = 0;
SpherePainter(this.texture, this.angle, this.rotacao);
void updateTransform() { final Matrix4 m = Matrix4.compose( position, Quaternion.euler( radians(rotation.y), radians(rotation.x), radians(rotation.z)), scale); transform.setFrom(m); }
// Função para transformar os vértices com a rotação aplicada void transformVertices( List vertices, Matrix4 model, Matrix4 view, Matrix4 projection, Float32List positions, Float32List positionsZ, ) { final Matrix4 transform = model * view * projection; // Tente esta ordem
- Assistant