@fils/phy-three 0.0.1
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/lib/helper/ThreePhyHelper.d.ts +18 -0
- package/lib/helper/ThreePhyHelper.js +75 -0
- package/lib/main.d.ts +3 -0
- package/lib/main.js +3 -0
- package/lib/mesh/PhyThreeMesh.d.ts +28 -0
- package/lib/mesh/PhyThreeMesh.js +48 -0
- package/package.json +26 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phy Three Helper
|
|
3
|
+
* A ThreeJS helper for @fils/phy
|
|
4
|
+
* Supporting particles and springs
|
|
5
|
+
* To-Do: Move to @fils/phy-three
|
|
6
|
+
*/
|
|
7
|
+
import { Physics } from "@fils/phy";
|
|
8
|
+
import { InstancedMesh, LineSegments, Object3D } from "three";
|
|
9
|
+
export interface PhyThreeHelperParameters {
|
|
10
|
+
particleRadius?: number;
|
|
11
|
+
}
|
|
12
|
+
export declare class PhyThreeHelper extends Object3D {
|
|
13
|
+
particles: InstancedMesh;
|
|
14
|
+
physics: Physics;
|
|
15
|
+
springs: LineSegments;
|
|
16
|
+
constructor(phy: Physics, _params?: PhyThreeHelperParameters);
|
|
17
|
+
update(): void;
|
|
18
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phy Three Helper
|
|
3
|
+
* A ThreeJS helper for @fils/phy
|
|
4
|
+
* Supporting particles and springs
|
|
5
|
+
* To-Do: Move to @fils/phy-three
|
|
6
|
+
*/
|
|
7
|
+
import { BufferAttribute, BufferGeometry, Color, InstancedMesh, LineBasicMaterial, LineSegments, MeshBasicMaterial, Object3D, SphereGeometry } from "three";
|
|
8
|
+
const dummy = new Object3D();
|
|
9
|
+
const ParticleMat = new MeshBasicMaterial({
|
|
10
|
+
color: 0xffffff
|
|
11
|
+
});
|
|
12
|
+
const color1 = new Color(0xee3333);
|
|
13
|
+
const color2 = new Color(0x3333ee);
|
|
14
|
+
export class PhyThreeHelper extends Object3D {
|
|
15
|
+
constructor(phy, _params = {}) {
|
|
16
|
+
super();
|
|
17
|
+
this.physics = phy;
|
|
18
|
+
const ParticleGeo = new SphereGeometry(_params.particleRadius || .02, 32, 16);
|
|
19
|
+
this.particles = new InstancedMesh(ParticleGeo, ParticleMat, phy.particles.length);
|
|
20
|
+
let i = 0;
|
|
21
|
+
for (const p of phy.particles) {
|
|
22
|
+
this.particles.setColorAt(i++, p.locked ? color1 : color2);
|
|
23
|
+
}
|
|
24
|
+
this.add(this.particles);
|
|
25
|
+
const pos = [];
|
|
26
|
+
const col = [];
|
|
27
|
+
for (const s of phy.springs) {
|
|
28
|
+
const a = s.a.position;
|
|
29
|
+
const b = s.b.position;
|
|
30
|
+
pos.push(a.x, a.y, a.z);
|
|
31
|
+
pos.push(b.x, b.y, b.z);
|
|
32
|
+
if (s.a.locked)
|
|
33
|
+
col.push(color1.r, color1.g, color1.b);
|
|
34
|
+
else
|
|
35
|
+
col.push(color2.r, color2.g, color2.b);
|
|
36
|
+
if (s.b.locked)
|
|
37
|
+
col.push(color1.r, color1.g, color1.b);
|
|
38
|
+
else
|
|
39
|
+
col.push(color2.r, color2.g, color2.b);
|
|
40
|
+
}
|
|
41
|
+
const geo = new BufferGeometry();
|
|
42
|
+
geo.setAttribute('position', new BufferAttribute(new Float32Array(pos), 3));
|
|
43
|
+
geo.setAttribute('color', new BufferAttribute(new Float32Array(col), 3));
|
|
44
|
+
this.springs = new LineSegments(geo, new LineBasicMaterial({
|
|
45
|
+
color: 0xffffff,
|
|
46
|
+
vertexColors: true
|
|
47
|
+
}));
|
|
48
|
+
this.add(this.springs);
|
|
49
|
+
this.update();
|
|
50
|
+
}
|
|
51
|
+
update() {
|
|
52
|
+
const particles = this.physics.particles;
|
|
53
|
+
for (let i = 0, len = particles.length; i < len; i++) {
|
|
54
|
+
const p = particles[i];
|
|
55
|
+
dummy.position.copy(p.position);
|
|
56
|
+
dummy.updateMatrix();
|
|
57
|
+
this.particles.setMatrixAt(i, dummy.matrix);
|
|
58
|
+
}
|
|
59
|
+
this.particles.instanceMatrix.needsUpdate = true;
|
|
60
|
+
const pos = this.springs.geometry.attributes.position;
|
|
61
|
+
let k = 0;
|
|
62
|
+
for (let i = 0, len = this.physics.springs.length; i < len; i++) {
|
|
63
|
+
const s = this.physics.springs[i];
|
|
64
|
+
pos.array[k * 3] = s.a.position.x;
|
|
65
|
+
pos.array[k * 3 + 1] = s.a.position.y;
|
|
66
|
+
pos.array[k * 3 + 2] = s.a.position.z;
|
|
67
|
+
k++;
|
|
68
|
+
pos.array[k * 3] = s.b.position.x;
|
|
69
|
+
pos.array[k * 3 + 1] = s.b.position.y;
|
|
70
|
+
pos.array[k * 3 + 2] = s.b.position.z;
|
|
71
|
+
k++;
|
|
72
|
+
}
|
|
73
|
+
pos.needsUpdate = true;
|
|
74
|
+
}
|
|
75
|
+
}
|
package/lib/main.d.ts
ADDED
package/lib/main.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Vec } from "@fils/math";
|
|
2
|
+
import { Particle, Physics } from "@fils/phy";
|
|
3
|
+
import { Mesh } from "three";
|
|
4
|
+
/**
|
|
5
|
+
* Mesh Particle
|
|
6
|
+
*/
|
|
7
|
+
export interface MeshParticleParameters {
|
|
8
|
+
x: number;
|
|
9
|
+
y: number;
|
|
10
|
+
z: number;
|
|
11
|
+
}
|
|
12
|
+
export declare class MeshParticle extends Particle {
|
|
13
|
+
restPosition: Vec;
|
|
14
|
+
constructor(_params: MeshParticleParameters, id?: number, drag?: number);
|
|
15
|
+
}
|
|
16
|
+
export interface PhyMeshParameters {
|
|
17
|
+
dragGen?(): number;
|
|
18
|
+
springStrGen?(): number;
|
|
19
|
+
useQuad?: boolean;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Wrapper to create a phy mesh
|
|
23
|
+
* with particles and springs given a ThreeJS Mesh
|
|
24
|
+
*/
|
|
25
|
+
export declare class PhyThreeMesh extends Physics {
|
|
26
|
+
particles: MeshParticle[];
|
|
27
|
+
constructor(mesh: Mesh, _params?: PhyMeshParameters);
|
|
28
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Vec } from "@fils/math";
|
|
2
|
+
import { Particle, Physics, Spring } from "@fils/phy";
|
|
3
|
+
import { Vector3 } from "three";
|
|
4
|
+
export class MeshParticle extends Particle {
|
|
5
|
+
constructor(_params, id, drag) {
|
|
6
|
+
super(id, drag);
|
|
7
|
+
this.setPosition(_params.x, _params.y, _params.z);
|
|
8
|
+
this.restPosition = new Vec(_params.x, _params.y, _params.z);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
const DEFAULT_DRAG = .5;
|
|
12
|
+
const DEFAULT_SPRING_STRENGTH = .5;
|
|
13
|
+
const tmp = new Vector3();
|
|
14
|
+
/**
|
|
15
|
+
* Wrapper to create a phy mesh
|
|
16
|
+
* with particles and springs given a ThreeJS Mesh
|
|
17
|
+
*/
|
|
18
|
+
export class PhyThreeMesh extends Physics {
|
|
19
|
+
constructor(mesh, _params = {}) {
|
|
20
|
+
super();
|
|
21
|
+
mesh.updateMatrix();
|
|
22
|
+
const geo = mesh.geometry;
|
|
23
|
+
const pos = geo.attributes.position;
|
|
24
|
+
// add particles
|
|
25
|
+
for (let i = 0; i < pos.count; i++) {
|
|
26
|
+
tmp.set(pos.array[i * 3], pos.array[i * 3 + 1], pos.array[i * 3 + 2]).applyMatrix4(mesh.matrix);
|
|
27
|
+
const p = new MeshParticle({
|
|
28
|
+
x: tmp.x,
|
|
29
|
+
y: tmp.y,
|
|
30
|
+
z: tmp.z
|
|
31
|
+
}, i, _params.dragGen ? _params.dragGen() : DEFAULT_DRAG);
|
|
32
|
+
this.addParticle(p);
|
|
33
|
+
}
|
|
34
|
+
// add springs
|
|
35
|
+
const index = geo.index;
|
|
36
|
+
const part = this.particles;
|
|
37
|
+
for (let i = 0; i < index.count / 3; i++) {
|
|
38
|
+
const s1 = new Spring(part[index.array[i * 3]], part[index.array[i * 3 + 1]], _params.springStrGen ? _params.springStrGen() : DEFAULT_SPRING_STRENGTH);
|
|
39
|
+
this.addSpring(s1);
|
|
40
|
+
if (_params.useQuad !== true) {
|
|
41
|
+
const s2 = new Spring(part[index.array[i * 3 + 1]], part[index.array[i * 3 + 2]], _params.springStrGen ? _params.springStrGen() : DEFAULT_SPRING_STRENGTH);
|
|
42
|
+
this.addSpring(s2);
|
|
43
|
+
const s3 = new Spring(part[index.array[i * 3 + 2]], part[index.array[i * 3]], _params.springStrGen ? _params.springStrGen() : DEFAULT_SPRING_STRENGTH);
|
|
44
|
+
this.addSpring(s3);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fils/phy-three",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "ThreeJS utils for @phy/three",
|
|
5
|
+
"main": "lib/main.js",
|
|
6
|
+
"repository": "git@github.com:fil-studio/fils.git",
|
|
7
|
+
"homepage": "https://github.com/fil-studio/fils/tree/main/packages/phy-three",
|
|
8
|
+
"author": "Fil Studio <hello@fil.studio>",
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"prepare": "yarn build",
|
|
12
|
+
"build": "tsc"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"lib"
|
|
16
|
+
],
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"typescript": "^4.5.4"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@fils/phy": "^0.0.4"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"three": "*"
|
|
25
|
+
}
|
|
26
|
+
}
|