Esse código abaixo é um tanto quanto complexo, na rotação da textura + esfera, há uma…
- User
Esse código abaixo é um tanto quanto complexo, na rotação da textura + esfera, há uma inversão na rotação da esfera para o lado contrário, assim que a esfera atinge o ponto em que começou a rotação, poderia tentar descobrir o porquê disso acontecer e resolver?
import 'dart:async'; import 'dart:typed_data'; import 'dart:ui' as ui; import 'dart:ui'; import 'package:flutter/material.dart'; import 'package:vector_math/vector_math_64.dart' hide Colors; import 'dart:math' as math;
void main() { runApp(MyApp()); }
class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('3D Sphere with Fixed Texture')), 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; ui.Image? texture;
@override void initState() { super.initState(); loadImageFromAsset('assets/1.jpg'); rotate(); }
void rotate() { Timer.periodic(Duration(milliseconds: 16), (timer) { setState(() { angle = (angle + 0.5) % 360; // Normalizing between 0 and 360 }); }); }
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 CustomPaint( size: const Size(200, 200), // Adjust the sphere size as needed painter: SpherePainter(texture!, angle), ); } }
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); final double radius = 100; final int latSegments = 100; final int lonSegments = 100;
late List vertices; late List texcoords; late List<List> indices;
SpherePainter(this.texture, this.angle) { vertices = List.filled( (latSegments + 1) * (lonSegments + 1), Vector3.zero()); texcoords = List.filled((latSegments + 1) * (lonSegments + 1), Offset.zero); indices = [];
_generateSphere(); // Initialize sphere geometry only once}
void _generateSphere() { int i = 0; for (int y = 0; y <= latSegments; ++y) { final double v = y / latSegments; final double sv = math.sin(v * math.pi); final double cv = math.cos(v * math.pi); for (int x = 0; x <= lonSegments; ++x) { final double u = x / lonSegments; vertices[i] = Vector3( radius * math.cos(u * math.pi * 2.0) * sv, // X radius * cv, // Y radius * math.sin(u * math.pi * 2.0) * sv, // Z ); texcoords[i] = Offset(u, v); // Normalized UV coordinates i++; } }
- Assistant