logo

G

  • Tutorials
  • API
  • Examples
  • Plugins
  • Productsantv logo arrow
  • 6.1.26
  • Getting Started
  • Section I - Defining the Scenario
  • Section II - Using the renderer
  • Section III - Adding some interaction
  • Diving Deeper
    • Scene Graph
    • 创造一个“太阳系”
    • 使用相机
    • 使用插件
    • 实现一个简单的动画
    • GPGPU 初体验
    • 进入 3D 世界
    • 使用 Box2D 物理引擎
    • 使用 matter.js 物理引擎
    • 使用 Yoga 布局引擎
    • Takeover D3's rendering
    • Takeover Observable Plot's rendering
    • Choose a renderer
    • Rendering on demand
  • Advanced Topics
    • 性能优化
    • 自定义图形
    • 理解事件传播路径
    • Using react-g
    • Exporting the contents of the canvas
    • Using lite version

进入 3D 世界

Previous
GPGPU 初体验
Next
使用 Box2D 物理引擎

Resource

Ant Design
Galacea Effects
Umi-React Application Framework
Dumi-Component doc generator
ahooks-React Hooks Library

Community

Ant Financial Experience Tech
seeconfSEE Conf-Experience Tech Conference

Help

GitHub
StackOverflow

more productsMore Productions

Ant DesignAnt Design-Enterprise UI design language
yuqueYuque-Knowledge creation and Sharing tool
EggEgg-Enterprise-class Node development framework
kitchenKitchen-Sketch Tool set
GalaceanGalacean-Interactive solution
xtechLiven Experience technology
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

通过 g-plugin-3d 插件的支持,我们可以绘制 3D 图形,当然渲染器必须指定为 g-webgl。

示例

注册 3D 插件

创建画布和渲染器与之前的教程完全一致,注册 g-plugin-3d 插件

import { Canvas, CanvasEvent } from '@antv/g';
import { Renderer } from '@antv/g-webgl';
import { Plugin as Plugin3D } from '@antv/g-plugin-3d';
// create a renderer
const renderer = new Renderer();
renderer.registerPlugin(new Plugin3D());
// create a canvas
const canvas = new Canvas({
container: 'container',
width: 600,
height: 500,
renderer,
});

获取 GPU Device

在创建 3D 图形时,需要使用 材质 和 几何,它们都需要使用 GPU 底层资源(Buffer 和 Texture),创建时需要获取 GPU Device:

(async () => {
// wait for canvas' initialization complete
await canvas.ready;
// use GPU device
const plugin = renderer.getPlugin('device-renderer');
const device = plugin.getDevice();
})();

创建几何、材质和 Mesh

不同于各种各样的 2D 图形(Circle、Rect),3D 图形使用 Mesh(三角网格)描述,它的形状由 几何 定义,外观样式由 材质 定义。例如这里我们使用 CubeGeometry 和 MeshBasicMaterial:

import { MeshBasicMaterial, CubeGeometry, Mesh } from '@antv/g-plugin-3d';
// 立方体几何
const cubeGeometry = new CubeGeometry(device, {
width: 200,
height: 200,
depth: 200,
});
// 基础材质
const basicMaterial = new MeshBasicMaterial(device);
const cube = new Mesh({
style: {
fill: '#1890FF',
opacity: 1,
geometry: cubeGeometry,
material: basicMaterial,
},
});

加入画布

创建好的 Mesh 和 2D 基础图形一样,可以进行变换。例如我们使用 setPosition 设置它的全局坐标:

cube.setPosition(300, 250, 0);
canvas.appendChild(cube);