Repository navigation
Expand file tree
/
Copy pathbox-stack.mjs
More file actions
228 lines (194 loc) · 7.63 KB
/
Copy pathbox-stack.mjs
File metadata and controls
228 lines (194 loc) · 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* Physics Pyramid Plugin - Creates a pyramid of boxes with spheres
*
* Features demonstrated:
* - Hex color strings (boxColor, sphereColor) → show color picker in UI
* - Number properties with reasonable ranges
* - Physics simulation with customizable parameters
*
* Properties:
* - baseSize: Number of boxes on the base row (2-10)
* - boxSize: Size of each box in world units
* - boxColor: Color of the boxes (#hex string → color picker)
* - sphereCount: Number of spheres to create
* - sphereSize: Size of spheres
* - sphereColor: Color of spheres (#hex string → color picker)
* - sphereHeight: Starting height for spheres
* - emissiveStrength: Glow intensity (0-1)
* - mass: Mass of each object (0 = static)
* - friction: Friction coefficient
* - restitution: Bounciness (0-1)
*/
export class BoxStack extends ArrivalScript {
static scriptName = 'Box Stack';
// Public properties - configurable in UI
baseSize = 5;
boxSize = 0.3;
boxColor = '#4a90d9';
sphereCount = 3;
sphereSize = 0.25;
sphereColor = '#d94a4a';
sphereHeight = 3;
emissiveStrength = 0.2;
mass = 1;
friction = 0.5;
restitution = 0.3;
static properties = {
baseSize: { title: 'Base Size', min: 2, max: 10 },
boxSize: { title: 'Box Size', min: 0.05, max: 2 },
boxColor: { title: 'Box Color' },
sphereCount: { title: 'Sphere Count', min: 0, max: 10 },
sphereSize: { title: 'Sphere Size', min: 0.05, max: 2 },
sphereColor: { title: 'Sphere Color' },
sphereHeight: { title: 'Sphere Height', min: 0, max: 20 },
emissiveStrength: { title: 'Emissive Strength', min: 0, max: 1 },
mass: { title: 'Mass (0 = static)', min: 0, max: 100 },
friction: { title: 'Friction', min: 0, max: 1 },
restitution: { title: 'Restitution', min: 0, max: 1 }
};
// Private properties
_boxes = [];
_spheres = [];
_boxMaterial = null;
_sphereMaterial = null;
initialize() {
console.log('Physics Pyramid initialized on entity:', this.entity.name);
// Create the materials
this._createMaterials();
// Build initial pyramid and spheres
this._rebuildAll();
}
_hexToRgb(hex) {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
if (result) {
return {
r: parseInt(result[1], 16) / 255,
g: parseInt(result[2], 16) / 255,
b: parseInt(result[3], 16) / 255
};
}
return { r: 0.5, g: 0.5, b: 0.5 };
}
_createMaterials() {
// Box material
const boxRgb = this._hexToRgb(this.boxColor);
this._boxMaterial = new pc.StandardMaterial();
this._boxMaterial.diffuse = new pc.Color(boxRgb.r, boxRgb.g, boxRgb.b);
this._boxMaterial.emissive = new pc.Color(
boxRgb.r * this.emissiveStrength,
boxRgb.g * this.emissiveStrength,
boxRgb.b * this.emissiveStrength
);
this._boxMaterial.update();
// Sphere material
const sphereRgb = this._hexToRgb(this.sphereColor);
this._sphereMaterial = new pc.StandardMaterial();
this._sphereMaterial.diffuse = new pc.Color(sphereRgb.r, sphereRgb.g, sphereRgb.b);
this._sphereMaterial.emissive = new pc.Color(
sphereRgb.r * this.emissiveStrength,
sphereRgb.g * this.emissiveStrength,
sphereRgb.b * this.emissiveStrength
);
this._sphereMaterial.update();
}
_clearAll() {
for (const box of this._boxes) {
if (box && !box._destroyed) box.destroy();
}
this._boxes = [];
for (const sphere of this._spheres) {
if (sphere && !sphere._destroyed) sphere.destroy();
}
this._spheres = [];
}
_rebuildAll() {
this._clearAll();
const base = Math.max(2, Math.min(10, Math.floor(this.baseSize)));
const size = Math.max(0.05, this.boxSize);
// Build pyramid: each layer has one less box per side
for (let layer = 0; layer < base; layer++) {
const layerSize = base - layer;
const yPos = (size / 2) + layer * size;
// Offset to center each layer
const offset = (layerSize - 1) * size / 2;
for (let x = 0; x < layerSize; x++) {
for (let z = 0; z < layerSize; z++) {
const box = new pc.Entity(`PyramidBox_${layer}_${x}_${z}`);
this.entity.addChild(box);
box.addComponent('render', {
type: 'box',
material: this._boxMaterial
});
const xPos = x * size - offset;
const zPos = z * size - offset;
box.setLocalPosition(xPos, yPos, zPos);
box.setLocalScale(size, size, size);
box.addComponent('collision', {
type: 'box',
halfExtents: new pc.Vec3(size / 2, size / 2, size / 2)
});
box.addComponent('rigidbody', {
type: this.mass > 0 ? 'dynamic' : 'static',
mass: this.mass,
friction: this.friction,
restitution: this.restitution
});
this._boxes.push(box);
}
}
}
// Create spheres
const sphereCount = Math.max(0, Math.min(10, Math.floor(this.sphereCount)));
const sphereRadius = Math.max(0.05, this.sphereSize / 2);
for (let i = 0; i < sphereCount; i++) {
const sphere = new pc.Entity(`Sphere_${i}`);
this.entity.addChild(sphere);
sphere.addComponent('render', {
type: 'sphere',
material: this._sphereMaterial
});
// Position spheres spread out above the pyramid
const angle = (i / sphereCount) * Math.PI * 2;
const radius = 0.5 + i * 0.2;
const xPos = Math.sin(angle) * radius;
const zPos = Math.cos(angle) * radius;
const yPos = this.sphereHeight + i * 0.3;
sphere.setLocalPosition(xPos, yPos, zPos);
sphere.setLocalScale(this.sphereSize, this.sphereSize, this.sphereSize);
sphere.addComponent('collision', {
type: 'sphere',
radius: sphereRadius
});
sphere.addComponent('rigidbody', {
type: this.mass > 0 ? 'dynamic' : 'static',
mass: this.mass * 0.5, // Spheres are lighter
friction: this.friction,
restitution: this.restitution + 0.2 // Spheres are bouncier
});
this._spheres.push(sphere);
}
console.log(`Physics Pyramid: Created ${this._boxes.length} boxes and ${this._spheres.length} spheres`);
}
onPropertyChanged(name, value, oldValue) {
// Structural changes require full rebuild
if (name === 'baseSize' || name === 'boxSize' || name === 'sphereCount') {
this._rebuildAll();
return;
}
// Material color changes
if (name === 'boxColor' || name === 'sphereColor' || name === 'emissiveStrength') {
this._createMaterials();
}
}
destroy() {
this._clearAll();
if (this._boxMaterial) {
this._boxMaterial.destroy();
this._boxMaterial = null;
}
if (this._sphereMaterial) {
this._sphereMaterial.destroy();
this._sphereMaterial = null;
}
}
}