【问题标题】:Three.JS Lines with different materials and directional light intensity三.不同材质和定向光强的JS线条
【发布时间】:2013-04-24 21:28:51
【问题描述】:

three.js 很好,但是我有一点问题。

我在这里创建了一个小提琴:

http://jsfiddle.net/ntsim/aekPu/4/

var railingGeo = new THREE.Geometry();
    railingGeo.vertices.push(new THREE.Vector3(50, 25, 50));
    railingGeo.vertices.push(new THREE.Vector3(50, 35, 50));

    railingGeo.vertices.push(new THREE.Vector3(-50, 35, 50));
    railingGeo.vertices.push(new THREE.Vector3(-50, 25, 50));
    railingGeo.vertices.push(new THREE.Vector3(-50, 35, 50));

    railingGeo.vertices.push(new THREE.Vector3(-50, 35, -50));
    railingGeo.vertices.push(new THREE.Vector3(-50, 25, -50));
    railingGeo.vertices.push(new THREE.Vector3(-50, 35, -50));

    railingGeo.vertices.push(new THREE.Vector3(50, 35, -50));
    railingGeo.vertices.push(new THREE.Vector3(50, 25, -50));
    railingGeo.vertices.push(new THREE.Vector3(50, 35, -50));

    railingGeo.vertices.push(new THREE.Vector3(50, 35, 50));

    var yellowLineBasicMaterial = new THREE.LineBasicMaterial({
        color: 0x777700,
        shading: THREE.FlatShading
    });

    var yellowMeshPhongMaterial = new THREE.MeshPhongMaterial({ //shininess: 40,
        specular: 0x770000,
        ambient: 0x770000,
        emissive: 0x000000,
        color: 0x777700,
        shading: THREE.FlatShading
    });

    var railing=new THREE.Line(railingGeo,yellowLineBasicMaterial); // railing glows yellow when lights are out
    //var railing = new THREE.Line(railingGeo, yellowMeshPhongMaterial); // railing dims when lights goo out but WEBGL ERROR ocurs 
    // error is:WebGL: INVALID_OPERATION: vertexAttribPointer: no bound ARRAY_BUFFER 

    building.add(railing);
    return building;

在这个场景中,我创建了一个小建筑,屋顶上有栏杆,带有一个定向灯。 光的强度随时间在 0 到 100 之间振荡。

我的问题是关于栏杆的,其实就是一组简单的线条。

我希望整个建筑物和栏杆随着灯光强度变为零而变暗。

检查我的小提琴的第 72 和 73 行。

当我使用 LineBasicMaterial 创建栏杆时,无论光线强度如何,栏杆都会发光。这不是我想要的。

如果我将栏杆的材质属性更改为 MeshPhongMaterial,那么 当光强度变为零时,栏杆确实会逐渐变暗,但此代码会在 Chrome 浏览器控制台上产生“WEBGL 错误:INVALID_OPERATION: vertexAttribPointer: no bound ARRAY_BUFFER”。

我想我明白为什么会出现 WEBGL 错误。我认为是因为线条没有法线来反射光线?或类似的东西。

无论如何,在我看来,我应该能够用我的线条强制某种材质属性,以便线条颜色强度可以通过场景的整体光强度以某种方式降低,而无需使用 WEBGL错误。几年前,我使用 opengl 编写了一个 c++ 程序,并且能够毫无问题地做到这一点。这可能是 THREE.js 错误或 WEBGL 问题吗?当光线强度发生变化时,我可以动态改变线条的颜色值,但有更好的方法吗?

谢谢!

【问题讨论】:

    标签: javascript three.js


    【解决方案1】:

    LineBasicMaterial不响应灯光,其他材质不适合做线条,原因你说的。

    你可以这样做:

    yellowLineBasicMaterial.color.setRGB( light.intensity / 100, light.intensity / 100, 0 );
    

    另一种解决方案是使用CubeGeometry作为栏杆,MeshPhongMaterial作为栏杆材质,并设置wireframe: true。这样的话,你应该得到你想要的效果。

    three.js r.58

    【讨论】:

      猜你喜欢
      • 2021-12-17
      • 1970-01-01
      • 2014-03-01
      • 2012-11-20
      • 1970-01-01
      • 2014-08-06
      • 2021-05-21
      • 2017-01-13
      相关资源
      最近更新 更多