美文网首页
修改网格的属性

修改网格的属性

作者: Codifier | 来源:发表于2019-11-08 15:20 被阅读0次
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Mesh Properties</title>
    <script src="../../three-part/threejs/three.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();

        // create the ground plane
        let planeGeometry = new THREE.PlaneGeometry(60, 40, 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;
        scene.add(plane);

        // add ambient lighting
        let ambientLight = new THREE.AmbientLight(0x3c3c3c);
        scene.add(ambientLight);

        // add spotlight
        let spotLight = new THREE.SpotLight(0xffffff, 1, 180, Math.PI/4);
        spotLight.shadow.mapSize.height = 2048;
        spotLight.shadow.mapSize.width = 2048;
        spotLight.position.set(-40, 30, 30);
        spotLight.castShadow = true;
        scene.add(spotLight);

        var material = new THREE.MeshLambertMaterial({color: 0x44ff44});
        var geometry = new THREE.BoxGeometry(5, 8, 3);
        var cube = new THREE.Mesh(geometry, material);
        cube.position.y = 4;
        cube.castShadow = true;
        scene.add(cube);

        // attributes which can be modified in GUI
        const controls = {
            "scaleX" : 1,
            "scaleY" : 1,
            "scaleZ" : 1,
            "positionX" : 0,
            "positionY" : 4,
            "positionZ" : 0,
            "rotationX" : 0,
            "rotationY" : 0,
            "rotationZ" : 0,
            "scale" : 1,
            "translateX" : 0,
            "translateY" : 0,
            "translateZ" : 0,
            "visible" : true,
            "translate" : function(){
                cube.translateX(this.translateX);
                cube.translateY(this.translateY);
                cube.translateZ(this.translateZ);
                this.positionX = cube.position.x;
                this.positionY = cube.position.y;
                this.positionZ = cube.position.z;
            },
        };
        // init GUI
        initGUI();

        renderScene();

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

            let guiScale = gui.addFolder('scale');
            guiScale.add(controls, 'scaleX', 0, 5);
            guiScale.add(controls, 'scaleY', 0, 5);
            guiScale.add(controls, 'scaleZ', 0, 5);

            let guiPosition = gui.addFolder('position');
            let contX = guiPosition.add(controls, 'positionX', -10, 10);
            let contY = guiPosition.add(controls, 'positionY', -4, 20);
            let contZ = guiPosition.add(controls, 'positionZ', -10, 10);

            contX.listen();
            contX.onChange(function (value) {
                cube.position.x = controls.positionX;
            });

            contY.listen();
            contY.onChange(function (value) {
                cube.position.y = controls.positionY;
            });

            contZ.listen();
            contZ.onChange(function (value) {
                cube.position.z = controls.positionZ;
            });

            let guiRotation = gui.addFolder('rotation');
            guiRotation.add(controls, 'rotationX', -4, 4);
            guiRotation.add(controls, 'rotationY', -4, 4);
            guiRotation.add(controls, 'rotationZ', -4, 4);

            let guiTranslate = gui.addFolder('translate');

            guiTranslate.add(controls, 'translateX', -10, 10);
            guiTranslate.add(controls, 'translateY', -10, 10);
            guiTranslate.add(controls, 'translateZ', -10, 10);
            guiTranslate.add(controls, 'translate');

            gui.add(controls, 'visible');
        }

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

        function renderScene(){
            trackballControls.update(clock.getDelta());
            stats.update();
            cube.visible = controls.visible;
            cube.rotation.x = controls.rotationX;
            cube.rotation.y = controls.rotationY;
            cube.rotation.z = controls.rotationZ;
            cube.scale.set(controls.scaleX, controls.scaleY, controls.scaleZ);
            requestAnimationFrame(renderScene);
            renderer.render(scene, camera);
        }
    }
</script>
</body>
</html>

运行结果:


相关文章

  • 修改网格的属性

    运行结果:

  • CSS Grid 布局完全指南3-网格项(Grid Items)

    上一篇,介绍了grid的网格容器属性,接下来继续介绍grid的网格项 属性。 网格项(Grid Items) 属性...

  • 网格属性

    网格有哪些属性 三角网格可以看作是一个图结构,由顶点,边和面(三角片)三个元素组成。网格的常见属性也由这三个元素的...

  • grid布局

    一、容器属性:display:grid ;网格布局display:inline-grid ;行内 // 网格列宽g...

  • CSS Grid 属性

    Test it 网格容器(Grid Container) 属性 display grid-template-col...

  • JS基础部分

    把元素移除视线的办法 属性操作 元素.属性innerHTML通过元素.属性修改的是行内属性;修改className...

  • 3、React运行阶段函数使用

    componentWillReceiveProps: 父组件修改属性触发,可以修改新属性、修改状态,组件发生之前...

  • iOS网格的简单操作和使用

    最基础网格搭建,后续再修改 @property (nonatomic,strong) UICollectionVi...

  • LDAP常用操作

    查询 增加元素 修改属性 修改多个属性: 删除原始

  • C#DataGradeView 2019-04-19

    DataGradeView数据视图网格控件 一 属性: AutoGenerateColumns 控制是否自动生成列...

网友评论

      本文标题:修改网格的属性

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