This is the fifth day of my participation in the More text Challenge. For details, see more text Challenge

This chapter will mainly introduce the coordinate system transformation in Oasis Engine, it is my understanding after learning, if there is any error, please correct!!

Space and coordinate system

The existence of space and coordinate system mainly solves one problem: how to express where I am?

In reality, if someone asks you where are you?

We can easily answer: I am on chenghua Avenue to erxian Bridge

This answer clearly expresses his position.

In Oasis, it is necessary to use cartesian right-handed coordinate system (x, y, z) to express the position clearly

Any of various Spaces in graphics

Model space

  1. Model Spaces are also called local Spaces
  2. The model is built according to the origin of the local space
  3. The origin of the model coordinate system in Oasis is at the center of the model

The world space

  1. World space is global 3D space
  2. Objects change their position in world space through various transformations
  3. The origin of the spatial coordinate system in Oasis is in the center of the screen. The coordinates of all objects in the space coordinate system are the coordinates relative to the origin of the space coordinate system.

View space

  1. View space is also known as camera space
  2. Camera space transforms the world space coordinate system into the coordinates in front of the user’s view.
  3. The camera position in Oasis is the origin position. The coordinates of all objects in the camera coordinate system are relative to the position of the camera.

Projection space

  1. Projection space is also known as clipping space, standard equipment coordinate system.
  2. The projection space is a cube where x, y, and z are [-1, 1].
  3. Fragments outside the projection space will not enter the later rendering process.

Space transformation

3D space transformation

  1. In general, a 3D space can be described in 3D Cartesian coordinates.
  2. Each coordinate system has its own origin, scale, and axis.
  3. The process of transforming an object in one coordinate system into another coordinate system is called a spatial transformation.

Coordinate transformation in Oasis

Oasis encapsulates the method of coordinate transformation and provides coordinate transformation in the Camera interface

When using coordinate transformation in Oasis, you need to use isOrthographic to switch the camera to an orthogonal projection

An orthogonal projection means that the objects in the visible area near and far are the same size. The visible region generated by the orthogonal projection model is called the box-shaped visible region, and the box-shaped visible region model is as follows:

cameramash.isOrthographic = true

Here are some common methods

screenToWorldPoint

screenToWorldPoint(point: Vector3, out: Vector3): Vector3 converts points from screen space to world space. The point screen space point coordinates out world coordinates returns out world coordinatesCopy the code

Example: Load image material to the upper left corner of the screen

  // Load the texture resources
  const texture = {
    url: require('.. /assets/z.png')}const textureList: Texture2D = await engine.resourceManager.load(texture)
  
  // Create a Sprite entity
  const spriteEntity = rootEntity.createChild('sprite1')
  
  // Determine the screen coordinates
  // Because the origin of the screen coordinates is in the top left corner of the canvas, and the origin of the Sprite entities is in the center
  // Divide the Sprite's height and width by 2 to make the top left corner of the Sprite coincide with the top left corner of the canvas
  const screenpoint = new Vector3(textureList.height / 2, textureList.width / 2)
  
  // Create world coordinates
  const out = new Vector3()
  
  // Use screenToWorldPoint to convert screen coordinates to world coordinates
  // The screenToWorldPoint method returns the modified out value
  cameramash.screenToWorldPoint(screenpoint, out)
  
  // Use transform. Position to set the entity coordinates
  spriteEntity.transform.position = out
Copy the code

To complete the effect

Use the same method to locate the four corners of the screen

worldToScreenPoint

worldToScreenPoint(point: Vector3, out: Vector4): Vector4 converts points from world space to screen space. The point world coordinate out screen space point coordinate returns out screen space point coordinateCopy the code

Example: Convert world coordinate origin to screen coordinate

  const worldPoint = new Vector3(0.0.0)
  const screenOut = new Vector4()
  cameramash.worldToScreenPoint(worldPoint, screenOut)
  console.log(` screenOut. X:${screenOut.x}; screenOut.y: ${screenOut.y}`)
  / / screenOut. X: 960; ScreenOut. Y: 503.5
Copy the code

end

The coordinates are determined, and you are one step closer to completing the small goal of the game

respect by myself