playcanvas-engine by freshtechbro/claudedesignskills
npx skills add https://github.com/freshtechbro/claudedesignskills --skill playcanvas-engine轻量级 WebGL/WebGPU 游戏引擎,采用实体-组件架构,集成了可视化编辑器,并专注于性能优化设计。
当您遇到以下情况时触发此技能:
对比:
根 PlayCanvas 应用程序管理渲染循环。
import * as pc from 'playcanvas';
// 创建画布
const canvas = document.createElement('canvas');
document.body.appendChild(canvas);
// 创建应用程序
const app = new pc.Application(canvas, {
keyboard: new pc.Keyboard(window),
mouse: new pc.Mouse(canvas),
touch: new pc.TouchDevice(canvas),
gamepads: new pc.GamePads()
});
// 配置画布
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
app.setCanvasResolution(pc.RESOLUTION_AUTO);
// 处理调整大小
window.addEventListener('resize', () => app.resizeCanvas());
// 启动应用程序
app.start();
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
PlayCanvas 使用 ECS 架构:实体包含组件。
// 创建实体
const entity = new pc.Entity('myEntity');
// 添加到场景层次结构
app.root.addChild(entity);
// 添加组件
entity.addComponent('model', {
type: 'box'
});
entity.addComponent('script');
// 变换
entity.setPosition(0, 1, 0);
entity.setEulerAngles(0, 45, 0);
entity.setLocalScale(2, 2, 2);
// 父子层次结构
const parent = new pc.Entity('parent');
const child = new pc.Entity('child');
parent.addChild(child);
应用程序在更新循环期间触发事件。
app.on('update', (dt) => {
// dt 是以秒为单位的增量时间
entity.rotate(0, 10 * dt, 0);
});
app.on('prerender', () => {
// 渲染前
});
app.on('postrender', () => {
// 渲染后
});
核心组件扩展实体功能:
模型组件 :
entity.addComponent('model', {
type: 'box', // 'box', 'sphere', 'cylinder', 'cone', 'capsule', 'asset'
material: material,
castShadows: true,
receiveShadows: true
});
相机组件 :
entity.addComponent('camera', {
clearColor: new pc.Color(0.1, 0.2, 0.3),
fov: 45,
nearClip: 0.1,
farClip: 1000,
projection: pc.PROJECTION_PERSPECTIVE // 或 PROJECTION_ORTHOGRAPHIC
});
光照组件 :
entity.addComponent('light', {
type: pc.LIGHTTYPE_DIRECTIONAL, // DIRECTIONAL, POINT, SPOT
color: new pc.Color(1, 1, 1),
intensity: 1,
castShadows: true,
shadowDistance: 50
});
刚体组件 (需要物理引擎):
entity.addComponent('rigidbody', {
type: pc.BODYTYPE_DYNAMIC, // STATIC, DYNAMIC, KINEMATIC
mass: 1,
friction: 0.5,
restitution: 0.3
});
entity.addComponent('collision', {
type: 'box',
halfExtents: new pc.Vec3(0.5, 0.5, 0.5)
});
创建包含相机、光照和模型的完整场景。
import * as pc from 'playcanvas';
// 初始化应用程序
const canvas = document.createElement('canvas');
document.body.appendChild(canvas);
const app = new pc.Application(canvas);
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
app.setCanvasResolution(pc.RESOLUTION_AUTO);
window.addEventListener('resize', () => app.resizeCanvas());
// 创建相机
const camera = new pc.Entity('camera');
camera.addComponent('camera', {
clearColor: new pc.Color(0.2, 0.3, 0.4)
});
camera.setPosition(0, 2, 5);
camera.lookAt(0, 0, 0);
app.root.addChild(camera);
// 创建方向光
const light = new pc.Entity('light');
light.addComponent('light', {
type: pc.LIGHTTYPE_DIRECTIONAL,
castShadows: true
});
light.setEulerAngles(45, 30, 0);
app.root.addChild(light);
// 创建地面
const ground = new pc.Entity('ground');
ground.addComponent('model', {
type: 'plane'
});
ground.setLocalScale(10, 1, 10);
app.root.addChild(ground);
// 创建立方体
const cube = new pc.Entity('cube');
cube.addComponent('model', {
type: 'box',
castShadows: true
});
cube.setPosition(0, 1, 0);
app.root.addChild(cube);
// 动画立方体
app.on('update', (dt) => {
cube.rotate(10 * dt, 20 * dt, 30 * dt);
});
app.start();
使用资产管理加载外部 3D 模型。
// 为模型创建资产
const modelAsset = new pc.Asset('model', 'container', {
url: '/models/character.glb'
});
// 添加到资产注册表
app.assets.add(modelAsset);
// 加载资产
modelAsset.ready((asset) => {
// 从加载的模型创建实体
const entity = asset.resource.instantiateRenderEntity();
app.root.addChild(entity);
// 缩放和定位
entity.setLocalScale(2, 2, 2);
entity.setPosition(0, 0, 0);
});
app.assets.load(modelAsset);
包含错误处理 :
modelAsset.ready((asset) => {
console.log('模型已加载:', asset.name);
const entity = asset.resource.instantiateRenderEntity();
app.root.addChild(entity);
});
modelAsset.on('error', (err) => {
console.error('加载模型失败:', err);
});
app.assets.load(modelAsset);
使用 PBR 工作流创建自定义材质。
// 创建材质
const material = new pc.StandardMaterial();
material.diffuse = new pc.Color(1, 0, 0); // 红色
material.metalness = 0.5;
material.gloss = 0.8;
material.update();
// 应用到实体
entity.model.material = material;
// 使用纹理
const textureAsset = new pc.Asset('diffuse', 'texture', {
url: '/textures/brick_diffuse.jpg'
});
app.assets.add(textureAsset);
app.assets.load(textureAsset);
textureAsset.ready((asset) => {
material.diffuseMap = asset.resource;
material.update();
});
// 包含所有贴图的 PBR 材质
const pbrMaterial = new pc.StandardMaterial();
// 加载所有纹理
const textures = {
diffuse: '/textures/albedo.jpg',
normal: '/textures/normal.jpg',
metalness: '/textures/metalness.jpg',
gloss: '/textures/roughness.jpg',
ao: '/textures/ao.jpg'
};
Object.keys(textures).forEach(key => {
const asset = new pc.Asset(key, 'texture', { url: textures[key] });
app.assets.add(asset);
asset.ready((loadedAsset) => {
switch(key) {
case 'diffuse':
pbrMaterial.diffuseMap = loadedAsset.resource;
break;
case 'normal':
pbrMaterial.normalMap = loadedAsset.resource;
break;
case 'metalness':
pbrMaterial.metalnessMap = loadedAsset.resource;
break;
case 'gloss':
pbrMaterial.glossMap = loadedAsset.resource;
break;
case 'ao':
pbrMaterial.aoMap = loadedAsset.resource;
break;
}
pbrMaterial.update();
});
app.assets.load(asset);
});
使用 Ammo.js 进行物理模拟。
import * as pc from 'playcanvas';
// 使用 Ammo.js 初始化
const app = new pc.Application(canvas, {
keyboard: new pc.Keyboard(window),
mouse: new pc.Mouse(canvas)
});
// 加载 Ammo.js
const ammoScript = document.createElement('script');
ammoScript.src = 'https://cdn.jsdelivr.net/npm/ammo.js@0.0.10/ammo.js';
document.body.appendChild(ammoScript);
ammoScript.onload = () => {
Ammo().then((AmmoLib) => {
window.Ammo = AmmoLib;
// 创建静态地面
const ground = new pc.Entity('ground');
ground.addComponent('model', { type: 'plane' });
ground.setLocalScale(10, 1, 10);
ground.addComponent('rigidbody', {
type: pc.BODYTYPE_STATIC
});
ground.addComponent('collision', {
type: 'box',
halfExtents: new pc.Vec3(5, 0.1, 5)
});
app.root.addChild(ground);
// 创建动态立方体
const cube = new pc.Entity('cube');
cube.addComponent('model', { type: 'box' });
cube.setPosition(0, 5, 0);
cube.addComponent('rigidbody', {
type: pc.BODYTYPE_DYNAMIC,
mass: 1,
friction: 0.5,
restitution: 0.5
});
cube.addComponent('collision', {
type: 'box',
halfExtents: new pc.Vec3(0.5, 0.5, 0.5)
});
app.root.addChild(cube);
// 施加力
cube.rigidbody.applyForce(10, 0, 0);
cube.rigidbody.applyTorque(0, 10, 0);
app.start();
});
};
创建可重用的脚本组件。
// 定义脚本类
const RotateScript = pc.createScript('rotate');
// 脚本属性(在编辑器中暴露)
RotateScript.attributes.add('speed', {
type: 'number',
default: 10,
title: '旋转速度'
});
RotateScript.attributes.add('axis', {
type: 'vec3',
default: [0, 1, 0],
title: '旋转轴'
});
// 初始化方法
RotateScript.prototype.initialize = function() {
console.log('RotateScript 已初始化');
};
// 更新方法(每帧调用)
RotateScript.prototype.update = function(dt) {
this.entity.rotate(
this.axis.x * this.speed * dt,
this.axis.y * this.speed * dt,
this.axis.z * this.speed * dt
);
};
// 清理
RotateScript.prototype.destroy = function() {
console.log('RotateScript 已销毁');
};
// 用法
const entity = new pc.Entity('rotatingCube');
entity.addComponent('model', { type: 'box' });
entity.addComponent('script');
entity.script.create('rotate', {
attributes: {
speed: 20,
axis: new pc.Vec3(0, 1, 0)
}
});
app.root.addChild(entity);
脚本生命周期方法 :
const MyScript = pc.createScript('myScript');
MyScript.prototype.initialize = function() {
// 所有资源加载完成后调用一次
};
MyScript.prototype.postInitialize = function() {
// 所有实体初始化后调用
};
MyScript.prototype.update = function(dt) {
// 每帧渲染前调用
};
MyScript.prototype.postUpdate = function(dt) {
// 每帧更新后调用
};
MyScript.prototype.swap = function(old) {
// 热重载支持
};
MyScript.prototype.destroy = function() {
// 实体销毁时清理
};
处理键盘、鼠标和触摸输入。
// 键盘
if (app.keyboard.isPressed(pc.KEY_W)) {
entity.translate(0, 0, -speed * dt);
}
if (app.keyboard.wasPressed(pc.KEY_SPACE)) {
entity.rigidbody.applyImpulse(0, 10, 0);
}
// 鼠标
app.mouse.on(pc.EVENT_MOUSEDOWN, (event) => {
if (event.button === pc.MOUSEBUTTON_LEFT) {
console.log('左键点击位置', event.x, event.y);
}
});
app.mouse.on(pc.EVENT_MOUSEMOVE, (event) => {
const dx = event.dx;
const dy = event.dy;
camera.rotate(-dy * 0.2, -dx * 0.2, 0);
});
// 触摸
app.touch.on(pc.EVENT_TOUCHSTART, (event) => {
event.touches.forEach((touch) => {
console.log('触摸位置', touch.x, touch.y);
});
});
// 射线投射(鼠标拾取)
app.mouse.on(pc.EVENT_MOUSEDOWN, (event) => {
const camera = app.root.findByName('camera');
const cameraComponent = camera.camera;
const from = cameraComponent.screenToWorld(
event.x,
event.y,
cameraComponent.nearClip
);
const to = cameraComponent.screenToWorld(
event.x,
event.y,
cameraComponent.farClip
);
const result = app.systems.rigidbody.raycastFirst(from, to);
if (result) {
console.log('命中:', result.entity.name);
result.entity.model.material.emissive = new pc.Color(1, 0, 0);
}
});
播放骨骼动画和补间动画。
骨骼动画 :
// 加载动画模型
const modelAsset = new pc.Asset('character', 'container', {
url: '/models/character.glb'
});
app.assets.add(modelAsset);
modelAsset.ready((asset) => {
const entity = asset.resource.instantiateRenderEntity();
app.root.addChild(entity);
// 获取动画组件
entity.addComponent('animation', {
assets: [asset],
speed: 1.0,
loop: true,
activate: true
});
// 播放特定动画
entity.animation.play('Walk', 0.2); // 0.2秒混合时间
// 稍后,过渡到跑步
entity.animation.play('Run', 0.5);
});
app.assets.load(modelAsset);
属性补间 :
// 动画化位置
entity.tween(entity.getLocalPosition())
.to({ x: 5, y: 2, z: 0 }, 2.0, pc.SineInOut)
.start();
// 动画化旋转
entity.tween(entity.getLocalEulerAngles())
.to({ x: 0, y: 180, z: 0 }, 1.0, pc.Linear)
.loop(true)
.yoyo(true)
.start();
// 动画化材质颜色
const color = material.emissive;
app.tween(color)
.to(new pc.Color(1, 0, 0), 1.0, pc.SineInOut)
.yoyo(true)
.loop(true)
.start();
// 链式补间
entity.tween(entity.getLocalPosition())
.to({ y: 2 }, 1.0)
.to({ y: 0 }, 1.0)
.delay(0.5)
.repeat(3)
.start();
将 PlayCanvas 包装在 React 组件中。
import React, { useEffect, useRef } from 'react';
import * as pc from 'playcanvas';
function PlayCanvasScene() {
const canvasRef = useRef(null);
const appRef = useRef(null);
useEffect(() => {
// 初始化
const app = new pc.Application(canvasRef.current);
appRef.current = app;
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
app.setCanvasResolution(pc.RESOLUTION_AUTO);
// 创建场景
const camera = new pc.Entity('camera');
camera.addComponent('camera', {
clearColor: new pc.Color(0.1, 0.2, 0.3)
});
camera.setPosition(0, 0, 5);
app.root.addChild(camera);
const cube = new pc.Entity('cube');
cube.addComponent('model', { type: 'box' });
app.root.addChild(cube);
const light = new pc.Entity('light');
light.addComponent('light');
light.setEulerAngles(45, 0, 0);
app.root.addChild(light);
app.on('update', (dt) => {
cube.rotate(10 * dt, 20 * dt, 30 * dt);
});
app.start();
// 清理
return () => {
app.destroy();
};
}, []);
return (
<canvas
ref={canvasRef}
style={{ width: '100%', height: '100vh' }}
/>
);
}
export default PlayCanvasScene;
使用 PlayCanvas 编辑器项目。
// 从 PlayCanvas 编辑器导出
// 下载构建文件,然后在代码中加载:
import * as pc from 'playcanvas';
const app = new pc.Application(canvas);
// 加载导出的项目配置
fetch('/config.json')
.then(response => response.json())
.then(config => {
// 加载场景
app.scenes.loadSceneHierarchy(config.scene_url, (err, parent) => {
if (err) {
console.error('加载场景失败:', err);
return;
}
// 启动应用程序
app.start();
// 按名称查找实体
const player = app.root.findByName('Player');
const enemy = app.root.findByName('Enemy');
// 访问脚本
player.script.myScript.doSomething();
});
});
重用实体而不是创建/销毁。
class EntityPool {
constructor(app, count) {
this.app = app;
this.pool = [];
this.active = [];
for (let i = 0; i < count; i++) {
const entity = new pc.Entity('pooled');
entity.addComponent('model', { type: 'box' });
entity.enabled = false;
app.root.addChild(entity);
this.pool.push(entity);
}
}
spawn(position) {
let entity = this.pool.pop();
if (!entity) {
// 池已耗尽,创建新的
entity = new pc.Entity('pooled');
entity.addComponent('model', { type: 'box' });
this.app.root.addChild(entity);
}
entity.enabled = true;
entity.setPosition(position);
this.active.push(entity);
return entity;
}
despawn(entity) {
entity.enabled = false;
const index = this.active.indexOf(entity);
if (index > -1) {
this.active.splice(index, 1);
this.pool.push(entity);
}
}
}
// 用法
const pool = new EntityPool(app, 100);
const bullet = pool.spawn(new pc.Vec3(0, 0, 0));
// 稍后
pool.despawn(bullet);
减少远处对象的几何体复杂度。
// 手动 LOD 切换
app.on('update', () => {
const distance = camera.getPosition().distance(entity.getPosition());
if (distance < 10) {
entity.model.asset = highResModel;
} else if (distance < 50) {
entity.model.asset = mediumResModel;
} else {
entity.model.asset = lowResModel;
}
});
// 或者禁用远处的实体
app.on('update', () => {
entities.forEach(entity => {
const distance = camera.getPosition().distance(entity.getPosition());
entity.enabled = distance < 100;
});
});
组合静态网格以减少绘制调用。
// 为实体启用静态批处理
entity.model.batchGroupId = 1;
// 批处理具有相同组 ID 的所有实体
app.batcher.generate([entity1, entity2, entity3]);
使用压缩的纹理格式。
// 创建纹理时,使用压缩格式
const texture = new pc.Texture(app.graphicsDevice, {
width: 512,
height: 512,
format: pc.PIXELFORMAT_DXT5, // GPU 压缩
minFilter: pc.FILTER_LINEAR_MIPMAP_LINEAR,
magFilter: pc.FILTER_LINEAR,
mipmaps: true
});
问题 : 场景渲染但没有任何反应。
// ❌ 错误 - 忘记启动
const app = new pc.Application(canvas);
// ... 创建实体 ...
// 什么都没发生!
// ✅ 正确
const app = new pc.Application(canvas);
// ... 创建实体 ...
app.start(); // 关键!
问题 : 在迭代期间修改场景图。
// ❌ 错误 - 在迭代期间修改数组
app.on('update', () => {
entities.forEach(entity => {
if (entity.shouldDestroy) {
entity.destroy(); // 修改数组!
}
});
});
// ✅ 正确 - 标记为删除,之后清理
const toDestroy = [];
app.on('update', () => {
entities.forEach(entity => {
if (entity.shouldDestroy) {
toDestroy.push(entity);
}
});
});
app.on('postUpdate', () => {
toDestroy.forEach(entity => entity.destroy());
toDestroy.length = 0;
});
问题 : 未清理已加载的资产。
// ❌ 错误 - 资产从未清理
function loadModel() {
const asset = new pc.Asset('model', 'container', { url: '/model.glb' });
app.assets.add(asset);
app.assets.load(asset);
// 资产永远留在内存中
}
// ✅ 正确 - 完成后清理
function loadModel() {
const asset = new pc.Asset('model', 'container', { url: '/model.glb' });
app.assets.add(asset);
asset.ready(() => {
// 使用模型
});
app.assets.load(asset);
// 稍后清理
return () => {
app.assets.remove(asset);
asset.unload();
};
}
const cleanup = loadModel();
// 稍后: cleanup();
问题 : 变换未正确传播。
// ❌ 错误 - 在子节点上设置世界变换
const parent = new pc.Entity();
const child = new pc.Entity();
parent.addChild(child);
child.setPosition(5, 0, 0); // 局部位置
parent.setPosition(10, 0, 0);
// 子节点在世界空间中的位置是 (15, 0, 0)
// ✅ 正确 - 理解局部与世界
child.setLocalPosition(5, 0, 0); // 显式局部
// 或
const worldPos = new pc.Vec3(15, 0, 0);
child.setPosition(worldPos); // 显式世界
问题 : 物理组件不工作。
// ❌ 错误 - Ammo.js 未加载
const entity = new pc.Entity();
entity.addComponent('rigidbody', { type: pc.BODYTYPE_DYNAMIC });
// 错误: Ammo 未定义
// ✅ 正确 - 确保 Ammo.js 已加载
const script = document.createElement('script');
script.src = 'https://cdn.jsdelivr.net/npm/ammo.js@0.0.10/ammo.js';
document.body.appendChild(script);
script.onload = () => {
Ammo().then((AmmoLib) => {
window.Ammo = AmmoLib;
// 现在物理生效
const entity = new pc.Entity();
entity.addComponent('rigidbody', { type: pc.BODYTYPE_DYNAMIC });
entity.addComponent('collision', { type: 'box' });
});
};
问题 : 画布不填充容器或不响应调整大小。
// ❌ 错误 - 固定尺寸画布
const canvas = document.createElement('canvas');
canvas.width = 800;
canvas.height = 600;
// ✅ 正确 - 响应式画布
const canvas = document.createElement('canvas');
const app = new pc.Application(canvas);
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
app.setCanvasResolution(pc.RESOLUTION_AUTO);
window.addEventListener('resize', () => app.resizeCanvas());
const app = new pc.Application(canvas);
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
app.setCanvasResolution(pc.RESOLUTION_AUTO);
app.start();
const entity = new pc.Entity('name');
entity.addComponent('model', { type: 'box' });
entity.setPosition(x, y, z);
app.root.addChild(entity);
app.on('update', (dt) => {
// 逻辑在此处
});
const asset = new pc.Asset('name', 'type', { url: '/path' });
app.assets.add(asset);
asset.ready(() => { /* 使用资产 */ });
app.assets.load(asset);
相关技能 : 对于更底层的 WebGL 控制,请参考 threejs-webgl。对于 React 集成模式,请参见 react-three-fiber。对于物理密集型模拟,请参考 babylonjs-engine。
每周安装数
85
代码仓库
GitHub 星标数
11
首次出现
2026年2月27日
安全审计
安装于
opencode85
github-copilot83
codex83
amp83
cline83
kimi-cli83
Lightweight WebGL/WebGPU game engine with entity-component architecture, visual editor integration, and performance-focused design.
Trigger this skill when you see:
Compare with:
The root PlayCanvas application manages the rendering loop.
import * as pc from 'playcanvas';
// Create canvas
const canvas = document.createElement('canvas');
document.body.appendChild(canvas);
// Create application
const app = new pc.Application(canvas, {
keyboard: new pc.Keyboard(window),
mouse: new pc.Mouse(canvas),
touch: new pc.TouchDevice(canvas),
gamepads: new pc.GamePads()
});
// Configure canvas
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
app.setCanvasResolution(pc.RESOLUTION_AUTO);
// Handle resize
window.addEventListener('resize', () => app.resizeCanvas());
// Start the application
app.start();
PlayCanvas uses ECS architecture: Entities contain Components.
// Create entity
const entity = new pc.Entity('myEntity');
// Add to scene hierarchy
app.root.addChild(entity);
// Add components
entity.addComponent('model', {
type: 'box'
});
entity.addComponent('script');
// Transform
entity.setPosition(0, 1, 0);
entity.setEulerAngles(0, 45, 0);
entity.setLocalScale(2, 2, 2);
// Parent-child hierarchy
const parent = new pc.Entity('parent');
const child = new pc.Entity('child');
parent.addChild(child);
The application fires events during the update loop.
app.on('update', (dt) => {
// dt is delta time in seconds
entity.rotate(0, 10 * dt, 0);
});
app.on('prerender', () => {
// Before rendering
});
app.on('postrender', () => {
// After rendering
});
Core components extend entity functionality:
Model Component :
entity.addComponent('model', {
type: 'box', // 'box', 'sphere', 'cylinder', 'cone', 'capsule', 'asset'
material: material,
castShadows: true,
receiveShadows: true
});
Camera Component :
entity.addComponent('camera', {
clearColor: new pc.Color(0.1, 0.2, 0.3),
fov: 45,
nearClip: 0.1,
farClip: 1000,
projection: pc.PROJECTION_PERSPECTIVE // or PROJECTION_ORTHOGRAPHIC
});
Light Component :
entity.addComponent('light', {
type: pc.LIGHTTYPE_DIRECTIONAL, // DIRECTIONAL, POINT, SPOT
color: new pc.Color(1, 1, 1),
intensity: 1,
castShadows: true,
shadowDistance: 50
});
Rigidbody Component (requires physics):
entity.addComponent('rigidbody', {
type: pc.BODYTYPE_DYNAMIC, // STATIC, DYNAMIC, KINEMATIC
mass: 1,
friction: 0.5,
restitution: 0.3
});
entity.addComponent('collision', {
type: 'box',
halfExtents: new pc.Vec3(0.5, 0.5, 0.5)
});
Create a complete scene with camera, light, and models.
import * as pc from 'playcanvas';
// Initialize application
const canvas = document.createElement('canvas');
document.body.appendChild(canvas);
const app = new pc.Application(canvas);
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
app.setCanvasResolution(pc.RESOLUTION_AUTO);
window.addEventListener('resize', () => app.resizeCanvas());
// Create camera
const camera = new pc.Entity('camera');
camera.addComponent('camera', {
clearColor: new pc.Color(0.2, 0.3, 0.4)
});
camera.setPosition(0, 2, 5);
camera.lookAt(0, 0, 0);
app.root.addChild(camera);
// Create directional light
const light = new pc.Entity('light');
light.addComponent('light', {
type: pc.LIGHTTYPE_DIRECTIONAL,
castShadows: true
});
light.setEulerAngles(45, 30, 0);
app.root.addChild(light);
// Create ground
const ground = new pc.Entity('ground');
ground.addComponent('model', {
type: 'plane'
});
ground.setLocalScale(10, 1, 10);
app.root.addChild(ground);
// Create cube
const cube = new pc.Entity('cube');
cube.addComponent('model', {
type: 'box',
castShadows: true
});
cube.setPosition(0, 1, 0);
app.root.addChild(cube);
// Animate cube
app.on('update', (dt) => {
cube.rotate(10 * dt, 20 * dt, 30 * dt);
});
app.start();
Load external 3D models with asset management.
// Create asset for model
const modelAsset = new pc.Asset('model', 'container', {
url: '/models/character.glb'
});
// Add to asset registry
app.assets.add(modelAsset);
// Load asset
modelAsset.ready((asset) => {
// Create entity from loaded model
const entity = asset.resource.instantiateRenderEntity();
app.root.addChild(entity);
// Scale and position
entity.setLocalScale(2, 2, 2);
entity.setPosition(0, 0, 0);
});
app.assets.load(modelAsset);
With error handling :
modelAsset.ready((asset) => {
console.log('Model loaded:', asset.name);
const entity = asset.resource.instantiateRenderEntity();
app.root.addChild(entity);
});
modelAsset.on('error', (err) => {
console.error('Failed to load model:', err);
});
app.assets.load(modelAsset);
Create custom materials with PBR workflow.
// Create material
const material = new pc.StandardMaterial();
material.diffuse = new pc.Color(1, 0, 0); // Red
material.metalness = 0.5;
material.gloss = 0.8;
material.update();
// Apply to entity
entity.model.material = material;
// With textures
const textureAsset = new pc.Asset('diffuse', 'texture', {
url: '/textures/brick_diffuse.jpg'
});
app.assets.add(textureAsset);
app.assets.load(textureAsset);
textureAsset.ready((asset) => {
material.diffuseMap = asset.resource;
material.update();
});
// PBR material with all maps
const pbrMaterial = new pc.StandardMaterial();
// Load all textures
const textures = {
diffuse: '/textures/albedo.jpg',
normal: '/textures/normal.jpg',
metalness: '/textures/metalness.jpg',
gloss: '/textures/roughness.jpg',
ao: '/textures/ao.jpg'
};
Object.keys(textures).forEach(key => {
const asset = new pc.Asset(key, 'texture', { url: textures[key] });
app.assets.add(asset);
asset.ready((loadedAsset) => {
switch(key) {
case 'diffuse':
pbrMaterial.diffuseMap = loadedAsset.resource;
break;
case 'normal':
pbrMaterial.normalMap = loadedAsset.resource;
break;
case 'metalness':
pbrMaterial.metalnessMap = loadedAsset.resource;
break;
case 'gloss':
pbrMaterial.glossMap = loadedAsset.resource;
break;
case 'ao':
pbrMaterial.aoMap = loadedAsset.resource;
break;
}
pbrMaterial.update();
});
app.assets.load(asset);
});
Use Ammo.js for physics simulation.
import * as pc from 'playcanvas';
// Initialize with Ammo.js
const app = new pc.Application(canvas, {
keyboard: new pc.Keyboard(window),
mouse: new pc.Mouse(canvas)
});
// Load Ammo.js
const ammoScript = document.createElement('script');
ammoScript.src = 'https://cdn.jsdelivr.net/npm/ammo.js@0.0.10/ammo.js';
document.body.appendChild(ammoScript);
ammoScript.onload = () => {
Ammo().then((AmmoLib) => {
window.Ammo = AmmoLib;
// Create static ground
const ground = new pc.Entity('ground');
ground.addComponent('model', { type: 'plane' });
ground.setLocalScale(10, 1, 10);
ground.addComponent('rigidbody', {
type: pc.BODYTYPE_STATIC
});
ground.addComponent('collision', {
type: 'box',
halfExtents: new pc.Vec3(5, 0.1, 5)
});
app.root.addChild(ground);
// Create dynamic cube
const cube = new pc.Entity('cube');
cube.addComponent('model', { type: 'box' });
cube.setPosition(0, 5, 0);
cube.addComponent('rigidbody', {
type: pc.BODYTYPE_DYNAMIC,
mass: 1,
friction: 0.5,
restitution: 0.5
});
cube.addComponent('collision', {
type: 'box',
halfExtents: new pc.Vec3(0.5, 0.5, 0.5)
});
app.root.addChild(cube);
// Apply force
cube.rigidbody.applyForce(10, 0, 0);
cube.rigidbody.applyTorque(0, 10, 0);
app.start();
});
};
Create reusable script components.
// Define script class
const RotateScript = pc.createScript('rotate');
// Script attributes (editor-exposed)
RotateScript.attributes.add('speed', {
type: 'number',
default: 10,
title: 'Rotation Speed'
});
RotateScript.attributes.add('axis', {
type: 'vec3',
default: [0, 1, 0],
title: 'Rotation Axis'
});
// Initialize method
RotateScript.prototype.initialize = function() {
console.log('RotateScript initialized');
};
// Update method (called every frame)
RotateScript.prototype.update = function(dt) {
this.entity.rotate(
this.axis.x * this.speed * dt,
this.axis.y * this.speed * dt,
this.axis.z * this.speed * dt
);
};
// Cleanup
RotateScript.prototype.destroy = function() {
console.log('RotateScript destroyed');
};
// Usage
const entity = new pc.Entity('rotatingCube');
entity.addComponent('model', { type: 'box' });
entity.addComponent('script');
entity.script.create('rotate', {
attributes: {
speed: 20,
axis: new pc.Vec3(0, 1, 0)
}
});
app.root.addChild(entity);
Script lifecycle methods :
const MyScript = pc.createScript('myScript');
MyScript.prototype.initialize = function() {
// Called once after all resources are loaded
};
MyScript.prototype.postInitialize = function() {
// Called after all entities have initialized
};
MyScript.prototype.update = function(dt) {
// Called every frame before rendering
};
MyScript.prototype.postUpdate = function(dt) {
// Called every frame after update
};
MyScript.prototype.swap = function(old) {
// Hot reload support
};
MyScript.prototype.destroy = function() {
// Cleanup when entity is destroyed
};
Handle keyboard, mouse, and touch input.
// Keyboard
if (app.keyboard.isPressed(pc.KEY_W)) {
entity.translate(0, 0, -speed * dt);
}
if (app.keyboard.wasPressed(pc.KEY_SPACE)) {
entity.rigidbody.applyImpulse(0, 10, 0);
}
// Mouse
app.mouse.on(pc.EVENT_MOUSEDOWN, (event) => {
if (event.button === pc.MOUSEBUTTON_LEFT) {
console.log('Left click at', event.x, event.y);
}
});
app.mouse.on(pc.EVENT_MOUSEMOVE, (event) => {
const dx = event.dx;
const dy = event.dy;
camera.rotate(-dy * 0.2, -dx * 0.2, 0);
});
// Touch
app.touch.on(pc.EVENT_TOUCHSTART, (event) => {
event.touches.forEach((touch) => {
console.log('Touch at', touch.x, touch.y);
});
});
// Raycasting (mouse picking)
app.mouse.on(pc.EVENT_MOUSEDOWN, (event) => {
const camera = app.root.findByName('camera');
const cameraComponent = camera.camera;
const from = cameraComponent.screenToWorld(
event.x,
event.y,
cameraComponent.nearClip
);
const to = cameraComponent.screenToWorld(
event.x,
event.y,
cameraComponent.farClip
);
const result = app.systems.rigidbody.raycastFirst(from, to);
if (result) {
console.log('Hit:', result.entity.name);
result.entity.model.material.emissive = new pc.Color(1, 0, 0);
}
});
Play skeletal animations and tweens.
Skeletal animation :
// Load animated model
const modelAsset = new pc.Asset('character', 'container', {
url: '/models/character.glb'
});
app.assets.add(modelAsset);
modelAsset.ready((asset) => {
const entity = asset.resource.instantiateRenderEntity();
app.root.addChild(entity);
// Get animation component
entity.addComponent('animation', {
assets: [asset],
speed: 1.0,
loop: true,
activate: true
});
// Play specific animation
entity.animation.play('Walk', 0.2); // 0.2s blend time
// Later, transition to run
entity.animation.play('Run', 0.5);
});
app.assets.load(modelAsset);
Property tweening :
// Animate position
entity.tween(entity.getLocalPosition())
.to({ x: 5, y: 2, z: 0 }, 2.0, pc.SineInOut)
.start();
// Animate rotation
entity.tween(entity.getLocalEulerAngles())
.to({ x: 0, y: 180, z: 0 }, 1.0, pc.Linear)
.loop(true)
.yoyo(true)
.start();
// Animate material color
const color = material.emissive;
app.tween(color)
.to(new pc.Color(1, 0, 0), 1.0, pc.SineInOut)
.yoyo(true)
.loop(true)
.start();
// Chain tweens
entity.tween(entity.getLocalPosition())
.to({ y: 2 }, 1.0)
.to({ y: 0 }, 1.0)
.delay(0.5)
.repeat(3)
.start();
Wrap PlayCanvas in React components.
import React, { useEffect, useRef } from 'react';
import * as pc from 'playcanvas';
function PlayCanvasScene() {
const canvasRef = useRef(null);
const appRef = useRef(null);
useEffect(() => {
// Initialize
const app = new pc.Application(canvasRef.current);
appRef.current = app;
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
app.setCanvasResolution(pc.RESOLUTION_AUTO);
// Create scene
const camera = new pc.Entity('camera');
camera.addComponent('camera', {
clearColor: new pc.Color(0.1, 0.2, 0.3)
});
camera.setPosition(0, 0, 5);
app.root.addChild(camera);
const cube = new pc.Entity('cube');
cube.addComponent('model', { type: 'box' });
app.root.addChild(cube);
const light = new pc.Entity('light');
light.addComponent('light');
light.setEulerAngles(45, 0, 0);
app.root.addChild(light);
app.on('update', (dt) => {
cube.rotate(10 * dt, 20 * dt, 30 * dt);
});
app.start();
// Cleanup
return () => {
app.destroy();
};
}, []);
return (
<canvas
ref={canvasRef}
style={{ width: '100%', height: '100vh' }}
/>
);
}
export default PlayCanvasScene;
Work with PlayCanvas Editor projects.
// Export from PlayCanvas Editor
// Download build files, then load in code:
import * as pc from 'playcanvas';
const app = new pc.Application(canvas);
// Load exported project config
fetch('/config.json')
.then(response => response.json())
.then(config => {
// Load scene
app.scenes.loadSceneHierarchy(config.scene_url, (err, parent) => {
if (err) {
console.error('Failed to load scene:', err);
return;
}
// Start application
app.start();
// Find entities by name
const player = app.root.findByName('Player');
const enemy = app.root.findByName('Enemy');
// Access scripts
player.script.myScript.doSomething();
});
});
Reuse entities instead of creating/destroying.
class EntityPool {
constructor(app, count) {
this.app = app;
this.pool = [];
this.active = [];
for (let i = 0; i < count; i++) {
const entity = new pc.Entity('pooled');
entity.addComponent('model', { type: 'box' });
entity.enabled = false;
app.root.addChild(entity);
this.pool.push(entity);
}
}
spawn(position) {
let entity = this.pool.pop();
if (!entity) {
// Pool exhausted, create new
entity = new pc.Entity('pooled');
entity.addComponent('model', { type: 'box' });
this.app.root.addChild(entity);
}
entity.enabled = true;
entity.setPosition(position);
this.active.push(entity);
return entity;
}
despawn(entity) {
entity.enabled = false;
const index = this.active.indexOf(entity);
if (index > -1) {
this.active.splice(index, 1);
this.pool.push(entity);
}
}
}
// Usage
const pool = new EntityPool(app, 100);
const bullet = pool.spawn(new pc.Vec3(0, 0, 0));
// Later
pool.despawn(bullet);
Reduce geometry for distant objects.
// Manual LOD switching
app.on('update', () => {
const distance = camera.getPosition().distance(entity.getPosition());
if (distance < 10) {
entity.model.asset = highResModel;
} else if (distance < 50) {
entity.model.asset = mediumResModel;
} else {
entity.model.asset = lowResModel;
}
});
// Or disable distant entities
app.on('update', () => {
entities.forEach(entity => {
const distance = camera.getPosition().distance(entity.getPosition());
entity.enabled = distance < 100;
});
});
Combine static meshes to reduce draw calls.
// Enable static batching for entity
entity.model.batchGroupId = 1;
// Batch all entities with same group ID
app.batcher.generate([entity1, entity2, entity3]);
Use compressed texture formats.
// When creating textures, use compressed formats
const texture = new pc.Texture(app.graphicsDevice, {
width: 512,
height: 512,
format: pc.PIXELFORMAT_DXT5, // GPU-compressed
minFilter: pc.FILTER_LINEAR_MIPMAP_LINEAR,
magFilter: pc.FILTER_LINEAR,
mipmaps: true
});
Problem : Scene renders but nothing happens.
// ❌ Wrong - forgot to start
const app = new pc.Application(canvas);
// ... create entities ...
// Nothing happens!
// ✅ Correct
const app = new pc.Application(canvas);
// ... create entities ...
app.start(); // Critical!
Problem : Modifying scene graph during iteration.
// ❌ Wrong - modifying array during iteration
app.on('update', () => {
entities.forEach(entity => {
if (entity.shouldDestroy) {
entity.destroy(); // Modifies array!
}
});
});
// ✅ Correct - mark for deletion, clean up after
const toDestroy = [];
app.on('update', () => {
entities.forEach(entity => {
if (entity.shouldDestroy) {
toDestroy.push(entity);
}
});
});
app.on('postUpdate', () => {
toDestroy.forEach(entity => entity.destroy());
toDestroy.length = 0;
});
Problem : Not cleaning up loaded assets.
// ❌ Wrong - assets never cleaned up
function loadModel() {
const asset = new pc.Asset('model', 'container', { url: '/model.glb' });
app.assets.add(asset);
app.assets.load(asset);
// Asset stays in memory forever
}
// ✅ Correct - clean up when done
function loadModel() {
const asset = new pc.Asset('model', 'container', { url: '/model.glb' });
app.assets.add(asset);
asset.ready(() => {
// Use model
});
app.assets.load(asset);
// Clean up later
return () => {
app.assets.remove(asset);
asset.unload();
};
}
const cleanup = loadModel();
// Later: cleanup();
Problem : Transforms not propagating correctly.
// ❌ Wrong - setting world transform on child
const parent = new pc.Entity();
const child = new pc.Entity();
parent.addChild(child);
child.setPosition(5, 0, 0); // Local position
parent.setPosition(10, 0, 0);
// Child is at (15, 0, 0) in world space
// ✅ Correct - understand local vs world
child.setLocalPosition(5, 0, 0); // Explicit local
// or
const worldPos = new pc.Vec3(15, 0, 0);
child.setPosition(worldPos); // Explicit world
Problem : Physics components don't work.
// ❌ Wrong - Ammo.js not loaded
const entity = new pc.Entity();
entity.addComponent('rigidbody', { type: pc.BODYTYPE_DYNAMIC });
// Error: Ammo is not defined
// ✅ Correct - ensure Ammo.js is loaded
const script = document.createElement('script');
script.src = 'https://cdn.jsdelivr.net/npm/ammo.js@0.0.10/ammo.js';
document.body.appendChild(script);
script.onload = () => {
Ammo().then((AmmoLib) => {
window.Ammo = AmmoLib;
// Now physics works
const entity = new pc.Entity();
entity.addComponent('rigidbody', { type: pc.BODYTYPE_DYNAMIC });
entity.addComponent('collision', { type: 'box' });
});
};
Problem : Canvas doesn't fill container or respond to resize.
// ❌ Wrong - fixed size canvas
const canvas = document.createElement('canvas');
canvas.width = 800;
canvas.height = 600;
// ✅ Correct - responsive canvas
const canvas = document.createElement('canvas');
const app = new pc.Application(canvas);
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
app.setCanvasResolution(pc.RESOLUTION_AUTO);
window.addEventListener('resize', () => app.resizeCanvas());
const app = new pc.Application(canvas);
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
app.setCanvasResolution(pc.RESOLUTION_AUTO);
app.start();
const entity = new pc.Entity('name');
entity.addComponent('model', { type: 'box' });
entity.setPosition(x, y, z);
app.root.addChild(entity);
app.on('update', (dt) => {
// Logic here
});
const asset = new pc.Asset('name', 'type', { url: '/path' });
app.assets.add(asset);
asset.ready(() => { /* use asset */ });
app.assets.load(asset);
Related Skills : For lower-level WebGL control, reference threejs-webgl. For React integration patterns, see react-three-fiber. For physics-heavy simulations, reference babylonjs-engine.
Weekly Installs
85
Repository
GitHub Stars
11
First Seen
Feb 27, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykWarn
Installed on
opencode85
github-copilot83
codex83
amp83
cline83
kimi-cli83
Three.js 3D Web开发教程 - WebGL/WebGPU图形编程、动画与性能优化指南
540 周安装