-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask5-pcd-load.html
184 lines (145 loc) · 8.54 KB
/
task5-pcd-load.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>My first three.js app</title>
<style>
body { margin: 0; }
</style>
</head>
<body>
<script type="module">
import * as THREE from "./three.js-dev/build/three.module.js"; // importing everything from the module - 'three.module.js''.
import { OrbitControls } from "./three.js-dev/examples/jsm/controls/OrbitControls.js"; // importing only single value, exported name OrbitControls should have been defined
// as 'class OrbitControls' and now visible in current scope (зона видимости).
import { PCDLoader } from "./three.js-dev/examples/jsm/loaders/PCDLoader.js"; // class PCDLoader
import * as BufferGeometryUtils from './three.js-dev/examples/jsm/utils/BufferGeometryUtils.js';
import { CinematicCamera } from './three.js-dev/examples/jsm/cameras/CinematicCamera.js';
let camera, scene, raycaster, renderer, stats, mouse;
let INTERSECTED; // crossed, пересеклись
//const radius = 8;
raycaster = new THREE.Raycaster();
let mesh, loader;
initialise();
function initialise() {
// Init camera
camera = new THREE.PerspectiveCamera(
30, //field of view in degrees
window.innerWidth / window.innerHeight, //aspect ratio - соотношение сторон
0.01, // near clip frame - the coordinate of starting point to view on Z axe for frame, z=-n
40 //far clip frame, z=-f, z=x=y=0 - view/eye point, frustum view - truncated pyramid - усеченная пирамида
);
camera.position.set(0,0,1);
// Init scene
scene = new THREE.Scene();
// Init renderer
renderer = new THREE.WebGLRenderer( { antialias: true } ); //antialias - whether to perform antialiasing (сглаживание). Default is false
renderer.setPixelRatio( window.devicePixelRatio ); // Sets device pixel ratio (соотношение). This is usually used for HiDPI device to prevent bluring output canvas.
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
renderer.outputEncoding = THREE.sRGBEncoding; //Defines the output encoding of the renderer. Default is THREE.LinearEncoding=3000, THREE.sRGBEncoding = 3001.
/*scene.add( new THREE.AmbientLight( 0xffffff, 0.3 ) );
const light = new THREE.DirectionalLight( 0xffff, 0.35 );
light.position.set( 1, 1, 1 ).normalize();
scene.add( light );*/
// controls
// Orbit controls allow the camera to orbit around a target.
const controls = new OrbitControls( //Constructor made initialing before using another methods of the class
camera, //The camera to be controlled.
renderer.domElement //The HTML element used for event listeners.
);
controls.addEventListener( //Registers an event handler (обработчик) for the specified type with the object.
'change', // type of event, which should be processed.
render // listener - function in JavaScript or EventListener, which will get notification if event have happened.
);
controls.minDistance = 0.5;
controls.maxDistance = 10;
controls.enablePan = false;
/*const geometry = new THREE.BoxGeometry( 100, 100, 100 );
const object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: 0xffff00 } ) );*/
loader = new PCDLoader();
loader.load( './three.js-dev/examples/models/pcd/binary/Zaghetto.pcd', function ( mesh ) { //works with function geometry, 'let geometry = new BufferGeometry();'
mesh.geometry.center(); // const geometry = new BufferGeometry(); center() - to center the geometry based on the bounding box.
mesh.geometry.rotateX(Math.PI); // Rotate the geometry about the X axis. This is typically done as a one time operation, and not during a loop.
// Use Object3D.rotation for typical real-time mesh rotation.
// Our object have been upside down (вверх ногами).
// rotateX ( radians : Float ).
// Math.PI = 180' = 3.14
mesh.material.color = new THREE.Color("white"); //.setHex(0xfff8f0);
scene.add(mesh);
render();
});
mouse = new THREE.Vector2();
document.addEventListener(//Registers an event handler (обработчик) for the specified type with the object.
"pointermove", //'mousemove', // type of event, which should be processed.
onMouseMove ); // listener - function in JavaScript or EventListener, which will get notification if event have happened.
//window.addEventListener( 'mousemove', onMouseMove, false );
window.addEventListener('resize', WindowResize);
// calculate_objects_intersecting_picking_ray (raycaster, mouse, camera);
// render();
// calculate_objects_intersecting_picking_ray (raycaster, mouse, camera);
// render();
//window.requestAnimationFrame(render);
}
function WindowResize(){
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function onMouseMove( event ) {
// calculate mouse position in normalized device coordinates
// (-1 to +1) for both components
// event.preventDefault();
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
calculate_objects_intersecting_picking_ray (raycaster, mouse, camera);
render();
//onMouseMove(event);
}
function render() {
renderer.render( scene, camera );
}
function calculate_objects_intersecting_picking_ray (raycaster, mouse, camera) {
// camera.position.x = 8; //radius; //* Math.sin( THREE.MathUtils.degToRad( theta ) );
// camera.position.y = 8; //radius; //* Math.sin( THREE.MathUtils.degToRad( theta ) );
// camera.position.z = 8; //radius; //* Math.cos( THREE.MathUtils.degToRad( theta ) );
console.log (camera.position.y);
//theta += 0.1;
camera.lookAt( scene.position );
camera.updateMatrixWorld();
raycaster.setFromCamera( //method from Raycaster class, setFromCamera ( coords : Vector2, camera : Camera ) : null
mouse, // coords — 2D coordinates of the mouse, in normalized device coordinates ---X and Y should be [-1;1].
camera // camera — camera from which the ray should originate
);
const points = scene.getObjectByName( 'Zaghetto.pcd' ); //name -- String to match to the children's Object3D.name property.
// Searches through an object and its children, starting with the object itself, and returns the first with a matching name.
// Note that for most objects the name is an empty string by default. You will have to set it manually to make use of this method.
const intersects = raycaster.intersectObjects( // method from Raycaster class,( object : Object3D) : Array, Array = intersects
scene.children // points // // .children : Object3D, array with object's children.
);
if ( intersects.length > 0 ) { // method 'length' for the arrays, to get know the length.
if (INTERSECTED != intersects[0].object) {
if (INTERSECTED) {INTERSECTED.material.emissive.setHex(INTERSECTED.currentHex); // Material - abstract class for materials. Material() creates a generic (общий) material.
console.log("888888");}
INTERSECTED = intersects[0].object;
INTERSECTED.currentHex = INTERSECTED.material.color.getHex(); // : Integer. Returns the hexadecimal value of this color. Internal (внутренняя) function for color conversion (преобразование).
INTERSECTED.material.color.setHex(0xff0000);
}
}
//else if (INTERSECTED !== null) {
else {
console.log(INTERSECTED);
if ( INTERSECTED ) {
INTERSECTED.material.color.setHex (INTERSECTED.currentHex); //color = new THREE.Color("yellow");
console.log("22");
// console.log(INTERSECTED);
}
//INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
INTERSECTED = null;
}
//calculate_objects_intersecting_picking_ray (raycaster, mouse, camera);
render();
}
</script>
</body>
</html>