|
| 1 | +import { |
| 2 | + AmbientLight, |
| 3 | + BoxGeometry, |
| 4 | + DirectionalLight, |
| 5 | + GridHelper, |
| 6 | + Mesh, |
| 7 | + MeshBasicMaterial, |
| 8 | + MeshStandardMaterial, |
| 9 | + OrthographicCamera, |
| 10 | + PlaneGeometry, |
| 11 | + Scene, |
| 12 | + SessionMode, |
| 13 | + Shape, |
| 14 | + ShapeGeometry, |
| 15 | + SphereGeometry, |
| 16 | + TorusGeometry, |
| 17 | + World, |
| 18 | + XRCylinderLayer, |
| 19 | + XRQuadLayer, |
| 20 | +} from '@iwsdk/core'; |
| 21 | + |
| 22 | +// --------------------------------------------------------------------------- |
| 23 | +// Helper: create a star Shape |
| 24 | +// --------------------------------------------------------------------------- |
| 25 | +function createStarShape( |
| 26 | + outerRadius: number, |
| 27 | + innerRadius: number, |
| 28 | + points: number, |
| 29 | +): Shape { |
| 30 | + const shape = new Shape(); |
| 31 | + for (let i = 0; i < points * 2; i++) { |
| 32 | + const angle = (i * Math.PI) / points - Math.PI / 2; |
| 33 | + const r = i % 2 === 0 ? outerRadius : innerRadius; |
| 34 | + const x = Math.cos(angle) * r; |
| 35 | + const y = Math.sin(angle) * r; |
| 36 | + if (i === 0) { |
| 37 | + shape.moveTo(x, y); |
| 38 | + } else { |
| 39 | + shape.lineTo(x, y); |
| 40 | + } |
| 41 | + } |
| 42 | + shape.closePath(); |
| 43 | + return shape; |
| 44 | +} |
| 45 | + |
| 46 | +// --------------------------------------------------------------------------- |
| 47 | +// Helper: create a rounded rectangle Shape |
| 48 | +// --------------------------------------------------------------------------- |
| 49 | +function createRoundedRectShape(w: number, h: number, r: number): Shape { |
| 50 | + const hw = w / 2; |
| 51 | + const hh = h / 2; |
| 52 | + const shape = new Shape(); |
| 53 | + shape.moveTo(-hw + r, -hh); |
| 54 | + shape.lineTo(hw - r, -hh); |
| 55 | + shape.quadraticCurveTo(hw, -hh, hw, -hh + r); |
| 56 | + shape.lineTo(hw, hh - r); |
| 57 | + shape.quadraticCurveTo(hw, hh, hw - r, hh); |
| 58 | + shape.lineTo(-hw + r, hh); |
| 59 | + shape.quadraticCurveTo(-hw, hh, -hw, hh - r); |
| 60 | + shape.lineTo(-hw, -hh + r); |
| 61 | + shape.quadraticCurveTo(-hw, -hh, -hw + r, -hh); |
| 62 | + return shape; |
| 63 | +} |
| 64 | + |
| 65 | +// --------------------------------------------------------------------------- |
| 66 | +// Quad layer content – a star-shaped panel with a rotating cube |
| 67 | +// --------------------------------------------------------------------------- |
| 68 | + |
| 69 | +const quadScene = new Scene(); |
| 70 | +// No background color — transparent outside the star |
| 71 | + |
| 72 | +const quadCamera = new OrthographicCamera(-1, 1, 1, -1, 0.1, 10); |
| 73 | +quadCamera.position.set(0, 0, 3); |
| 74 | + |
| 75 | +const quadLight = new AmbientLight(0xffffff, 0.6); |
| 76 | +quadScene.add(quadLight); |
| 77 | +const quadDirLight = new DirectionalLight(0xffffff, 1); |
| 78 | +quadDirLight.position.set(1, 2, 3); |
| 79 | +quadScene.add(quadDirLight); |
| 80 | + |
| 81 | +// Star-shaped background |
| 82 | +const starShape = createStarShape(0.9, 0.4, 5); |
| 83 | +const starBg = new Mesh( |
| 84 | + new ShapeGeometry(starShape), |
| 85 | + new MeshBasicMaterial({ color: 0x202040 }), |
| 86 | +); |
| 87 | +starBg.position.z = -0.1; // behind the cube |
| 88 | +quadScene.add(starBg); |
| 89 | + |
| 90 | +const cube = new Mesh( |
| 91 | + new BoxGeometry(0.4, 0.4, 0.4), |
| 92 | + new MeshStandardMaterial({ color: 0x4488ff }), |
| 93 | +); |
| 94 | +quadScene.add(cube); |
| 95 | + |
| 96 | +// --------------------------------------------------------------------------- |
| 97 | +// Cylinder layer content – rounded-corner panel with orbiting shapes |
| 98 | +// --------------------------------------------------------------------------- |
| 99 | + |
| 100 | +const cylScene = new Scene(); |
| 101 | +// No background color — transparent outside the rounded rect |
| 102 | + |
| 103 | +const cylCamera = new OrthographicCamera(-2, 2, 1, -1, 0.1, 10); |
| 104 | +cylCamera.position.set(0, 0, 3); |
| 105 | + |
| 106 | +const cylLight = new AmbientLight(0xffffff, 0.6); |
| 107 | +cylScene.add(cylLight); |
| 108 | +const cylDirLight = new DirectionalLight(0xffffff, 1); |
| 109 | +cylDirLight.position.set(-1, 2, 3); |
| 110 | +cylScene.add(cylDirLight); |
| 111 | + |
| 112 | +// Rounded rectangle background |
| 113 | +const roundedRect = createRoundedRectShape(3.8, 1.8, 0.3); |
| 114 | +const roundedBg = new Mesh( |
| 115 | + new ShapeGeometry(roundedRect), |
| 116 | + new MeshBasicMaterial({ color: 0x402020 }), |
| 117 | +); |
| 118 | +roundedBg.position.z = -0.1; |
| 119 | +cylScene.add(roundedBg); |
| 120 | + |
| 121 | +const torus = new Mesh( |
| 122 | + new TorusGeometry(0.3, 0.1, 16, 32), |
| 123 | + new MeshStandardMaterial({ color: 0xff4444 }), |
| 124 | +); |
| 125 | +cylScene.add(torus); |
| 126 | + |
| 127 | +const sphere = new Mesh( |
| 128 | + new SphereGeometry(0.2, 16, 16), |
| 129 | + new MeshStandardMaterial({ color: 0x44ff44 }), |
| 130 | +); |
| 131 | +cylScene.add(sphere); |
| 132 | + |
| 133 | +// --------------------------------------------------------------------------- |
| 134 | +// Main app |
| 135 | +// --------------------------------------------------------------------------- |
| 136 | + |
| 137 | +World.create(document.getElementById('scene-container') as HTMLDivElement, { |
| 138 | + xr: { |
| 139 | + sessionMode: SessionMode.ImmersiveVR, |
| 140 | + features: { layers: true }, |
| 141 | + }, |
| 142 | +}).then((world) => { |
| 143 | + world.camera.position.set(0, 1.5, 0); |
| 144 | + |
| 145 | + // --- Main scene content (rendered in the projection layer) --- |
| 146 | + const grid = new GridHelper(10, 10, 0x888888, 0x444444); |
| 147 | + world.scene.add(grid); |
| 148 | + |
| 149 | + const floor = new Mesh( |
| 150 | + new PlaneGeometry(10, 10), |
| 151 | + new MeshBasicMaterial({ color: 0x333333 }), |
| 152 | + ); |
| 153 | + floor.rotation.x = -Math.PI / 2; |
| 154 | + floor.position.y = -0.01; |
| 155 | + world.scene.add(floor); |
| 156 | + |
| 157 | + const pillar1 = new Mesh( |
| 158 | + new BoxGeometry(0.3, 2, 0.3), |
| 159 | + new MeshBasicMaterial({ color: 0xcc8844 }), |
| 160 | + ); |
| 161 | + pillar1.position.set(-2, 1, -3); |
| 162 | + world.scene.add(pillar1); |
| 163 | + |
| 164 | + const pillar2 = new Mesh( |
| 165 | + new BoxGeometry(0.3, 2, 0.3), |
| 166 | + new MeshBasicMaterial({ color: 0xcc8844 }), |
| 167 | + ); |
| 168 | + pillar2.position.set(2, 1, -3); |
| 169 | + world.scene.add(pillar2); |
| 170 | + |
| 171 | + const orb = new Mesh( |
| 172 | + new SphereGeometry(0.4, 32, 32), |
| 173 | + new MeshBasicMaterial({ color: 0xffaa00 }), |
| 174 | + ); |
| 175 | + orb.position.set(0, 0.4, -3); |
| 176 | + world.scene.add(orb); |
| 177 | + |
| 178 | + const startTime = performance.now(); |
| 179 | + |
| 180 | + // --- Quad layer: star-shaped floating panel --- |
| 181 | + const quadEntity = world.createTransformEntity(); |
| 182 | + quadEntity.object3D!.position.set(-0.8, 1.5, -2); |
| 183 | + quadEntity.addComponent(XRQuadLayer, { |
| 184 | + width: 1.0, |
| 185 | + height: 1.0, |
| 186 | + pixelWidth: 1024, |
| 187 | + pixelHeight: 1024, |
| 188 | + renderCallback: () => { |
| 189 | + const elapsed = (performance.now() - startTime) / 1000; |
| 190 | + quadEntity.object3D!.position.y = 1.5 + Math.sin(elapsed) * 0.2; |
| 191 | + orb.position.x = Math.cos(elapsed * 0.5) * 1; |
| 192 | + orb.position.y = 1.5 + Math.sin(elapsed * 0.5) * 1; |
| 193 | + cube.rotation.x = elapsed * 0.7; |
| 194 | + cube.rotation.y = elapsed; |
| 195 | + world.renderer.render(quadScene, quadCamera); |
| 196 | + }, |
| 197 | + }); |
| 198 | + |
| 199 | + // --- Cylinder layer: rounded-corner curved panel --- |
| 200 | + const cylEntity = world.createTransformEntity(); |
| 201 | + cylEntity.object3D!.position.set(0.8, 1.5, -2); |
| 202 | + cylEntity.addComponent(XRCylinderLayer, { |
| 203 | + radius: 2.0, |
| 204 | + centralAngle: Math.PI / 3, |
| 205 | + aspectRatio: 2.0, |
| 206 | + pixelWidth: 1920, |
| 207 | + pixelHeight: 960, |
| 208 | + renderCallback: () => { |
| 209 | + const elapsed = (performance.now() - startTime) / 1000; |
| 210 | + torus.position.x = Math.cos(elapsed) * 0.8; |
| 211 | + torus.position.y = Math.sin(elapsed * 0.7) * 0.3; |
| 212 | + torus.rotation.x = elapsed * 1.2; |
| 213 | + sphere.position.x = Math.cos(elapsed + Math.PI) * 0.8; |
| 214 | + sphere.position.y = Math.sin(elapsed * 0.5 + 1) * 0.3; |
| 215 | + world.renderer.render(cylScene, cylCamera); |
| 216 | + }, |
| 217 | + }); |
| 218 | +}); |
0 commit comments