凸面体

作者: Codifier | 来源:发表于2019-11-12 16:37 被阅读0次
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Convex Geometry</title>
    <script src="../../three-part/threejs/three.js"></script>
    <script src="../../three-part/threejs/ConvexGeometry.js"></script>
    <script src="../../three-part/threejs/ConvexHull.js"></script>
    <script src="../../three-part/utils/stats.min.js"></script>
    <script src="../../three-part/utils/dat.gui.min.js"></script>
    <script src="../controls/TrackballControls.js"></script>
    <script src="../util/util.js"></script>
    <style>
        body {
            margin: 0;
            overflow: hidden;
        }
    </style>
</head>
<body>
<div id="container"></div>
<script type="text/javascript">
    init();
    function init() {
        // show FPS
        let stats = initStats();
        // resize
        window.addEventListener('resize', onResize, false);

        let scene = new THREE.Scene();
        let camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.x = -30;
        camera.position.y = 40;
        camera.position.z = 30;
        camera.lookAt(scene.position);

        let renderer = new THREE.WebGLRenderer();
        renderer.setClearColor(new THREE.Color(0x000000));
        renderer.setSize(window.innerWidth, window.innerHeight);
        renderer.shadowMap.enabled = true;
        document.getElementById("container").appendChild(renderer.domElement);

        // init trackball control
        let trackballControls = initTrackballControls(camera, renderer);
        let clock = new THREE.Clock();

        // add ambient light
        let ambientLight = new THREE.AmbientLight("#606008", 1);
        scene.add(ambientLight);

        let directionalLight = new THREE.DirectionalLight("#ffffff");
        directionalLight.position.set(-10, 50, -10);
        directionalLight.intensity = 2;
        directionalLight.castShadow = true;
        scene.add(directionalLight);

        // add a plane
        let planeGeometry = new THREE.PlaneGeometry(100, 100, 1, 1);
        let planeMaterial = new THREE.MeshLambertMaterial({
            color: 0xffffff
        });
        let plane = new THREE.Mesh(planeGeometry, planeMaterial);
        plane.receiveShadow = true;
        plane.rotation.x = -0.5 * Math.PI;
        plane.position.set(0, -20, 0);
        scene.add(plane);

        let group = null;
        generateConvex(group);

        // attributes which can be modified in GUI
        const controls = {

        };
        // init GUI
        initGUI();

        renderScene();

        function initGUI(){
            let gui = new dat.GUI();
        }

        function generateConvex(group) {
            if (group) scene.remove(group);
            // random point
            let points = [];
            for (let i = 0; i < 20; i++) {
                let randomX = -15 + Math.round(Math.random() * 30);
                let randomY = -15 + Math.round(Math.random() * 30);
                let randomZ = -15 + Math.round(Math.random() * 30);
                points.push(new THREE.Vector3(randomX, randomY, randomZ));
            }
            group = new THREE.Object3D();
            let material = new THREE.MeshBasicMaterial({
                color: 0xff0000,
                transparent: false
            });
            points.forEach(v => {
                let spGeom = new THREE.SphereGeometry(0.2);
                let spMesh = new THREE.Mesh(spGeom, material);
                spMesh.position.copy(v);
                group.add(spMesh);
            });
            scene.add(group);

            /**
             * ConvexGeometry( points : Array )
             * points — Array of Vector3s that the resulting convex hull will contain.
             * @type {THREE.ConvexGeometry}
             */
            let convexGeometry = new THREE.ConvexGeometry(points);
            convexGeometry.computeVertexNormals();
            convexGeometry.computeFaceNormals();
            convexGeometry.normalsNeedUpdate = true;
            let convexMaterial = new THREE.MeshLambertMaterial({
                color: 0x0000ff
            });
            let convexMesh = new THREE.Mesh(convexGeometry, convexMaterial);
            convexMesh.castShadow = true;
            scene.add(convexMesh);
        }

        function onResize() {
            camera.aspect = window.innerWidth / window.innerHeight;
            camera.updateProjectionMatrix();
            renderer.setSize(window.innerWidth, window.innerHeight);
        }

        function renderScene(){
            trackballControls.update(clock.getDelta());
            stats.update();
            requestAnimationFrame(renderScene);
            renderer.render(scene, camera);
        }
    }
</script>
</body>
</html>

运行结果:



总结:
创建ConvexGeometry之前需要引入ConvexGeometry.js和ConvexHull.js。

相关文章

  • 凸面体

    运行结果: 总结:创建ConvexGeometry之前需要引入ConvexGeometry.js和ConvexHu...

  • 13three.js凸面体

    等待网络

  • 怎么通过一些小症状分辨白内障?

    晶状体混浊称为白内障 (cataract) 。晶状体为双凸面、有弹性、无血管的透明组织,具有复杂的代谢过程,营养主...

  • 老花镜当配即配不要拖延

    我们的眼睛看东西,像照相机一样,必须不断地改变焦距,远远近近的景物才会清晰地尽收眼底。晶状体是一个透明的双凸面体...

  • 凹凸面镜

    品相不平弯面镜 真情实景弄翻颠 丧心博得愚人笑 莫怪玻璃骗地天 (七绝、仄起、平水韵、一先) 2019/03/12...

  • 关于卫生巾底膜印刷确认工作的相关说明

    关于卫生巾底膜印刷确认工作的相关说明: 01、底膜分凸面和平面两个面,印刷面在凸面,还是平面,需要确认。 02、底...

  • 2018-04-12中心医院神经外科利用VR技术完成手术

    脑膜瘤是颅内比较常见的一种肿瘤,多好发于大脑凸面、 矢状窦旁、颅底、大脑镰旁等部位。而位于大脑凸面者多因肿瘤侵蚀,...

  • 随笔

    在回宿舍的路上,看到有一个白衬衣的男生在路口的凸面镜子前整理自己的头发。我经过过那,凸面镜子是给车子用的,...

  • 机器学习第三周学习笔记

    新词 Logistic Regression: 逻辑回归 Convex: 凸面的 Gradient: 倾斜度,坡度...

  • 凸面镜||高尚的工作

    女孩儿叫高尚,生于一九九六年,已经大学毕业三年。齐齐的半长披肩发,瘦瘦的,个子也不矮,白色丝绸衬衫掖在直直的拖地裤...

网友评论

      本文标题:凸面体

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