123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webvr - paint</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
- <link type="text/css" rel="stylesheet" href="main.css">
- <!-- WebXR Device API (For Chrome M76+), expires 09/09/2019 -->
- <meta http-equiv="origin-trial" content="Ai1/G787sugfmWtk1xQExa8N6OqwDsJyNn+OwpA1J4PozR1lixRYIQ4Tmp00vrGWS8FQQ2iDyqjwaewrOfYvPAUAAABTeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJYUkRldmljZU03NiIsImV4cGlyeSI6MTU2ODAyMzQ3OH0=">
- </head>
- <body>
- <div id="info">
- <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webvr - paint
- </div>
- <script src="js/vr/HelioWebXRPolyfill.js"></script>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { WEBVR } from './jsm/vr/WebVR.js';
- var container;
- var camera, scene, renderer;
- var controller1, controller2;
- var line;
- var shapes = {};
- var up = new THREE.Vector3( 0, 1, 0 );
- var vector1 = new THREE.Vector3();
- var vector2 = new THREE.Vector3();
- var vector3 = new THREE.Vector3();
- var vector4 = new THREE.Vector3();
- init();
- initGeometry();
- animate();
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x222222 );
- camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 50 );
- var geometry = new THREE.BoxBufferGeometry( 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;
- scene.add( table );
- var geometry = new THREE.PlaneBufferGeometry( 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;
- scene.add( floor );
- var grid = new THREE.GridHelper( 10, 20, 0x111111, 0x111111 );
- grid.material.depthTest = false; // avoid z-fighting
- scene.add( grid );
- scene.add( new THREE.HemisphereLight( 0x888877, 0x777788 ) );
- var light = new THREE.DirectionalLight( 0xffffff, 0.5 );
- light.position.set( 0, 4, 0 );
- scene.add( light );
- //
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.gammaInput = true;
- renderer.gammaOutput = true;
- renderer.vr.enabled = true;
- container.appendChild( renderer.domElement );
- document.body.appendChild( WEBVR.createButton( renderer ) );
- // controllers
- function onSelectStart() {
- this.userData.isSelecting = true;
- }
- function onSelectEnd() {
- this.userData.isSelecting = false;
- }
- controller1 = renderer.vr.getController( 0 );
- controller1.addEventListener( 'selectstart', onSelectStart );
- controller1.addEventListener( 'selectend', onSelectEnd );
- controller1.userData.points = [ new THREE.Vector3(), new THREE.Vector3() ];
- controller1.userData.matrices = [ new THREE.Matrix4(), new THREE.Matrix4() ];
- scene.add( controller1 );
- controller2 = renderer.vr.getController( 1 );
- controller2.addEventListener( 'selectstart', onSelectStart );
- controller2.addEventListener( 'selectend', onSelectEnd );
- controller2.userData.points = [ new THREE.Vector3(), new THREE.Vector3() ];
- controller2.userData.matrices = [ new THREE.Matrix4(), new THREE.Matrix4() ];
- scene.add( controller2 );
- //
- var geometry = new THREE.CylinderBufferGeometry( 0.01, 0.02, 0.08, 5 );
- geometry.rotateX( - Math.PI / 2 );
- var material = new THREE.MeshStandardMaterial( { flatShading: true } );
- var mesh = new THREE.Mesh( geometry, material );
- var pivot = new THREE.Mesh( new THREE.IcosahedronBufferGeometry( 0.01, 2 ) );
- pivot.name = 'pivot';
- pivot.position.z = - 0.05;
- mesh.add( pivot );
- controller1.add( mesh.clone() );
- controller2.add( mesh.clone() );
- //
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function initGeometry() {
- var geometry = new THREE.BufferGeometry();
- var positions = new THREE.BufferAttribute( new Float32Array( 1000000 * 3 ), 3 );
- positions.dynamic = true;
- geometry.addAttribute( 'position', positions );
- var normals = new THREE.BufferAttribute( new Float32Array( 1000000 * 3 ), 3 );
- normals.dynamic = true;
- geometry.addAttribute( 'normal', normals );
- var colors = new THREE.BufferAttribute( new Float32Array( 1000000 * 3 ), 3 );
- colors.dynamic = true;
- geometry.addAttribute( 'color', colors );
- geometry.drawRange.count = 0;
- var material = new THREE.MeshStandardMaterial( {
- roughness: 0.9,
- metalness: 0.0,
- vertexColors: THREE.VertexColors
- } );
- line = new THREE.Mesh( geometry, material );
- line.frustumCulled = false;
- scene.add( line );
- // Shapes
- shapes[ 'tube' ] = getTubeShapes( 1.0 );
- }
- function getTubeShapes( size ) {
- var PI2 = Math.PI * 2;
- var sides = 10;
- var array = [];
- var radius = 0.01 * size;
- for ( var i = 0; i < sides; i ++ ) {
- var angle = ( i / sides ) * PI2;
- array.push( new THREE.Vector3( Math.sin( angle ) * radius, Math.cos( angle ) * radius, 0 ) );
- }
- return array;
- }
- function stroke( controller, point1, point2, matrix1, matrix2 ) {
- var color = new THREE.Color( 0xffffff );
- var size = 1;
- var shapes = getTubeShapes( size );
- 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 = shapes.length; j < jl; j ++ ) {
- var vertex1 = shapes[ j ];
- var vertex2 = shapes[ ( 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;
- }
- function updateGeometry( start, end ) {
- if ( start === end ) return;
- var offset = start * 3;
- var count = ( end - start ) * 3;
- var geometry = line.geometry;
- var attributes = geometry.attributes;
- attributes.position.updateRange.offset = offset;
- attributes.position.updateRange.count = count;
- attributes.position.needsUpdate = true;
- attributes.normal.updateRange.offset = offset;
- attributes.normal.updateRange.count = count;
- attributes.normal.needsUpdate = true;
- attributes.color.updateRange.offset = offset;
- attributes.color.updateRange.count = count;
- attributes.color.needsUpdate = true;
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- //
- function handleController( controller ) {
- var pivot = controller.getObjectByName( 'pivot' );
- if ( pivot ) {
- var matrix = 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 ( controller.userData.isSelecting === true ) {
- stroke( controller, point1, point2, matrix1, matrix2 );
- }
- point2.copy( point1 );
- matrix2.copy( matrix1 );
- }
- }
- function animate() {
- renderer.setAnimationLoop( render );
- }
- function render() {
- var count = line.geometry.drawRange.count;
- handleController( controller1 );
- handleController( controller2 );
- updateGeometry( count, line.geometry.drawRange.count );
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|