Cameras and lights from a file: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: document (line 25), roundtrip (line 53), scene (line 59), check (line 84).
1/// A camera and a light, written into a GLB and read back out of it.
2///
3/// Quoted by `gltf_cameras_lights.md` and shown whole in the Source tab.
4library;
6import 'package:flutter3d/flutter3d.dart';
7import 'package:flutter3d_showcase/src/demo/demo.dart';
9/// The `gltf-cameras-lights` page: a camera and a sun written into a GLB
10/// beside a cube and read back. Its check is that both survive, the camera
11/// with its projection.
12final class GltfCamerasLightsDemo extends ShowcaseDemo {
13 late final GltfAsset _readBack;
16 void configureView(DemoContext context) {
17 context.orbit
18 ..distance = 3.5
19 ..pitch = 0.3
20 ..yaw = 0.6;
21 }
24 Future<void> prepare(DemoContext context) async {
25 // glTF's own `KHR_lights_punctual` and `camera` are held on a document as
26 // `ModelLight` and `ModelCamera`, addressed from a node the same way a
27 // surface is. This document has one of each, plus a cube to see by.
28 final document = PlainModelDocument(
29 surfaces: <ModelSurface>[
30 ModelSurface(mesh: CuboidShape(size: Vector3.all(1.0)).build()),
31 ],
32 lights: <ModelLight>[
33 ModelLight(
34 type: LightType.directional,
35 color: LinearColor(1.0, 0.95, 0.85),
36 // Lux, as KHR_lights_punctual has a directional light.
37 intensity: 13900.0,
38 ),
39 ],
40 cameras: <ModelCamera>[
41 const ModelCamera(
42 projection: ModelPerspectiveCamera(fieldOfView: 0.9, znear: 0.1),
43 name: 'framing',
44 ),
45 ],
46 nodes: <ModelNode>[
47 ModelNode(surfaces: <int>[0]),
48 ModelNode(name: 'sun', lightIndex: 0),
49 ModelNode(name: 'framing', cameraIndex: 0),
50 ],
51 );
53 final bytes = GltfWriter(document).writeGlb();
54 _readBack = await GltfLoader().load(bytes);
55 }
58 Scene build(DemoContext context) {
59 final scene = Scene();
60 for (final ModelSurface surface in _readBack.surfaces) {
61 scene.add(
62 MeshNode(
63 DeviceMesh.upload(context.device, surface.mesh),
64 RenderMaterial(
65 baseColor: LinearColor.fromSrgb(0.7, 0.75, 0.82, 1.0),
66 roughness: 0.6,
67 ),
68 name: 'cube',
69 ),
70 );
71 }
72 for (final ModelLight light in _readBack.lights) {
73 scene.add(
74 // The file's lux is the node's lux: no conversion on the way.
75 LightNode(name: light.name, intensity: light.intensity)
76 ..setLocalForward(Vector3(-0.4, -0.9, -0.3)),
77 );
78 }
79 return scene;
80 }
83 void verify(Scene scene, FrameResult frame) {
84 if (_readBack.lights.length != 1) {
85 throw StateError('the light did not survive the round trip');
86 }
87 if (_readBack.cameras.length != 1) {
88 throw StateError('the camera did not survive the round trip');
89 }
90 final ModelCameraProjection projection =
91 _readBack.cameras.single.projection;
92 if (projection is! ModelPerspectiveCamera) {
93 throw StateError('the camera came back as the wrong projection');
94 }
95 if (frame.drawCalls < 1) {
96 throw StateError('the cube was not drawn');
97 }
98 }
99}