por que a renderização do objeto resulta em uma posição diferente em ambos os códigos,…
- User
por que a renderização do objeto resulta em uma posição diferente em ambos os códigos, no caso, o primeiro código deveria resultar na mesma posição do segundo:
primeiro código:
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 = 7; 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 } }
// 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);
// Matriz de translação para centralizar o objeto no meio da tela Matrix4 centralizeMatrix = Matrix4.translationValues( MediaQuery.of(context).size.width / 2, MediaQuery.of(context).size.height / 2, 0); // Matrix4 centralizeMatrix = Matrix4.translationValues(1000 / 2, 1000 / 2, 0); // Configura a matriz de transformação do objeto final Matrix4 objectTransform = Matrix4.compose(position, rotationQuat, scale); // Aplica a centralização na matriz do objeto transform.setFrom(centralizeMatrix * objectTransform); - Assistant