@jack120/test 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitmodules +3 -0
- package/.prettierrc +6 -0
- package/.vscode/c_cpp_properties.json +14 -0
- package/.vscode/launch.json +24 -0
- package/.vscode/settings.json +8 -0
- package/CMakeLists.txt +52 -0
- package/adamas-config.json +5 -0
- package/dist/index.js +2415 -0
- package/external/CMakeLists.txt +1 -0
- package/external/Unity-NodeJS-RPC/CMakeLists.txt +13 -0
- package/external/Unity-NodeJS-RPC/README.md +1 -0
- package/external/Unity-NodeJS-RPC/RpcClient.cpp +265 -0
- package/external/Unity-NodeJS-RPC/include/RpcClient.h +118 -0
- package/external/Unity-NodeJS-RPC/main.cpp +76 -0
- package/external/Unity-NodeJS-RPC/nlohmann/json.hpp +25526 -0
- package/external/Unity-NodeJS-RPC/server/Program.cs +52 -0
- package/external/Unity-NodeJS-RPC/server/json.cs +66 -0
- package/external/Unity-NodeJS-RPC/server/rpc.cs +369 -0
- package/external/Unity-NodeJS-RPC/server/sample1.csproj +10 -0
- package/external/Unity-NodeJS-RPC/server/sample1.sln +24 -0
- package/external/Unity-NodeJS-RPC/server/unity/RpcUnity.cs.txt +60 -0
- package/index.ts +36 -0
- package/library/adamas-types.d.ts +70 -0
- package/library/debug.ts +7 -0
- package/library/device.ts +279 -0
- package/library/entity.ts +35 -0
- package/library/index.ts +19 -0
- package/library/interaction/interaction.ts +281 -0
- package/library/native-bindings-osx.node +0 -0
- package/library/native-bindings-win.node +0 -0
- package/library/networking/state-sync.ts +62 -0
- package/library/physics/collider.ts +252 -0
- package/library/physics/rigidbody.ts +119 -0
- package/library/render/camera.ts +172 -0
- package/library/render/light.ts +169 -0
- package/library/render/material.ts +258 -0
- package/library/render/mesh.ts +208 -0
- package/library/render/primitives.ts +76 -0
- package/library/render/renderable.ts +137 -0
- package/library/render/renderer.ts +124 -0
- package/library/render/scene.ts +89 -0
- package/library/render/texture.ts +247 -0
- package/library/render/transform.ts +259 -0
- package/library/rpc.ts +81 -0
- package/library/utilities/base64.ts +63 -0
- package/loader-template.ts +419 -0
- package/native.cc +111 -0
- package/package.json +33 -0
- package/project.adamas.json +457 -0
- package/rusk.glb +0 -0
- package/samples/device-sample.ts +30 -0
- package/samples/interaction-sample.ts +134 -0
- package/samples/physics-sample.ts +39 -0
- package/samples/rendering-sample.ts +88 -0
- package/samples/spawn-cube.ts +422 -0
- package/samples/state-sync-sample.ts +25 -0
- package/tsconfig.json +20 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { EntityManager, Entity } from "@adamas/entity";
|
|
2
|
+
import { RenderableManager } from "@adamas/render/renderable";
|
|
3
|
+
import {
|
|
4
|
+
MaterialManager,
|
|
5
|
+
ShaderProperties,
|
|
6
|
+
ShaderType,
|
|
7
|
+
} from "@adamas/render/material";
|
|
8
|
+
import { importGltfAndRender } from "@adamas/utilities/gltfImporter";
|
|
9
|
+
import { TransformManager } from "@adamas/render/transform";
|
|
10
|
+
import { NewCubeMesh, NewQuadMesh } from "@adamas/render/primitives";
|
|
11
|
+
import { CameraManager } from "@adamas/render/camera";
|
|
12
|
+
import { TextureFormat, TextureManager } from "@adamas/render/texture";
|
|
13
|
+
import { quat, vec3, vec4 } from "gl-matrix";
|
|
14
|
+
import { RigidbodyManager } from "@adamas/physics/rigidbody";
|
|
15
|
+
import { ColliderManager } from "@adamas/physics/collider";
|
|
16
|
+
import { GrabInteractableManager } from "@adamas/interaction/interaction";
|
|
17
|
+
|
|
18
|
+
export const renderGltf = async (path: string = "./rusk.glb") => {
|
|
19
|
+
return await importGltfAndRender(path);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const renderCube = () => {
|
|
23
|
+
let entity: Entity = EntityManager.Create("test entity");
|
|
24
|
+
console.log("entity handle", entity);
|
|
25
|
+
|
|
26
|
+
// Create renderable component
|
|
27
|
+
RenderableManager.Create(entity);
|
|
28
|
+
|
|
29
|
+
// Create mesh and attach to renderable
|
|
30
|
+
const meshHandle = NewCubeMesh();
|
|
31
|
+
RenderableManager.SetMesh(entity, meshHandle);
|
|
32
|
+
|
|
33
|
+
// Create material and attach to renderable
|
|
34
|
+
const materialHandle = MaterialManager.Create(ShaderType.URP_LIT, entity);
|
|
35
|
+
MaterialManager.SetColor(
|
|
36
|
+
materialHandle,
|
|
37
|
+
ShaderProperties.BaseColor,
|
|
38
|
+
vec4.fromValues(1.0, 1.0, 1.0, 1.0),
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
// Play around with the transform of the entity
|
|
42
|
+
TransformManager.SetLocalPosition(entity, vec3.fromValues(0, 1, 0));
|
|
43
|
+
TransformManager.SetLocalScale(entity, vec3.fromValues(2, 1, 2));
|
|
44
|
+
|
|
45
|
+
setTimeout(() => {
|
|
46
|
+
console.log("Entity name: ", EntityManager.GetName(entity));
|
|
47
|
+
EntityManager.SetName(entity, "updated name");
|
|
48
|
+
console.log("Updated name: ", EntityManager.GetName(entity));
|
|
49
|
+
}, 10000);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const cameraRenderTexture = () => {
|
|
53
|
+
const camEntity = EntityManager.Create("camera");
|
|
54
|
+
const textureHandle = TextureManager.CreateRenderTexture(
|
|
55
|
+
1920,
|
|
56
|
+
1080,
|
|
57
|
+
0,
|
|
58
|
+
TextureFormat.RGBA32,
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
CameraManager.Create(camEntity);
|
|
62
|
+
CameraManager.SetRenderTexture(camEntity, textureHandle);
|
|
63
|
+
|
|
64
|
+
const displayEntity = EntityManager.Create("display");
|
|
65
|
+
RenderableManager.Create(displayEntity);
|
|
66
|
+
RenderableManager.SetMesh(displayEntity, NewQuadMesh());
|
|
67
|
+
const materialHandle = MaterialManager.Create(ShaderType.URP_LIT);
|
|
68
|
+
RenderableManager.SetMaterial(displayEntity, materialHandle);
|
|
69
|
+
MaterialManager.SetTexture(
|
|
70
|
+
materialHandle,
|
|
71
|
+
ShaderProperties.BaseMap,
|
|
72
|
+
textureHandle,
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
RigidbodyManager.Create(displayEntity);
|
|
76
|
+
RigidbodyManager.SetIsKinematic(displayEntity, true);
|
|
77
|
+
RigidbodyManager.SetUseGravity(displayEntity, false);
|
|
78
|
+
const collider = ColliderManager.CreateBox(displayEntity);
|
|
79
|
+
// ColliderManager.Setboxs(collider, 1.0);
|
|
80
|
+
GrabInteractableManager.Create(displayEntity);
|
|
81
|
+
|
|
82
|
+
TransformManager.SetParent(camEntity, displayEntity);
|
|
83
|
+
TransformManager.SetLocalRotation(
|
|
84
|
+
camEntity,
|
|
85
|
+
quat.fromEuler(quat.fromValues(1, 0, 0, 0), 0, 180, 0),
|
|
86
|
+
);
|
|
87
|
+
TransformManager.SetLocalPosition(displayEntity, vec3.fromValues(0, 2, -1));
|
|
88
|
+
};
|
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
import { Device } from "@adamas/device";
|
|
2
|
+
import { Debug } from "@adamas/debug";
|
|
3
|
+
import { EntityManager, Entity } from "@adamas/entity";
|
|
4
|
+
import { RenderableManager } from "@adamas/render/renderable";
|
|
5
|
+
import {
|
|
6
|
+
MaterialManager,
|
|
7
|
+
ShaderProperties,
|
|
8
|
+
ShaderType,
|
|
9
|
+
} from "@adamas/render/material";
|
|
10
|
+
import { TransformManager } from "@adamas/render/transform";
|
|
11
|
+
import { vec3, vec4 } from "gl-matrix";
|
|
12
|
+
import { NewCubeMesh } from "@adamas/render/primitives";
|
|
13
|
+
|
|
14
|
+
// Prefab system state
|
|
15
|
+
interface PrefabSystem {
|
|
16
|
+
leftPreviewCube: Entity | null;
|
|
17
|
+
rightPreviewCube: Entity | null;
|
|
18
|
+
leftButtonPrevState: number;
|
|
19
|
+
rightButtonPrevState: number;
|
|
20
|
+
spawnedCubes: Entity[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const prefabSystem: PrefabSystem = {
|
|
24
|
+
leftPreviewCube: null,
|
|
25
|
+
rightPreviewCube: null,
|
|
26
|
+
leftButtonPrevState: 0,
|
|
27
|
+
rightButtonPrevState: 0,
|
|
28
|
+
spawnedCubes: [],
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
console.log(process.cwd());
|
|
32
|
+
|
|
33
|
+
// Debug command handler
|
|
34
|
+
Debug.OnCmdCallback((command) => {
|
|
35
|
+
console.log("COMMAND: ", command);
|
|
36
|
+
|
|
37
|
+
// Handle debug commands for the prefab system
|
|
38
|
+
if (command === "clear_cubes") {
|
|
39
|
+
clearAllSpawnedCubes();
|
|
40
|
+
console.log("Cleared all spawned cubes");
|
|
41
|
+
} else if (command === "count_cubes") {
|
|
42
|
+
console.log(`Total spawned cubes: ${prefabSystem.spawnedCubes.length}`);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Creates a red cube prefab at the specified position
|
|
48
|
+
*/
|
|
49
|
+
function createRedCubePrefab(position: vec3): Entity {
|
|
50
|
+
const entity = EntityManager.Create("Red Cube");
|
|
51
|
+
|
|
52
|
+
// Create renderable component
|
|
53
|
+
RenderableManager.Create(entity);
|
|
54
|
+
RenderableManager.SetMesh(entity, NewCubeMesh());
|
|
55
|
+
|
|
56
|
+
// Create red material
|
|
57
|
+
const materialHandle = MaterialManager.Create(ShaderType.URP_LIT, entity);
|
|
58
|
+
MaterialManager.SetColor(
|
|
59
|
+
materialHandle,
|
|
60
|
+
ShaderProperties.BaseColor,
|
|
61
|
+
vec4.fromValues(1.0, 0.2, 0.2, 1.0),
|
|
62
|
+
); // Bright red
|
|
63
|
+
|
|
64
|
+
// Set position and scale
|
|
65
|
+
TransformManager.SetLocalPosition(entity, position);
|
|
66
|
+
TransformManager.SetLocalScale(entity, vec3.fromValues(0.3, 0.3, 0.3));
|
|
67
|
+
|
|
68
|
+
return entity;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Creates a blue cube prefab at the specified position
|
|
73
|
+
*/
|
|
74
|
+
function createBlueCubePrefab(position: vec3): Entity {
|
|
75
|
+
const entity = EntityManager.Create("Blue Cube");
|
|
76
|
+
|
|
77
|
+
// Create renderable component
|
|
78
|
+
RenderableManager.Create(entity);
|
|
79
|
+
RenderableManager.SetMesh(entity, NewCubeMesh());
|
|
80
|
+
|
|
81
|
+
// Create red material
|
|
82
|
+
const materialHandle = MaterialManager.Create(ShaderType.URP_LIT, entity);
|
|
83
|
+
MaterialManager.SetColor(
|
|
84
|
+
materialHandle,
|
|
85
|
+
ShaderProperties.BaseColor,
|
|
86
|
+
vec4.fromValues(0.2, 0.2, 1.0, 1.0),
|
|
87
|
+
); // Bright red
|
|
88
|
+
|
|
89
|
+
// Set position and scale
|
|
90
|
+
TransformManager.SetLocalPosition(entity, position);
|
|
91
|
+
TransformManager.SetLocalScale(entity, vec3.fromValues(0.3, 0.3, 0.3));
|
|
92
|
+
|
|
93
|
+
return entity;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Creates a semi-transparent preview cube
|
|
98
|
+
*/
|
|
99
|
+
function createPreviewCube(color: "red" | "blue", name: string): Entity {
|
|
100
|
+
const entity = EntityManager.Create(name);
|
|
101
|
+
|
|
102
|
+
// Create renderable component
|
|
103
|
+
RenderableManager.Create(entity);
|
|
104
|
+
RenderableManager.SetMesh(entity, NewCubeMesh());
|
|
105
|
+
|
|
106
|
+
// Create semi-transparent material
|
|
107
|
+
const materialHandle = MaterialManager.Create(ShaderType.URP_LIT, entity);
|
|
108
|
+
if (color === "red") {
|
|
109
|
+
MaterialManager.SetColor(
|
|
110
|
+
materialHandle,
|
|
111
|
+
ShaderProperties.BaseColor,
|
|
112
|
+
vec4.fromValues(1.0, 0.2, 0.2, 0.5),
|
|
113
|
+
); // Semi-transparent red
|
|
114
|
+
} else {
|
|
115
|
+
MaterialManager.SetColor(
|
|
116
|
+
materialHandle,
|
|
117
|
+
ShaderProperties.BaseColor,
|
|
118
|
+
vec4.fromValues(0.2, 0.2, 1.0, 0.5),
|
|
119
|
+
); // Semi-transparent blue
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Set scale to be slightly smaller for previews
|
|
123
|
+
TransformManager.SetLocalScale(entity, vec3.fromValues(0.2, 0.2, 0.2));
|
|
124
|
+
|
|
125
|
+
return entity;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Initialize the preview cubes that float above controllers
|
|
130
|
+
*/
|
|
131
|
+
function initializePreviewSystem() {
|
|
132
|
+
console.log("Initializing prefab preview system...");
|
|
133
|
+
|
|
134
|
+
// Wait a bit for the VR system to initialize, then create preview cubes
|
|
135
|
+
setTimeout(() => {
|
|
136
|
+
try {
|
|
137
|
+
// Test if we can get controller world positions
|
|
138
|
+
const leftPos = Device.GetLeftWorldPosition();
|
|
139
|
+
const rightPos = Device.GetRightWorldPosition();
|
|
140
|
+
|
|
141
|
+
console.log("Controller world position test:", { leftPos, rightPos });
|
|
142
|
+
|
|
143
|
+
// Create preview cubes
|
|
144
|
+
prefabSystem.leftPreviewCube = createPreviewCube(
|
|
145
|
+
"red",
|
|
146
|
+
"Left Preview (Red)",
|
|
147
|
+
);
|
|
148
|
+
prefabSystem.rightPreviewCube = createPreviewCube(
|
|
149
|
+
"blue",
|
|
150
|
+
"Right Preview (Blue)",
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
// Set initial positions if available
|
|
154
|
+
if (leftPos && Array.isArray(leftPos) && leftPos.length >= 3) {
|
|
155
|
+
TransformManager.SetLocalPosition(
|
|
156
|
+
prefabSystem.leftPreviewCube,
|
|
157
|
+
vec3.fromValues(
|
|
158
|
+
Number(leftPos[0]),
|
|
159
|
+
Number(leftPos[1]) + 0.2,
|
|
160
|
+
Number(leftPos[2]),
|
|
161
|
+
),
|
|
162
|
+
);
|
|
163
|
+
} else {
|
|
164
|
+
// Fallback position for left preview
|
|
165
|
+
TransformManager.SetLocalPosition(
|
|
166
|
+
prefabSystem.leftPreviewCube,
|
|
167
|
+
vec3.fromValues(-0.3, 1.5, 0),
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (rightPos && Array.isArray(rightPos) && rightPos.length >= 3) {
|
|
172
|
+
TransformManager.SetLocalPosition(
|
|
173
|
+
prefabSystem.rightPreviewCube,
|
|
174
|
+
vec3.fromValues(
|
|
175
|
+
Number(rightPos[0]),
|
|
176
|
+
Number(rightPos[1]) + 0.2,
|
|
177
|
+
Number(rightPos[2]),
|
|
178
|
+
),
|
|
179
|
+
);
|
|
180
|
+
} else {
|
|
181
|
+
// Fallback position for right preview
|
|
182
|
+
TransformManager.SetLocalPosition(
|
|
183
|
+
prefabSystem.rightPreviewCube,
|
|
184
|
+
vec3.fromValues(0.3, 1.5, 0),
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
console.log(
|
|
189
|
+
"Preview cubes created - Red cube above left controller, Blue cube above right controller",
|
|
190
|
+
);
|
|
191
|
+
} catch (error) {
|
|
192
|
+
console.error("Error initializing preview system:", error);
|
|
193
|
+
// Create preview cubes at fallback positions
|
|
194
|
+
prefabSystem.leftPreviewCube = createPreviewCube(
|
|
195
|
+
"red",
|
|
196
|
+
"Left Preview (Red)",
|
|
197
|
+
);
|
|
198
|
+
prefabSystem.rightPreviewCube = createPreviewCube(
|
|
199
|
+
"blue",
|
|
200
|
+
"Right Preview (Blue)",
|
|
201
|
+
);
|
|
202
|
+
TransformManager.SetLocalPosition(
|
|
203
|
+
prefabSystem.leftPreviewCube,
|
|
204
|
+
vec3.fromValues(-0.3, 1.5, 0),
|
|
205
|
+
);
|
|
206
|
+
TransformManager.SetLocalPosition(
|
|
207
|
+
prefabSystem.rightPreviewCube,
|
|
208
|
+
vec3.fromValues(0.3, 1.5, 0),
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
}, 2000); // Wait 2 seconds for VR system to initialize
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Update preview cube positions to follow controllers
|
|
216
|
+
*/
|
|
217
|
+
function updatePreviewPositions() {
|
|
218
|
+
try {
|
|
219
|
+
// Get controller WORLD positions (absolute in world space)
|
|
220
|
+
const leftPos = Device.GetLeftWorldPosition();
|
|
221
|
+
const rightPos = Device.GetRightWorldPosition();
|
|
222
|
+
|
|
223
|
+
// Update left preview cube (red) position - offset upward
|
|
224
|
+
if (
|
|
225
|
+
prefabSystem.leftPreviewCube &&
|
|
226
|
+
leftPos &&
|
|
227
|
+
Array.isArray(leftPos) &&
|
|
228
|
+
leftPos.length >= 3
|
|
229
|
+
) {
|
|
230
|
+
TransformManager.SetLocalPosition(
|
|
231
|
+
prefabSystem.leftPreviewCube,
|
|
232
|
+
vec3.fromValues(
|
|
233
|
+
Number(leftPos[0]),
|
|
234
|
+
Number(leftPos[1]) + 0.2, // Float 20cm above controller
|
|
235
|
+
Number(leftPos[2]),
|
|
236
|
+
),
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Update right preview cube (blue) position - offset upward
|
|
241
|
+
if (
|
|
242
|
+
prefabSystem.rightPreviewCube &&
|
|
243
|
+
rightPos &&
|
|
244
|
+
Array.isArray(rightPos) &&
|
|
245
|
+
rightPos.length >= 3
|
|
246
|
+
) {
|
|
247
|
+
TransformManager.SetLocalPosition(
|
|
248
|
+
prefabSystem.rightPreviewCube,
|
|
249
|
+
vec3.fromValues(
|
|
250
|
+
Number(rightPos[0]),
|
|
251
|
+
Number(rightPos[1]) + 0.2, // Float 20cm above controller
|
|
252
|
+
Number(rightPos[2]),
|
|
253
|
+
),
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
} catch (error) {
|
|
257
|
+
// Silently handle cases where controllers aren't available
|
|
258
|
+
console.warn("Error updating preview positions:", error);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Handle controller input for spawning cubes
|
|
264
|
+
*/
|
|
265
|
+
function handleControllerInput() {
|
|
266
|
+
try {
|
|
267
|
+
// Get current button states
|
|
268
|
+
const leftSelectValue = Device.GetLeftSelect();
|
|
269
|
+
const rightSelectValue = Device.GetRightSelect();
|
|
270
|
+
|
|
271
|
+
// Convert to simple pressed state (> 0.5 threshold)
|
|
272
|
+
// Handle both direct numbers and nested result objects
|
|
273
|
+
const leftSelectNum =
|
|
274
|
+
typeof leftSelectValue === "number" ? leftSelectValue : 0;
|
|
275
|
+
const rightSelectNum =
|
|
276
|
+
typeof rightSelectValue === "number" ? rightSelectValue : 0;
|
|
277
|
+
|
|
278
|
+
const leftPressed = leftSelectNum > 0.5 ? 1 : 0;
|
|
279
|
+
const rightPressed = rightSelectNum > 0.5 ? 1 : 0;
|
|
280
|
+
|
|
281
|
+
// Check for button press events (transition from not pressed to pressed)
|
|
282
|
+
if (leftPressed && !prefabSystem.leftButtonPrevState) {
|
|
283
|
+
spawnRedCube();
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
if (rightPressed && !prefabSystem.rightButtonPrevState) {
|
|
287
|
+
spawnBlueCube();
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// Update previous states
|
|
291
|
+
prefabSystem.leftButtonPrevState = leftPressed;
|
|
292
|
+
prefabSystem.rightButtonPrevState = rightPressed;
|
|
293
|
+
} catch (error) {
|
|
294
|
+
// Silently handle cases where controllers aren't available
|
|
295
|
+
console.warn("Error handling controller input:", error);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Spawn a red cube at the left controller position
|
|
301
|
+
*/
|
|
302
|
+
function spawnRedCube() {
|
|
303
|
+
try {
|
|
304
|
+
const leftPos = Device.GetLeftWorldPosition();
|
|
305
|
+
if (leftPos && Array.isArray(leftPos) && leftPos.length >= 3) {
|
|
306
|
+
// Spawn slightly forward from the controller
|
|
307
|
+
const spawnPos = vec3.fromValues(
|
|
308
|
+
Number(leftPos[0]),
|
|
309
|
+
Number(leftPos[1]),
|
|
310
|
+
Number(leftPos[2]) + 0.5, // 50cm forward
|
|
311
|
+
);
|
|
312
|
+
|
|
313
|
+
const newCube = createRedCubePrefab(spawnPos);
|
|
314
|
+
prefabSystem.spawnedCubes.push(newCube);
|
|
315
|
+
|
|
316
|
+
console.log(
|
|
317
|
+
`Spawned red cube #${prefabSystem.spawnedCubes.length} at world position:`,
|
|
318
|
+
spawnPos,
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
} catch (error) {
|
|
322
|
+
console.error("Error spawning red cube:", error);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Spawn a blue cube at the right controller position
|
|
328
|
+
*/
|
|
329
|
+
function spawnBlueCube() {
|
|
330
|
+
try {
|
|
331
|
+
const rightPos = Device.GetRightWorldPosition();
|
|
332
|
+
if (rightPos && Array.isArray(rightPos) && rightPos.length >= 3) {
|
|
333
|
+
// Spawn slightly forward from the controller
|
|
334
|
+
const spawnPos = vec3.fromValues(
|
|
335
|
+
Number(rightPos[0]),
|
|
336
|
+
Number(rightPos[1]),
|
|
337
|
+
Number(rightPos[2]) + 0.5, // 50cm forward
|
|
338
|
+
);
|
|
339
|
+
|
|
340
|
+
const newCube = createBlueCubePrefab(spawnPos);
|
|
341
|
+
prefabSystem.spawnedCubes.push(newCube);
|
|
342
|
+
|
|
343
|
+
console.log(
|
|
344
|
+
`Spawned blue cube #${prefabSystem.spawnedCubes.length} at world position:`,
|
|
345
|
+
spawnPos,
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
} catch (error) {
|
|
349
|
+
console.error("Error spawning blue cube:", error);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Clear all spawned cubes from the scene
|
|
355
|
+
*/
|
|
356
|
+
function clearAllSpawnedCubes() {
|
|
357
|
+
prefabSystem.spawnedCubes.forEach((entity) => {
|
|
358
|
+
EntityManager.Destroy(entity);
|
|
359
|
+
});
|
|
360
|
+
prefabSystem.spawnedCubes = [];
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Main update loop
|
|
365
|
+
*/
|
|
366
|
+
function update() {
|
|
367
|
+
updatePreviewPositions();
|
|
368
|
+
handleControllerInput();
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// Initialize the prefab system
|
|
372
|
+
initializePreviewSystem();
|
|
373
|
+
|
|
374
|
+
// Set up update loop (60 FPS) - start after initialization delay
|
|
375
|
+
setTimeout(() => {
|
|
376
|
+
setInterval(update, 16); // ~60 FPS
|
|
377
|
+
console.log("Update loop started");
|
|
378
|
+
}, 3000);
|
|
379
|
+
|
|
380
|
+
// Heartbeat for debugging
|
|
381
|
+
setInterval(() => {
|
|
382
|
+
console.log(
|
|
383
|
+
`Prefab System Status - Spawned cubes: ${prefabSystem.spawnedCubes.length}`,
|
|
384
|
+
);
|
|
385
|
+
|
|
386
|
+
// Log controller states for debugging
|
|
387
|
+
try {
|
|
388
|
+
// Local positions (relative to XR Origin)
|
|
389
|
+
const leftPos = Device.GetLeftPosition();
|
|
390
|
+
const rightPos = Device.GetRightPosition();
|
|
391
|
+
// World positions (absolute in world space)
|
|
392
|
+
const leftWorldPos = Device.GetLeftWorldPosition();
|
|
393
|
+
const rightWorldPos = Device.GetRightWorldPosition();
|
|
394
|
+
const leftSelect = Device.GetLeftSelect();
|
|
395
|
+
const rightSelect = Device.GetRightSelect();
|
|
396
|
+
|
|
397
|
+
console.log("Controller Debug:", {
|
|
398
|
+
leftLocalPos: leftPos,
|
|
399
|
+
rightLocalPos: rightPos,
|
|
400
|
+
leftWorldPos: leftWorldPos,
|
|
401
|
+
rightWorldPos: rightWorldPos,
|
|
402
|
+
leftSelect: leftSelect,
|
|
403
|
+
rightSelect: rightSelect,
|
|
404
|
+
leftWorldFormatted: Array.isArray(leftWorldPos)
|
|
405
|
+
? `[${leftWorldPos[0]}, ${leftWorldPos[1]}, ${leftWorldPos[2]}]`
|
|
406
|
+
: "Invalid",
|
|
407
|
+
rightWorldFormatted: Array.isArray(rightWorldPos)
|
|
408
|
+
? `[${rightWorldPos[0]}, ${rightWorldPos[1]}, ${rightWorldPos[2]}]`
|
|
409
|
+
: "Invalid",
|
|
410
|
+
});
|
|
411
|
+
} catch (error) {
|
|
412
|
+
console.log("Controller data not available:", error);
|
|
413
|
+
}
|
|
414
|
+
}, 30000);
|
|
415
|
+
|
|
416
|
+
console.log("VR Cube Prefab System initialized!");
|
|
417
|
+
console.log("- Red cube preview above left controller (press select to spawn)");
|
|
418
|
+
console.log(
|
|
419
|
+
"- Blue cube preview above right controller (press select to spawn)",
|
|
420
|
+
);
|
|
421
|
+
console.log("- Debug commands: 'clear_cubes', 'count_cubes'");
|
|
422
|
+
console.log("- Using WORLD positions for accurate placement in VR space");
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { StateSync } from "@adamas/networking/state-sync";
|
|
2
|
+
|
|
3
|
+
export const stateSyncExample = (): void => {
|
|
4
|
+
interface vec3 {
|
|
5
|
+
x: number;
|
|
6
|
+
y: number;
|
|
7
|
+
z: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const defaultVec3: vec3 = { x: 0, y: 0, z: 0 };
|
|
11
|
+
let variable = StateSync.CreateNetworkState<vec3>("ce", defaultVec3);
|
|
12
|
+
variable.x = 3;
|
|
13
|
+
|
|
14
|
+
console.log("GetNetworkID", StateSync.GetNetworkID());
|
|
15
|
+
let i = 0;
|
|
16
|
+
|
|
17
|
+
setInterval(() => {
|
|
18
|
+
if (StateSync.IsStateAuthority()) {
|
|
19
|
+
variable.x = i++;
|
|
20
|
+
console.log("[StateAuth TRUE] variable.x", variable.x);
|
|
21
|
+
} else {
|
|
22
|
+
console.log("[StateAuth FALSE] variable.x", variable.x);
|
|
23
|
+
}
|
|
24
|
+
}, 2000);
|
|
25
|
+
};
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2020",
|
|
4
|
+
"moduleResolution": "node16",
|
|
5
|
+
"resolveJsonModule": true,
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"sourceMap": true,
|
|
8
|
+
"declarationMap": true,
|
|
9
|
+
"typeRoots": ["./node_modules/@types"], // prevent typechecking in parent node_modules
|
|
10
|
+
"outDir": "dist",
|
|
11
|
+
"rootDir": "library",
|
|
12
|
+
"module": "node16",
|
|
13
|
+
"strict": true,
|
|
14
|
+
"noEmit": true
|
|
15
|
+
},
|
|
16
|
+
"exclude": ["samples", "index.ts", "loader-template.ts"],
|
|
17
|
+
"ts-node": {
|
|
18
|
+
"require": ["tsconfig-paths/register"]
|
|
19
|
+
}
|
|
20
|
+
}
|