【问题标题】:Threejs cube is not showing its edgesThreejs立方体没有显示它的边缘
【发布时间】:2021-07-26 22:29:57
【问题描述】:

我正在潜入threejs 的世界。我采用了其中一个示例项目并对其进行了修改。我刚刚添加了一些带有颜色的立方体几何图形。问题是,立方体几何显示没有边缘。你无法区分脸在哪里结束,它都被漂白了。不知道是灯光问题还是材质问题。我的代码如下。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>three.js webgl - cloth simulation</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <link type="text/css" rel="stylesheet" href="main.css">
    <style>
        body {
            background-color: #cce0ff;
            color: #000;
        }
        a {
            color: #080;
        }
    </style>
</head>

<body>

    <div id="info">Simple Cabinet Box in 3D Space<br/>
    </div>
    <script type="module">
        import * as THREE from '../build/three.module.js';
        //import Stats from './jsm/libs/stats.module.js';
        import { GUI } from './jsm/libs/dat.gui.module.js';
        import { OrbitControls } from './jsm/controls/OrbitControls.js';

        function plane( width, height )
        {
            return function ( u, v, target )
            {
                const x = ( u - 0.5 ) * width;
                const y = ( v + 0.5 ) * height;
                const z = 0;
                target.set( x, y, z );
            };
        }

        let container;
        let camera, scene, renderer;

        init();
        animate( 0 );

        function init() {
            container = document.createElement( 'div' );
            document.body.appendChild( container );

            // scene
            scene = new THREE.Scene();
            scene.background = new THREE.Color( 0xffffff );

            // camera
            camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 10000 );
            camera.position.set( 0, 0, 100 );

            // lights
            //scene.add( new THREE.AmbientLight( 0x666666 ) );
            const light = new THREE.DirectionalLight( 0xdfebff, 1 );
            light.position.set( 50, 200, 100 );
            light.position.multiplyScalar( 1.3 );
            light.castShadow = true;
            light.shadow.mapSize.width = 1024;
            light.shadow.mapSize.height = 1024;
            const d = 300;
            light.shadow.camera.left = - d;
            light.shadow.camera.right = d;
            light.shadow.camera.top = d;
            light.shadow.camera.bottom = - d;
            light.shadow.camera.far = 1000;
            scene.add( light );


            //cabinet properties
            const c_width = 24;
            const c_height = 34.5;
            const c_depth = 23.125;
            const c_left_side_thickness = 0.75;

            //Left End
            const geometry = new THREE.BoxGeometry( c_left_side_thickness, c_height, c_depth);
            const material = new THREE.MeshBasicMaterial( { color: 0xC2B8B6 } );
            const left_side = new THREE.Mesh( geometry, material );
            left_side.position.set((c_width/2)*-1 + c_left_side_thickness/2,0,0);

            const right_side = new THREE.Mesh( geometry, material );
            right_side.position.set(c_width/2 - c_left_side_thickness/2,0,0);

            const geometry2 = new THREE.BoxGeometry( c_width - c_left_side_thickness * 2, c_left_side_thickness, c_depth);
            const bottom = new THREE.Mesh( geometry2, material );
            bottom.position.set(0,(c_height/2)*-1 + c_left_side_thickness/2,0);

            const top = new THREE.Mesh( geometry2, material );
            top.position.set(0,c_height / 2 - c_left_side_thickness/2,0);

            const cabinet = new THREE.Group();
            cabinet.add( left_side );
            cabinet.add( right_side );
            cabinet.add( bottom );
            cabinet.add( top );

            scene.add( cabinet );

            //
            // const geometry2 = new THREE.BoxGeometry( 10, 10, 10 );
            // const edges = new THREE.EdgesGeometry( geometry2 );
            // const line = new THREE.LineSegments( edges, new THREE.LineBasicMaterial( { color: 0xffffff } ) );
            // line.position.set(0,100,0);
            // scene.add( line );

            // ground
            const loader = new THREE.TextureLoader();
            const groundTexture = loader.load( 'textures/terrain/grasslight-big.jpg' );
            groundTexture.wrapS = groundTexture.wrapT = THREE.RepeatWrapping;
            groundTexture.repeat.set( 25, 25 );
            groundTexture.anisotropy = 16;
            groundTexture.encoding = THREE.sRGBEncoding;
            const groundMaterial = new THREE.MeshLambertMaterial( { map: groundTexture } );
            let mesh = new THREE.Mesh( new THREE.PlaneGeometry( 20000, 20000 ), groundMaterial );
            mesh.position.y = - 250;
            mesh.rotation.x = - Math.PI / 2;
            mesh.receiveShadow = true;
            scene.add( mesh );

            // renderer
            renderer = new THREE.WebGLRenderer( { antialias: true } );
            renderer.setPixelRatio( window.devicePixelRatio );
            renderer.setSize( window.innerWidth, window.innerHeight );

            container.appendChild( renderer.domElement );

            renderer.outputEncoding = THREE.sRGBEncoding;
            renderer.shadowMap.enabled = true;

            // controls
            const controls = new OrbitControls( camera, renderer.domElement );
            controls.maxPolarAngle = Math.PI * 0.5;
            controls.minDistance = 100;
            controls.maxDistance = 500;

            window.addEventListener( 'resize', onWindowResize );

            if ( typeof TESTING !== 'undefined' )
            {
                for ( let i = 0; i < 50; i ++ )
                {
                    simulate( 500 - 10 * i );
                }
            }
        }

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

        function animate( now )
        {
            requestAnimationFrame( animate );
            render();
        }

        function render()
        {
            renderer.render( scene, camera );
        }
    </script>
