일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 빅 데이타
- 빅 데이터
- 김양재 목사
- 김양재 목사님
- 딥러닝
- Statistics
- 인공지능
- MongoDB
- R
- 확률
- openCV
- 빅데이터
- node.js
- 김양재
- nodeJS
- Artificial Intelligence
- data science
- No SQL
- c++
- 데이터 과학
- 주일설교
- 몽고디비
- 우리들교회
- probability
- Big Data
- WebGL
- 빅데이타
- 통계
- Deep learning
- Machine Learning
Archives
- Today
- Total
Scientific Computing & Data Science
[WebGL] Model Animation Control 본문
이번 예제는 미리 정의된 특정 동작 애니메이션에 대하여 키보드로 컨트롤하는 내용입니다. 본 예제는 Three.js WebGL 프레임웍을 이용하여 Video Game을 제작할 때 아주 유용하게 응용될 수 있을 것입니다. 컨텐츠의 복잡도를 줄이기 위해 Android 모델을 이용하였습니다.
* NOTE: 본 예제는 FULL SCREEN MODE로 들어가야 Android 컨트롤이 가능합니다.
Download Project
08-02-model-animation-control.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 | // 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 obj; var animOffset = 0, // starting frame of animation walking = false, duration = 1000, // milliseconds to complete animation keyframes = 20, // total number of animation frames interpolation = duration / keyframes, // milliseconds per frame lastKeyframe = 0, // previous keyframe currentKeyframe = 0; 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 : 'm'.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 ); renderer.shadowMapEnabled = true; // Point Light var light = new THREE.PointLight(0xffffff); light.position.set(-100,200,100); scene.add(light); // Ambient Light var ambientLight = new THREE.AmbientLight(0x111111); scene.add(ambientLight); // Direction Light (Shadow casting) var dirLight = new THREE.DirectionalLight(0xffffff); dirLight.shadowCameraVisible = true; dirLight.shadowDarkness = 0.95; dirLight.intensity = 1.0; dirLight.castShadow = true; dirLight.position.set( 0, 100, 200 ); scene.add(dirLight); // set the target position of the lights var lightTarget = new THREE.Object3D(); var targetPosition = new Array(); targetPosition[0] = 0; targetPosition[1] = -30; targetPosition[2] = 0; lightTarget.position.set(targetPosition[0], targetPosition[1], targetPosition[2]); scene.add(lightTarget); dirLight.target = lightTarget; // FLOOR var floorTexture = new THREE.ImageUtils.loadTexture( 'img/Wood_floor.jpg' ); floorTexture.wrapS = floorTexture.wrapT = THREE.RepeatWrapping; floorTexture.repeat.set( 1, 1 ); var floorMaterial = new THREE.MeshBasicMaterial( { map: floorTexture, side: THREE.DoubleSide } ); var floorGeometry = new THREE.PlaneGeometry(1000, 1000, 10, 10); var floor = new THREE.Mesh(floorGeometry, floorMaterial); floor.position.y = -0.5; floor.rotation.x = Math.PI / 2; floor.receiveShadow = true; scene.add(floor); // SKYBOX/FOG var skyBoxGeometry = new THREE.CubeGeometry( 10000, 10000, 10000 ); 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( 0x9999ff, 0.00025 ); //////////// // CUSTOM // //////////// var jsonLoader = new THREE.JSONLoader(); jsonLoader.load( "models/android-animations.js", addModelToScene ); } function addModelToScene( geometry, materials ) { // for preparing animation for (var i = 0; i < materials.length; i++) materials[i].morphTargets = true; var material = new THREE.MeshFaceMaterial( materials ); obj = new THREE.Mesh( geometry, material ); obj.scale.set(10,10,10); obj.castShadow = true; scene.add( obj ); } function animate() { requestAnimationFrame( animate ); render(); update(); } function update() { // delta = change in time since last call (seconds) delta = clock.getDelta(); var moveDistance = 100 * delta; walking = false; // move forwards / backwards if ( keyboard.pressed("s") ) obj.translateZ( -moveDistance ); if ( keyboard.pressed("w") ) obj.translateZ( moveDistance ); // rotate left/right if ( keyboard.pressed("a") ) obj.rotation.y += delta; if ( keyboard.pressed("d") ) obj.rotation.y -= delta; var walkingKeys = ["w", "s", "a", "d"]; for (var i = 0; i < walkingKeys.length; i++) { if ( keyboard.pressed(walkingKeys[i]) ) walking = true; } controls.update(); stats.update(); } function render() { if ( obj && walking ) // obj exists + walking signal enabled { // Alternate morph targets time = new Date().getTime() % duration; keyframe = Math.floor( time / interpolation ) + animOffset; if ( keyframe != currentKeyframe ) { obj.morphTargetInfluences[ lastKeyframe ] = 0; obj.morphTargetInfluences[ currentKeyframe ] = 1; obj.morphTargetInfluences[ keyframe ] = 0; lastKeyframe = currentKeyframe; currentKeyframe = keyframe; } obj.morphTargetInfluences[ keyframe ] = ( time % interpolation ) / interpolation; obj.morphTargetInfluences[ lastKeyframe ] = 1 - obj.morphTargetInfluences[ keyframe ]; } renderer.render( scene, camera ); } | cs |
'Programming > WebGL(ThreeJS)' 카테고리의 다른 글
[WebGL] Collision Detection (0) | 2015.08.07 |
---|---|
[WebGL] Viewport 4분할 (0) | 2015.08.07 |
[WebGL] Tween Animation (0) | 2015.08.07 |
[WebGL] Blender Animation Scene 로딩하기 (0) | 2015.08.07 |
[WebGL] Blender Scene 불러오기 (0) | 2015.08.07 |
Comments