with one click
galacean-init
此 Skill 包含了 Galacean Engine 的初始化流程,包括 Engine 创建、场景自适应、相机与灯光的基本设置
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
Menu
此 Skill 包含了 Galacean Engine 的初始化流程,包括 Engine 创建、场景自适应、相机与灯光的基本设置
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
Based on SOC occupation classification
此 Skill 约定了 Galacean Engine 中自定义着色器的编写方式,包含 Shader.create、BaseMaterial 参数绑定、动画驱动、透明混合与常见排错。
此 Skill 包含了使用 Galacean Engine 物理引擎的注意事项、距离检测替代方案以及常见问题解决方案
此 Skill 包含了在 Galacean Engine 中通过 Script 脚本处理物体交互(如点击)的核心模式,基于 onPointerClick 实现
此 Skill 包含了在 Galacean Engine 中创建物理碰撞体的核心模式,涵盖动态碰撞体 (DynamicCollider) 和 静态碰撞体 (StaticCollider) 的实现
使用 Galacean Engine 开发2D游戏的核心模式,包括真2D(Sprite)和伪2D(3D正交投影)实现
此 Skill 包含了 Galacean Engine 相机的配置方法,包括透视/正交投影设置、俯视视角配置、窗口适配等
| name | galacean-init |
| description | 此 Skill 包含了 Galacean Engine 的初始化流程,包括 Engine 创建、场景自适应、相机与灯光的基本设置 |
在 Galacean Engine 中,创建一个 3D 场景通常需要初始化引擎 (WebGLEngine),设置画布自适应,并配置基本的相机 (Camera) 和灯光 (DirectLight)。
初始化通常需要以下核心类:
import { WebGLEngine, Camera, DirectLight, Vector3, Color } from "@galacean/engine";
适用场景:项目的入口文件,启动游戏循环。
核心步骤:
engine.run()。代码模式:
// 引入物理引擎包
import { LitePhysics } from '@galacean/engine-physics-lite';
import { PhysXPhysics, PhysXRuntimeMode } from '@galacean/engine-physics-physx';
function createCanvas(width?: number, height?: number): HTMLCanvasElement {
const canvas = document.createElement('canvas')
canvas.id = 'canvas'
const aspect = window.innerWidth / window.innerHeight
canvas.width = width || 1280
canvas.height = height || canvas.width / aspect
return canvas
}
async function initEngine() {
const canvas = createCanvas();
document.body.appendChild(canvas);
// 获取设备像素比 (DPR) - 用于解决高分辨率屏幕模糊问题
const dpr = window.devicePixelRatio || 1;
// 设置 canvas 实际像素尺寸以匹配设备像素比
const resizeCanvas = () => {
const rect = canvas.getBoundingClientRect();
canvas.width = rect.width * dpr;
canvas.height = rect.height * dpr;
};
resizeCanvas();
const engine = await WebGLEngine.create({
canvas,
// 场景 1: 需要真实的物理反馈(如重力、反弹、复杂的物理交互)
// 需要引用完整的 physics (PhysX)
physics: new PhysXPhysics(PhysXRuntimeMode.Auto, {
wasmModeUrl: './libs/physx.release.js',
javaScriptModeUrl: './libs/physx.release.downgrade.js'
}),
// 场景 2: 只需要基础的碰撞检测(Trigger)、点击事件(Raycast)
// 可以引用轻量的物理引擎 (LitePhysics)
// physics: new LitePhysics(),
// 场景 3: 不需要点击事件、碰撞检测
// 不配置 physics
});
// 设置引擎的像素比,确保渲染清晰度
engine.canvas.resizeByClientSize(dpr);
canvas.style.touchAction = 'none'
const scene = engine.sceneManager.activeScene
const rootEntity = scene.createRootEntity()
// 窗口大小变化时更新 canvas 尺寸
window.addEventListener('resize', () => {
engine.canvas.resizeByClientSize(dpr);
});
engine.run()
}
适用场景:解决在 Retina 屏、手机等高 DPR (Device Pixel Ratio) 设备上画面模糊的问题。
核心机制:
devicePixelRatio(通常为 1, 2, 3)resizeByClientSize(dpr) 方法通知引擎适配代码模式:
async function initHighDPREngine() {
const canvas = document.getElementById('canvas') as HTMLCanvasElement;
// 1. 获取设备像素比
const dpr = window.devicePixelRatio || 1;
// 2. 初始化时设置 canvas 实际像素尺寸
const resizeCanvas = () => {
const rect = canvas.getBoundingClientRect();
canvas.width = rect.width * dpr;
canvas.height = rect.height * dpr;
};
resizeCanvas();
// 3. 创建引擎
const engine = await WebGLEngine.create({
canvas,
physics: new LitePhysics(),
});
// 4. 关键:设置引擎的像素比
engine.canvas.resizeByClientSize(dpr);
// 5. 窗口变化时重新适配
window.addEventListener('resize', () => {
engine.canvas.resizeByClientSize(dpr);
// 如果有相机,也需要更新 aspectRatio
// camera.aspectRatio = window.innerWidth / window.innerHeight;
});
return engine;
}
注意事项:
resizeByClientSize(dpr)canvas.width/height,需要与 resizeByClientSize 保持一致scene.ambientLight 和直射光 DirectLight。PostProcess 组件。代码模式:
export async function createEnvironment(engine: WebGLEngine, rootEntity: Entity) {
const scene = engine.sceneManager.activeScene
// 设置背景为透明
const { background } = scene
background.mode = BackgroundMode.SolidColor
background.solidColor.set(1, 1, 1, 0)
// 创建背景精灵
const bg = rootEntity.createChild('bg')
const bgSprite = await engine.resourceManager
.load({
url:'xxx.png',
type: AssetType.Texture2D
})
const bgSpriteRenderer = bg.addComponent(SpriteRenderer)
bgSpriteRenderer.sprite = new Sprite(engine, bgSprite, new Rect(0, 0, 1, 1))
bgSpriteRenderer.width = 7
bgSpriteRenderer.height = 13
// 创建相机
const cameraEntity = rootEntity.createChild('camera')
const camera = cameraEntity.addComponent(Camera)
camera.enableFrustumCulling = false
console.log('camera', camera)
cameraEntity.transform.setPosition(0, 0, 15)
// 添加后处理效果(Bloom)
const postEntity = rootEntity.createChild('postProcess')
const postProcess = postEntity.addComponent(PostProcess)
const bloomEffect = postProcess.addEffect(BloomEffect)
bloomEffect.intensity.value = 2
bloomEffect.scatter.value = 0.8
bloomEffect.threshold.value = 0.5
console.log('postProcess', postProcess)
// 设置场景灯光
scene.ambientLight.diffuseSolidColor.set(1, 1, 1, 1)
scene.ambientLight.diffuseIntensity = 0.6
const lightEntity = rootEntity.createChild('light')
lightEntity.addComponent(DirectLight)
lightEntity.transform.setPosition(1, 1, 1)
lightEntity.transform.lookAt(new Vector3())
}