不能将MeshLambertMaterial与自定义BufferGeometry一起使用

时间:2019-08-09 10:30:59

标签: javascript three.js

我刚刚编写了一个简单的程序来在Three.js中生成金字塔形的屋顶形状(因为这是一个较大项目的一部分,所以我无法向您展示所有代码)。因此,这些形状以及其他对象(如拉伸,立方体等)都应该在场景中渲染,所有这些都应在BufferGeometry中并使用MeshLambertMaterial渲染。但是,我生成的屋顶看起来好像是用MeshBasicMaterial生成的,我不知道为什么... 这是我的代码的最低版本,因此您可以对其进行测试:

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
        <script src="three.min.js"></script>
        <script src="OrbitControls.js"></script>
        <style type="text/css">
            body{margin:0;}
        </style>
    </head>
    <body>
        <script>
var camera, SCENE, renderer, controls, SUN, OBJ;

init();
animate();

function init() {
    camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 1000 );

    SCENE = new THREE.Scene();
    SCENE.background = new THREE.Color( 0xBBDDFF );

    SUN = new THREE.DirectionalLight( 0xffffff, 0.6 );
    SCENE.add( SUN );
    SCENE.add( new THREE.AmbientLight( 0xcccccc, 0.8 ) );


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

    controls = new THREE.OrbitControls( camera, renderer.domElement );
    controls.minDistance = 1;
    controls.maxDistance = 1000;

    var geometry = new THREE.BufferGeometry();
    var material = new THREE.MeshLambertMaterial( { color:0xff0000} );
    vertices = new Float32Array([
        0,0,0,
        0,1,0,
        1,0,0,

        0,0,0,
        0,0,1,
        0,1,0,

        0,0,0,
        1,0,0,
        0,0,1,

        0,1,0,
        0,0,1,
        1,0,0,
        ]);
    geometry.addAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
    OBJ = new THREE.Mesh(geometry,material);
    SCENE.add(OBJ);

    camera.position.set(OBJ.position.x,10,OBJ.position.z-10);
    camera.lookAt(OBJ.position);
    controls.target.set(OBJ.position.x,1,OBJ.position.z);
    camera.updateProjectionMatrix();

    SUN.position.copy( camera.position );
    SUN.target = OBJ;

    document.body.appendChild( renderer.domElement );

    renderer.render( SCENE, camera );
}

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

1 个答案:

答案 0 :(得分:0)

您可以在“ material”变量中创建MeshLambertMaterial。但是,当您创建网格时,可以使用上面未描述的一些'vertexColorMaterial'变量。也许这是您的代码中的问题?