-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
172 lines (136 loc) · 5.02 KB
/
main.js
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
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
import WebGL from 'three/addons/capabilities/WebGL.js';
// Create a renderer
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Create a scene
let scene = new THREE.Scene();
scene.background = new THREE.Color(0x87CEEB);
// Create a camera
const camera = new THREE.PerspectiveCamera(100, window.innerWidth / window.innerHeight, 0.1, 10000);
camera.up.set(0, 0, 1);
camera.position.set(1.0, 1.0, 1.0);
camera.lookAt(0, 0, 0);
// Create an orbit control
const orbit = new OrbitControls(camera, renderer.domElement);
orbit.update();
// Create a grid helper
const gridHelper = new THREE.GridHelper(10, 10);
gridHelper.rotateX(-Math.PI / 2);
scene.add(gridHelper);
// Create an axes helper
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);
// Create a ambient light
const ambientLight = new THREE.AmbientLight(0xffffff);
scene.add(ambientLight);
// Create a plane
// const planeGeometry = new THREE.PlaneGeometry(10, 10);
// const planeMaterial = new THREE.MeshBasicMaterial({ color: 0xffffff });
// const plane = new THREE.Mesh(planeGeometry, planeMaterial);
// scene.add(plane);
import {CSS2DRenderer} from 'three/examples/jsm/renderers/CSS2DRenderer.js';
const labelRenderer = new CSS2DRenderer();
labelRenderer.setSize(window.innerWidth, window.innerHeight);
labelRenderer.domElement.style.position = 'absolute';
labelRenderer.domElement.style.top = '0px';
labelRenderer.domElement.style.pointerEvents = 'none';
document.body.appendChild(labelRenderer.domElement);
function resetScene() {
while (scene.children.length > 0) {
scene.remove(scene.children[0]);
}
scene.add(gridHelper);
scene.add(axesHelper);
scene.add(ambientLight);
}
////////////////
// USD import //
////////////////
import { Usd } from './src/pxr/pxr.js';
import { getObject3DFromXform } from './src/multiverse/multiverse_view.js';
import { createGuiFromStage } from './src/multiverse/multiverse_gui.js';
let object3D = null;
let stage = null;
async function usdViewFromPath(path) {
try {
stage = await Usd.Stage.Open(path);
const defaultPrim = stage.GetDefaultPrim();
object3D = getObject3DFromXform(defaultPrim);
scene.add(object3D);
createGuiFromStage(scene, stage);
} catch (error) {
console.error('Failed to load file:', error);
}
}
function usdViewFromContent(content) {
resetScene();
stage = new Usd.Stage(content);
const defaultPrim = stage.GetDefaultPrim();
object3D = getObject3DFromXform(defaultPrim);
scene.add(object3D);
createGuiFromStage(scene, stage);
}
// const usdFilePath = '/assets/milk_box/milk_box_flatten.usda';
// const usdFilePath = '/assets/cold_cutting_2/cold_cutting_2_flatten.usda';
// const usdFilePath = '/assets/ApartmentECAI/ApartmentECAI_flatten.usda';
const usdFilePath = 'assets/resources/panda/panda_flatten.usda'
usdViewFromPath(import.meta.env.BASE_URL + usdFilePath);
document.getElementById('fileInput').addEventListener('change', function(event) {
const file = event.target.files[0];
if (file) {
console.log('File selected:', file);
const reader = new FileReader();
reader.onload = function(e) {
const content = e.target.result;
usdViewFromContent(content);
};
reader.readAsText(file); // Adjust based on file type
}
});
document.getElementById('downloadBtn').addEventListener('click', () => {
if (stage == null) {
console.error('Stage is null');
return;
}
// Define the text content of the file
const fileContent = stage.ExportToString();
const defaultPrim = stage.GetDefaultPrim();
const primName = defaultPrim.GetName();
// Create a blob with the file content
const blob = new Blob([fileContent], { type: 'text/plain' });
// Create a temporary link element
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = primName + '.usda';
// Append the link to the body
document.body.appendChild(link);
// Programatically click the link to trigger the download
link.click();
// Remove the link from the body
document.body.removeChild(link);
});
///////////////
// Main loop //
///////////////
function animate(time_in_ms) {
requestAnimationFrame(animate);
renderer.render(scene, camera);
labelRenderer.render(scene, camera);
}
if (WebGL.isWebGLAvailable()) {
// Initiate function or other initializations here
animate();
}
else {
const warning = WebGL.getWebGLErrorMessage();
document.getElementById('container').appendChild(warning);
}
window.addEventListener('resize', function () {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
labelRenderer.setSize(window.innerWidth, window.innerHeight);
});