por algum motivo, ao adicionar mais que 10 esferas, por exemplo, 16 esferas e tentar…
- User
por algum motivo, ao adicionar mais que 10 esferas, por exemplo, 16 esferas e tentar mudar a rotação delas. Às vezes nenhuma é renderizada, ou elas simplesmente param de girar na rotação. Por que isso acontece e como eu posso corrigir o código dessa lib:
library sphere;
import 'dart:async'; import 'dart:typed_data'; import 'dart:math' as math; import 'dart:ui' as ui; import 'package:flutter/material.dart'; import 'package:flutter/services.dart' show rootBundle;
class Sphere extends StatefulWidget { const Sphere({ Key? key, required this.surface, required this.radius, this.latitude = 0, this.longitude = 0, this.alignment = Alignment.center, this.positionOffset = Offset.zero, // Offset de posição para atualizar externamente }) : super(key: key);
final String surface; final double radius; final double latitude; final double longitude; final Alignment alignment; final Offset positionOffset; // Novo parâmetro para controlar a posição
@override _SphereState createState() => _SphereState(); }
class _SphereState extends State { Uint32List? surface; double surfaceWidth = 0; double surfaceHeight = 0;
@override void initState() { super.initState(); _loadSurface(); }
void _loadSurface() { // Faça o carregamento da superfície apenas uma vez e armazene para todas as esferas if (surface == null) { rootBundle.load(widget.surface).then((data) { ui.decodeImageFromList(data.buffer.asUint8List(), (image) { image.toByteData(format: ui.ImageByteFormat.rawRgba).then((pixels) { surface = pixels!.buffer.asUint32List(); surfaceWidth = image.width.toDouble(); surfaceHeight = image.height.toDouble(); setState(() {}); // Redesenhar a esfera ao carregar a textura }); }); }); } }
Future<SphereImage?> _buildSphere(double maxWidth, double maxHeight) async { if (surface == null) return null;
final double rotationX = widget.latitude * math.pi / 180; final double rotationZ = widget.longitude * math.pi / 180; final double r = widget.radius; final minX = math.max(-r, (-1 - widget.alignment.x) * maxWidth / 2); final minY = math.max(-r, (-1 + widget.alignment.y) * maxHeight / 2); final maxX = math.min(r, (1 - widget.alignment.x) * maxWidth / 2); final maxY = math.min(r, (1 + widget.alignment.y) * maxHeight / 2); final width = maxX - minX; final height = maxY - minY; if (width <= 0 || height <= 0) return null; final sphere = Uint32List(width.toInt() * height.toInt()); var angle = math.pi / 2 - rotationX; final sinx = math.sin(angle); final cosx = math.cos(angle); angle = rotationZ + math.pi / 2; final sinz = math.sin(angle); final cosz = math.cos(angle); final surfaceXRate = (surfaceWidth - 1) / (2.0 * math.pi); final surfaceYRate = (surfaceHeight - 1) / (math.pi); for (var y = minY; y < maxY; y++) { final sphereY = (height - y + minY - 1).toInt() * width; for (var x = minX; x < maxX; x++) { var z = r * r - x * x - y * y; if (z > 0) { z = math.sqrt(z); var x1 = x, y1 = y, z1 = z; double x2, y2, z2; y2 = y1 * cosx - z1 * sinx; z2 = y1 * sinx + z1 * cosx; y1 = y2; z1 = z2; x2 = x1 * cosz - y1 * sinz; y2 = x1 * sinz + y1 * cosz; x1 = x2; y1 = y2; final lat = math.asin(z1 / r); final lon = math.atan2(y1, x1); final x0 = (lon + math.pi) * surfaceXRate; final y0 = (math.pi / 2 - lat) * surfaceYRate; final color = surface![(y0.toInt() * surfaceWidth + x0).toInt()]; sphere[(sphereY + x - minX).toInt()] = color; } } } final completer = Completer<SphereImage>(); ui.decodeImageFromPixels( sphere.buffer.asUint8List(), width.toInt(), height.toInt(), ui.PixelFormat.rgba8888, (image) { final sphereImage = SphereImage( image: image, radius: r, origin: Offset(-minX, -minY), offset: Offset( (widget.alignment.x + 1) * maxWidth / 2, (widget.alignment.y + 1) * maxHeight / 2, ) + widget.positionOffset, ); completer.complete(sphereImage); }, ); return completer.future; - Assistant