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
|
<body>
<div id="model" style="width:500px; height: 600px;margin:100px auto;"> </div>
<!-- 包下载路径 -->
<!-- https://github.com/mrdoob/three.js/blob/dev/examples/js/loaders/STLLoader.js -->
<script src="assets/js/3d/three.js"></script>
<script src="assets/js/3d/OrbitControls.js"></script>
<script src="assets/js/3d/GLTFLoader.js"></script>
<script src="assets/js/3d/RGBELoader.js"></script>
<script src="assets/js/3d/STLLoader.js"></script>
<script>
function glbViewer(elementID) {
var elem = document.getElementById(elementID)
var camera = new THREE.PerspectiveCamera(70,
elem.clientWidth / elem.clientHeight, 1, 1000);
var renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
renderer.setSize(elem.clientWidth, elem.clientHeight);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1;
renderer.outputEncoding = THREE.sRGBEncoding;
elem.appendChild(renderer.domElement);
window.addEventListener('resize', function () {
renderer.setSize(elem.clientWidth, elem.clientHeight);
camera.aspect = elem.clientWidth / elem.clientHeight;
camera.updateProjectionMatrix();
}, false);
var controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.minDistance = 2;
controls.maxDistance = 10;
controls.target.set(0, 0.5, - 0.2);
controls.autoRotate = false; //自动旋转
controls.autoRotateSpeed = 5; //旋转速度
controls.update();
var scene = new THREE.Scene();
new THREE.RGBELoader()
// .setPath('assets/3d/')
.load('shanghai_riverside_4k.hdr', function (texture) {
texture.mapping = THREE.EquirectangularReflectionMapping;
scene.background = new THREE.Color(0xffffff);
scene.environment = texture;
const loader = new THREE.GLTFLoader()
.setPath('assets/models/');
loader.load('white.glb', function (gltf) {
camera.position.set(4, 0.3, -1.2);
gltf.scene.position.y = -1.8;
scene.add(gltf.scene);
var animate = function () {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
};
animate();
});
});
}
</script>
<script type="text/javascript">
window.onload = function () {
glbViewer("model")
}
</script>
<!-- <script>
function STLViewer(model, elementID) {
var elem = document.getElementById(elementID)
var camera = new THREE.PerspectiveCamera(70,
elem.clientWidth / elem.clientHeight, 1, 1000);
var renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
renderer.setSize(elem.clientWidth, elem.clientHeight);
elem.appendChild(renderer.domElement);
window.addEventListener('resize', function () {
renderer.setSize(elem.clientWidth, elem.clientHeight);
camera.aspect = elem.clientWidth / elem.clientHeight;
camera.updateProjectionMatrix();
}, false);
var controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.rotateSpeed = 0.05;
controls.dampingFactor = 0.1;
controls.enableZoom = true;
controls.autoRotate = true; //自动旋转
controls.autoRotateSpeed = .75;
var scene = new THREE.Scene();
scene.add(new THREE.HemisphereLight(0xe74c3c, 1));
(new THREE.STLLoader()).load(model, function (geometry) {
const material = new THREE.MeshPhysicalMaterial({
metalness: 0.25,
roughness: 0.1,
opacity: 1.0,
transparent: true,
transmission: 0.99,
clearcoat: 1.0,
clearcoatRoughness: 0.25
})
var mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
var middle = new THREE.Vector3();
geometry.computeBoundingBox();
geometry.boundingBox.getCenter(middle);
mesh.geometry.applyMatrix4(new THREE.Matrix4().makeTranslation(
-middle.x, -middle.y, -middle.z));
var largestDimension = Math.max(geometry.boundingBox.max.x,
geometry.boundingBox.max.y,
geometry.boundingBox.max.z)
camera.position.y = -largestDimension * 1.6;
var animate = function () {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
};
animate();
});
}
</script>
<script type="text/javascript">
window.onload = function () {
STLViewer("exa.stl", "model")
}
</script> -->
</body>
|