vector_math 패키지는 그래픽, 애니메이션 및 게임 개발을 위한 2D 및 3D 벡터, 행렬, 사원수 연산의 포괄적인 세트를 제공합니다. 구글에서 개발한 이 패키지는 여러 플랫폼에서 Flutter 애플리케이션에 최적화된 고성능 수학 유틸리티를 제공합니다. 데스크톱, 모바일 등 다양한 환경에서 점곱, 외적, 변환, 회전과 같은 기본 기하 계산을 지원하여 공간 데이터를 효율적으로 처리할 수 있도록 합니다. 가볍고 잘 문서화되어 있으며, Flutter의 렌더링 파이프라인과 원활하게 통합됩니다.
사용 사례
2D/3D 그래픽 렌더링
게임 개발
애니메이션 시스템
공간 변환
물리 시뮬레이션
카메라 및 객체 위치 지정
주요 기능
2차 및 3차 벡터 및 행렬 지원
3D 회전을 위한 사원수 지원
고성능 수학 연산
크로스 플랫폼 호환성
깔끔하고 직관적인 API 설계
내장된 변환 유틸리티
적합한 대상
게임 개발 중인 Flutter 개발자
정확한 위치 지정이 필요한 UI 애니메이션
증강현실 애플리케이션
그래픽 집약형 앱
수학 집약적인 앱 로직
고려 사항
일반적인 수학용으로 의도되지 않음
선형 대수에 대한 이해가 필요합니다
기하 연산에 제한됨
내장된 직렬화 없음
다른 수학 또는 그래픽 패키지와 함께 사용하는 것이 가장 좋습니다
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
Introduction
A Vector math library for 2D and 3D applications.
Features
2D, 3D, and 4D vector and matrix types.
Quaternion type for animating rotations.
Collision detection: AABB, rays, spheres, ...
Utilities like color and common rendering related operations
Flexible getters and setters, for example, position.xwz = color.grb;.
import'package:vector_math/vector_math.dart';
voidmain() {
Vector3 x = Vector3.zero(); // Zero vectorVector4 y = Vector4.all(4.0); // Vector with 4.0 in all lanes
x.zyx = y.xzz; // Sets z,y,x the values in x,z,z
}
2. Transforming a vector.
import'dart:math';
import'package:vector_math/vector_math.dart';
voidmain() {
// Rotation of PI/2 degrees around the Y axis followed by a// translation of (5.0, 2.0, 3.0).Matrix4 T = Matrix4.rotationY(PI * 0.5)..translate(5.0, 2.0, 3.0);
// A point.Vector3 position = Vector3(1.0, 1.0, 1.0);
// Transform position by T.
T.transform3(position);
}
3. Invert a matrix
import'dart:math';
import'package:vector_math/vector_math.dart';
voidmain() {
// Rotation of 90 degrees around the Y axis followed by a// translation of (5.0, 2.0, 3.0).Matrix4 T = Matrix4.rotationY(PI * 0.5)..translate(5.0, 2.0, 3.0);
// Invert T.
T.invert();
// Invert just the rotation in T.
T.invertRotation();
}
4. Rotate a vector using a quaternion
import'dart:math';
import'package:vector_math/vector_math.dart';
voidmain() {
// The X axis.Vector3 axis = Vector3(1.0, 0.0, 0.0);
// 90 degrees.double angle = PI / 2.0;
// Quaternion encoding a 90 degree rotation along the X axis.Quaternion q = Quaternion.axisAngle(axis, angle);
// A point.Vector3 point = Vector3(1.0, 1.0, 1.0);
// Rotate point by q.
q.rotate(point);
}
5. Check if two axis aligned bounding boxes intersect
import'package:vector_math/vector_math.dart';
voidmain() {
// Define the first box with a minimum and a maximum.Aabb2 aabbOne = Aabb2.minMax(Vector2.zero(), Vector2(4.0, 4.0));
// Define the second boxAabb2 aabbTwo = Aabb2.minMax(Vector2(5.0, 5.0), Vector2(6.0, 6.0));
// Extend the second box to contain a point
aabbTwo.hullPoint(Vector2(3.0, 3.0));
// Check if the two boxes intersect, returns true in this case.bool intersect = aabbOne.intersectsWithAabb2(aabbTwo);
}
6. Check where a ray and a sphere intersect
import'package:vector_math/vector_math.dart';
voidmain() {
// Define a ray starting at the origin and going into positive x-direction.Ray ray = Ray.originDirection(Vector3.zero(), Vector3(1.0, 0.0, 0.0));
// Defines a sphere with the center (5.0 0.0 0.0) and a radius of 2.Sphere sphere = Sphere.centerRadius(Vector3(5.0, 0.0, 0.0), 2.0);
// Checks if the ray intersect with the sphere and returns the distance of the// intersection from the origin of the ray. Would return null if no intersection// is found.double distanceFromOrigin = ray.intersectsWithSphere(sphere);
// Evaluate the position of the intersection, in this case (3.0 0.0 0.0).Vector3 position = ray.at(distanceFromOrigin);
}
7. Work with colors
import'package:vector_math/vector_math.dart';
voidmain() {
// Access a build-in color, colors are stored in 4-dimensional vectors.Vector4 red = Colors.red;
Vector4 gray = Vector4.zero();
// Convert the red color to a grayscaled color.Colors.toGrayscale(red, gray);
// Parse a blue color from a hex string.Vector4 blue = Vector4.zero();
Colors.fromHexString('#0000FF', blue);
// Convert the blue color from RGB to HSL.Colors.rgbToHsl(blue, blue);
// Reduce the lightness of the color by 50%.
blue.z *= 0.5;
// Convert the HSL color back to RGB.Colors.hslToRgb(blue, blue);
}
8. Use 64bit float precision (instead of 32bit)
// Use different import statement.// Which is a drop-in replacement for 'package:vector_math/vector_math.dart'import'package:vector_math/vector_math_64.dart';
voidmain() {
// Types work the same, but using 64 bit storage.Vector3 x = Vector3.zero();
Matrix4 m = Matrix4.identity();
}
Development
To run the unit tests:
~/src/vector_math/> dart test
To automatically generate the latest version of vector_math_64: