美文网首页
three.js 笔记四 shape

three.js 笔记四 shape

作者: 合肥黑 | 来源:发表于2023-01-03 09:13 被阅读0次
一、基本流程

参考
ThreeJs 图形绘制基础
https://github.com/mrdoob/three.js/blob/master/examples/webgl_geometry_shapes.html

在 ThreeJs 中,提供了一套 Shape 和 Curve 相关的 API 来帮助我们在 3D 场景中绘制出我们想要的图形。图形绘制一般流程为:构造 Shape、构造 BufferGeometry 、构造 Mesh 并添加到场景中。

1.构造shape
var circleRadius = 40;
var circleShape = new THREE.Shape();
circleShape.moveTo( 0, circleRadius );

circleShape.quadraticCurveTo( circleRadius, circleRadius, circleRadius, 0 );
circleShape.quadraticCurveTo( circleRadius, - circleRadius, 0, - circleRadius );
circleShape.quadraticCurveTo( - circleRadius, - circleRadius, - circleRadius, 0 );
circleShape.quadraticCurveTo( - circleRadius, circleRadius, 0, circleRadius );
2.构造 BufferGeometry
var points = shape.getPoints();
var geometryPoints = new THREE.BufferGeometry().setFromPoints( subPoints );
3.构造 Mesh 并添加到场景中
function addLineShape( shape, color, x, y, z, rx, ry, rz, s ) {

    // lines

    shape.autoClose = true;

    var points = shape.getPoints();

    console.log( "addLineShape ", points );
    let length = points.length;

    let val = 0;

    function drawLine(  ) {

        if(val == length) return;

        let subPoints1 = points[val];
        let subPoints2 = points[(val + 1) % length];

        let subPoints = [];
        subPoints.push(subPoints1);
        subPoints.push(subPoints2);

        var geometryPoints = new THREE.BufferGeometry().setFromPoints( subPoints );

        // solid line

        var line = new THREE.Line( geometryPoints, new THREE.LineBasicMaterial( { color: color } ) );
        line.position.set( x, y, z );
        line.rotation.set( rx, ry, rz );
        line.scale.set( s, s, s );
        scene.add( line );

        val++;

        setTimeout(drawLine,16);
    }

    drawLine();
}
二、测试用例
1.自动闭合
shapeRef.moveTo(500,500);
shapeRef.lineTo(1000,500);
shapeRef.lineTo(1000,1000);

shapeRef.moveTo(1500,500);
shapeRef.lineTo(2000,500);
shapeRef.lineTo(2000,1000);
shapeRef.lineTo(1500,1500);
image.png

根据图形推断,moveTo到达一个点P后,最后画的那条线会自动向点P补一条线进行闭合。

2.THREE.Shape 类的一些绘图函数

参考
three.js 几何体(二)

函数名 作用
moveTo(x, y) 将绘图点移动到指定的 x、y 坐标处
lineTo(x, y) 从当前位置(例如 moveTo 设定的位置)画一条线到指定的 x、y 坐标处
quadricCurveTo(cpx, cpy, x, y)(二次曲线) 此函数为二次曲线。你可以使用两种方法来画曲线:quadricCurveTo 或者 bezierCurveTo。这两种曲线的不同之处在于指定曲线曲率的方法不一样,如下图所示:对于二次曲线,除了指定结束点(x, y)外,还需要额外指定一个点(cpx, cpy)来控制曲线的曲率(不用指定起始点,因为路径的当前位置就是起始点);对于三次曲线,除了指定结束点(x, y)外,还需要额外指定两个点(cpx1, cpy1, cpx2, cpy2)来控制曲线的曲率。
bezierCurveTo(cpx1, cpy1, cpx2, cpy2, x, y)(贝塞尔曲线) 此函数为三次曲线。相关说明参考上一行的二次曲线。
splineThru(vector2Array) 此函数沿着参数指定的坐标集合绘制一条光滑的样条曲线。其参数为一THREE.Vector2 对象数组。
arc(x, y, radius, startAngle, endAngle, clockwise) 次函数绘制一个圆或者一段弧。x, y 用来指定圆心与当前位置的偏移量。radius 设定圆的大小。而 startAngle 及 endAngle 则用来定义圆弧要画多长。布尔属性 clockwise 决定这段弧是顺时针画还是逆时针画。
absArc(x, y, radius, startAngle, endAngle, clockwise) 参考 arc 函数。只不过其指定的圆心位置是绝对位置,而不是相对当前位置的偏移量。
ellipse(x, y, radiusX, radiusY, startAngle, endAngle, clockwise) 参考 arc 函数。只是椭圆可以分别指定 x 轴半径及 y 轴半径。
absEllipse(x, y, radiusX, radiusY, startAngle, endAngle, clockwise) 参考 ellipse 函数。只不过其指定的圆心位置是绝对位置,而不是相对当前位置的偏移量。

相关文章

  • three.js 笔记四 shape

    一、基本流程 参考ThreeJs 图形绘制基础[https://www.jianshu.com/p/a8ec586...

  • 3D世界

    前言最近在学习three.js,以下是我的笔记。 相关概念了解 1.WebGL与Three.js 1.1什么是We...

  • Three.js笔记(四)Webpack

    简述 前面章节加载Three.js的方式很简单,但不幸的是它有很多的限制。由于文件会变得很大,那种方式里面没有包含...

  • THREE.js_的使用(例子)

    THREE.js 下载 THREE.js 官方文档 THREE.js 中文基础教程 THREE.MeshLine ...

  • Drawable资源之shape与Java代码

    XML中shape资源 shape有6个属性: corners:设置圆角,即四个角的弧度 gradient:颜色渐...

  • Three.js

    Three.js 1. 概述 1.1 什么是Three.js Three.js是一个3D javascript库。...

  • three.js笔记

    three.js 专业名词 场景(Scene)、相机(camera)和渲染器(WebGLRenderer)rend...

  • Three.js 之初级入门

    #. 前言 Three.js[https://github.com/mrdoob/three.js] 是基于原生 ...

  • egret_碰撞检测

    var shape = new egret.Shape(); shape.graphics.beginFill(0...

  • Android中shape属性详解

    在开发中经常会用到shape标签来定义控件的背景,shape标签总共有四个图形选项,分别是rectangle(矩形...

网友评论

      本文标题:three.js 笔记四 shape

      本文链接:https://www.haomeiwen.com/subject/phxwjrtx.html