Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 68e51c4

Browse files
authoredFeb 25, 2021
Merge pull request #54 from Floppy/obj-loading
Load OBJ files properly
2 parents 8c4245e + 86b7c58 commit 68e51c4

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed
 

‎app/javascript/src/preview.js

+24-18
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@ class PartPreview {
77
this.canvas = canvas
88
this.scene = new THREE.Scene()
99
this.renderer = new THREE.WebGLRenderer({ canvas: this.canvas })
10-
this.objects = new THREE.Group()
11-
this.scene.add(this.objects)
12-
this.material = new THREE.MeshNormalMaterial({
13-
flatShading: true
14-
})
15-
this.geometry = null
1610
this.camera = new THREE.PerspectiveCamera(45, this.canvas.width / this.canvas.height, 0.1, 1000)
1711
this.camera.position.z = 50
1812
// Trigger loading when canvas becomes visible
@@ -44,21 +38,30 @@ class PartPreview {
4438
}
4539

4640
onLoad (model) {
41+
const material = new THREE.MeshNormalMaterial({
42+
flatShading: true
43+
})
44+
let object = null
4745
if (model.type === 'BufferGeometry') {
48-
this.geometry = model
46+
object = new THREE.Mesh(model, material)
4947
} else {
50-
this.geometry = model.geometry || model.children[0].geometry
48+
model.traverse(function (node) {
49+
if (node instanceof THREE.Mesh) {
50+
node.material = material
51+
}
52+
})
53+
object = model
5154
}
52-
// Create mesh and transform to screen coords from print
55+
// Transform to screen coords from print
5356
const coordSystemTransform = new THREE.Matrix4()
5457
coordSystemTransform.set(
5558
1, 0, 0, 0, // x -> x
5659
0, 0, 1, 0, // z -> y
5760
0, -1, 0, 0, // y -> -z
5861
0, 0, 0, 1)
59-
const mesh = new THREE.Mesh(this.geometry.applyMatrix4(coordSystemTransform), this.material)
62+
object.applyMatrix4(coordSystemTransform)
6063
// Calculate bounding volumes
61-
const bbox = new THREE.Box3().setFromObject(mesh)
64+
const bbox = new THREE.Box3().setFromObject(object)
6265
const centre = new THREE.Vector3()
6366
bbox.getCenter(centre)
6467
const bsphere = new THREE.Sphere()
@@ -69,11 +72,11 @@ class PartPreview {
6972
this.camera.position.y = bsphere.radius * 0.75
7073
this.camera.lookAt(0, modelheight / 2, 0)
7174
// Centre the model
72-
mesh.position.set(-centre.x, -bbox.min.y, -centre.z)
73-
this.objects.add(mesh)
75+
object.position.set(-centre.x, -bbox.min.y, -centre.z)
76+
this.scene.add(object)
7477
// Add the grid
7578
this.gridHelper = new THREE.GridHelper(260, 26, 'magenta', 'cyan')
76-
this.objects.add(this.gridHelper)
79+
this.scene.add(this.gridHelper)
7780
}
7881

7982
onLoadError (error) {
@@ -82,7 +85,7 @@ class PartPreview {
8285

8386
animate () {
8487
if (this.canvas.closest('html')) { // There's probably more efficient way to do this than checking every frame, but I can't make MutationObserver work right now
85-
this.objects.rotation.y += 0.01
88+
this.scene.rotation.y += 0.01
8689
this.renderer.render(this.scene, this.camera)
8790
window.requestAnimationFrame(this.animate.bind(this))
8891
} else {
@@ -91,9 +94,12 @@ class PartPreview {
9194
}
9295

9396
cleanup () {
94-
if (this.geometry) this.geometry.dispose()
95-
if (this.gridHelper) this.gridHelper.geometry.dispose()
96-
this.material.dispose()
97+
this.scene.traverse(function (node) {
98+
if (node instanceof THREE.Mesh) {
99+
node.geometry.dispose()
100+
node.material.dispose()
101+
}
102+
})
97103
this.renderer.dispose()
98104
}
99105
}

‎app/views/libraries/show.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<h1><%= @library.name %></h1>
33
<div class="row row-cols-2">
44
<div class="col-9">
5-
<div class="row row-cols-2 row-cols-md-4 mb-4">
5+
<div class="row row-cols-2 row-cols-md-3 mb-3">
66
<% @models.each do |model| %>
77
<div class="col mb-4">
88
<div class="card">

0 commit comments

Comments
 (0)
Please sign in to comment.