05-08 05:38
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[WebGL] Mouse Dragging 본문

Programming/WebGL(ThreeJS)

[WebGL] Mouse Dragging

cinema4dr12 2015. 8. 7. 22:21

이번 예제는 마우스 드래그를 통해 컨텐츠를 컨트롤 하는 것에 관한 것입니다. 개인적으로는 본 테스트 프로젝트를 만들면서 꽤 재밌었던 기억이 납니다. 매우 심플하지만 응용하면 요긴한 프로젝트라는 생각이 듭니다.

Download Project

12-01-mouse-drag.zip


Click here to view with full screen mode


Operations

Mouse Left Button Click + Drag

main.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
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
// standard global variables
var container, scene, camera, renderer, controls, stats;
var keyboard = new THREEx.KeyboardState();
var clock = new THREE.Clock();
 
// custom global variables
var myArray = [];
var angleArray = [];
var mouseStart = { x: 0, y: 0 };
var mouseEnd = { x: 0, y: 0 };
var mouseFlag = -1;
var step = 0;
var sf = 0.5;
var radius = 150;
 
init();
animate();
 
// FUNCTIONS
function init()
{
    // SCENE
    scene = new THREE.Scene();
 
    // CAMERA
    var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;
    var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 0.1, FAR = 20000;
    camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR);
    scene.add(camera);
    camera.position.set(0,0,700);
    camera.lookAt(scene.position);
 
    // RENDERER
    if ( Detector.webgl )
        renderer = new THREE.WebGLRenderer( {antialias:true} );
    else
        renderer = new THREE.CanvasRenderer(); 
    renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
    container = document.getElementById'ThreeJS' );
    container.appendChild( renderer.domElement );
 
    // EVENTS
    THREEx.WindowResize(renderer, camera);
    THREEx.FullScreen.bindKey({ charCode : 'f'.charCodeAt(0) });
 
    // LIGHT
    var light = new THREE.PointLight(0xffffff);
    light.position.set(0,150,100);
    scene.add(light);
 
    // IMAGE PLANES
    for (var i = 0; i<10; i++){
        var planeTexture = new THREE.ImageUtils.loadTexture( 'img/'+i.toString()+'.jpg' );
        planeTexture.wrapS = planeTexture.wrapT = THREE.RepeatWrapping; 
        planeTexture.repeat.set( 11 );
        var planeMaterial = new THREE.MeshBasicMaterial( { map: planeTexture, side: THREE.DoubleSide } );
        var planeGeometry = new THREE.PlaneGeometry(70901010);
        var plane = new THREE.Mesh(planeGeometry, planeMaterial);
        plane.position.x = -100;
        plane.position.z = Math.sin((i/10)*Math.PI*2)*radius;
        plane.position.y = Math.cos((i/10)*Math.PI*2)*radius;
        myArray.push(plane);
        scene.add(myArray[i]);
    }
 
    // PLACEMENT OF IMAGE PLANES
    for(var i = 0 ; i<10; i++){
        angleArray[i] = (i/10)*Math.PI*2;
        myArray[i].position.z = Math.sin(angleArray[i])*radius;
        myArray[i].position.y = Math.cos(angleArray[i])*radius;
    }
    
    // SKYBOX/FOG
    var skyBoxGeometry = new THREE.CubeGeometry( 100001000010000 );
    var skyBoxMaterial = new THREE.MeshBasicMaterial( { color: 0x000000, side: THREE.BackSide } );
    var skyBox = new THREE.Mesh( skyBoxGeometry, skyBoxMaterial );
    scene.add(skyBox);
    
    // MOUSE EVENTS
    document.addEventListener'mousedown', onDocumentMouseDown, false );
    document.addEventListener'mousemove', onDocumentMouseMove, false );
    document.addEventListener'mouseup', onDocumentMouseUp, false );
}
 
// EXECUTE WHEN MOUSE BUTTON CLICKED
function onDocumentMouseDown( event )
{
    mouseFlag = 1;
    
    // update the mouse variable
    mouseStart.y = event.clientY/window.innerHeight;
    mouseEnd.y = event.clientY/window.innerHeight;
 
    for(var i = 0 ; i<10; i++){
        myArray[i].position.z = Math.sin(angleArray[i])*radius;
        myArray[i].position.y = Math.cos(angleArray[i])*radius;
    }
}
 
// EXECUTE WHEN MOUSE MOVES
function onDocumentMouseMove( event )
{
    if(mouseFlag == 1)
    {
        // update the mouse variable
        mouseEnd.y = event.clientY/window.innerHeight;
        step = (mouseEnd.y - mouseStart.y)/sf;
 
        for(var i = 0 ; i<10; i++){
            myArray[i].position.z = Math.sin(angleArray[i] + step)*radius;
            myArray[i].position.y = Math.cos(angleArray[i] + step)*radius;
        }
    }
}
 
// EXECUTE WHEN MOUSE BUTTON RELEASED
function onDocumentMouseUp( event )
{
    mouseFlag = -1;
 
    for(var i = 0 ; i<10; i++){
        angleArray[i] = angleArray[i] + step;
    }
}
 
function animate() 
{
    requestAnimationFrame( animate );
    render();        
    update();
}
 
function update()
{
    // add some code for update
}
 
function render() 
{
    renderer.render( scene, camera );
}
cs


'Programming > WebGL(ThreeJS)' 카테고리의 다른 글

[WebGL] Embedded HTML  (0) 2015.08.07
[WebGL] Content Awareness  (0) 2015.08.07
[WebGL] Leap Motion Controller 2  (0) 2015.08.07
[WebGL] Leap Motion 1  (0) 2015.08.07
[WebGL] PhysiJS Freefall  (0) 2015.08.07
Comments