Hello, I tried to copy and edit the script - spawner.ts from ‘Studio: Splat Playground’ into my custom VPS project studio. But the spawned balls didnt show like in the simulator.
import * as ecs from '@8thwall/ecs'
const colors = [
'#ff3b30', '#ff9500', '#ffcc00', '#4cd964', '#5ac8fa', '#007aff', '#AD50FF', '#ff2d90',
]
const hexToRgb = (hex) => {
hex = hex.replace(/^#/, '')
let r, g, b
if (hex.length === 3) {
r = parseInt(hex[0] + hex[0], 16)
g = parseInt(hex[1] + hex[1], 16)
b = parseInt(hex[2] + hex[2], 16)
} else if (hex.length === 6) {
r = parseInt(hex.substring(0, 2), 16)
g = parseInt(hex.substring(2, 4), 16)
b = parseInt(hex.substring(4, 6), 16)
} else {
throw new Error('Invalid hex code')
}
return { r, g, b }
}
const spawner = ecs.registerComponent({
name: 'sphere-spawner',
schema: {
spawnInterval: ecs.f32,
sphereRadius: ecs.f32,
sphereMass: ecs.f32,
spawnPositionX: ecs.f32, // Custom X-axis position
spawnPositionZ: ecs.f32, // Custom Z-axis position
spawnHeight: ecs.f32, // Spawn height adjustment (Y-axis)
},
schemaDefaults: {
spawnInterval: 150,
sphereRadius: 0.03,
sphereMass: 1,
spawnPositionX: 0, // Default position on X
spawnPositionZ: 0, // Default position on Z
spawnHeight: 2, // Default spawn height
},
data: {
lastSpawnTime: ecs.f64,
},
add: (world, component) => {
component.data.lastSpawnTime = world.time.elapsed
},
tick: (world, component) => {
const currentTime = world.time.elapsed
const { spawnInterval, sphereRadius, sphereMass, spawnPositionX, spawnPositionZ, spawnHeight } = component.schema
const { lastSpawnTime } = component.data
if (currentTime - lastSpawnTime >= spawnInterval) {
const sphereEntity = world.createEntity()
// Use custom X, Z, and Y positions
world.setPosition(sphereEntity, spawnPositionX, spawnHeight, spawnPositionZ)
ecs.SphereGeometry.set(world, sphereEntity, { radius: sphereRadius })
const randColor = hexToRgb(colors[Math.floor(Math.random() * colors.length)])
ecs.Material.set(world, sphereEntity, {
r: randColor.r,
g: randColor.g,
b: randColor.b,
roughness: 0,
metalness: 0,
})
ecs.Collider.set(world, sphereEntity, {
shape: ecs.ColliderShape.Sphere,
radius: sphereRadius,
mass: sphereMass,
restitution: 0.6,
friction: 0.3,
rollingFriction: 0.1,
spinningFriction: 0.1,
})
world.time.setTimeout(() => {
world.deleteEntity(sphereEntity)
}, 10000)
component.data.lastSpawnTime = currentTime
}
},
remove: (world, component) => {
// Clean up code if needed
},
})
export { spawner }