Skip to content
Merged

dev #35

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 110 additions & 1 deletion packages/example/resources/mesh.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,114 @@
}
]
}
]
],

"materials": {
"mesh": {
"type": "MeshStandardMaterial",
"color": 16777215,
"roughness": 1,
"metalness": 0,
"emissive": 0,
"envMapRotation": [0, 0, 0, "XYZ"],
"envMapIntensity": 1,
"blending": 2,
"blendColor": 0
}
},
"geometries": {
"cube": {
"metadata": {
"version": 4.7,
"type": "BufferGeometry",
"generator": "BufferGeometry.toJSON"
},
"uuid": "f84219bf-6602-4909-ae37-7921da261845",
"type": "BufferGeometry",
"data": {
"attributes": {
"position": {
"itemSize": 3,
"type": "Float32Array",
"array": [
0.05, 0.05, 0.05, 0.05, -0.05, 0.05, 0.05, 0.05,
-0.05, 0.05, -0.05, 0.05, 0.05, -0.05, -0.05, 0.05,
0.05, -0.05, -0.05, 0.05, -0.05, -0.05, -0.05,
-0.05, -0.05, 0.05, 0.05, -0.05, -0.05, -0.05,
-0.05, -0.05, 0.05, -0.05, 0.05, 0.05, -0.05, 0.05,
-0.05, -0.05, 0.05, 0.05, 0.05, 0.05, -0.05, -0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, -0.05,
-0.05, -0.05, 0.05, -0.05, -0.05, -0.05, 0.05,
-0.05, 0.05, -0.05, -0.05, -0.05, 0.05, -0.05,
-0.05, 0.05, -0.05, 0.05, -0.05, 0.05, 0.05, -0.05,
-0.05, 0.05, 0.05, 0.05, 0.05, -0.05, -0.05, 0.05,
0.05, -0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
-0.05, 0.05, -0.05, -0.05, -0.05, 0.05, -0.05, 0.05,
-0.05, -0.05, -0.05, -0.05, -0.05, -0.05, 0.05,
-0.05
],
"normalized": false
},
"normal": {
"itemSize": 3,
"type": "Float32Array",
"array": [
1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0,
-1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0,
0, -1, 0, 0, -1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1,
0, 0, -1, 0, 0, -1, 0, 0, -1
],
"normalized": false
},
"uv": {
"itemSize": 2,
"type": "Float32Array",
"array": [
0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1,
1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0,
1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0,
0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0,
1, 0, 1, 1
],
"normalized": false
}
},
"groups": [
{
"start": 0,
"count": 6,
"materialIndex": 0
},
{
"start": 6,
"count": 6,
"materialIndex": 1
},
{
"start": 12,
"count": 6,
"materialIndex": 2
},
{
"start": 18,
"count": 6,
"materialIndex": 3
},
{
"start": 24,
"count": 6,
"materialIndex": 4
},
{
"start": 30,
"count": 6,
"materialIndex": 5
}
]
}
}
}
}
58 changes: 0 additions & 58 deletions packages/example/src/axesHelper.ts

This file was deleted.

17 changes: 2 additions & 15 deletions packages/example/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
AmbientLight,
BoxGeometry,
AxesHelper,
Clock,
Color,
DirectionalLight,
Expand All @@ -16,8 +16,6 @@ import {
} from 'three'
import { ParticleEffect, ParticleEffectLoader } from 'three-particles'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'
import { addAxesHelper } from './axesHelper'
import { AdditiveBlending } from 'three/src/constants'

console.log('Hello!')

Expand All @@ -37,7 +35,7 @@ grid.material.opacity = 0.2
grid.material.transparent = true
scene.add(grid)

addAxesHelper(scene)
scene.add(new AxesHelper(3))

const canvas = document.querySelector('#mainCanvas') as HTMLCanvasElement

