05-09 18:17
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[WebGL] Shadow Basic 본문

Programming/WebGL(ThreeJS)

[WebGL] Shadow Basic

cinema4dr12 2015. 8. 6. 22:58

이번 예제에서는 Three.js WebGL 프레임웍에서의 그림자를 구현하는 방법에 관한 것입니다.

Download Project

04-01-shadow-basic.zip


Click here to view with full screen mode


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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// MAIN
 
// standard global variables
var container, scene, camera, renderer, controls, stats;
var keyboard = new THREEx.KeyboardState();
var clock = new THREE.Clock();
// custom global variables
var cube, rotY, step;
var textMesh, rotX;
 
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,150,400);
    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) });
 
    // CONTROLS
    controls = new THREE.OrbitControls( camera, renderer.domElement );
 
    // STATS
    stats = new Stats();
    stats.domElement.style.position = 'absolute';
    stats.domElement.style.bottom = '0px';
    stats.domElement.style.zIndex = 100;
    container.appendChild( stats.domElement );
 
    // LIGHT
    //var light = new THREE.PointLight(0xffffff);
    //light.position.set(0,250,0);
    //scene.add(light);
    
    // SKYBOX/FOG
    var skyBoxGeometry = new THREE.CubeGeometry( 100001000010000 );
    var skyBoxMaterial = new THREE.MeshBasicMaterial( { color: 0x9999ff, side: THREE.BackSide } );
    var skyBox = new THREE.Mesh( skyBoxGeometry, skyBoxMaterial );
    // scene.add(skyBox);
    scene.fog = new THREE.FogExp2( 0x9999ff0.00025 );
    
    ////////////
    // CUSTOM //
    ////////////
    // must enable shadows on the renderer 
    renderer.shadowMapEnabled = true;
    
    // "shadow cameras" show the light source and direction
    
    // spotlight #1 -- yellow, dark shadow
    var spotlight1 = new THREE.SpotLight(0xffc051);
    spotlight1.position.set(-60,150,-30);
    spotlight1.shadowCameraVisible = true;
    spotlight1.shadowDarkness = 0.95;
    spotlight1.intensity = 2;
    spotlight1.castShadow = true// enable shadow casting ability for the light
    scene.add(spotlight1);
 
    // spotlight #2 -- red, light shadow
    var spotlight2 = new THREE.SpotLight(0x7cc0ff);
    spotlight2.position.set(60,150,-60);
    scene.add(spotlight2);
    spotlight2.shadowCameraVisible = true;
    spotlight2.shadowDarkness = 0.70;
    spotlight2.intensity = 2;
    spotlight2.castShadow = true;
 
    // set the target position of the lights
    var lightTarget = new THREE.Object3D();
    var targetPosition = new Array();
    targetPosition[0= 0;
    targetPosition[1= 50;
    targetPosition[2= 0;
    lightTarget.position.set(targetPosition[0], targetPosition[1], targetPosition[2]);
    scene.add(lightTarget);
    spotlight1.target = lightTarget;
    spotlight2.target = lightTarget;
 
    // cube: mesh to cast shadows
    var cubeGeometry = new THREE.CubeGeometry( 505050 );
    var cubeMaterial = new THREE.MeshLambertMaterial( { color: 0x888888 } );
    cube = new THREE.Mesh( cubeGeometry, cubeMaterial );
    step = Math.PI/180;
    rotY = 0;
    cube.rotation.y = rotY;
    cube.position.set(targetPosition[0], targetPosition[1], targetPosition[2]);
    // the mesh is flagged to cast shadows
    cube.castShadow = true;
    scene.add(cube);
 
    // text: mesh to cast shadows
    var materialFront = new THREE.MeshBasicMaterial( { color: 0x00f523 } );
    var materialSide = new THREE.MeshBasicMaterial( { color: 0xffc75c } );
    var materialArray = [ materialFront, materialSide ];
    var textGeom = new THREE.TextGeometry( "WEBGL"
    {
        size: 30, height: 4, curveSegments: 3,
        font: "helvetiker", weight: "bold", style: "normal",
        bevelThickness: 1, bevelSize: 1, bevelEnabled: true,
        material: 0, extrudeMaterial: 1
    });
    var textMaterial = new THREE.MeshFaceMaterial(materialArray);
    textMesh = new THREE.Mesh( textGeom, textMaterial );
    textMesh.castShadow = true;
    textGeom.computeBoundingBox();
    var textWidth = textGeom.boundingBox.max.x - textGeom.boundingBox.min.x;
    textMesh.position.set( -0.5 * textWidth, 30100 );
    rotX = 0;
    textMesh.rotation.x = rotX;
    scene.add(textMesh);
    
    // floor: mesh to receive shadows
    var floorTexture = new THREE.ImageUtils.loadTexture( 'img/Wood_floor.jpg' );
    floorTexture.wrapS = floorTexture.wrapT = THREE.RepeatWrapping; 
    floorTexture.repeat.set( 11 );
    // Note the change to Lambert material.
    var floorMaterial = new THREE.MeshLambertMaterial( { map: floorTexture, side: THREE.DoubleSide } );
    var floorGeometry = new THREE.PlaneGeometry(10001000100100);
    var floor = new THREE.Mesh(floorGeometry, floorMaterial);
    floor.position.y = -0.5;
    floor.rotation.x = Math.PI / 2;
    floor.receiveShadow = true// the mesh is flagged to receive shadows
    scene.add(floor);
    
    // create "light-ball" meshes
    var sphereGeometry = new THREE.SphereGeometry( 10168 );
    var darkMaterial = new THREE.MeshBasicMaterial( { color: 0x000000 } );
        
    var wireframeMaterial = new THREE.MeshBasicMaterial( 
        { color: 0xffc051, wireframe: true, transparent: true } ); 
    var shape = THREE.SceneUtils.createMultiMaterialObject( 
        sphereGeometry, [ darkMaterial, wireframeMaterial ] );
    shape.position = spotlight1.position;
    scene.add( shape );
    
    var wireframeMaterial = new THREE.MeshBasicMaterial( 
        { color: 0x7cc0ff, wireframe: true, transparent: true } ); 
    var shape = THREE.SceneUtils.createMultiMaterialObject( 
        sphereGeometry, [ darkMaterial, wireframeMaterial ] );
    shape.position = spotlight2.position;
    scene.add( shape );
}
 
function animate() 
{
    requestAnimationFrame( animate );
    render();        
    update();
}
 
function update()
{
    rotY += step;
    cube.rotation.y = rotY;
 
    rotX += step;
    textMesh.rotation.x = rotX;
    
    controls.update();
    stats.update();
}
 
function render() 
{
    renderer.render( scene, camera );
}
cs


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

[WebGL] Multiple Cameras  (0) 2015.08.06
[WebGL] Texture Basic  (0) 2015.08.06
[WebGL] Refraction  (0) 2015.08.06
[WebGL] Cube Map  (0) 2015.08.06
[WebGL] Material Basic  (0) 2015.08.06
Comments