</body>

【问题讨论】:

    标签: three.js


    【解决方案1】:

    您必须为您的盒子使用发光材质(例如MeshPhongMaterial)。 MeshBasicMaterial 是一种不发光的材质,不会对灯光做出反应。此外,在定向旁边使用环境光会使场景看起来更自然。

    let container;
    let camera, scene, renderer;
    
    init();
    animate(0);
    
    function init() {
      container = document.createElement('div');
      document.body.appendChild(container);
    
      // scene
      scene = new THREE.Scene();
      scene.background = new THREE.Color(0xffffff);
    
      // camera
      camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 1, 10000);
      camera.position.set(0, 0, 100);
    
      // lights
      scene.add( new THREE.AmbientLight( 0xffffff, 0.4 ) );
      
      const light = new THREE.DirectionalLight(0xffffff, 0.8);
      light.position.set(50, 200, 100);
      light.position.multiplyScalar(1.3);
      light.castShadow = true;
      light.shadow.mapSize.width = 1024;
      light.shadow.mapSize.height = 1024;
      const d = 300;
      light.shadow.camera.left = -d;
      light.shadow.camera.right = d;
      light.shadow.camera.top = d;
      light.shadow.camera.bottom = -d;
      light.shadow.camera.far = 1000;
      scene.add(light);
    
      //cabinet properties
      const c_width = 24;
      const c_height = 34.5;
      const c_depth = 23.125;
      const c_left_side_thickness = 0.75;
    
      //Left End
      const geometry = new THREE.BoxGeometry(c_left_side_thickness, c_height, c_depth);
      const material = new THREE.MeshPhongMaterial({
        color: 0xC2B8B6
      });
      const left_side = new THREE.Mesh(geometry, material);
      left_side.position.set((c_width / 2) * -1 + c_left_side_thickness / 2, 0, 0);
    
      const right_side = new THREE.Mesh(geometry, material);
      right_side.position.set(c_width / 2 - c_left_side_thickness / 2, 0, 0);
    
      const geometry2 = new THREE.BoxGeometry(c_width - c_left_side_thickness * 2, c_left_side_thickness, c_depth);
      const bottom = new THREE.Mesh(geometry2, material);
      bottom.position.set(0, (c_height / 2) * -1 + c_left_side_thickness / 2, 0);
    
      const top = new THREE.Mesh(geometry2, material);
      top.position.set(0, c_height / 2 - c_left_side_thickness / 2, 0);
    
      const cabinet = new THREE.Group();
      cabinet.add(left_side);
      cabinet.add(right_side);
      cabinet.add(bottom);
      cabinet.add(top);
    
      scene.add(cabinet);
    
      // ground
      const loader = new THREE.TextureLoader();
      const groundTexture = loader.load('https://threejs.org/examples/textures/terrain/grasslight-big.jpg');
      groundTexture.wrapS = groundTexture.wrapT = THREE.RepeatWrapping;
      groundTexture.repeat.set(25, 25);
      groundTexture.anisotropy = 16;
      groundTexture.encoding = THREE.sRGBEncoding;
      const groundMaterial = new THREE.MeshLambertMaterial({
        map: groundTexture
      });
      let mesh = new THREE.Mesh(new THREE.PlaneGeometry(20000, 20000), groundMaterial);
      mesh.position.y = -250;
      mesh.rotation.x = -Math.PI / 2;
      mesh.receiveShadow = true;
      scene.add(mesh);
    
      // renderer
      renderer = new THREE.WebGLRenderer({
        antialias: true
      });
      renderer.setPixelRatio(window.devicePixelRatio);
      renderer.setSize(window.innerWidth, window.innerHeight);
    
      container.appendChild(renderer.domElement);
    
      renderer.outputEncoding = THREE.sRGBEncoding;
      renderer.shadowMap.enabled = true;
    
      // controls
      const controls = new THREE.OrbitControls(camera, renderer.domElement);
      controls.maxPolarAngle = Math.PI * 0.5;
      controls.minDistance = 100;
      controls.maxDistance = 500;
    
      window.addEventListener('resize', onWindowResize);
    
    }
    
    function onWindowResize() {
      camera.aspect = window.innerWidth / window.innerHeight;
      camera.updateProjectionMatrix();
      renderer.setSize(window.innerWidth, window.innerHeight);
    }
    
    function animate() {
      requestAnimationFrame(animate);
      render();
    }
    
    function render() {
      renderer.render(scene, camera);
    }
    <script src="https://cdn.jsdelivr.net/npm/three@0.128.0/build/three.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.js"></script>

    【讨论】:

    • 你摇滚!谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    相关资源
    最近更新 更多