Expand Down Expand Up @@ -90,17 +88,6 @@ function onResize() {
let particleEffect: ParticleEffect | null = null
const loader = new ParticleEffectLoader()

// Provide external material and geometry for the effect
loader.setMaterials({
mesh: new MeshStandardMaterial({
color: 0xffffff,
metalness: 0,
roughness: 1,
blending: AdditiveBlending,
}),
})
loader.setGeometries({ cube: new BoxGeometry(0.1, 0.1, 0.1) })

loader
.loadAsync('./mesh.json')
.then((model) => {
Expand Down
9 changes: 4 additions & 5 deletions packages/three-particles/src/object/ParticleEffect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,29 @@ export class ParticleEffect extends Group {
*/
update(dT: number): void {
this.forEachEmitter((instance) => {
instance.state.update(dT)
instance.updateGeometry()
instance.update(dT)
})
}

/**
* Rewinds all emitters.
*/
rewind(): void {
this.forEachEmitter((instance) => instance.state.rewind())
this.forEachEmitter((instance) => instance.rewind())
}

/**
* Stops all emitters
*/
stop(allowCompletion: boolean): void {
this.forEachEmitter((instance) => instance.state.stop(allowCompletion))
this.forEachEmitter((instance) => instance.stop(allowCompletion))
}

/**
* Resets all emitters.
*/
reset(): void {
this.forEachEmitter((instance) => instance.state.reset())
this.forEachEmitter((instance) => instance.reset())
}

clone(): this {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class ParticleEmitterInstancedMesh
implements ParticleEmitterObject
{
readonly isParticleEmitterObject = true as const
readonly state: ParticleEmitterState
private readonly state: ParticleEmitterState

private readonly color = new Color()
private readonly capacity: number
Expand All @@ -33,7 +33,9 @@ export class ParticleEmitterInstancedMesh
this.frustumCulled = false
}

updateGeometry(): void {
update(dT: number): void {
// progress simulation
this.state.update(dT)
if (!this.state.model.enabled) return

let index = 0
Expand All @@ -44,6 +46,10 @@ export class ParticleEmitterInstancedMesh
// Position
this.obj.position.copy(p.position)

// Rotation
this.obj.rotation.copy(p.rotationFinal)

// Scale
this.obj.scale.copy(p.scale)

this.obj.updateMatrix()
Expand All @@ -62,4 +68,16 @@ export class ParticleEmitterInstancedMesh
this.instanceMatrix.needsUpdate = true
if (this.instanceColor) this.instanceColor.needsUpdate = true
}

rewind(): void {
this.state.rewind()
}

stop(allowCompletion: boolean): void {
this.state.stop(allowCompletion)
}

reset(): void {
this.state.reset()
}
}
9 changes: 4 additions & 5 deletions packages/three-particles/src/object/ParticleEmitterObject.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { ParticleEmitterState } from '../state'

export interface ParticleEmitterObject {
readonly isParticleEmitterObject: true

readonly state: ParticleEmitterState

updateGeometry(): void
update(dT: number): void
rewind(): void
stop(allowCompletion: boolean): void
reset(): void
}

export function isParticleEmitterObject(
Expand Down
24 changes: 17 additions & 7 deletions packages/three-particles/src/object/ParticleEmitterPoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class ParticleEmitterPoints
implements ParticleEmitterObject
{
readonly isParticleEmitterObject = true
readonly state: ParticleEmitterState
private readonly state: ParticleEmitterState

constructor(model: ParticleEmitterModel) {
super(model.geometry ?? undefined, model.material ?? undefined)
Expand All @@ -35,13 +35,11 @@ export class ParticleEmitterPoints
// this.geometry.boundingSphere = new Sphere(new Vector3(0, 0, 0), 10)
}

/**
* Updates this emitter's buffer geometry.
* Typically invoked by the particle effect that owns this emitter.
*/
updateGeometry(): void {
update(dT: number): void {
// Progress internal particle simulation
this.state.update(dT)
// Update geometry buffers from state
if (!this.state.model.enabled) return
// Access the typed array for position data:
const posArr = this.geometry.attributes.position.array as Float32Array
const colorArr = this.geometry.attributes.color.array as Float32Array

Expand All @@ -67,4 +65,16 @@ export class ParticleEmitterPoints
this.geometry.attributes.position.needsUpdate = true
this.geometry.attributes.color.needsUpdate = true
}

rewind(): void {
this.state.rewind()
}

stop(allowCompletion: boolean): void {
this.state.stop(allowCompletion)
}

reset(): void {
this.state.reset()
}
}
Loading