-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
206 lines (157 loc) · 6.75 KB
/
index.html
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - loaders - OBJ loader</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
</head>
<body>
<!-- <div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - OBJLoader test
</div> -->
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.6.3/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three/build/three.module.js",
"three/addons/": "https://unpkg.com/three/examples/jsm/",
"three-mesh-bvh": "https://cdn.jsdelivr.net/npm/three-mesh-bvh@0.6.3/+esm"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { MeshBVH, MeshBVHVisualizer, CONTAINED } from 'three-mesh-bvh';
import { OBJLoader } from 'three/addons/loaders/OBJLoader.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { computeBoundsTree, disposeBoundsTree, acceleratedRaycast } from 'three-mesh-bvh';
THREE.BufferGeometry.prototype.computeBoundsTree = computeBoundsTree;
THREE.BufferGeometry.prototype.disposeBoundsTree = disposeBoundsTree;
THREE.Mesh.prototype.raycast = acceleratedRaycast;
let camera, scene, renderer;
let object;
let plane;
let slicePlane;
let outlineLines;
init();
function init() {
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.5, 20 );
camera.position.z = 3;
camera.position.y = 3;
// scene
scene = new THREE.Scene();
scene.background = new THREE.Color('#CCCCCC');
const ambientLight = new THREE.AmbientLight( 0xffffff );
scene.add( ambientLight );
const pointLight = new THREE.PointLight( 0xffffff, 30 );
camera.add( pointLight );
scene.add( camera );
// plane
var planeGeometry = new THREE.PlaneGeometry(3, 3);
var planeMaterial = new THREE.MeshLambertMaterial({ color: 0xCCCCCC});
plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.rotation.x = -0.5 * Math.PI;
plane.position.y = -1;
plane.receiveShadow = true;
scene.add(plane);
// slicePlane
// create line geometry with enough data to hold 100000 segments
const lineGeometry = new THREE.BufferGeometry();
const linePosAttr = new THREE.BufferAttribute( new Float32Array( 300000 ), 3, false );
linePosAttr.setUsage( THREE.DynamicDrawUsage );
lineGeometry.setAttribute( 'position', linePosAttr );
outlineLines = new THREE.LineSegments( lineGeometry, new THREE.LineBasicMaterial() );
outlineLines.material.color.set( 0x00acc1 ).convertSRGBToLinear();
outlineLines.frustumCulled = false;
outlineLines.renderOrder = 3;
// slicePlane
var slicePlaneGeometry = new THREE.PlaneGeometry(1, 1);
var slicePlaneMaterial = new THREE.MeshLambertMaterial({ color: 0xFF0000});
slicePlane = new THREE.Mesh(slicePlaneGeometry, slicePlaneMaterial);
slicePlane.rotation.x = -0.5 * Math.PI;
slicePlane.position.y = 0;
slicePlane.receiveShadow = true;
scene.add(slicePlane);
// manager
function loadModel() {
object.traverse( function ( child ) {
if ( child.isMesh ) child.material.map = texture;
});
object.position.y = -0.55;
// object.scale.setScalar( 0.01 );
object.rotation.x = Math.PI / 180*270;
scene.add( object );
render();
}
const manager = new THREE.LoadingManager( loadModel );
// texture
const textureLoader = new THREE.TextureLoader( manager );
const texture = textureLoader.load( 'model/model-textureuv.png', render );
texture.colorSpace = THREE.SRGBColorSpace;
// model
function onProgress( xhr ) {
if ( xhr.lengthComputable ) {
const percentComplete = xhr.loaded / xhr.total * 100;
console.log( 'model ' + Math.round( percentComplete, 2 ) + '% downloaded' );
if(percentComplete >= 100){
window.setTimeout(startProcessing, 2000);
}
}
}
function onError() {}
const loader = new OBJLoader( manager );
loader.load( 'model/model_05_color.obj', function ( obj ) {
object = obj;
}, onProgress, onError );
//
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
//
const controls = new OrbitControls( camera, renderer.domElement );
controls.minDistance = 2;
controls.maxDistance = 5;
controls.addEventListener( 'change', render );
//
window.addEventListener( 'resize', onWindowResize );
}
function startProcessing() {
console.log('startProcessing');
// const geometry = new TorusKnotBufferGeometry(1, 0.2, 50, 50, 2, 3)
const model = scene.children[4].children[0];
const mergedGeometry = model.geometry;
const bvhMesh = new MeshBVH(mergedGeometry, { maxLeafTris: 3 });
// const model = gltf.scene.children[ 0 ];
// console.log(scene['children']);
// console.log();
// model.material = new THREE.MeshBasicMaterial();
// model.position.set( 0, 0, 0 );
// model.quaternion.identity();
// color the surface of the geometry with an EQUAL depth to limit the amount of
// fragment shading that has to run.
const surfaceModel = model.clone();
surfaceModel.material = new THREE.MeshStandardMaterial( {
depthFunc: THREE.EqualDepth,
} );
surfaceModel.renderOrder = 1;
outlineLines.scale.copy( model.scale );
outlineLines.position.set( 0, 0, 0 );
outlineLines.quaternion.identity();
model.updateMatrixWorld( true );
// --------------------------------------------------------
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function render() {
renderer.render( scene, camera );
}
</script>
</body>
</html>