123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>Three.js Bones Browser</title>
- <link rel="shortcut icon" href="../../files/favicon.ico" />
- <link rel="stylesheet" type="text/css" href="../../files/main.css">
- <style>
- canvas {
- display: block;
- width: 100%;
- height: 100%;
- }
- #newWindow {
- display: block;
- position: absolute;
- bottom: 0.3em;
- left: 0.5em;
- color: #fff;
- }
- </style>
- </head>
- <body>
- <a id='newWindow' href='./bones-browser.html' target='_blank'>Open in New Window</a>
- <script type="module">
- import {
- Bone,
- Color,
- CylinderBufferGeometry,
- DoubleSide,
- Float32BufferAttribute,
- MeshPhongMaterial,
- PerspectiveCamera,
- PointLight,
- Scene,
- SkinnedMesh,
- Skeleton,
- SkeletonHelper,
- Vector3,
- Uint16BufferAttribute,
- WebGLRenderer
- } from "../../build/three.module.js";
- import { GUI } from '../../examples/jsm/libs/dat.gui.module.js';
- import { OrbitControls } from '../../examples/jsm/controls/OrbitControls.js';
- var gui, scene, camera, renderer, orbit, lights, mesh, bones, skeletonHelper;
- var state = {
- animateBones: false
- };
- function initScene() {
- gui = new GUI();
- scene = new Scene();
- scene.background = new Color( 0x444444 );
- camera = new PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 200 );
- camera.position.z = 30;
- camera.position.y = 30;
- renderer = new WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- document.body.appendChild( renderer.domElement );
- orbit = new OrbitControls( camera, renderer.domElement );
- orbit.enableZoom = false;
- lights = [];
- lights[ 0 ] = new PointLight( 0xffffff, 1, 0 );
- lights[ 1 ] = new PointLight( 0xffffff, 1, 0 );
- lights[ 2 ] = new PointLight( 0xffffff, 1, 0 );
- lights[ 0 ].position.set( 0, 200, 0 );
- lights[ 1 ].position.set( 100, 200, 100 );
- lights[ 2 ].position.set( - 100, - 200, - 100 );
- scene.add( lights[ 0 ] );
- scene.add( lights[ 1 ] );
- scene.add( lights[ 2 ] );
- window.addEventListener( 'resize', function () {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }, false );
- initBones();
- setupDatGui();
- }
- function createGeometry( sizing ) {
- var geometry = new CylinderBufferGeometry(
- 5, // radiusTop
- 5, // radiusBottom
- sizing.height, // height
- 8, // radiusSegments
- sizing.segmentCount * 3, // heightSegments
- true // openEnded
- );
- var position = geometry.attributes.position;
- var vertex = new Vector3();
- var skinIndices = [];
- var skinWeights = [];
- for ( var i = 0; i < position.count; i ++ ) {
- vertex.fromBufferAttribute( position, i );
- var y = ( vertex.y + sizing.halfHeight );
- var skinIndex = Math.floor( y / sizing.segmentHeight );
- var skinWeight = ( y % sizing.segmentHeight ) / sizing.segmentHeight;
- skinIndices.push( skinIndex, skinIndex + 1, 0, 0 );
- skinWeights.push( 1 - skinWeight, skinWeight, 0, 0 );
- }
- geometry.setAttribute( 'skinIndex', new Uint16BufferAttribute( skinIndices, 4 ) );
- geometry.setAttribute( 'skinWeight', new Float32BufferAttribute( skinWeights, 4 ) );
- return geometry;
- }
- function createBones( sizing ) {
- bones = [];
- var prevBone = new Bone();
- bones.push( prevBone );
- prevBone.position.y = - sizing.halfHeight;
- for ( var i = 0; i < sizing.segmentCount; i ++ ) {
- var bone = new Bone();
- bone.position.y = sizing.segmentHeight;
- bones.push( bone );
- prevBone.add( bone );
- prevBone = bone;
- }
- return bones;
- }
- function createMesh( geometry, bones ) {
- var material = new MeshPhongMaterial( {
- skinning: true,
- color: 0x156289,
- emissive: 0x072534,
- side: DoubleSide,
- flatShading: true
- } );
- var mesh = new SkinnedMesh( geometry, material );
- var skeleton = new Skeleton( bones );
- mesh.add( bones[ 0 ] );
- mesh.bind( skeleton );
- skeletonHelper = new SkeletonHelper( mesh );
- skeletonHelper.material.linewidth = 2;
- scene.add( skeletonHelper );
- return mesh;
- }
- function setupDatGui() {
- var folder = gui.addFolder( "General Options" );
- folder.add( state, "animateBones" );
- folder.__controllers[ 0 ].name( "Animate Bones" );
- folder.add( mesh, "pose" );
- folder.__controllers[ 1 ].name( ".pose()" );
- var bones = mesh.skeleton.bones;
- for ( var i = 0; i < bones.length; i ++ ) {
- var bone = bones[ i ];
- folder = gui.addFolder( "Bone " + i );
- folder.add( bone.position, 'x', - 10 + bone.position.x, 10 + bone.position.x );
- folder.add( bone.position, 'y', - 10 + bone.position.y, 10 + bone.position.y );
- folder.add( bone.position, 'z', - 10 + bone.position.z, 10 + bone.position.z );
- folder.add( bone.rotation, 'x', - Math.PI * 0.5, Math.PI * 0.5 );
- folder.add( bone.rotation, 'y', - Math.PI * 0.5, Math.PI * 0.5 );
- folder.add( bone.rotation, 'z', - Math.PI * 0.5, Math.PI * 0.5 );
- folder.add( bone.scale, 'x', 0, 2 );
- folder.add( bone.scale, 'y', 0, 2 );
- folder.add( bone.scale, 'z', 0, 2 );
- folder.__controllers[ 0 ].name( "position.x" );
- folder.__controllers[ 1 ].name( "position.y" );
- folder.__controllers[ 2 ].name( "position.z" );
- folder.__controllers[ 3 ].name( "rotation.x" );
- folder.__controllers[ 4 ].name( "rotation.y" );
- folder.__controllers[ 5 ].name( "rotation.z" );
- folder.__controllers[ 6 ].name( "scale.x" );
- folder.__controllers[ 7 ].name( "scale.y" );
- folder.__controllers[ 8 ].name( "scale.z" );
- }
- }
- function initBones() {
- var segmentHeight = 8;
- var segmentCount = 4;
- var height = segmentHeight * segmentCount;
- var halfHeight = height * 0.5;
- var sizing = {
- segmentHeight: segmentHeight,
- segmentCount: segmentCount,
- height: height,
- halfHeight: halfHeight
- };
- var geometry = createGeometry( sizing );
- var bones = createBones( sizing );
- mesh = createMesh( geometry, bones );
- mesh.scale.multiplyScalar( 1 );
- scene.add( mesh );
- }
- function render() {
- requestAnimationFrame( render );
- var time = Date.now() * 0.001;
- //Wiggle the bones
- if ( state.animateBones ) {
- for ( var i = 0; i < mesh.skeleton.bones.length; i ++ ) {
- mesh.skeleton.bones[ i ].rotation.z = Math.sin( time ) * 2 / mesh.skeleton.bones.length;
- }
- }
- renderer.render( scene, camera );
- }
- initScene();
- render();
- </script>
- </body>
- </html>
|