简体   繁体   English

平面几何,每段颜色不同,部分透明

[英]Plane geometry, each segment different color, some of them transparent

I am using three.js and I want to create geometry 16x16 size.我正在使用 three.js,我想创建 16x16 大小的几何图形。 I want that each segment is different color and some of them is transparent.我希望每一段都是不同的颜色,其中一些是透明的。 What is the best solution for this problem?这个问题的最佳解决方案是什么? Should i render each pixel as a single plane geometry?我应该将每个像素渲染为一个平面几何体吗? Or there is possible way to change single segment color/transparency.或者有可能改变单段颜色/透明度的方法。

Desired result期望的结果

I suggest you an instance of PlaneGeometry , transform it to a so called non-indexed geometry and then add an additional color buffer attribute.我建议您使用PlaneGeometry的一个实例,将其转换为所谓的非索引几何体,然后添加一个额外的颜色缓冲区属性。 Full example:完整示例:

 let camera, scene, renderer; init(); render(); function init() { camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 10); camera.position.z = 1; scene = new THREE.Scene(); // background const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); canvas.width = canvas.height = 128; ctx.fillStyle = '#ddd'; ctx.fillRect(0, 0, 128, 128); ctx.fillStyle = '#555'; ctx.fillRect(0, 0, 64, 64); ctx.fillStyle = '#999'; ctx.fillRect(32, 32, 32, 32); ctx.fillStyle = '#555'; ctx.fillRect(64, 64, 64, 64); ctx.fillStyle = '#777'; ctx.fillRect(96, 96, 32, 32); mapBg = new THREE.CanvasTexture(canvas); mapBg.wrapS = mapBg.wrapT = THREE.RepeatWrapping; mapBg.repeat.set(64, 32); scene.background = mapBg; // plane mesh const geometry = new THREE.PlaneGeometry(1, 1, 10, 10).toNonIndexed(); const positionAttribute = geometry.getAttribute('position'); const colors = []; const color = new THREE.Color(); for (let i = 0; i < positionAttribute.count; i += 6) { color.setRGB(Math.random(), Math.random(), Math.random()); const alpha = Math.random(); colors.push(color.r, color.g, color.b, alpha); colors.push(color.r, color.g, color.b, alpha); colors.push(color.r, color.g, color.b, alpha); colors.push(color.r, color.g, color.b, alpha); colors.push(color.r, color.g, color.b, alpha); colors.push(color.r, color.g, color.b, alpha); } geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 4)); const material = new THREE.MeshBasicMaterial({ vertexColors: true, transparent: true }); const mesh = new THREE.Mesh(geometry, material); scene.add(mesh); // renderer = new THREE.WebGLRenderer({antialias: true}); renderer.setPixelRatio(window.devicePixelRatio); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); } function render() { renderer.render(scene, camera); }
 body { margin: 0; }
 <script src="https://cdn.jsdelivr.net/npm/three@0.141/build/three.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM