VR开发 入门 使用Three.js 开发的WebVR demo
2017-03-19 20:10
711 查看
使用Three.js,并且参考网上开源的代码,终于实现了第一个VR程序。
在线演示:
https://ritterliu.github.io/WebVR_Demo/
[html]
view plain
copy
<!DOCTYPE html>
<html>
<head>
<title>WebVR Demo</title>
<style>
body {
width: 100%;
height: 100%;
background-color: #000;
}
</style>
</head>
<body>
<script src="./js/three.min.js"></script>
<script src="./js/StereoEffect.js"></script>
<script src="./js/OrbitControls.js"></script>
<script src="./js/DeviceOrientationControls.js"></script>
<script src="./js/helvetiker_regular.typeface.js"></script>
<script>
var scene, camera, renderer, effect, element, controls, word = "Ritter's VR World";
init();
function init() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(90, window.innerWidth / window.innerHeight, 0.001, 700);
camera.position.set(0, 15, 0);
scene.add(camera);
renderer = new THREE.WebGLRenderer();
element = renderer.domElement;
document.body.appendChild(renderer.domElement);
effect = new THREE.StereoEffect(renderer);
//Handle mouse control
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.target.set(
camera.position.x + 0.01,
camera.position.y,
camera.position.z
);
window.addEventListener('deviceorientation', setDeviceOrientationControls, true);
//Create light
var light = new THREE.PointLight( 0xffffff, 1.2, 0 );
light.position.set(0, 50, 0);
scene.add(light);
// Create floor
var floorTexture = THREE.ImageUtils.loadTexture('img/grass.jpg');
floorTexture.wrapS = THREE.RepeatWrapping;
floorTexture.wrapT = THREE.RepeatWrapping;
floorTexture.repeat = new THREE.Vector2(50, 50);
var floorMaterial = new THREE.MeshPhongMaterial({
map: floorTexture
});
var floorGeometry = new THREE.PlaneBufferGeometry(1000, 1000);
var floor = new THREE.Mesh(floorGeometry, floorMaterial);
floor.rotation.x = -Math.PI / 2;
scene.add(floor);
// Create box
var geometry = new THREE.BoxGeometry(6, 6, 6);
var material = new THREE.MeshNormalMaterial();
var cube = new THREE.Mesh(geometry, material);
cube.position.set(-15, 30, 10);
scene.add(cube);
//Create text
var textGeometry = new THREE.TextGeometry(word, {
size: 5,
height: 1
});
var text = new THREE.Mesh(textGeometry, new THREE.MeshBasicMaterial({
color: 0xffffff
}));
text.position.set(15, 15, -25);
text.rotation.set(0, 30, 0);
scene.add(text);
animate();
}
// Our preferred controls via DeviceOrientation
function setDeviceOrientationControls(e) {
controls = new THREE.DeviceOrientationControls(camera, true);
controls.connect();
controls.update();
window.removeEventListener('deviceorientation', setDeviceOrientationControls, true);
}
function animate() {
requestAnimationFrame(animate);
var width = window.innerWidth;
var height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
effect.setSize(width, height);
camera.updateProjectionMatrix();
controls.update();
effect.render(scene, camera);
}
</script>
</body>
</html>
源码下载:
https://github.com/ritterliu/WebVR_Demo
参考文献:
http://threejs.org/docs/ http://mt.sohu.com/20151026/n424132314.shtml http://www.sitepoint.com/bringing- href="http://lib.csdn.net/base/vr" target=_blank>VR-to-web-google-cardboard-three-js/
在线演示:
https://ritterliu.github.io/WebVR_Demo/
[html]
view plain
copy
<!DOCTYPE html>
<html>
<head>
<title>WebVR Demo</title>
<style>
body {
width: 100%;
height: 100%;
background-color: #000;
}
</style>
</head>
<body>
<script src="./js/three.min.js"></script>
<script src="./js/StereoEffect.js"></script>
<script src="./js/OrbitControls.js"></script>
<script src="./js/DeviceOrientationControls.js"></script>
<script src="./js/helvetiker_regular.typeface.js"></script>
<script>
var scene, camera, renderer, effect, element, controls, word = "Ritter's VR World";
init();
function init() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(90, window.innerWidth / window.innerHeight, 0.001, 700);
camera.position.set(0, 15, 0);
scene.add(camera);
renderer = new THREE.WebGLRenderer();
element = renderer.domElement;
document.body.appendChild(renderer.domElement);
effect = new THREE.StereoEffect(renderer);
//Handle mouse control
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.target.set(
camera.position.x + 0.01,
camera.position.y,
camera.position.z
);
window.addEventListener('deviceorientation', setDeviceOrientationControls, true);
//Create light
var light = new THREE.PointLight( 0xffffff, 1.2, 0 );
light.position.set(0, 50, 0);
scene.add(light);
// Create floor
var floorTexture = THREE.ImageUtils.loadTexture('img/grass.jpg');
floorTexture.wrapS = THREE.RepeatWrapping;
floorTexture.wrapT = THREE.RepeatWrapping;
floorTexture.repeat = new THREE.Vector2(50, 50);
var floorMaterial = new THREE.MeshPhongMaterial({
map: floorTexture
});
var floorGeometry = new THREE.PlaneBufferGeometry(1000, 1000);
var floor = new THREE.Mesh(floorGeometry, floorMaterial);
floor.rotation.x = -Math.PI / 2;
scene.add(floor);
// Create box
var geometry = new THREE.BoxGeometry(6, 6, 6);
var material = new THREE.MeshNormalMaterial();
var cube = new THREE.Mesh(geometry, material);
cube.position.set(-15, 30, 10);
scene.add(cube);
//Create text
var textGeometry = new THREE.TextGeometry(word, {
size: 5,
height: 1
});
var text = new THREE.Mesh(textGeometry, new THREE.MeshBasicMaterial({
color: 0xffffff
}));
text.position.set(15, 15, -25);
text.rotation.set(0, 30, 0);
scene.add(text);
animate();
}
// Our preferred controls via DeviceOrientation
function setDeviceOrientationControls(e) {
controls = new THREE.DeviceOrientationControls(camera, true);
controls.connect();
controls.update();
window.removeEventListener('deviceorientation', setDeviceOrientationControls, true);
}
function animate() {
requestAnimationFrame(animate);
var width = window.innerWidth;
var height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
effect.setSize(width, height);
camera.updateProjectionMatrix();
controls.update();
effect.render(scene, camera);
}
</script>
</body>
</html>
源码下载:
https://github.com/ritterliu/WebVR_Demo
参考文献:
http://threejs.org/docs/ http://mt.sohu.com/20151026/n424132314.shtml http://www.sitepoint.com/bringing- href="http://lib.csdn.net/base/vr" target=_blank>VR-to-web-google-cardboard-three-js/
相关文章推荐
- VR开发 入门 使用Three.js 开发的WebVR demo
- Three.JS学习 9:WEBVR 入门demo
- web前端开发入门提高 js书籍(附下载地址)
- THREE.JS入门教程(1)THREE.JS使用前了解
- ios开发入门- WebView使用
- web前端开发分享-css,js入门篇(转)
- Node.js开发入门—用MongoDB改造LoginDemo
- ios开发入门- WebView使用
- WEB前端开发学习----6.CSS 和 JS 在html的使用方法
- Node.js开发入门—使用AngularJS内置服务
- ios开发入门- WebView使用
- Node.js开发入门—使用http访问外部世界
- THREE.JS入门教程(1)THREE.JS使用前了解
- 使用angular.js开发的一个简易todo demo
- Node.js开发入门—使用jade模板引擎
- 快速使用node.js进行web开发
- PHP-Web应用程序开发:使用模板(入门)
- JavaWeb常用开发技术总结-js、spring、javabean、mysql数据库、dwr的使用等
- Node.js 入门手册:那些最流行的 Web 开发框架
- Node.js开发入门—Express安装与使用