three.js从入门到精通系列教程024 - three.js通过自定义曲线和自定义形状绘制面具

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>three.js从入门到精通系列教程024 - three.js通过自定义曲线和自定义形状绘制面具</title>
    <script src="ThreeJS/three.js"></script>
    <script src="ThreeJS/jquery.js"></script>
    <script src="ThreeJS/OrbitControls.js"></script>
</head>

<body>
    <center id="myContainer"></center>
    <script>
        //创建渲染器
        var myRenderer = new THREE.WebGLRenderer({ antialias: true });
        myRenderer.setSize(window.innerWidth, window.innerHeight);
        myRenderer.setClearColor('white', 1.0);
        $("#myContainer").append(myRenderer.domElement);
        var myScene = new THREE.Scene();
        var myPointLight = new THREE.PointLight('white');
        myPointLight.position.set(320, 200, 400);
        myScene.add(myPointLight);
        var myCamera = new THREE.PerspectiveCamera(45, 1, 1, 1000);
        myCamera.position.set(200, 200, 200);
        myCamera.up.x = -1;
        myCamera.up.y = 1;
        myCamera.up.z = -1;
        myCamera.lookAt({ x: 0, y: 0, z: 0 });
        //绘制自定义平面图形
        var myShape = new THREE.Shape();
        //设置起点位置
        myShape.moveTo(20, 10);
        //从起点绘制直线到(20, 40)
        myShape.lineTo(20, 40);
        //绘制贝塞尔曲线
        myShape.bezierCurveTo(15, 25, -5, 25, -30, 40);
        //绘制曲线通过指定的三个点
        myShape.splineThru([new THREE.Vector2(-22, 30),
        new THREE.Vector2(-18, 20), new THREE.Vector2(-20, 10),]);
        //绘制二次曲线
        myShape.quadraticCurveTo(0, -15, 20, 10);
        //绘制椭圆(眼睛)
        var myAbsellipse1 = new THREE.Path();
        myAbsellipse1.absellipse(6, 20, 2, 3, 0, Math.PI * 2, true);
        myShape.holes.push(myAbsellipse1);
        //绘制椭圆(眼睛)
        var myAbsellipse2 = new THREE.Path();
        myAbsellipse2.absellipse(-10, 20, 2, 3, 0, Math.PI * 2, true);
        myShape.holes.push(myAbsellipse2);
        //绘制半圆弧(嘴巴)
        var myAbsarc = new THREE.Path();
        myAbsarc.absarc(0, 5, 2, 0, Math.PI, true);
        myShape.holes.push(myAbsarc);
        //设置在y轴方向拉伸图形
        var myCurve = new THREE.CatmullRomCurve3([
            new THREE.Vector3(0, 0, 0), new THREE.Vector3(0, 1, 0)]);
        //创建拉伸之后的平面图形
        var myGeometry = new THREE.ExtrudeGeometry(myShape, { extrudePath: myCurve });
        var myMaterial = new THREE.MeshPhongMaterial({ color: 'cyan' });
        var myMesh = new THREE.Mesh(myGeometry, myMaterial);
        myMesh.translateX(-50);
        myMesh.translateZ(-200);
        myMesh.translateY(-150);
        myMesh.scale.y = 6;
        myMesh.scale.x = 6;
        myMesh.scale.z = 6;
        myScene.add(myMesh);
        //渲染自定义平面图形
        animate();
        function animate() {
            myRenderer.render(myScene, myCamera);
            requestAnimationFrame(animate);
        }
        var myOrbitControls = new THREE.OrbitControls(myCamera);
        myOrbitControls.enableRotate =true; //启用旋转
        myOrbitControls.enablePan = true; //启用平移
        myOrbitControls.enableZoom =true; //启用缩放
    </script>
</body>

</html>

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

smartsmile2012

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值