- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试检测用户单击对象上纹理中的位置,以便我可以重新绘制纹理作为响应。
我知道我可以使用颜色编码纹理将对象渲染到单独的渲染目标,并使用 gl.readPixels 来查看单击了哪个编码像素,然后计算回纹理中的 X 和 Y 坐标.
我似乎能够对 Y 轴可靠地执行此操作,但不能对 X 轴执行此操作。
这是我的(删除了无关的代码)Three.js 设置:
var canvas = document.getElementByID("output"),
renderer = new THREE.WebGLRenderer({
canvas: canvas,
alpha: true,
antialias: true
}),
back = new THREE.WebGLRenderTarget(canvas.width, canvas.height),
scene = new THREE.Scene(),
pickingScene = new THREE.Scene(),
pickingPixelBuffer = new Uint8Array(4),
camera = new THREE.PerspectiveCamera(50, canvas.width / canvas.height, 0.1, 1000),
textureWidth = 1024,
textureHeight = 1024,
texture = generateTexture(textureWidth, textureHeight),
pickingTexture = generatePickingTexture(textureWidth, textureHeight),
obj = textured(shell(w, 5, 10), texture),
objPicker = textured(shell(w, 5, 10), pickingTexture);
back.generateMipmaps = false;
scene.add(camera);
scene.add(obj);
pickingScene.add(objPicker);
对于这样的对象:
拾取纹理最终看起来像这样:
generateTexture
函数实际上并不那么重要。 textured
函数只是将纹理应用于几何对象的简写:
function textured(geometry, txt){
var material = new THREE.MeshBasicMaterial({
color: 0xffffff,
map: txt,
transparent: false,
shading: THREE.FlatShading,
side: THREE.DoubleSide
});
var obj = new THREE.Mesh(geometry, material);
return obj;
}
generatePickingTexture
函数如下:
function generatePickingTexture(w, h){
var canvas = document.createElement("canvas");
canvas.width = w;
canvas.height = h;
var texture = new THREE.Texture(canvas);
var gfx = texture.image.getContext("2d"),
l = w * h,
pixels = gfx.createImageData(w, h);
for(var i = 0, p = 0;
i < l;
++i, p += 4){
pixels.data[p] = (0xff0000 & i) >> 16;
pixels.data[p+1] = (0x00ff00 & i) >> 8;
pixels.data[p+2] = 0x0000ff & i;
pixels.data[p+3] = 0xff;
}
gfx.putImageData(pixels, 0, 0);
texture.needsUpdate = true;
return texture;
}
然后我尝试执行挑选:
function pick(){
renderer.render(pickingScene, camera, back, true);
var gl = renderer.getContext();
gl.readPixels(pointerX, canvas.height - pointerY, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pickingPixelBuffer);
var i = (pickingPixelBuffer[0] << 16) |
(pickingPixelBuffer[1] << 8) |
pickingPixelBuffer[2],
// x = i % textureWidth,
x = (i - Math.floor(textureWidth / 512) * 256) % textureWidth, // EDIT2: this is the hack
y = i / textureWidth;
console.log(x, y);
}
正如我所说,y 坐标效果非常好。但x坐标偏离了很多。当我在屏幕上向下拖动鼠标时,x 坐标会向右扭曲,跳跃约纹理宽度的 1/4。当我在屏幕上拖动鼠标时,如果可以避免任何垂直鼠标移动,则 x 坐标会以正确的偏移量移动,但不在正确的位置。每次我击中 1/4 标记之一时,它似乎都会跳转位置。
由于它是 1/4,看来我生成纹理的数学是错误的。但对于我的一生来说,我看不到它。
编辑:确实,如果我将纹理限制为只有 256 像素宽,它就可以完美工作。
EDIT2:我已经找到了解决问题的
pick
函数的破解方法,但我不明白它为什么起作用。
EDIT3:嗯,“工作”的条件是对象在屏幕上的显示比例为 1:1。不同方向仍然存在一些问题,但这与其他问题无关。我怀疑这与纹理的重新采样有关。
EDIT4:这是默认的纹理过滤设置。
最佳答案
哦!整个过程都是纹理过滤。我需要将我的 generatePickingTexture
函数更改为:
function generatePickingTexture(w, h){
var canvas = document.createElement("canvas");
canvas.width = w;
canvas.height = h;
var texture = new THREE.Texture(canvas, THREE.UVMapping, THREE.RepeatWrapping, THREE.RepeatWrapping, THREE.NearestFilter, THREE.NearestFilter);
var gfx = canvas.getContext("2d"),
l = w * h,
pixels = gfx.createImageData(w, h);
for(var i = 0, p = 0; i < l; ++i, p += 4){
pixels.data[p] = (0xff0000 & i) >> 16;
pixels.data[p+1] = (0x00ff00 & i) >> 8;
pixels.data[p+2] = (0x0000ff & i) >> 0;
pixels.data[p+3] = 0xff;
}
gfx.putImageData(pixels, 0, 0);
texture.needsUpdate = true;
return texture;
}
关于javascript - 纹理坐标拾取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28201530/
@秋意正寒 先讲怎么用,再讲大致原理。 1 拾取坐标 > 仅拾取椭球体表面坐标 使用 Camera.prototype.pickEllipsoid 方法,接受一个必选的屏幕坐标,返回一个三维世界坐标
我知道 OpenGL 选择模式已被弃用,并且除了少数 SGI 盒和 3DLabs GPU 之外从未进行过硬件加速。但我无法摆脱它(不是我的代码)。在其 C++ 代码下方: void GLWidget:
我有一个 .ipa,里面有 arm64 和 armv7 个片段。如果我在同时支持 arm64 和 armv7 的 iDevice 上运行它,运行时将选择哪个切片? 我可以通过打印 NSLog 或某种方
我想在建模为高度图的世界地面上的 OpenGL 场景中跟踪鼠标坐标。目前没有像硬件镶嵌这样的花哨的东西。注意这个问题是不是 关于物体拾取。 目前我正在执行以下操作,由于回读操作,这显然会降低性能: 渲
我正在将一个数组从 php 传递到 javascript,但它似乎在数组的开头和结尾处拾取了一个额外的“。 我的数组从 PHP 文件发送 json_encode($CheckItems."|".$Ch
我的光线拾取代码有问题。我的代码 我正在使用此代码进行选择计算: /*----------------------------------------------------------- Funct
我需要根据特定深度(用于光线转换)将 2D 鼠标坐标转换为 3D 世界坐标。我没有直接在 C++ 中使用 DirectX,我使用的是专有语言,但如果你用 C++ 或伪代码(首选 C++)给我一个答案,
我正在开发一款等距游戏,但在制定方 block 选择算法时遇到了问题。 这就是我渲染等距 map 的方式: for (int x = 0; x < 50; x++) { for (int y
我尝试在应用程序中导出 UTI,但 macOS (10.3.2) 未拾取它。我已经在一个较小的项目中复制了我的问题。这是 info.plist 的相关部分: UTExportedTypeDeclara
我正在尝试使用 Pyglet 的 OpenGL 包装器实现拾取,但我在转换 C tutorial 时遇到了问题到 Python。具体在下面的部分。 #define BUFSIZE 512GLuint
我有一个定义为 singleTop 的 Activity,因此只有一个实例存在。 我用一些数据设置了一个 Notification Intent,并将其包含在 PendingIntent 中,
场景中有一些物体,有些可能会遮挡其他物体。当我单击鼠标或拖动选择以获得选择矩形时,我只想选择/拾取从这个角度可以看到的对象。应用程序当前使用 GL_SELECT 渲染模式,但正如我们所知,这也会选择被
我在一个小项目中使用了SpringBootApplication。架构很简单: 入口文件: @SpringBootApplication @PropertySource("classpath:appl
我有以下媒体查询,以选择 iPad 视网膜和更小的显示器。 @media only screen and (min-device-width : 768px) and (max-device-widt
我需要将 Oauth 设置为我将要处理的 PHP 项目的一部分。 为此我跑了; sudo pecl install oauth 这完成了 OK,它指示我将 extension=oauth.so 添加到
我正在尝试使用 Angular4 和 Google Analytics 集成自定义维度。我看过类似的主题,但没有任何内容可以准确解决这个问题。 在 GA 中我设置了自定义维度: 它以索引 1 显示:
我试图阻止用户与 vtkCaptionWidget 中的 anchor 进行交互 (http://www.vtk.org/Wiki/VTK/Examples/Cxx/Widgets/CaptionWi
我找不到使用光线追踪方法在 3D 中拾取的正确且可理解的表达方式。有人用任何语言实现过这个算法吗?分享直接工作的代码,因为由于伪代码无法编译,所以它们通常是由缺少的部分编写的。 最佳答案 您所拥有的是
我有一个相对较新的 MVC5 项目,正在使用 TeamCity 构建并由 Octopus Deploy 部署。一切都很棒,直到我通过 NuGet 添加 SQLite。当项目构建完成后,我在 bin 目
我正在尝试使用 Spring Data REST(版本 2.3.0.RELEASE)进行预测。我读了reference documentation ,并收集到这些是我需要的部分: JPA 实体 @En
我是一名优秀的程序员,十分优秀!