como posso fazer vector3(0,0,0), ou vector3.zero(), ser referente ao meio da tela? class…
- User
como posso fazer vector3(0,0,0), ou vector3.zero(), ser referente ao meio da tela?
class SpherePainter extends CustomPainter { final ui.Image texture; BuildContext context;
Vector3 position = Vector3.zero(); final Vector3 scale = Vector3(1, 1, 1); Vector3 rotation = Vector3.zero();
Quaternion rotationQuat = Quaternion.identity();
// Geometria da esfera static List staticVertices = []; static List staticTexcoords = []; static List<List> staticIndices = []; static bool isSphereGenerated = false;
final Matrix4 transform = Matrix4.identity(); final double radius = 12; final int latSegments = 10; // Ajustado para melhor performance final int lonSegments = 10;
SpherePainter(this.texture, this.position, this.context) { if (!isSphereGenerated) { _generateSphere(); isSphereGenerated = true; } }
void _generateSphere() { 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; staticVertices.add(Vector3( radius * math.cos(u * math.pi * 2.0) * sv, // X radius * cv, // Y radius * math.sin(u * math.pi * 2.0) * sv, // Z )); staticTexcoords.add(Offset(u, v)); // Coordenadas UV normalizadas } }
code// Preenche os índices dos triângulos com ordem anti-horária consistente for (int y = 0; y < latSegments; ++y) { for (int x = 0; x < lonSegments; ++x) { final int current = (lonSegments + 1) * y + x; final int next = current + 1; final int below = (lonSegments + 1) * (y + 1) + x; final int belowNext = below + 1; staticIndices .add([current, below, next]); // Triângulo inferior esquerdo staticIndices .add([next, below, belowNext]); // Triângulo superior direito } }}
void rotate(Vector3 axis, double angleRadians) { // Atualiza o quaternion de rotação, mas não aplica a transformação ainda Quaternion increment = Quaternion.axisAngle(axis, angleRadians); rotationQuat = increment * rotationQuat; }
void updateTransform(BuildContext context) { // Aplica a transformação baseada na rotação (rotationQuat), posição e escala final Matrix4 m = Matrix4.compose(position, rotationQuat, scale); transform.setFrom(m);
}
@override void paint(Canvas canvas, Size size) { //canvas.translate(size.width / 2, size.height / 2); //rotate(Vector3(math.sin(-rotateAngle), math.cos(rotateAngle), 0), angle);
- Assistant