118 篇文章 235 订阅

# 案例实现

var geometry = new THREE.CylinderGeometry(
5,                       // 顶部圆柱体的半径
5,                       // 底部圆柱体的半径
sizing.height,           // 圆柱体的高度
8,                       // 圆柱周围的分段面数
sizing.segmentCount * 3, // 沿圆柱体高度的面的行数
true                     // 圆柱体的末端是打开
);

//遍历几何体所有的顶点
for ( var i = 0; i < geometry.vertices.length; i ++ ) {

//根据顶点的位置计算出骨骼影响下标和权重

var vertex = geometry.vertices[ i ];
var y = ( vertex.y + sizing.halfHeight );

var skinIndex = Math.floor( y / sizing.segmentHeight );
var skinWeight = ( y % sizing.segmentHeight ) / sizing.segmentHeight;

geometry.skinIndices.push( new THREE.Vector4( skinIndex, skinIndex + 1, 0, 0 ) );
geometry.skinWeights.push( new THREE.Vector4( 1 - skinWeight, skinWeight, 0, 0 ) );

}

return geometry;

var prevBone = new THREE.Bone();
bones.push( prevBone );
prevBone.position.y = - sizing.halfHeight;

for ( var i = 0; i < sizing.segmentCount; i ++ ) {

var bone = new THREE.Bone();
bone.position.y = sizing.segmentHeight;
bones.push( bone ); //添加到骨骼数组
prevBone = bone;

}

mesh = new THREE.SkinnedMesh( geometry, [material, lineMaterial] );
var skeleton = new THREE.Skeleton( bones ); //创建骨架

mesh.add( bones[ 0 ] ); //将骨骼添加到模型里面

mesh.bind( skeleton ); //模型绑定骨架

//添加骨骼辅助标记
skeletonHelper = new THREE.SkeletonHelper( mesh );
skeletonHelper.material.linewidth = 2;
scene.add( skeletonHelper );

# 案例代码

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
html, body {
margin: 0;
height: 100%;
}

canvas {
display: block;
}
</style>

</body>
<script src="https://cdn.bootcss.com/three.js/91/three.min.js"></script>
<script src="/lib/js/controls/OrbitControls.js"></script>
<script src="https://cdn.bootcss.com/stats.js/r17/Stats.min.js"></script>
<script src="https://cdn.bootcss.com/dat-gui/0.7.1/dat.gui.min.js"></script>
<script src="/lib/js/Detector.js"></script>

<script>
var renderer, camera, scene, gui, light, stats, controls, mesh, skeletonHelper, bones;
var state = {

animateBones : false

};

function initRender() {
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0xeeeeee);
//告诉渲染器需要阴影效果
document.body.appendChild(renderer.domElement);
}

function initCamera() {
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 40, 50);
}

function initScene() {
scene = new THREE.Scene();
}

//初始化dat.GUI简化试验流程
function initGui() {
gui = new dat.GUI();
var folder = gui.addFolder( "General Options" );

folder.__controllers[ 0 ].name( "Animate Bones" );

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 initLight() {

light = new THREE.PointLight(0xffffff);
light.position.set(0, 50, 0);

//告诉平行光需要开启阴影投射

}

function initModel() {

//辅助工具
var helper = new THREE.AxesHelper(50);

//制作模型
var segmentHeight = 6; //每一节骨骼的的高度
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 );

}

