Skip to content

Commit e4139cf

Browse files
authored
USDZExporter: Add option for onlyVisible (#31489)
1 parent 960880c commit e4139cf

File tree

3 files changed

+84
-4
lines changed

3 files changed

+84
-4
lines changed

docs/examples/en/exporters/USDZExporter.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ <h3>[method:undefined parse]( [param:Object3D scene], [param:Function onComplete
9393
<ul>
9494
<li>`maxTextureSize` - int. Restricts the image maximum size (both width and height) to the given value. Default is 1024.</li>
9595
<li>`includeAnchoringProperties` - bool. Whether to include anchoring properties for AR. Default is true.</li>
96+
<li>`onlyVisible` - bool. Export only visible 3D objects. Default is true.</li>
9697
<li>`quickLookCompatible` - bool. Whether to make the exported USDZ compatible with Apple's QuickLook. Default is false.</li>
9798
<li>`ar` - Object. AR-specific options including anchoring type and plane anchoring alignment.</li>
9899
</ul>

examples/jsm/exporters/USDZExporter.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ class USDZExporter {
196196
planeAnchoring: { alignment: 'horizontal' },
197197
},
198198
includeAnchoringProperties: true,
199+
onlyVisible: true,
199200
quickLookCompatible: false,
200201
maxTextureSize: 1024,
201202
},
@@ -240,7 +241,7 @@ class USDZExporter {
240241
const materials = {};
241242
const textures = {};
242243

243-
buildHierarchy( scene, sceneNode, materials, usedNames, files );
244+
buildHierarchy( scene, sceneNode, materials, usedNames, files, options );
244245

245246
const materialsNode = buildMaterials(
246247
materials,
@@ -426,13 +427,13 @@ function buildHeader() {
426427

427428
// Xform
428429

429-
function buildHierarchy( object, parentNode, materials, usedNames, files ) {
430+
function buildHierarchy( object, parentNode, materials, usedNames, files, options ) {
430431

431432
for ( let i = 0, l = object.children.length; i < l; i ++ ) {
432433

433434
const child = object.children[ i ];
434435

435-
if ( ! child.visible ) continue;
436+
if ( child.visible === false && options.onlyVisible === true ) continue;
436437

437438
let childNode;
438439

@@ -489,7 +490,7 @@ function buildHierarchy( object, parentNode, materials, usedNames, files ) {
489490
if ( childNode ) {
490491

491492
parentNode.addChild( childNode );
492-
buildHierarchy( child, childNode, materials, usedNames, files );
493+
buildHierarchy( child, childNode, materials, usedNames, files, options );
493494

494495
}
495496

@@ -1203,6 +1204,7 @@ function buildCamera( camera, usedNames ) {
12031204
* @typedef {Object} USDZExporter~Options
12041205
* @property {number} [maxTextureSize=1024] - The maximum texture size that is going to be exported.
12051206
* @property {boolean} [includeAnchoringProperties=true] - Whether to include anchoring properties or not.
1207+
* @property {boolean} [onlyVisible=true] - Export only visible 3D objects.
12061208
* @property {Object} [ar] - If `includeAnchoringProperties` is set to `true`, the anchoring type and alignment
12071209
* can be configured via `ar.anchoring.type` and `ar.planeAnchoring.alignment`.
12081210
* @property {boolean} [quickLookCompatible=false] - Whether to make the exported USDZ compatible to QuickLook

test/unit/addons/exporters/USDZExporter.tests.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,83 @@ export default QUnit.module( 'Addons', () => {
9090

9191
} );
9292

93+
QUnit.test( 'export scene with onlyVisible option', async ( assert ) => {
94+
95+
const exporter = new USDZExporter( );
96+
97+
const scene = new Scene();
98+
99+
const geometry = new BoxGeometry( 1, 1, 1 );
100+
const material1 = new MeshStandardMaterial( { color: 0xff0000 } );
101+
const material2 = new MeshStandardMaterial( { color: 0x00ff00 } );
102+
103+
const box1 = new Mesh( geometry, material1 );
104+
box1.name = 'box1';
105+
box1.position.set( - 1, 0, 0 );
106+
107+
const box2 = new Mesh( geometry, material2 );
108+
box2.name = 'box2';
109+
box2.position.set( 1, 0, 0 );
110+
box2.visible = false;
111+
112+
scene.add( box1 );
113+
scene.add( box2 );
114+
115+
// onlyVisible = true
116+
117+
const options = {
118+
onlyVisible: true,
119+
};
120+
const exportResult = await exporter.parseAsync( scene, options );
121+
122+
assert.ok(
123+
exportResult.buffer instanceof ArrayBuffer,
124+
'Export returns an ArrayBuffer'
125+
);
126+
assert.ok(
127+
exportResult.buffer.byteLength > 0,
128+
'ArrayBuffer has non-zero length'
129+
);
130+
131+
const unzipped = unzipSync( exportResult );
132+
const fileNames = Object.keys( unzipped );
133+
const modelFileName = 'model.usda';
134+
135+
assert.ok( fileNames.includes( modelFileName ), `ZIP contains ${modelFileName}` );
136+
137+
const usdaContent = strFromU8( unzipped[ modelFileName ] );
138+
assert.ok( isValidUSDA( usdaContent ), `${modelFileName} is valid USDA` );
139+
140+
assert.ok( usdaContent.includes( 'box1' ), 'USDA contains box1' );
141+
assert.ok( ! usdaContent.includes( 'box2' ), 'USDA does not contain box2' );
142+
143+
// onlyVisible = false
144+
145+
options.onlyVisible = false;
146+
const exportResult2 = await exporter.parseAsync( scene, options );
147+
148+
assert.ok(
149+
exportResult2.buffer instanceof ArrayBuffer,
150+
'Export returns an ArrayBuffer'
151+
);
152+
assert.ok(
153+
exportResult2.buffer.byteLength > 0,
154+
'ArrayBuffer has non-zero length'
155+
);
156+
157+
const unzipped2 = unzipSync( exportResult2 );
158+
const fileNames2 = Object.keys( unzipped2 );
159+
160+
assert.ok( fileNames2.includes( modelFileName ), `ZIP contains ${modelFileName}` );
161+
162+
const usdaContent2 = strFromU8( unzipped2[ modelFileName ] );
163+
assert.ok( isValidUSDA( usdaContent2 ), `${modelFileName} is valid USDA` );
164+
165+
assert.ok( usdaContent2.includes( 'box1' ), 'USDA contains box1' );
166+
assert.ok( usdaContent2.includes( 'box2' ), 'USDA contains box2' );
167+
168+
} );
169+
93170
} );
94171

95172
} );

0 commit comments

Comments
 (0)