05-18 01:46
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[WebGL] WebGL Template 본문

Programming/WebGL(ThreeJS)

[WebGL] WebGL Template

cinema4dr12 2015. 8. 6. 17:07

이번 글에서는 가장 기본적인 Sphere 오브젝트를 하나 표시하는 정도의 수준으로 일종의 Three.js 프레임웍 기반의 WebGL Project 템플릿을 소개하고자 합니다.

Download Project

00-01-template.zip


FULL SCREEN

Operations

Mouse Left Button Click & Drag: Camera Rotating

Mouse Wheel: Camera Zoom In & Out

Mouse Right Button Click & Drag: Camera Panning

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
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
//////////    
// MAIN //
//////////
 
// standard global variables
var container, scene, camera, renderer, controls, stats;
var keyboard = new THREEx.KeyboardState();
var clock = new THREE.Clock();
 
var myVar;
 
// custom global variables
var cube;
 
// initialization
init();
 
// animation loop / game loop
animate();
 
///////////////
// FUNCTIONS //
///////////////
            
function init() 
{
    ///////////
    // SCENE //
    ///////////
    scene = new THREE.Scene();
 
    ////////////
    // CAMERA //
    ////////////
    // set the view size in pixels (custom or according to window size)
    // var SCREEN_WIDTH = 400, SCREEN_HEIGHT = 300;
    var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;    
    // camera attributes
    var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 0.1, FAR = 20000;
    // set up camera
    camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR);
    // add the camera to the scene
    scene.add(camera);
    // the camera defaults to position (0,0,0)
    //     so pull it back (z = 400) and up (y = 100) and set the angle towards the scene origin
    camera.position.set(0,150,400);
    camera.lookAt(scene.position);    
    
    //////////////
    // RENDERER //
    //////////////    
    // create and start the renderer; choose antialias setting.
    if ( Detector.webgl )
        renderer = new THREE.WebGLRenderer( {antialias:true} );
    else
        renderer = new THREE.CanvasRenderer(); 
    
    renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
    
    // attach div element to variable to contain the renderer
    container = document.getElementById'ThreeJS' );
    // alternatively: to create the div at runtime, use:
    //   container = document.createElement( 'div' );
    //    document.body.appendChild( container );
    
    // attach renderer to the container div
    container.appendChild( renderer.domElement );
    
    ////////////
    // EVENTS //
    ////////////
    // automatically resize renderer
    THREEx.WindowResize(renderer, camera);
    // toggle full-screen on given key press
    THREEx.FullScreen.bindKey({ charCode : 'f'.charCodeAt(0) });
    
    //////////////
    // CONTROLS //
    //////////////
    // move mouse and: left   click to rotate, 
    //                 middle click to zoom, 
    //                 right  click to pan
    controls = new THREE.OrbitControls( camera, renderer.domElement );
    
    ///////////
    // STATS //
    ///////////
    // displays current and past frames per second attained by scene
    stats = new Stats();
    stats.domElement.style.position = 'absolute';
    stats.domElement.style.bottom = '0px';
    stats.domElement.style.zIndex = 100;
    //container.appendChild( stats.domElement );
    
    ///////////
    // LIGHT //
    ///////////
    // create a light
    var light = new THREE.PointLight(0xffffff);
    light.position.set(0,250,0);
    scene.add(light);
    var ambientLight = new THREE.AmbientLight(0x111111);
    // scene.add(ambientLight);
    
    ///////////
    // FLOOR //
    ///////////
    // note: 4x4 checkboard pattern scaled so that each square is 25 by 25 pixels.
    var floorTexture = new THREE.ImageUtils.loadTexture( 'img/Wood_floor.jpg' );
    floorTexture.wrapS = floorTexture.wrapT = THREE.RepeatWrapping; 
    floorTexture.repeat.set( 11 );
    // DoubleSide: render texture on both sides of mesh
    var floorMaterial = new THREE.MeshBasicMaterial( { map: floorTexture, side: THREE.DoubleSide } );
    var floorGeometry = new THREE.PlaneGeometry(1000100011);
    var floor = new THREE.Mesh(floorGeometry, floorMaterial);
    floor.position.y = -0.5;
    floor.rotation.x = Math.PI / 2;
    scene.add(floor);
    
    /////////
    // SKY //
    /////////
    // recommend either a skybox or fog effect (can't use both at the same time) 
    // without one of these, the scene's background color is determined by webpage background
 
    // make sure the camera's "far" value is large enough so that it will render the skyBox!
    var skyBoxGeometry = new THREE.CubeGeometry( 100001000010000 );
    // BackSide: render faces from inside of the cube, instead of from outside (default).
    var skyBoxMaterial = new THREE.MeshBasicMaterial( { color: 0x9999ff, side: THREE.BackSide } );
    var skyBox = new THREE.Mesh( skyBoxGeometry, skyBoxMaterial );
    // scene.add(skyBox);
    
    // fog must be added to scene before first render
    scene.fog = new THREE.FogExp2( 0x9999ff0.00025 );
 
    // ADD A SPHERE GEOMETRY
    var sphereGeometry = new THREE.SphereGeometry( 503216 ); // radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength
    var sphereMaterial = new THREE.MeshLambertMaterial( { color: 0x8ef421 } );
    sphereMesh = new THREE.Mesh( sphereGeometry, sphereMaterial );
    sphereMesh.position.set(0,50,0);
    scene.add(sphereMesh);
}
 
function animate() 
{
    requestAnimationFrame( animate );
    render();        
    update();
}
 
function update()
{
    // delta = change in time since last call (in seconds)
    var delta = clock.getDelta(); 
 
    // functionality provided by THREEx.KeyboardState.js
    if ( keyboard.pressed("1") )
        document.getElementById('message').innerHTML = ' Have a nice day! - 1';    
    if ( keyboard.pressed("2") )
        document.getElementById('message').innerHTML = ' Have a nice day! - 2 ';    
        
    controls.update();
    stats.update();
}
 
function render() 
{    
    renderer.render( scene, camera );
}
cs


Comments