简体   繁体   English

正交相机底部中心的 Position 精灵

[英]Position sprite at bottom center of orthographic camera

I'm trying to position sprites at the bottom center of the orthographic camera.我正在尝试在正交相机的底部中心放置 position 个精灵。

This is what I have currently -这就是我目前拥有的 -

The object: object:

function Obj() {
  const texture = useLoader(THREE.TextureLoader, loadedimg);

  const spriteRef = useRef();

  return (
    <group>
      <sprite
        position={[1,  ???, 0]}
        ref={spriteRef}
        scale={[texture.image.width * 1.5, texture.image.height * 1.5, 1]}
      >
        <spriteMaterial
          attach="material"
          map={texture}
          sizeAttenuation={false}
        />
      </sprite>
    </group>
  );
}

The camera rendering:相机渲染:

      <Canvas orthographic camera>
        <Suspense fallback={<React.Fragment>loading...</React.Fragment>}>
          <Obj />
        </Suspense>
      </Canvas

I've tried to come up with a calculation that could work for all different sprites (they all have different heights so for example one of them would be positioned at the bottom by setting position={[1, -128, 0]} while for the other those coordinates would just leave it floating due to it being shorter in height).我试图想出一种适用于所有不同精灵的计算方法(它们都有不同的高度,例如,通过设置position={[1, -128, 0]}将其中一个定位在底部,而对于另一个,这些坐标只会让它浮动,因为它的高度较短)。 I looked at the documentation and the center attribute for sprites doesn't seem to do what I'd like it to, all of the examples I've seen position it at the corners of the screen or right in the middle.我查看了文档,精灵的center属性似乎没有按照我的意愿执行,我看到的所有示例 position 它都在屏幕的角落或中间。 Thanks for your help !谢谢你的帮助 !

With an Orthographic camera, it's easy to find the bottom of the frustum as long as the camera doesn't rotate.使用正交相机,只要相机不旋转,就很容易找到截锥体的底部。 Since all edges of an ortho cam are parallel, the bottom can be obtained with camera.bottom , regardless of how deep in the z-axis the sprite is.由于 ortho cam 的所有边缘都是平行的,因此可以使用camera.bottom获得底部,无论精灵在 z 轴上有多深。 The top is camera.top and then you can just find a value in between those two to be placed wherever you'd like.顶部是camera.top ,然后您可以在这两者之间找到一个值,然后将其放置在您想要的任何位置。

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

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