-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex14.js
87 lines (71 loc) · 2.57 KB
/
ex14.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
import * as THREE from 'three';
import TWEEN from 'tween';
import { OrbitControls } from 'three-orbitcontrols-ts';
//init
const scene = new THREE.Scene()
const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000)
const renderer = new THREE.WebGLRenderer()
renderer.setSize(window.innerWidth, window.innerHeight)
document.body.appendChild(renderer.domElement)
//camera
camera.position.set(10,5,30)
camera.lookAt(10,5,0)
const pixelGroup = new THREE.Group()
const colorGroup = new THREE.Group()
// Pixels
for (let i = 0; i < 25; i++) {
for (let o = 0; o < 20; o++) {
let geometry = new THREE.PlaneGeometry( 1, 1, 1 );
let material = new THREE.MeshBasicMaterial( {color: 0xffff00, side: THREE.DoubleSide} );
let pixel = new THREE.Mesh( geometry, material );
pixel.position.z = 0
pixel.position.x = o
pixel.position.y = i
pixelGroup.add(pixel)
}
}
scene.add(pixelGroup)
// Color Select
let colors = [0xff0000, 0x00FF00, 0x0000FF, 0x00FFFF]
for (let i = 0; i < colors.length; i++) {
let geometry = new THREE.PlaneGeometry( 1, 1, 1 );
let material = new THREE.MeshBasicMaterial( {color: colors[i], side: THREE.DoubleSide} );
let pixelColor = new THREE.Mesh(geometry, material)
pixelColor.position.set(i * i, -10, 0)
colorGroup.add(pixelColor)
}
scene.add(colorGroup)
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2()
const onDocumentMouseMove = ( event ) => {
event.preventDefault();
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
}
let selectedPlane
let selectedColorPlane = 0xff0000
const onDocumentMouseDown = ( event ) => {
selectedPlane.object.material.color.set( selectedColorPlane );
}
const animate = () => {
requestAnimationFrame( animate )
render()
}
function render() {
TWEEN.update();
raycaster.setFromCamera( mouse, camera );
//pixel intersect
const intersects = raycaster.intersectObjects( pixelGroup.children, true );
if ( intersects.length > 0 ) {
selectedPlane = intersects[ 0 ]
}
//color select
const intersectsColor = raycaster.intersectObjects( colorGroup.children, true );
if ( intersectsColor.length > 0 ) {
selectedColorPlane = intersectsColor[ 0 ].object.material.color
}
renderer.render( scene, camera )
}
document.addEventListener('mousedown', onDocumentMouseDown, false);
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
animate()