-
Notifications
You must be signed in to change notification settings - Fork 4
/
vff.js
93 lines (77 loc) · 2.44 KB
/
vff.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
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 75, 1, 0.1, 1000 );
var isMouseDown = false;
var renderer = new THREE.WebGLRenderer({antialias: true});
renderer.localClippingEnabled = true;
renderer.setSize(500, 500 );
var container = document.getElementById("container");
container.appendChild( renderer.domElement);
//add orbit control
const orbit = new THREE.OrbitControls(camera, renderer.domElement);
orbit.enableZoom = false;
orbit.enableKeys = false;
scene.background = new THREE.Color( 0x505050 );
scene.add( new THREE.HemisphereLight( 0x606060, 0x404040 ) );
var light = new THREE.DirectionalLight( 0xffffff, 1 );
light.position.set( 0, 1, 0 ).normalize();
scene.add( light );
resetCamera();
var geometry;
var loader = new THREE.STLLoader();
loader.load( './models/AMNH-FI-101479_M21282-40430__cephalon.stl', function ( loadedGeometry ) {
geometry = loadedGeometry;
buildObject();
render();
});
function getRandomClippingPlane() {
// pick clipping axis
var x, z;
if (Math.random() > 0.5){
x = 1;
z = 0;
} else {
x = 0;
z = 1;
}
return new THREE.Plane( new THREE.Vector3( x, 0, z ), 2 * Math.random() - 1 );
}
function buildObject(){
var localPlane = getRandomClippingPlane();
var localPlane2 = getRandomClippingPlane();
var material = new THREE.MeshLambertMaterial( { color: 0xE3DAC9, clippingPlanes: [localPlane, localPlane2]} );
var mesh = new THREE.Mesh( geometry, material);
mesh.scale.set(0.5, 0.5, 0.5);
var rotationX = Math.random() > 0.5 ? 0 : Math.PI;
var rotationY = Math.random() * 2 * Math.PI;
var rotationZ = (0.25 * Math.random() - 0.125) * 2 * Math.PI;
mesh.rotation.set(rotationX, rotationY, rotationZ);
scene.add( mesh );
}
var saveImageButton = document.getElementById("save-image-btn");
function render() {
if (isMouseDown){
requestAnimationFrame(render);
}
renderer.render( scene, camera );
var imgData = renderer.domElement.toDataURL('image/png');
saveImageButton.href = imgData;
}
function resetCamera(){
orbit.reset();
camera.position.set(0, 10, 0)
camera.lookAt(0, 0, 0);
}
function refresh(){
scene.remove(scene.children[2]);
render();
resetCamera();
buildObject();
render();
}
document.addEventListener("mousedown", function(e){
isMouseDown = true;
render();
});
document.addEventListener("mouseup", function(e){
isMouseDown = false;
});