//创建集合体
function createGeometry ( sizing ) {

var geometry = new THREE.CylinderGeometry(
5,                       // 顶部圆柱体的半径
5,                       // 底部圆柱体的半径
sizing.height,           // 圆柱体的高度
8,                       // 圆柱周围的分段面数
sizing.segmentCount * 3, // 沿圆柱体高度的面的行数
true                     // 圆柱体的末端是打开
);

//添加绘制第二个纹理的面
var len = geometry.faces.length;

for(var i=0; i < len; i++){
var face = geometry.faces[i].clone();
face.materialIndex = 1;
geometry.faces.push(face);
}

//将vertexUv报错解决掉
var len = geometry.faceVertexUvs[0].length;

for(var i=0; i < len; i++){
geometry.faceVertexUvs[0].push(geometry.faceVertexUvs[0][i]);
}

//遍历几何体所有的顶点
for ( var i = 0; i < geometry.vertices.length; i ++ ) {

//根据顶点的位置计算出骨骼影响下标和权重

var vertex = geometry.vertices[ i ];
var y = ( vertex.y + sizing.halfHeight );

var skinIndex = Math.floor( y / sizing.segmentHeight );
var skinWeight = ( y % sizing.segmentHeight ) / sizing.segmentHeight;

geometry.skinIndices.push( new THREE.Vector4( skinIndex, skinIndex + 1, 0, 0 ) );
geometry.skinWeights.push( new THREE.Vector4( 1 - skinWeight, skinWeight, 0, 0 ) );

}

return geometry;

}

//创建骨骼
function createBones ( sizing ) {

bones = [];

var prevBone = new THREE.Bone();
bones.push( prevBone );
prevBone.position.y = - sizing.halfHeight;

for ( var i = 0; i < sizing.segmentCount; i ++ ) {

var bone = new THREE.Bone();
bone.position.y = sizing.segmentHeight;
bones.push( bone ); //添加到骨骼数组
prevBone = bone;

}

return bones;

}

function createMesh ( geometry, bones ) {

var material = new THREE.MeshPhongMaterial( {
skinning : true,
color: 0x156289,
emissive: 0x072534,
side: THREE.DoubleSide,
} );

var lineMaterial = new THREE.MeshBasicMaterial({
skinning : true,
wireframe: true
});

mesh = new THREE.SkinnedMesh( geometry, [material, lineMaterial] );
var skeleton = new THREE.Skeleton( bones ); //创建骨架

mesh.add( bones[ 0 ] ); //将骨骼添加到模型里面

mesh.bind( skeleton ); //模型绑定骨架

//添加骨骼辅助标记
skeletonHelper = new THREE.SkeletonHelper( mesh );
skeletonHelper.material.linewidth = 2;

return mesh;

}

//初始化性能插件
function initStats() {
stats = new Stats();
document.body.appendChild(stats.dom);
}

function initControls() {

controls = new THREE.OrbitControls(camera, renderer.domElement);

// 如果使用animate方法时，将此函数删除
// 使动画循环使用时阻尼或自转 意思是否有惯性
controls.enableDamping = true;
//动态阻尼系数 就是鼠标拖拽旋转灵敏度
//controls.dampingFactor = 0.25;
//是否可以缩放
controls.enableZoom = true;
//是否自动旋转
controls.autoRotate = false;
controls.autoRotateSpeed = 0.5;
//设置相机距离原点的最远距离
controls.minDistance = 1;
//设置相机距离原点的最远距离
controls.maxDistance = 2000;
//是否开启右键拖拽
controls.enablePan = true;
}

function 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;

}

}

controls.update();
}

//窗口变动触发的函数
function onWindowResize() {

camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);

}

function animate() {
//更新控制器
render();

//更新性能插件
stats.update();

renderer.render(scene, camera);

requestAnimationFrame(animate);
}

function draw() {
//兼容性判断

initStats();
initRender();
initScene();
initCamera();
initLight();
initModel();
initControls();
initGui();

animate();
window.onresize = onWindowResize;
}

</script>
</html>
• 3
点赞
• 5
收藏
• 打赏
• 7
评论
04-10 396
10-21
01-03 38
01-13 1035
11-30 729
12-02 2072
04-24 5273
03-24 1477
11-08 626
12-03 8111
04-02 2592
04-24 2966
07-14

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

©️2022 CSDN 皮肤主题：Age of Ai 设计师：meimeiellie

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。