|
| 1 | + |
| 2 | +import { |
| 3 | + Camera, |
| 4 | + MeshRenderer, |
| 5 | + PrimitiveMesh, |
| 6 | + Vector3, |
| 7 | + WebGLEngine, |
| 8 | + DirectLight, |
| 9 | + BlinnPhongMaterial, |
| 10 | + Color, |
| 11 | + Layer |
| 12 | +} from "@galacean/engine"; |
| 13 | +import { Gizmo, Group, State } from "../packages/gizmo/src"; |
| 14 | + |
| 15 | +export default { |
| 16 | + title: 'Toolkit/Gizmo', |
| 17 | + parameters: { |
| 18 | + canvas: { |
| 19 | + engineOptions: { |
| 20 | + alpha: true, |
| 21 | + antialias: true |
| 22 | + } |
| 23 | + } |
| 24 | + }, |
| 25 | + argTypes: { |
| 26 | + gizmoState: { |
| 27 | + control: { type: 'select' }, |
| 28 | + options: ['translate', 'rotate', 'scale', 'all'], |
| 29 | + mapping: { |
| 30 | + 'translate': State.translate, |
| 31 | + 'rotate': State.rotate, |
| 32 | + 'scale': State.scale, |
| 33 | + 'all': State.all |
| 34 | + }, |
| 35 | + defaultValue: 'translate' |
| 36 | + }, |
| 37 | + gizmoSize: { |
| 38 | + control: { type: 'range', min: 0, max: 2, step: 0.1 }, |
| 39 | + defaultValue: 0.8 |
| 40 | + } |
| 41 | + } |
| 42 | +}; |
| 43 | + |
| 44 | +export const GizmoDemo = { |
| 45 | + args: { |
| 46 | + gizmoState: "translate", |
| 47 | + gizmoSize: 0.8 |
| 48 | + }, |
| 49 | + render:async (args, context) => { |
| 50 | + const engine: WebGLEngine = await context.getEngine(); |
| 51 | + |
| 52 | + engine.canvas.resizeByClientSize(window.devicePixelRatio) |
| 53 | + |
| 54 | + const scene = engine.sceneManager.activeScene; |
| 55 | + const rootEntity = scene.createRootEntity("root"); |
| 56 | + |
| 57 | + const cameraEntity = rootEntity.createChild("camera"); |
| 58 | + cameraEntity.transform.setPosition(0, 2, 5); |
| 59 | + cameraEntity.transform.lookAt(new Vector3(0, 0, 0)); |
| 60 | + const camera = cameraEntity.addComponent(Camera); |
| 61 | + camera.enableFrustumCulling = true; |
| 62 | + |
| 63 | + const lightEntity = rootEntity.createChild("light"); |
| 64 | + lightEntity.transform.setPosition(1, 3, 2); |
| 65 | + lightEntity.transform.lookAt(new Vector3(0, 0, 0)); |
| 66 | + const light = lightEntity.addComponent(DirectLight); |
| 67 | + light.color = new Color(1, 1, 1, 1).toLinear(new Color()); |
| 68 | + |
| 69 | + const cubeEntity = rootEntity.createChild("cube"); |
| 70 | + const renderer = cubeEntity.addComponent(MeshRenderer); |
| 71 | + cubeEntity.layer = Layer.Layer22; |
| 72 | + renderer.mesh = PrimitiveMesh.createCuboid(engine, 1, 1, 1); |
| 73 | + |
| 74 | + const material = new BlinnPhongMaterial(engine); |
| 75 | + material.baseColor = new Color(0.7, 0.3, 0.3, 1.0).toLinear(new Color()); |
| 76 | + renderer.setMaterial(material); |
| 77 | + |
| 78 | + const group = new Group(); |
| 79 | + |
| 80 | + |
| 81 | + const gizmoEntity = rootEntity.createChild("editor-gizmo"); |
| 82 | + gizmoEntity.layer = Layer.Layer31; |
| 83 | + |
| 84 | + const gizmo = gizmoEntity.addComponent(Gizmo); |
| 85 | + |
| 86 | + gizmo.init(camera, group); |
| 87 | + |
| 88 | + group.addEntity(cubeEntity) |
| 89 | + gizmo.state = args.gizmoState; |
| 90 | + gizmo.size = args.gizmoSize; |
| 91 | + |
| 92 | + engine.on("gizmo-move-start", (axis) => { |
| 93 | + console.log(`Gizmo move started on axis: ${axis}`); |
| 94 | + }); |
| 95 | + |
| 96 | + engine.on("gizmo-move-end", () => { |
| 97 | + console.log("Gizmo move ended"); |
| 98 | + }); |
| 99 | + |
| 100 | + const updateGizmo = () => { |
| 101 | + gizmo.state = args.gizmoState; |
| 102 | + gizmo.size = args.gizmoSize; |
| 103 | + }; |
| 104 | + |
| 105 | + context.watch = (newArgs) => { |
| 106 | + args = newArgs; |
| 107 | + updateGizmo(); |
| 108 | + }; |
| 109 | + |
| 110 | + engine.run(); |
| 111 | + |
| 112 | + return document.createElement('div'); |
| 113 | + } |
| 114 | +}; |
| 115 | + |
0 commit comments