-
Notifications
You must be signed in to change notification settings - Fork 0
/
task6-ply-road-load.html
189 lines (141 loc) · 6.61 KB
/
task6-ply-road-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
185
186
187
188
189
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>task6-ply-load</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 { PLYLoader } from "./three.js-dev/examples/jsm/loaders/PLYLoader.js"; // class PLYLoader
let renderer, scene, camera;
initialise();
function initialise() {
// Init renderer
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio( window.devicePixelRatio );
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.
// Init scene
scene = new THREE.Scene();
// Init camera
camera = new THREE.PerspectiveCamera( // value in documentation, by default, almost always
45, //field of view in degrees
window.innerWidth / window.innerHeight, //aspect ratio - соотношение сторон
1, // near clip frame - the coordinate of starting point to view on Z axe for frame, z=-n
1000 //far clip frame, z=-f, z=x=y=0 - view/eye point, frustum view - truncated pyramid - усеченная пирамида
);
camera.position.set(0, 0, 9); // by default x=y=z=0; z is a camera position on z axis, for visualising
scene.add(camera);
// 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.001;
controls.maxDistance = 200;
controls.enablePan = false;
// ambient
scene.add(new THREE.AmbientLight(0x443333, .2));
const dirLight1 = new THREE.DirectionalLight( 0xffddcc, 1 );
dirLight1.position.set( 1, 0.75, 0.5 );
scene.add( dirLight1 );
const dirLight2 = new THREE.DirectionalLight( 0xccccff, 1 );
dirLight2.position.set( - 1, 0.75, - 0.5 );
scene.add( dirLight2 );
// light
const light = new THREE.PointLight(0xffffff, 1);
camera.add(light);
// model
// PLY file
//var loader = new THREE.PLYLoader();
var group = new THREE.Object3D();
var loader = new PLYLoader();
var particles;
loader.load( './three.js-dev/examples/models/ply/binary/model_210603_171859.ply', function ( geometry ) { //works with function geometry, 'let geometry = new BufferGeometry();'
console.log(geometry);
particles = geometry.attributes.color.count; // 298892 points
console.log(particles);
var positions = geometry.attributes.position.array; // x, y, z is going in sequence in array positions
var colors = geometry.attributes.color.array; // r, g, b is going in sequence in array colors
var x=[], y=[], z=[], index, r=[], g=[], b=[];
console.log(colors);
var j=0;
for (var i=0; i<positions.length; i=i+3){
x[j]=positions[i];
y[j]=positions[i+1];
z[j]=positions[i+2];
r[j]=colors[i];
g[j]=colors[i+1];
b[j]=colors[i+2];
j=j+1;
}
// Arrays x, y, z contain coordinate of the points respectively, Arrays r, b, b contain colors respectively.
const position_p = [];
const color_p = [];
const color = new THREE.Color();
var x_p, y_p, z_p, r_p, g_p, b_p;
for ( var k = 0; k < particles; k++ ) {
// positions
x_p = x[k];
y_p = y[k];
z_p = z[k];
position_p.push( x_p, y_p, z_p );
r_p = r[k];//Math.trunc(r[k]*256/100);
g_p = g[k];//Math.trunc(g[k]*256/100);
b_p = b[k];//Math.trunc(b[k]*256/100);
if (k<10){
console.log (k);
console.log (b_p);
}
color.setRGB( r_p, g_p, b_p );
color_p.push( color.r, color.g, color.b );
}
geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( position_p, 3 ) );
geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( color_p, 3 ) );
geometry.computeBoundingSphere();
geometry.center();
var material = new THREE.PointsMaterial({
// color: 0xbf9121,
size: 0.01,
//opacity: 0.6,
//transparent: true,
//blending: THREE.AdditiveBlending,
vertexColors: true
});
console.log(material);
group = new THREE.Points(geometry, material);
group.sortParticles = true;
console.log (geometry.computeVertexNormals()); // Computes vertex normals by averaging face normals.
group.position.y = 2;
group.position.z = -9;
group.rotation.x = - Math.PI*1;
group.rotation.y = - Math.PI*2;
console.log(group.count); // undefined
scene.add( group );
render();
} );
window.addEventListener('resize', WindowResize);
}
function WindowResize(){
renderer.setSize( window.innerWidth, window.innerHeight );
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
render();
}
function render() {
renderer.render( scene, camera );
}
</script>
</body>
</html>