-
Notifications
You must be signed in to change notification settings - Fork 0
/
task11-polygon-create.html
317 lines (231 loc) · 10.3 KB
/
task11-polygon-create.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<!DOCTYPE html>
<html lang="en">
<head>
<title>Find coordinates</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
</head>
<body>
<div id="container"></div>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - decal splatter<br/>
click to shoot
</div>
<script type="module">
import * as THREE from './three.js-dev/build/three.module.js';
import { OrbitControls } from './three.js-dev/examples/jsm/controls/OrbitControls.js';
import { PLYLoader } from "./three.js-dev/examples/jsm/loaders/PLYLoader.js"; // class PLYLoader
import { DecalGeometry } from './three.js-dev/examples/jsm/geometries/DecalGeometry.js';
const container = document.getElementById( 'container' );
let renderer, scene, camera;
let group;
let raycaster;
let line;
let toggle = 0;
let clock;
let p = new THREE.Vector3();
let n;
let mouseHelper;
let sphereInter;
const intersection = {
intersects: false,
point: new THREE.Vector3(),
normal: new THREE.Vector3()
};
const params = {
minScale: 10,
maxScale: 20,
rotate: true,
clear: function () {
removeDecals();
}
};
const decalMaterial = new THREE.MeshPhongMaterial( {
specular: 0x444444,
//map: decalDiffuse,
//normalMap: decalNormal,
normalScale: new THREE.Vector2( 1, 1 ),
shininess: 30,
transparent: true,
depthTest: true,
depthWrite: false,
polygonOffset: true,
polygonOffsetFactor: - 4,
wireframe: false
} );
let mesh;
const mouse = new THREE.Vector2();
const intersects = [];
const position = new THREE.Vector3();
const orientation = new THREE.Euler();
const size = new THREE.Vector3( 10, 10, 10 );
window.addEventListener( 'load', init );
function init() {
clock = new THREE.Clock();
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
scene = new THREE.Scene();
//scene.background = new THREE.Color( 0xbfe3dd );
camera = new THREE.PerspectiveCamera( 10, window.innerWidth / window.innerHeight, 1, 3000 );
camera.position.z = 500;
const controls = new OrbitControls( camera, renderer.domElement );
controls.minDistance = 50;
controls.maxDistance = 1000;
scene.add( new THREE.AmbientLight( 0x443333 ) );
let geometry_ = new THREE.BufferGeometry();
geometry_.setFromPoints( [ new THREE.Vector3(), new THREE.Vector3() ] );
//geometry_.setAttribute( 'array', new THREE.Float32BufferAttribute( position, 3 ) );
console.log ("00000 geometry_", geometry_);
line = new THREE.Line( geometry_, new THREE.LineBasicMaterial() );
scene.add( line );
//////////////////////////////////////////////////////////////////////////////////////
loadModel();
//console.log ("load geometry_", geometry_);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
raycaster = new THREE.Raycaster();
// raycaster.params.Points.threshold = threshold;
// console.log("raycaster.params.Points.threshold",raycaster.params.Points.threshold);
mouseHelper = new THREE.Mesh( new THREE.BoxGeometry( 1, 1, 10 ), new THREE.MeshNormalMaterial() );
mouseHelper.visible = true;
//mouseHelper.visible = false;
scene.add( mouseHelper );
window.addEventListener( 'resize', onWindowResize );
let moved = false;
controls.addEventListener( 'change', function () {
moved = true;
} );
animate();
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestAnimationFrame( animate );
renderer.render( scene, camera );
}
function loadModel() {
var particles;
var loader = new PLYLoader();
loader.load('./three.js-dev/examples/models/ply/binary/model.ply',function(geometry){//works with function geometry,'let geometry = new BufferGeometry();'; geometry type is const
// geometry consists of color, normal, position, Prototype
particles = geometry.attributes.color.count; // 298892 points
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_=[], r=[], g=[], b=[];
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;
}
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];
g_p = g[k];
b_p = b[k];
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({
size: 0.01,
vertexColors: true
});
group = new THREE.Points(geometry, material);
//group.position.y = 2;
group.rotation.x = - Math.PI*1;
scene.add( group );
group.scale.set(10, 10, 10); // appropriate proportion on the scene
let moved = false;
const geometry_sphere = new THREE.SphereGeometry( 1 );
const material_sphere = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
sphereInter = new THREE.Mesh( geometry_sphere, material_sphere );
sphereInter.visible = false;
scene.add( sphereInter );
window.addEventListener( 'pointerdown', function () {
moved = false;
} );
window.addEventListener( 'pointerup', function ( event ) {
if ( moved === false ) {
checkIntersection( event.clientX, event.clientY) ; //, geometry_ );
if ( intersection.intersects ) shoot();
}
} );
function onPointerMove( event ) {
if ( event.isPrimary ) {
checkIntersection( event.clientX, event.clientY ) ; //, geometry_ );
}
}
window.addEventListener( 'pointermove', onPointerMove );
function shoot() {
position.copy( intersection.point );
if ( moved === false ) {
sphereInter.visible = true;
sphereInter.position.copy( position );
line.position.copy(position);
} else {
sphereInter.visible = false;
}
renderer.render( scene, camera );
}
function checkIntersection( x, y, geometry_ ) {
if ( group === undefined ) {
return;
}
mouse.x = ( x / window.innerWidth ) * 2 - 1;
mouse.y = - ( y / window.innerHeight ) * 2 + 1;
raycaster.setFromCamera( mouse.clone(), camera );
raycaster.intersectObject( group, true, intersects ); // Checks all intersection between the ray and the object with or without the descendants. Intersections are returned sorted by distance, closest first.
if ( (intersects.length > 0) && toggle > 0.02 ) {
toggle=0;
p = intersects[ 0 ].point; // the point of intersection
mouseHelper.position.copy( p ); // mousehelper should be at point
intersection.point.copy( p );
const positions = line.geometry.attributes.position; //_p;
console.log("line", line);
positions.setXYZ( 0, p.x, p.y, p.z );
positions.needsUpdate = true;
console.log("+GEOMETRY_", geometry_);
console.log(intersection);
console.log("{x:", intersection.point.x,", y:", intersection.point.y, ",z:", intersection.point.z, "}");
intersection.intersects = true;
intersects.length = 0;
} else {
//intersection.intersects = false;
// intersection = {
// intersects: false,
//point: new THREE.Vector3(),
// normal: new THREE.Vector3()
// };
intersects.length = 0;
toggle += clock.getDelta();
// p=0;
}
}
////////////////////////////////////////
// return geometry;
//render();
} );
}
</script>
</body>
</html>