I cloned the Splat Playground studio and tried to replace the sphere with a leaf 3D model. I don’t know why the leaf doesn’t show up. This is my code, I also publish the project on my page:
import * as ecs from '@8thwall/ecs'
const spawner = ecs.registerComponent({
name: 'sphere-spawner',
schema: {
spawnInterval: ecs.f32,
leafMass: ecs.f32,
},
schemaDefaults: {
spawnInterval: 150,
leafMass: 0.1, // Adjust mass for leaf behavior
},
data: {
lastSpawnTime: ecs.f64,
},
add: (world, component) => {
component.data.lastSpawnTime = world.time.elapsed
},
tick: (world, component) => {
const currentTime = world.time.elapsed
const {spawnInterval, leafMass} = component.schema
const {lastSpawnTime} = component.data
if (currentTime - lastSpawnTime >= spawnInterval) {
// Create a new entity for the leaf
const leafEntity = world.createEntity()
// Calculate a random position within a range (adjust Y position if needed for floating)
const randomX = (Math.random() * 2 - 1) * 0.05
const randomZ = (Math.random() * 2 - 1) * 0.05
const randomY = 2 + (Math.random() * 0.5) // Random Y for floating effect
// Set the position of the leaf entity
ecs.Position.set(world, leafEntity, {x: randomX, y: randomY, z: randomZ})
// Add the GLTF model to the entity using ecs.GltfModel
ecs.GltfModel.set(world, leafEntity, {
src: './assets/red_fall_leaf.glb', // Path to your .glb model file
castShadow: true,
receiveShadow: true,
})
// Add random rotation for variety
ecs.Quaternion.set(world, leafEntity, {
x: Math.random(),
y: Math.random(),
z: Math.random(),
w: Math.random(),
})
// Configure physics collider (if necessary, adjust for leaves)
ecs.Collider.set(world, leafEntity, {
shape: ecs.ColliderShape.Box, // Adjust collider shape if needed
mass: leafMass,
restitution: 0.2, // Make leaves float more slowly
friction: 0.2,
})
// Set a timeout to remove the entity after 10 seconds (optional)
world.time.setTimeout(() => {
world.deleteEntity(leafEntity)
}, 10000) // Adjust time if you want leaves to stay longer
// Update last spawn time
component.data.lastSpawnTime = currentTime
}
},
remove: (world, component) => {
// Clean up code if needed
},
})
export {spawner}