|
@@ -0,0 +1,421 @@
|
|
|
+<!DOCTYPE html>
|
|
|
+<html lang="en">
|
|
|
+ <head>
|
|
|
+ <title>three.js webvr - htc vive - paint</title>
|
|
|
+ <meta charset="utf-8">
|
|
|
+ <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
|
|
|
+ <style>
|
|
|
+ body {
|
|
|
+ font-family: Monospace;
|
|
|
+ background-color: #101010;
|
|
|
+ color: #fff;
|
|
|
+ margin: 0px;
|
|
|
+ overflow: hidden;
|
|
|
+ }
|
|
|
+ a {
|
|
|
+ color: #f00;
|
|
|
+ }
|
|
|
+ </style>
|
|
|
+ </head>
|
|
|
+ <body>
|
|
|
+
|
|
|
+ <script src="../build/three.js"></script>
|
|
|
+
|
|
|
+ <script src="js/WebVR.js"></script>
|
|
|
+ <script src="js/effects/VREffect.js"></script>
|
|
|
+ <script src="js/controls/VRControls.js"></script>
|
|
|
+ <script src="js/ViveController.js"></script>
|
|
|
+
|
|
|
+ <script src="js/loaders/OBJLoader.js"></script>
|
|
|
+
|
|
|
+ <script>
|
|
|
+
|
|
|
+ if ( WEBVR.isLatestAvailable() === false ) {
|
|
|
+
|
|
|
+ document.body.appendChild( WEBVR.getMessage() );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ var container;
|
|
|
+ var camera, scene, renderer;
|
|
|
+ var effect, controls;
|
|
|
+ var controller1, controller2;
|
|
|
+
|
|
|
+ var line;
|
|
|
+ var shapes = {};
|
|
|
+
|
|
|
+ var up = new THREE.Vector3( 0, 1, 0 );
|
|
|
+ var vector = new THREE.Vector3();
|
|
|
+
|
|
|
+ var vector1 = new THREE.Vector3();
|
|
|
+ var vector2 = new THREE.Vector3();
|
|
|
+ var vector3 = new THREE.Vector3();
|
|
|
+ var vector4 = new THREE.Vector3();
|
|
|
+
|
|
|
+ var color = new THREE.Color( 0, 0, 0 );
|
|
|
+
|
|
|
+ var point4 = new THREE.Vector3();
|
|
|
+ var point5 = new THREE.Vector3();
|
|
|
+
|
|
|
+ init();
|
|
|
+ initGeometry();
|
|
|
+ animate();
|
|
|
+
|
|
|
+ function init() {
|
|
|
+
|
|
|
+ container = document.createElement( 'div' );
|
|
|
+ document.body.appendChild( container );
|
|
|
+
|
|
|
+ var info = document.createElement( 'div' );
|
|
|
+ info.style.position = 'absolute';
|
|
|
+ info.style.top = '10px';
|
|
|
+ info.style.width = '100%';
|
|
|
+ info.style.textAlign = 'center';
|
|
|
+ info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js</a> webgl - htc vive - paint';
|
|
|
+ container.appendChild( info );
|
|
|
+
|
|
|
+ scene = new THREE.Scene();
|
|
|
+ scene.background = new THREE.Color( 0x222222 );
|
|
|
+
|
|
|
+ camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 50 );
|
|
|
+ scene.add( camera );
|
|
|
+
|
|
|
+ var geometry = new THREE.BoxGeometry( 0.5, 0.8, 0.5 );
|
|
|
+ var material = new THREE.MeshStandardMaterial( { color: 0x444444, roughness: 1.0, metalness: 0.0 } );
|
|
|
+ var table = new THREE.Mesh( geometry, material );
|
|
|
+ table.position.y = 0.35;
|
|
|
+ table.position.z = 0.85;
|
|
|
+ table.castShadow = true;
|
|
|
+ table.receiveShadow = true;
|
|
|
+ scene.add( table );
|
|
|
+
|
|
|
+ /*
|
|
|
+ var table = new THREE.Mesh( geometry, material );
|
|
|
+ table.position.y = 0.35;
|
|
|
+ table.position.z = -0.85;
|
|
|
+ table.castShadow = true;
|
|
|
+ table.receiveShadow = true;
|
|
|
+ scene.add( table );
|
|
|
+ */
|
|
|
+
|
|
|
+ var geometry = new THREE.PlaneGeometry( 4, 4 );
|
|
|
+ var material = new THREE.MeshStandardMaterial( { color: 0x222222, roughness: 1.0, metalness: 0.0 } );
|
|
|
+ var floor = new THREE.Mesh( geometry, material );
|
|
|
+ floor.rotation.x = - Math.PI / 2;
|
|
|
+ floor.receiveShadow = true;
|
|
|
+ scene.add( floor );
|
|
|
+
|
|
|
+ scene.add( new THREE.GridHelper( 10, 40, 0x111111, 0x111111 ) );
|
|
|
+
|
|
|
+ scene.add( new THREE.HemisphereLight( 0x888877, 0x777788 ) );
|
|
|
+
|
|
|
+ var light = new THREE.DirectionalLight( 0xffffff );
|
|
|
+ light.position.set( 0, 6, 0 );
|
|
|
+ light.castShadow = true;
|
|
|
+ light.shadow.camera.top = 2;
|
|
|
+ light.shadow.camera.bottom = -2;
|
|
|
+ light.shadow.camera.right = 2;
|
|
|
+ light.shadow.camera.left = -2;
|
|
|
+ light.shadow.mapSize.set( 4096, 4096 );
|
|
|
+
|
|
|
+ scene.add( light );
|
|
|
+
|
|
|
+ // scene.add( new THREE.DirectionalLightHelper( light ) );
|
|
|
+ // scene.add( new THREE.CameraHelper( light.shadow.camera ) );
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ renderer = new THREE.WebGLRenderer( { antialias: true } );
|
|
|
+ renderer.setPixelRatio( window.devicePixelRatio );
|
|
|
+ renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
+ renderer.domElement.style.width = ( window.innerWidth * 2 ) + 'px';
|
|
|
+ renderer.sortObjects = false;
|
|
|
+ renderer.shadowMap.enabled = true;
|
|
|
+ renderer.gammaInput = true;
|
|
|
+ renderer.gammaOutput = true;
|
|
|
+ container.appendChild( renderer.domElement );
|
|
|
+
|
|
|
+ controls = new THREE.VRControls( camera );
|
|
|
+ controls.standing = true;
|
|
|
+
|
|
|
+ // controllers
|
|
|
+
|
|
|
+ controller1 = new THREE.ViveController( 0 );
|
|
|
+ controller1.standingMatrix = controls.getStandingMatrix();
|
|
|
+ controller1.userData.points = [ new THREE.Vector3(), new THREE.Vector3() ];
|
|
|
+ controller1.userData.matrices = [ new THREE.Matrix4(), new THREE.Matrix4() ];
|
|
|
+ scene.add( controller1 );
|
|
|
+
|
|
|
+ controller2 = new THREE.ViveController( 1 );
|
|
|
+ controller2.standingMatrix = controls.getStandingMatrix();
|
|
|
+ controller2.userData.points = [ new THREE.Vector3(), new THREE.Vector3() ];
|
|
|
+ controller2.userData.matrices = [ new THREE.Matrix4(), new THREE.Matrix4() ];
|
|
|
+ scene.add( controller2 );
|
|
|
+
|
|
|
+ var loader = new THREE.OBJLoader();
|
|
|
+ loader.setPath( 'models/obj/vive-controller/' );
|
|
|
+ loader.load( 'vr_controller_vive_1_5.obj', function ( object ) {
|
|
|
+
|
|
|
+ var loader = new THREE.TextureLoader();
|
|
|
+ loader.setPath( 'models/obj/vive-controller/' );
|
|
|
+
|
|
|
+ var controller = object.children[ 0 ];
|
|
|
+ controller.material.map = loader.load( 'onepointfive_texture.png' );
|
|
|
+ controller.material.specularMap = loader.load( 'onepointfive_spec.png' );
|
|
|
+ controller.castShadow = true;
|
|
|
+ controller.receiveShadow = true;
|
|
|
+
|
|
|
+ // var pivot = new THREE.Group();
|
|
|
+ // var pivot = new THREE.Mesh( new THREE.BoxGeometry( 0.01, 0.01, 0.01 ) );
|
|
|
+ var pivot = new THREE.Mesh( new THREE.IcosahedronGeometry( 0.002, 2 ) );
|
|
|
+ pivot.name = 'pivot';
|
|
|
+ pivot.position.y = -0.016;
|
|
|
+ pivot.position.z = -0.043;
|
|
|
+ pivot.rotation.x = Math.PI / 5.5;
|
|
|
+ controller.add( pivot );
|
|
|
+
|
|
|
+ var range = new THREE.Mesh( new THREE.IcosahedronGeometry( 0.03, 3 ), new THREE.MeshBasicMaterial( { opacity: 0.25, transparent: true } ) );
|
|
|
+ pivot.add( range );
|
|
|
+
|
|
|
+ controller1.add( controller.clone() );
|
|
|
+ controller2.add( controller.clone() );
|
|
|
+
|
|
|
+ } );
|
|
|
+
|
|
|
+ effect = new THREE.VREffect( renderer );
|
|
|
+
|
|
|
+ if ( WEBVR.isAvailable() === true ) {
|
|
|
+
|
|
|
+ document.body.appendChild( WEBVR.getButton( effect ) );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ window.addEventListener( 'resize', onWindowResize, false );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function initGeometry() {
|
|
|
+
|
|
|
+ var geometry = new THREE.BufferGeometry();
|
|
|
+
|
|
|
+ var positions = new THREE.BufferAttribute( new Float32Array( 1000000 * 3 ), 3 );
|
|
|
+ geometry.addAttribute( 'position', positions );
|
|
|
+
|
|
|
+ var normals = new THREE.BufferAttribute( new Float32Array( 1000000 * 3 ), 3 );
|
|
|
+ geometry.addAttribute( 'normal', normals );
|
|
|
+
|
|
|
+ var colors = new THREE.BufferAttribute( new Float32Array( 1000000 * 3 ), 3 );
|
|
|
+ geometry.addAttribute( 'color', colors );
|
|
|
+
|
|
|
+ geometry.drawRange.count = 0;
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ /*
|
|
|
+ var path = "textures/cube/SwedishRoyalCastle/";
|
|
|
+ var format = '.jpg';
|
|
|
+ var urls = [
|
|
|
+ path + 'px' + format, path + 'nx' + format,
|
|
|
+ path + 'py' + format, path + 'ny' + format,
|
|
|
+ path + 'pz' + format, path + 'nz' + format
|
|
|
+ ];
|
|
|
+
|
|
|
+ var reflectionCube = new THREE.CubeTextureLoader().load( urls );
|
|
|
+ */
|
|
|
+
|
|
|
+ var material = new THREE.MeshStandardMaterial( {
|
|
|
+ roughness: 0.9,
|
|
|
+ metalness: 0.0,
|
|
|
+ // envMap: reflectionCube,
|
|
|
+ vertexColors: THREE.VertexColors,
|
|
|
+ side: THREE.DoubleSide
|
|
|
+ } );
|
|
|
+
|
|
|
+ line = new THREE.Mesh( geometry, material );
|
|
|
+ line.frustumCulled = false;
|
|
|
+ line.castShadow = true;
|
|
|
+ line.receiveShadow = true;
|
|
|
+ // scene.add( line );
|
|
|
+
|
|
|
+ // Shapes
|
|
|
+
|
|
|
+ var PI2 = Math.PI * 2;
|
|
|
+
|
|
|
+ var sides = 10;
|
|
|
+ var array = [];
|
|
|
+
|
|
|
+ for ( var i = 0; i < sides; i ++ ) {
|
|
|
+
|
|
|
+ var angle = ( i / sides ) * PI2;
|
|
|
+ array.push( new THREE.Vector3( Math.sin( angle ) * 0.01, Math.cos( angle ) * 0.01, 0 ) );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ shapes[ 'tube' ] = array;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function stroke( point1, point2, matrix1, matrix2 ) {
|
|
|
+
|
|
|
+ var shape = shapes[ 'tube' ];
|
|
|
+
|
|
|
+ var geometry = line.geometry;
|
|
|
+ var attributes = geometry.attributes;
|
|
|
+ var count = geometry.drawRange.count;
|
|
|
+
|
|
|
+ var positions = attributes.position.array;
|
|
|
+ var normals = attributes.normal.array;
|
|
|
+ var colors = attributes.color.array;
|
|
|
+
|
|
|
+ for ( var j = 0, jl = shape.length; j < jl; j ++ ) {
|
|
|
+
|
|
|
+ var vertex1 = shape[ j ];
|
|
|
+ var vertex2 = shape[ ( j + 1 ) % jl ];
|
|
|
+
|
|
|
+ // positions
|
|
|
+
|
|
|
+ vector1.copy( vertex1 );
|
|
|
+ vector1.applyMatrix4( matrix2 );
|
|
|
+ vector1.add( point2 );
|
|
|
+
|
|
|
+ vector2.copy( vertex2 );
|
|
|
+ vector2.applyMatrix4( matrix2 );
|
|
|
+ vector2.add( point2 );
|
|
|
+
|
|
|
+ vector3.copy( vertex2 );
|
|
|
+ vector3.applyMatrix4( matrix1 );
|
|
|
+ vector3.add( point1 );
|
|
|
+
|
|
|
+ vector4.copy( vertex1 );
|
|
|
+ vector4.applyMatrix4( matrix1 );
|
|
|
+ vector4.add( point1 );
|
|
|
+
|
|
|
+ vector1.toArray( positions, ( count + 0 ) * 3 );
|
|
|
+ vector2.toArray( positions, ( count + 1 ) * 3 );
|
|
|
+ vector4.toArray( positions, ( count + 2 ) * 3 );
|
|
|
+
|
|
|
+ vector2.toArray( positions, ( count + 3 ) * 3 );
|
|
|
+ vector3.toArray( positions, ( count + 4 ) * 3 );
|
|
|
+ vector4.toArray( positions, ( count + 5 ) * 3 );
|
|
|
+
|
|
|
+ // normals
|
|
|
+
|
|
|
+ vector1.copy( vertex1 );
|
|
|
+ vector1.applyMatrix4( matrix2 );
|
|
|
+ vector1.normalize();
|
|
|
+
|
|
|
+ vector2.copy( vertex2 );
|
|
|
+ vector2.applyMatrix4( matrix2 );
|
|
|
+ vector2.normalize();
|
|
|
+
|
|
|
+ vector3.copy( vertex2 );
|
|
|
+ vector3.applyMatrix4( matrix1 );
|
|
|
+ vector3.normalize();
|
|
|
+
|
|
|
+ vector4.copy( vertex1 );
|
|
|
+ vector4.applyMatrix4( matrix1 );
|
|
|
+ vector4.normalize();
|
|
|
+
|
|
|
+ vector1.toArray( normals, ( count + 0 ) * 3 );
|
|
|
+ vector2.toArray( normals, ( count + 1 ) * 3 );
|
|
|
+ vector4.toArray( normals, ( count + 2 ) * 3 );
|
|
|
+
|
|
|
+ vector2.toArray( normals, ( count + 3 ) * 3 );
|
|
|
+ vector3.toArray( normals, ( count + 4 ) * 3 );
|
|
|
+ vector4.toArray( normals, ( count + 5 ) * 3 );
|
|
|
+
|
|
|
+ // colors
|
|
|
+
|
|
|
+ color.toArray( colors, ( count + 0 ) * 3 );
|
|
|
+ color.toArray( colors, ( count + 1 ) * 3 );
|
|
|
+ color.toArray( colors, ( count + 2 ) * 3 );
|
|
|
+
|
|
|
+ color.toArray( colors, ( count + 3 ) * 3 );
|
|
|
+ color.toArray( colors, ( count + 4 ) * 3 );
|
|
|
+ color.toArray( colors, ( count + 5 ) * 3 );
|
|
|
+
|
|
|
+ count += 6;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ geometry.drawRange.count = count;
|
|
|
+ attributes.position.needsUpdate = true;
|
|
|
+ attributes.normal.needsUpdate = true;
|
|
|
+ attributes.color.needsUpdate = true;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function onWindowResize() {
|
|
|
+
|
|
|
+ camera.aspect = window.innerWidth / window.innerHeight;
|
|
|
+ camera.updateProjectionMatrix();
|
|
|
+
|
|
|
+ effect.setSize( window.innerWidth, window.innerHeight );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ function animate() {
|
|
|
+
|
|
|
+ effect.requestAnimationFrame( animate );
|
|
|
+ render();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function handleController( controller, id ) {
|
|
|
+
|
|
|
+ var gamepad = controller.getGamepad();
|
|
|
+
|
|
|
+ if ( gamepad ) {
|
|
|
+
|
|
|
+ var matrix = controller.getObjectByName( 'pivot' ).matrixWorld;
|
|
|
+
|
|
|
+ var point1 = controller.userData.points[ 0 ];
|
|
|
+ var point2 = controller.userData.points[ 1 ];
|
|
|
+
|
|
|
+ var matrix1 = controller.userData.matrices[ 0 ];
|
|
|
+ var matrix2 = controller.userData.matrices[ 1 ];
|
|
|
+
|
|
|
+ point1.setFromMatrixPosition( matrix );
|
|
|
+ matrix1.lookAt( point2, point1, up );
|
|
|
+
|
|
|
+ if ( gamepad.buttons[ 0 ].pressed ) {
|
|
|
+
|
|
|
+ color.setHex( Math.random() * 0xffffff );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if ( gamepad.buttons[ 1 ].pressed ) {
|
|
|
+
|
|
|
+ if ( line.parent === null ) scene.add( line );
|
|
|
+
|
|
|
+ stroke( point1, point2, matrix1, matrix2 );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ point2.copy( point1 );
|
|
|
+ matrix2.copy( matrix1 );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function render() {
|
|
|
+
|
|
|
+ controls.update();
|
|
|
+
|
|
|
+ handleController( controller1, 0 );
|
|
|
+ handleController( controller2, 1 );
|
|
|
+
|
|
|
+ effect.render( scene, camera );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ </script>
|
|
|
+ </body>
|
|
|
+</html>
|