搜索插件、代码仓与 APP 分类

插件详情v2.4.4

vector_math

用于 2D 和 3D 应用的向量数学库。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1260万
喜欢数
382
Pub 分数
150

安装插件

flutter pub add vector_math

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

vector_math 包提供了一套全面的 2D 和 3D 向量、矩阵和四元数操作,专为图形、动画和游戏开发而设计。由 Google 开发,该库提供了高性能的数学工具,针对跨多个平台的 Flutter 应用进行了优化。该库支持核心几何计算,如点积、叉积、变换和旋转,使其成为高效处理空间数据的理想选择。它轻量级、文档完善,并且可无缝集成到 Flutter 的渲染管道中。

适合场景

  • 2D/3D 图形渲染
  • 游戏开发
  • 动画系统
  • 空间变换
  • 物理模拟
  • 相机和对象定位

主要特点

  • 对二维和三维向量及矩阵的支持
  • 支持3D旋转的四元数
  • 高性能数学运算
  • 跨平台兼容性
  • 简洁直观的 API 设计
  • 内置的转换工具

适合对象

  • Flutter 开发者正在开发游戏
  • UI动画需要精确的位置定位
  • 增强现实应用
  • 图形密集型应用
  • 数学密集型应用逻辑

使用建议

  • 不适用于通用数学
  • 需要理解线性代数
  • 限于几何操作
  • 无内置序列化
  • 最好与其他数学或图形包一起使用

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Dart pub package package publisher Coverage Status

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;.
  • Fully documented.
  • Well tested.
  • Heavily optimized.

Libraries using vector_math

Examples

1. Using the GLSL getter and setter syntax.

import 'package:vector_math/vector_math.dart';

void main() {
  Vector3 x = Vector3.zero(); // Zero vector
  Vector4 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';

void main() {
  // 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';

void main() {
  // 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';

void main() {
  // 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';

void main() {
  // Define the first box with a minimum and a maximum.
  Aabb2 aabbOne = Aabb2.minMax(Vector2.zero(), Vector2(4.0, 4.0));
  // Define the second box
  Aabb2 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';

void main() {
  // 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';

void main() {
  // 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';
void main() {
  // 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:

~/src/vector_math/> dart tool/generate_vector_math_64.dart

所属分类

源码仓库

使用此插件的项目