forked from repalash/xatlas-three
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpack-atlas.html
238 lines (190 loc) · 7.85 KB
/
pack-atlas.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<!DOCTYPE html>
<html lang="en">
<head>
<title>xAtlas.js UV Unwrapping</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="container"></div>
<div id="info">
xatlas.js - <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - UVUnwrapper - <a href="https://github.com/repalash" target="_blank" rel="noopener">repalash</a><br/>
<a href="https://github.com/repalash/xatlas-three/tree/master/public/pack-atlas.html" target="_blank" rel="noopener">View source</a><br/>
</div>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js",
"xatlas-three": "./build/index.js"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { UVUnwrapper } from 'xatlas-three';
import { GLTFLoader } from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/loaders/GLTFLoader.js';
import { OrbitControls } from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/controls/OrbitControls.js';
import { mergeVertices } from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/utils/BufferGeometryUtils.js';
// ShadowMap + LightMap Res and Number of Directional Lights
let camera, scene, renderer, controls, object;
const objects = [];
const unwrapper = new UVUnwrapper({BufferAttribute: THREE.BufferAttribute});
unwrapper.chartOptions = {
fixWinding: false,
maxBoundaryLength: 0,
maxChartArea: 0,
maxCost: 2,
maxIterations: 1,
normalDeviationWeight: 2,
normalSeamWeight: 4,
roundnessWeight: 0.009999999776482582,
straightnessWeight: 6,
textureSeamWeight: 0.5,
useInputMeshUvs: true,
}
unwrapper.packOptions = {
bilinear: true,
blockAlign: false,
bruteForce: false,
createImage: false,
maxChartSize: 0,
padding: 0,
resolution: 0,
rotateCharts: true,
rotateChartsToAxis: true,
texelsPerUnit: 0
}
unwrapper.useNormals = true
unwrapper.timeUnwrap = true // Logs time of unwrapping
unwrapper.logProgress = true // Logs unwrapping progress bar
const testMap = new THREE.TextureLoader().load( './check-map.png' );
unwrapper.loadLibrary((mode, progress)=>{
// console.log(mode, progress);
},
// '/node_modules/xatlasjs/dist/xatlas.wasm',
// window.location.origin + '/node_modules/xatlasjs/dist/xatlas.js',
'https://cdn.jsdelivr.net/npm/[email protected]/dist/xatlas.wasm',
'https://cdn.jsdelivr.net/npm/[email protected]/dist/xatlas.js',
).then(()=>{
console.log("xatlas loaded");
init();
animate();
}
);
function init() {
// renderer
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
// camera
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.set( 0, 100, 200 );
camera.name = 'Camera';
// scene
scene = new THREE.Scene();
scene.background = new THREE.Color( 0x949494 );
const dirLight = new THREE.DirectionalLight( 0xffffff, 1.0 );
dirLight.name = 'Dir. Light' ;
dirLight.position.set( 200, 200, 200 );
dirLight.shadow.camera.near = 100;
dirLight.shadow.camera.far = 5000;
dirLight.shadow.camera.right = 150;
dirLight.shadow.camera.left = - 150;
dirLight.shadow.camera.top = 150;
dirLight.shadow.camera.bottom = - 150;
objects.push( dirLight );
scene.add( dirLight );
// ground
const groundMesh = new THREE.Mesh(
new THREE.PlaneGeometry( 600, 600 ),
new THREE.MeshPhongMaterial( { color: 0xffffff, depthWrite: true, map: testMap } )
);
groundMesh.position.y = - 0.1;
groundMesh.rotation.x = - Math.PI / 2;
groundMesh.name = 'Ground Mesh';
objects.push( groundMesh );
scene.add( groundMesh );
// model
function loadModel() {
object.traverse( function ( child ) {
if ( child.isMesh && child.geometry && child.visible) {
child.name = 'Loaded Mesh';
child.castShadow = true;
child.receiveShadow = true;
child.material = new THREE.MeshPhongMaterial({
color: 0xffffff, map: testMap
});
objects.push( child );
} else {
child.layers.disableAll(); // Disable Rendering for this
}
} );
scene.add( object );
object.scale.set( 2, 2, 2 );
object.position.set( 0, - 16, 0 );
(async ()=>{
const geoms = [];
objects.forEach( function ( child ) {
if ( child.isMesh && child.geometry && child.visible) {
if(!child.geometry.index){
child.geometry = mergeVertices(child.geometry); // convert to indexed geometry
}
const geom = child.geometry
geom.computeBoundingSphere()
if(geom.boundingSphere.radius > 500)
return
geom.userData.worldScale = child.getWorldScale(new THREE.Vector3()).toArray();
geoms.push( geom );
}
} );
// Unwrap all geometries, where uvs doesn't exist. This is optional
for (const geom of geoms) {
if(!geom.attributes.uv)
await unwrapper.unwrapGeometry( geom );
}
// Pack all geometries into a single atlas
await unwrapper.packAtlas(geoms, 'uv');
for (const geom of geoms) {
delete geom.userData.worldScale;
}
})()
}
const manager = new THREE.LoadingManager( loadModel );
const loader = new GLTFLoader( manager );
loader.load( 'https://raw.githubusercontent.com/mrdoob/three.js/r129/examples/models/gltf/ShadowmappableMesh.glb', function ( obj ) {
object = obj.scene.children[ 0 ];
} );
// controls
controls = new OrbitControls( camera, renderer.domElement );
controls.enableDamping = true; // an animation loop is required when either damping or auto-rotation are enabled
controls.dampingFactor = 0.05;
controls.screenSpacePanning = true;
controls.minDistance = 100;
controls.maxDistance = 500;
controls.maxPolarAngle = Math.PI / 1.5;
controls.target.set( 0, 100, 0 );
window.addEventListener( 'resize', onWindowResize );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function render() {
// Update the inertia on the orbit controls
controls.update();
// Render Scene
renderer.render( scene, camera );
}
function animate() {
requestAnimationFrame( animate );
render();
}
</script>
</body>
</html>