05-17 06:36
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[WebGL] Web-Cam Texture 본문

Programming/WebGL(ThreeJS)

[WebGL] Web-Cam Texture

cinema4dr12 2015. 8. 6. 17:30

이번 예제는 Web-Cam으로부터 받은 화면 이미지를 각종 오브젝트의 texture로 출력하는 예제입니다.


웹캠이 없는 경우 각 오브젝트의 texture는 검게 출력되며, 웹 브라우저가 웹캠을 허용하겠냐는 메시지에 대하여 허용하지 않을 경우에도 검게 출력됩니다.

Download Project

01-02-webcam-texture.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]

// standard global variables var container, scene, camera, renderer, controls, stats; var keyboard = new THREEx.KeyboardState(); // custom global variables var video, videoImage, videoImageContext, videoTexture; var play; init(); animate(); // FUNCTIONS function init() { // PLAY OR PAUSE play = 1; // 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 ); // CONTROLS controls = new THREE.OrbitControls( camera, renderer.domElement ); // EVENTS THREEx.WindowResize(renderer, camera); THREEx.FullScreen.bindKey({ charCode : 'f'.charCodeAt(0) }); // 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); // 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; scene.add(floor); // SKYBOX/FOG scene.fog = new THREE.FogExp2( 0x2d2df9, 0.00025 ); /////////// // VIDEO // /////////// video = document.getElementById( 'monitor' ); videoImage = document.getElementById( 'videoImage' ); videoImageContext = videoImage.getContext( '2d' ); // background color if no video present videoImageContext.fillStyle = '#000000'; videoImageContext.fillRect( 0, 0, videoImage.width, videoImage.height ); videoTexture = new THREE.Texture( videoImage ); videoTexture.minFilter = THREE.LinearFilter; videoTexture.magFilter = THREE.LinearFilter; var movieMaterial = new THREE.MeshBasicMaterial( { map: videoTexture, overdraw: true, side:THREE.DoubleSide } ); // the geometry on which the movie will be displayed; // movie image will be scaled to fit these dimensions. var movieGeometry = new THREE.PlaneGeometry( 100, 100, 1, 1 ); var movieScreen = new THREE.Mesh( movieGeometry, movieMaterial ); movieScreen.position.set(0,50,0); scene.add(movieScreen); // ADD A CYLINDER GEOMETRY var cylinderGeometry = new THREE.CylinderGeometry( 50, 50, 64, 64 ); // radiusTop, radiusBottom, height, radiusSegments, heightSegments, openEnded var cylinderMaterial = new THREE.MeshLambertMaterial( { color: 0xdf18bc } ); cylinderMesh = new THREE.Mesh( cylinderGeometry, movieMaterial ); cylinderMesh.position.set(150,50,0); cylinderMesh.rotation.set(0,180,0); scene.add(cylinderMesh); // ADD A CUBE GEOMETRY var cubeGeometry = new THREE.CubeGeometry( 100, 100, 100 ); // width, height, depth, widthSegments, heightSegments, depthSegments var cubeMaterial = new THREE.MeshLambertMaterial( { color: 0x7dc9b6 } ); cubeMesh = new THREE.Mesh( cubeGeometry, movieMaterial ); cubeMesh.position.set(-150,50,-30); scene.add(cubeMesh); camera.position.set(0,150,300); camera.lookAt(movieScreen.position); } function animate() { requestAnimationFrame( animate ); render(); update(); } function update() { if ( keyboard.pressed("p") ) play *= -1; if(play == 1) video.play(); else video.pause(); controls.update(); stats.update(); } function render() { if ( video.readyState === video.HAVE_ENOUGH_DATA ) { videoImageContext.drawImage( video, 0, 0, videoImage.width, videoImage.height ); if ( videoTexture ) videoTexture.needsUpdate = true; } renderer.render( scene, camera ); }


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

[WebGL] Material Basic  (0) 2015.08.06
[WebGL] Geometry Basic  (0) 2015.08.06
[WebGL] Web-Cam으로 화면 출력하기  (0) 2015.08.06
[WebGL] WebGL Template  (0) 2015.08.06
[WebGL] Lesson 5 - Three.js 소개  (0) 2014.02.10
Comments