You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
4.8 KiB

2 years ago
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - collada - kinematics</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
</head>
<body>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> collada loader - kinematics<br/>
robot from <a href="https://github.com/rdiankov/collada_robots" target="_blank" rel="noopener">collada robots</a>
</div>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import Stats from 'three/addons/libs/stats.module.js';
import { TWEEN } from 'three/addons/libs/tween.module.min.js';
import { ColladaLoader } from 'three/addons/loaders/ColladaLoader.js';
let container, stats;
let camera, scene, renderer;
let particleLight;
let dae;
let kinematics;
let kinematicsTween;
const tweenParameters = {};
const loader = new ColladaLoader();
loader.load( './models/collada/abb_irb52_7_120.dae', function ( collada ) {
dae = collada.scene;
dae.traverse( function ( child ) {
if ( child.isMesh ) {
// model does not have normals
child.material.flatShading = true;
}
} );
dae.scale.x = dae.scale.y = dae.scale.z = 10.0;
dae.updateMatrix();
kinematics = collada.kinematics;
init();
animate();
} );
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
camera.position.set( 2, 2, 3 );
scene = new THREE.Scene();
// Grid
const grid = new THREE.GridHelper( 20, 20, 0x888888, 0x444444 );
scene.add( grid );
// Add the COLLADA
scene.add( dae );
particleLight = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
scene.add( particleLight );
// Lights
const light = new THREE.HemisphereLight( 0xffeeee, 0x111122 );
scene.add( light );
const pointLight = new THREE.PointLight( 0xffffff, 0.3 );
particleLight.add( pointLight );
renderer = new THREE.WebGLRenderer();
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
stats = new Stats();
container.appendChild( stats.dom );
setupTween();
//
window.addEventListener( 'resize', onWindowResize );
}
function setupTween() {
const duration = THREE.MathUtils.randInt( 1000, 5000 );
const target = {};
for ( const prop in kinematics.joints ) {
if ( kinematics.joints.hasOwnProperty( prop ) ) {
if ( ! kinematics.joints[ prop ].static ) {
const joint = kinematics.joints[ prop ];
const old = tweenParameters[ prop ];
const position = old ? old : joint.zeroPosition;
tweenParameters[ prop ] = position;
target[ prop ] = THREE.MathUtils.randInt( joint.limits.min, joint.limits.max );
}
}
}
kinematicsTween = new TWEEN.Tween( tweenParameters ).to( target, duration ).easing( TWEEN.Easing.Quadratic.Out );
kinematicsTween.onUpdate( function ( object ) {
for ( const prop in kinematics.joints ) {
if ( kinematics.joints.hasOwnProperty( prop ) ) {
if ( ! kinematics.joints[ prop ].static ) {
kinematics.setJointValue( prop, object[ prop ] );
}
}
}
} );
kinematicsTween.start();
setTimeout( setupTween, duration );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
//
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
TWEEN.update();
}
function render() {
const timer = Date.now() * 0.0001;
camera.position.x = Math.cos( timer ) * 20;
camera.position.y = 10;
camera.position.z = Math.sin( timer ) * 20;
camera.lookAt( 0, 5, 0 );
particleLight.position.x = Math.sin( timer * 4 ) * 3009;
particleLight.position.y = Math.cos( timer * 5 ) * 4000;
particleLight.position.z = Math.cos( timer * 4 ) * 3009;
renderer.render( scene, camera );
}
</script>
</body>
</html>