【问题标题】:Color based on mesh height Three.js基于网格高度的颜色 Three.js
【发布时间】:2018-10-29 18:30:19
【问题描述】:

我正在使用 Three.js 中的 STLLoader() 加载 STL 文件,并将其转换为网格。现在,我想根据高度为网格的每个立方体着色,如screenshot-color-by-height 所附图像所示。这在 Three.js 中是否可行,如果可以,最好的方法是什么?实际模型在这里:

https://casagroupproject.github.io/subpage1.html

【问题讨论】:

  • 您可以在模型几何体中使用着色器或设置顶点颜色。
  • 是否也可以使用TextureLoader?类似var loader = new THREE.TextureLoader(); material = new THREE.MeshPhongMaterial( { map: loader.load('delhiTex.jpg'), side: THREE.DoubleSide })

标签: javascript colors three.js mesh


【解决方案1】:

只是一个关于如何用顶点颜色制作它的概念:

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.set(2, 5, 10);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

var controls = new THREE.OrbitControls(camera, renderer.domElement);

var light = new THREE.DirectionalLight(0xffffff, 0.5);
light.position.setScalar(10);
scene.add(light);
scene.add(new THREE.AmbientLight(0xffffff, 0.5));

var planeGeom = new THREE.PlaneBufferGeometry(10, 10, 10, 10);
planeGeom.rotateX(-Math.PI * 0.5);
var yMin = 0;
var yMax = 2;
var colors = [];
for (let i = 0; i < planeGeom.attributes.position.count; i++) {
  let yVal = THREE.Math.randInt(yMin, yMax);
  let yNorm = (yVal - yMin) / (yMax - yMin);
  planeGeom.attributes.position.setY(i, yVal);
  colors.push(yNorm, yNorm, 1);
}
planeGeom.addAttribute('color', new THREE.BufferAttribute(new Float32Array(colors), 3));
planeGeom.computeVertexNormals();

var mesh = new THREE.Mesh(planeGeom, new THREE.MeshStandardMaterial({
  vertexColors: THREE.VertexColors
}));
scene.add(mesh)

render();

function render() {
  requestAnimationFrame(render);
  renderer.render(scene, camera);
}
body {
  overflow: hidden;
  margin: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/92/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>

【讨论】:

  • 谢谢 - 我会试试看!
  • @emily 只是出于好奇,有帮助吗?
  • 了解一般情况很有用,但我最终选择了另一种解决方案!
  • @emily 是否有指向您使用的解决方案的链接?真的很有趣:)
  • 由于时间不够,我最终使用了内置的 MeshNormalMaterial() (casagroupproject.github.io/cities.html#london)
猜你喜欢
  • 1970-01-01
  • 2016-10-25
  • 1970-01-01
  • 2017-03-14
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多