简体   繁体   English

Three.js / WebGL-球形-场景可见的国家/颜色

[英]Three.js / WebGL - Sphere - scene visible countries / colors

I had implemented a globe similar to this one But while trying to fetch the countries visible to the scene in Three.js when the globe is in a stationary position am stuck in a issue. 我已经实现了一个与地球类似的地球,但是当地球处于静止位置时试图在Three.js中获取场景中可见的国家时,仍然遇到了问题。 I can retrieve the country color code when I mouse over / click on a specific country using readPixels and get the appropriate color code of the pixel as shown below 当我将鼠标悬停/单击特定国家/地区时,可以使用readPixels检索国家/地区颜色代码,并获得相应的像素颜色代码,如下所示

 var gl = renderer.context; var mx = ( mouseX + renderer.context.canvas.width/2 );//(mouseX + renderer.context.canvas.width/2) * 0.25; var my = ( -mouseY + renderer.context.canvas.height/2 );//(-mouseY + renderer.context.canvas.height/2) * 0.25; mx = Math.floor( mx ); my = Math.floor( my ); var buf = new Uint8Array( 4 ); gl.readPixels( mx, my, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, buf ); 

But when the globe stopped rotating then I need to get the list of user viewable countries from the scene, I tried to use Raycaster to get the mesh and scan through all the pixels and get the country code but seems to be a performance hit for us 但是,当地球停止旋转时,我需要从场景中获取用户可见的国家/地区列表,我尝试使用Raycaster获取网格并扫描所有像素并获得国家/地区代码,但对我们而言似乎对性能造成了影响

Appreciate if someone can provide their views 欣赏有人可以提供自己的观点

You can render the scene to a texture (even with different shaders/uniforms). 您可以将场景渲染为纹理(即使使用不同的着色器/均匀度)。 There are examples on the official site, like this: http://threejs.org/examples/webgl_rtt.html 官方网站上有一些示例,例如: http : //threejs.org/examples/webgl_rtt.html

For instance you could have a scene where the texture of the globe is country coded , and when the user clicks, you render that scene into a texture and read single pixel from that texture. 例如,您可能有一个场景,其中地球纹理用国家/地区编码 ,并且当用户单击时,您将该场景渲染为纹理并从该纹理读取单个像素。

Be careful, when rendering to textures, you sometimes have to force update your textures by setting needsUpdate to true . 请注意,渲染到纹理时,有时必须通过将needsUpdate设置为true来强制更新纹理。

Hope I helped. 希望我能帮上忙。

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

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