Repository navigation
Expand file tree
/
Copy pathModel.js
More file actions
166 lines (164 loc) · 4.36 KB
/
Copy pathModel.js
File metadata and controls
166 lines (164 loc) · 4.36 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
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader'
import { MeshSurfaceSampler } from 'three/examples/jsm/math/MeshSurfaceSampler'
import {
Color,
AnimationMixer,
PointsMaterial,
Points,
MeshMatcapMaterial,
TextureLoader,
Vector3,
BufferGeometry,
Float32BufferAttribute,
AdditiveBlending,
MeshBasicMaterial,
Group,
Mesh,
} from 'three'
export default class Model {
constructor(obj) {
this.name = obj.name
this.meshes = obj.meshes
this.file = obj.url
this.scene = obj.scene
this.loader = new GLTFLoader()
this.dracoLoader = new DRACOLoader()
this.dracoLoader.setDecoderPath('./draco/')
this.loader.setDRACOLoader(this.dracoLoader)
this.textureLoader = new TextureLoader()
this.animations = obj.animationState || false
this.replaceMaterials = obj.replace || false
this.defaultMatcap = obj.replaceURL
? this.textureLoader.load(`${obj.replaceURL}`)
: this.textureLoader.load('/mat.png')
this.mixer = null
this.mixers = obj.mixers
this.defaultParticle = obj.particleURL
? this.textureLoader.load(`${obj.particleURL}`)
: this.textureLoader.load('/10.png')
this.scale = obj.scale || new Vector3(1, 1, 1)
this.position = obj.position || new Vector3(0, 0, 0)
this.rotation = obj.rotation || new Vector3(0, 0, 0)
this.palette = [
new Color('#FAAD80'),
new Color('#FF6767'),
new Color('#FF3D68'),
new Color('#A73489'),
]
}
init() {
this.loader.load(this.file, (gltf) => {
this.mesh = gltf.scene.children[0]
if (this.replaceMaterials) {
const replacementMaterial = new MeshMatcapMaterial({
matcap: this.defaultMatcap,
})
gltf.scene.traverse((child) => {
if (child.isMesh) {
child.material = replacementMaterial
}
})
}
gltf.scene.traverse((child) => {
if (child.isMesh){
child.material.side = 0
}
})
if (this.animations) {
this.mixer = new AnimationMixer(gltf.scene)
gltf.animations.forEach((clip) => {
this.mixer.clipAction(clip).play()
})
this.mixers.push(this.mixer)
}
this.meshes[`${this.name}`] = gltf.scene
this.meshes[`${this.name}`].position.set(
this.position.x,
this.position.y,
this.position.z
)
this.meshes[`${this.name}`].scale.set(
this.scale.x,
this.scale.y,
this.scale.z
)
this.meshes[`${this.name}`].rotation.set(
this.rotation.x,
this.rotation.y,
this.rotation.z
)
this.meshes[`${this.name}`].userData.groupName = this.name
this.scene.add(this.meshes[`${this.name}`])
})
}
initPoints() {
this.loader.load(this.file, (gltf) => {
const meshes = []
const pointCloud = new Group()
gltf.scene.traverse((child) => {
if (child.isMesh) {
meshes.push(child)
}
})
for (const mesh of meshes) {
pointCloud.add(this.createPoints(mesh))
}
console.log(pointCloud)
this.meshes[`${this.name}`] = pointCloud
this.meshes[`${this.name}`].scale.set(
this.scale.x,
this.scale.y,
this.scale.z
)
this.meshes[`${this.name}`].position.set(
this.position.x,
this.position.y,
this.position.z
)
this.meshes[`${this.name}`].rotation.set(
this.rotation.x,
this.rotation.y,
this.rotation.z
)
this.scene.add(this.meshes[`${this.name}`])
})
}
createPoints(_mesh) {
const sampler = new MeshSurfaceSampler(_mesh).build()
const numParticles = 3000
const particlesPosition = new Float32Array(numParticles * 3)
const particleColors = new Float32Array(numParticles * 3)
const newPosition = new Vector3()
for (let i = 0; i < numParticles; i++) {
sampler.sample(newPosition)
const color =
this.palette[Math.floor(Math.random() * this.palette.length)]
particleColors.set([color.r, color.g, color.b], i * 3)
particlesPosition.set(
[newPosition.x, newPosition.y, newPosition.z],
i * 3
)
}
const pointsGeometry = new BufferGeometry()
pointsGeometry.setAttribute(
'position',
new Float32BufferAttribute(particlesPosition, 3)
)
pointsGeometry.setAttribute(
'color',
new Float32BufferAttribute(particleColors, 3)
)
const pointsMaterial = new PointsMaterial({
vertexColors: true,
transparent: true,
alphaMap: this.defaultParticle,
alphaTest: 0.001,
depthWrite: false,
blending: AdditiveBlending,
size: 0.12,
})
const points = new Points(pointsGeometry, pointsMaterial)
return points
}
}