【问题标题】:THREE.Shape from color Green to Red三、从绿色到红色的形状
【发布时间】:2014-06-18 18:52:52
【问题描述】:

我使用 THREE.Shape 创建了一个简单的圆形并将其着色为绿色。

但是我希望更改颜色,使其从绿色(中间)变为红色(边框)。

我一直在查看来自 this 站点的示例,但我不明白如何为我的项目实施类似的方式。

创建圆圈的代码:

var arcShape = new THREE.Shape();
arcShape.absarc(100, 100, circleRadius, 0, Math.PI * 2, false);

var geometry = new THREE.ShapeGeometry(arcShape);
var material = new THREE.MeshBasicMaterial({ color: 0x00ff11, overdraw: 0.5, side: THREE.DoubleSide });

var mesh = new THREE.Mesh(geometry, material);
mesh.position = CirclePosition;
mesh.rotation.set(Algorithms.ConvertDegreesToRadians(-90), 0, 0);

【问题讨论】:

    标签: javascript colors three.js geometry shapes


    【解决方案1】:

    你引用的例子在这里并不重要。最简单的方法是只使用图像。如果您习惯使用仅与 WebGL 渲染器兼容的方法,我制作了一个 JSFiddle,展示了一个简单的 GLSL 着色器示例:http://jsfiddle.net/2qqdm/

    关键位是片段着色器:

    varying vec2 vUv;
    varying vec3 vPosition;
    uniform float radius;
    void main() {
        vec3 center = vec3(100.0, 100.0, 0.0);
        float redAmount = max(0.0, min(1.0, distance(vPosition, center) / radius));
        gl_FragColor = vec4(redAmount, 1.0 - redAmount, 0.0, 1.0);
    }
    

    您也可以在 JS 中使用顶点颜色完全做到这一点,这也与 CanvasRenderer 兼容;你只需要弄清楚中心顶点在哪里。

    【讨论】:

      【解决方案2】:

      另一种方法是使用顶点颜色

      var colorRed = new THREE.Color (0.9, 0.0, 0.0);
      var colorGreen = new THREE.Color (0.0, 0.9, 0.0);
      
      material = new THREE.MeshBasicMaterial({ vertexColors: THREE.VertexColors});
      
      geometry = new THREE.CircleGeometry(100, 10, 0, 3);
      
      var nmax = geometry.faces.length;
      for (var n=0; n<nmax; n++) {
          geometry.faces[n].vertexColors[0] = colorRed;
          geometry.faces[n].vertexColors[1] = colorRed;
          geometry.faces[n].vertexColors[2] = colorGreen;
      }
      

      【讨论】:

      • 效果也很好!谢谢!
      猜你喜欢
      • 1970-01-01
      • 2018-11-08
      • 2013-09-27
      • 2021-02-26
      • 2021-07-07
      • 1970-01-01
      • 2017-05-28
      • 2011-05-08
      • 1970-01-01
      相关资源
      最近更新 更多