Translation notice
This page was translated with machine translation and may contain inaccuracies. If you can help improve it, please open an issue or submit a pull request.
introduction
Display entities are one of the technical entities of Minecraft, and their role is mainly reflected in the visual aspect. These entities have no collision boxes, do not have any autonomous behavior, and can only be generated through technical means. If you don't specify NBT when generating, nothing will be displayed. Developers of vanilla technology can use the regular fields of the display entity to display some common content, such as normal-shaped blocks, items, and text. However, it would be a bit monotonous if only the display entity is used to display these regular contents.
The transformation field that displays the entity is a more complex field in the entity format. It uses matrix form or decomposition form to represent the rendering transformation of the entity, thereby creating some special effects.
matrix form
When using matrix form, the data type of the field transformation is a list. There are 16 elements in the list, and these elements are single-precision floating point numbers. This list is used to represent aThe row-major order affine transformation matrix of . In order to express the transformation of points in the three-dimensional space in matrix form, the original space is mapped to the affine space. For each point in the three-dimensional space, add aTo represent a point in affine space, that is. Let the point undergo a certain affine transformationlocated after, then it is written in the form of matrix multiplication:
Basic transformation forms include translation, rotation, scaling (mirror), and shearing. All transformations are based on the actual coordinates of the entity.
Pan
Assume that any point on the display entityexist、、Axis translation respectively、、get points after,but
Then the translation matrixfor
rotate
There are three ways of rotation, namely aroundaxis, aroundaxis and windingaxis rotation. to go aroundaxis rotationFor example, assume that the entity has a pointand entity anchor pointThe straight line formed byThe angle between the axes is,makeThe modulus is, then there is
aroundaxis rotationget, at this time there is
So there is
Convert it to an affine matrix and get
In the same way, aroundaxis rotationThe matrix form of
aroundaxis rotationThe matrix form of
Zoom
Assume that any point on the display entityalong、、Axis scaled separately、、Get points after doubling,but
Then the scaling matrixfor
like, it is uniform scaling; otherwise, it is non-uniform scaling.
mirror
For a scaling matrix, in particular, if、、If at least one of the three is negative, a mirror transformation will be performed. Negative scaling factors invert the coordinate system on the corresponding axis and change the direction of the surface normal, resulting in concave rendering. \
\
If you display any point on the entityalongAxis mirroring, no changes in other directions, easy to get the mirror matrix
in. The same principle can be followedaxis mirror, alongaxis mirror matrix、. Mirror transformations in multiple directions also It is easy to derive, for example, inaxis,axis, andThe matrix required to apply mirror transformation simultaneously in the axis direction (,,)for
cut
The shear transformation moves all points on the entity in a certain direction. The distance of any point on the straight line passing through the origin in that direction changes linearly with the distance between the straight line and the origin, which makes the image tilt. A shear transformation occurs in a plane composed of two orthogonal coordinate axes, with shearing in one direction and no transformation in the other direction. There are six pairs of orthogonal relationships between coordinate axes in the three-dimensional coordinate system, so there are six elementary shear transformations.
As shown in the figure, when the image is sheared in one direction, it actually has a shearing angle with the other direction., subscript () represents theCut in the direction and matchThe direction is at a certain shear angle. If the horizontal direction in the figure isaxis, the longitudinal direction isaxis, the shear angle is recorded as, obviously there are
butMake shear in the axial direction and connect it withThe matrix required for a certain shear angle in the axis directionfor
In the same way, the matrices required for the other six shear transformations can be derived. When the direction of the shear transformation isaxis, elementmust be located in the first row of the matrix,The axis is the second row,The axis is the third row; the direction at a shear angle to the transformation direction isaxis, elementMust be in the first column,The axis is the second column,The axis is the third column. For example, a clipping transformation inaxis direction, andThe axis direction is at a shear angle, thenLocated in the third row and first column.
The shear matrices described above only transform in one direction and form a certain shear angle with the other direction. If multiple different shear transformations are applied at the same time and the elements are filled in using the above rules, the shear matrix can be recorded as
If the shear transformation in a certain direction is not used, the corresponding position in the matrix will bewritten asThat’s it.
Combined transformation
One transformation may not suffice, and sometimes multiple transformations need to be applied simultaneously to represent complex transformations. For a finite number of affine transformations、、……, apply them to one point in turn, then the point obtained after transformationfor
Note that matrix multiplication follows the operation rules from right to left and does not support commutative law, but supports associative law, so there is
make,but,inis the combined transformation matrix. The order of various transformations in a combined transformation is very important, as the previous transformation may affect the result of the next transformation.
The matrices used in tagtransformation are all combined transformation matrices.
Application examples
Modify a block to display the NBT data of the entity so that it flows around theaxis rotation, aroundaxis rotation, aroundaxis rotation。 Find the combined transformation matrix, paying attention to the calculation from right to left:
Therefore, command should be
data merge entity @e[type=block_display,limit=1] {transformation:[-0.35f,-0.71f,0.61f,0.0f,0.87f,0.0f,0.5f,0.0f,-0.35f,0.71f,0.61f,0.0f,0.0f,0.0f,0.0f,1.0f]}Decomposed form
for theseaffine transformation matrix of size, whose elements、、is always 0,is always 1, if not 1, the entire matrix isscaling, so thatis 1. It can be written in blocks as follows:
The block array in the formulaIt's the upper left cornerArea, this area represents the linear transformation of the model, and stores all linear transformation data including rotation, scaling, mirroring and shearing. Note that this block array is not suitable for translation transformation, because translation transformation is not a linear transformation. And the block arrayThe three elements of are used only by translation transformations.
The transformation field in decomposed form is a block arrayData used after singular value decomposition. For any square matrix of order 3, there is always a third-order orthogonal square matrixand, 3rd order diagonal matrix,have
In the formula:
--matrixthe transposed matrix.
sayis the left singular vector matrix,is the right singular vector matrix, diagonal matrixThe three elements on the middle diagonal are called singular values. The calculation method of singular value decomposition is introduced below.
Taking the transposed matrix on the left and right sides of the equal sign in the above equation, we get
Because of the square matrixandis orthogonal, therefore、. then there is
Transform the above formula:
phalanxis a real symmetric matrix, obviously the above formula describes theSimilar diagonalization process, where, the orthogonal matrix used is the left singular vector matrix. If you remember、、yesThe three eigenvalues of , these eigenvalues are non-negative, readers can prove by themselves, so we have
find outThe diagonal matrix can be obtained by the three eigenvalues of. therefore,andThe solution steps are as follows——
Step 1:
From the characteristic equationbegAll eigenvalues of, and then find the diagonal matrix。
Step 2:
For each eigenvalue, by the system of equationsFind the corresponding feature vector。
Step 3:
If the obtained eigenvectors are not orthogonal to each other, then for the eigenvectorsPerform orthogonalization, and record the vector after orthogonalization as。
Step 4:
If the vector obtainedIf there is no unitization, then unitize it as,make. Calculation completed.
For the right singular vector matrix,have
In the same way, the right singular vector matrix can be obtained. The calculation steps are the same as the above steps for calculating the left singular vector matrix, whereIt is the same matrix as above, so there is no need to repeat the calculation. likereversible, then
In this way, the right singular vector matrix can be directly obtained without performing diagonalization calculations.。
The results of matrix singular value decomposition have geometric meaning, where、is the rotation transformation matrix,is the scaling transformation matrix. Any transformation can be decomposed into four processes: initial rotation transformation, scaling transformation, second rotation transformation and translation transformation. Therefore, useRepresents the initial rotation transformation, useRepresents scaling transformation, useRepresents another rotation transformation, and then introduces a translation vector on this basis, then we can get the transformation matrixThe decomposed form of , at this time the field transformation is a composite tag:
transformation:root tagright_rotation:The model performs rotation transformation before scaling transformation, that is, the first rotation transformation. Related to V in singular value decomposition. There are two available data forms: axial angle form and quaternion form. You can use axial angle form when writing, but when storing data, it will always be converted into quaternion form.scale:The scaling transformation of the model, related to ∑ in singular value decomposition. Use three-dimensional vectors.left_rotation:The rotation transformation after the model is scaled and transformed, that is, rotated again, is related to U in singular value decomposition. There are also two expression methods: axis-angle form and quaternion form. You can use axial angle form when writing, but when storing data, it will always be converted into quaternion form.translation:The translation transformation T of the model. Corresponds to the elements in the first three rows of the last column of the matrix form. Use three-dimensional vectors.
For the two fields right_rotationandleft_rotation, there are two data forms representing rotation: axis angle form and quaternion form. These two data forms are introduced below:
axial angle
Angular rotation can be understood as: a vectorAround an axis of length 1 passing through the origin (i.e. the actual position of the entity)rotation angleget vector. At this time there is。
For the convenience of analysis, the vectordecomposed into parallel to the axisvector ofand orthogonal to the axisvector of, so there is
WillUse containingandThe formula expression of , that is, calculatingexistProjection on:
So we can getexpression
for vectors, which can also be decomposed to get
In fact, in the vectorDuring the rotation process, the vectorNo changes occurred, i.e.

Now consider the vectorof rotation. It is not difficult to find that the rotation of the vector actually occurs on the circumference. At this time, it is orthogonal toThere are no other available axes in the plane of the axis, for which construction is simultaneously orthogonal toandaxis,have
Depend on
Knowandare equal, so the vectorcan be decomposed into parallelofand parallel toof,have
so get
When using axial angle to represent rotation, the fields right_rotationandleft_rotation are composite tags:
xxx_rotation:left_rotation or right_rotationangle:The angle of rotation around the axis, that is, the θ angle, in the angle system.axis:An ordered array of three elements used to define the rotation axis vector uu. Generally it can be written as a unit vector.
Quaternion form
When using quaternion form to represent rotation, the fields right_rotationandleft_rotation types are lists, and the data format is:
left_rotation: or right_rotation:: Represents the four elements of the quaternion, in order x, y, z, w.(list element)An element in a quaternion
All quaternions can be written in the following form:
in、、、,sayis a quaternionThe imaginary part ofFor the real part. Generally, vectors can be usedto represent a quaternion, or totreated as a vector, representing quaternions in scalar and vector form. The modulus of a quaternion is, stipulation: when, the quaternion is a unit quaternion. At the same time, there is also a provision: whenWhen , the quaternion can be called a pure quaternion.
For the rotation axis and vector in the axis-angle formula, it can be written in the form of pure quaternions, such as、. So there are:
The rotation can be expressed as
Iftreated as a quaternion,Right now, then we can get
Notice that the quaternion q above has the following properties:
This is a unit quaternion. Quaternions generally used for rotation transformation are unit quaternions. \emphasize{Non-unit quaternions will cause the model to be scaled while rotating}. So the vector rotation expressed in quaternion form is
make,in,but
In the formula: ——QuaternionsThe conjugate of,but。
As a result, the rotation formula expressed in quaternion form is obtained:
in. Each element in this quaternion is,,,
In the formula:
——Around the axisThe angle of rotation, the direction is counterclockwise.
——Rotation axison the coordinate axison the weight.
For a rendering transformation, let the quaternion used for its initial rotation be, the quaternion used to rotate again is, let the scaled data, shift data. Display any point on the entityConstruct quaternions
Perform the first rotation and get
Then applying the scaling transformation, we get
Under the combined action of the initial rotation and scaling transformation, the relative position of each point in the model will change. Only when rotating the quaternion for the first time(no rotation occurs) or scale the data(without scaling), the model will not deform. After that, the model will determine the final rotation angle based on another rotation transformation, and we get
Finally, a translation transformation is applied to determine the final position of the model to obtain the pointFinal position:
Application examples
Use block to display entity to display a glass. Requirement: Generate this display entity so that the diagonal line of the glass body is equal toThe axes are parallel. Rotate the display entity diagonally around the body, taking 4 seconds to rotate once.
The diagonal line of the body in the model starts fromarrive, now we need to make the model transform without deformingtransformed into(axis direction vector) parallel. It is now possible to directly determine the quaternion used to rotate again, the quantity to be determined is the rotation angleand axis of rotation。
Calculate the angle of rotation: convertUnitized, we get,therefore
The axis of rotation is perpendicular to the vector before and after rotation, we have
convert it into units. If the axis-angle formula is used, the data of left_rotation is:
left_rotation:{angle: 54.74f, axis: [-0.71f, 0.0f, 0.71f]}Compute rotated quaternion again
The model does not require initial rotation, scaling and translation, so,,. The transformation field in decomposed form is:
transformation:{right_rotation: [0.0f, 0.0f, 0.0f, 1.0f], scale: [1.0f, 1.0f, 1.0f], left_rotation: [-0.33f, 0.0f, 0.33f, 0.89f], translation: [1.0f, 1.0f, 1.0f]}The commands required to generate this display entity are:
summon block_display ~ ~ ~ {block_state:{Name:"minecraft:glass"},transformation:{right_rotation:[0.0f,0.0f,0.0f,1.0f],scale:[1.0f,1.0f,1.0f],left_rotation:[-0.33f,0.0f,0.33f,0.89f],translation:[1.0f,1.0f,1.0f]}}The value of the field left_rotationhas been defined. The rotation animation is completed by interpolation and can be defined byright_rotation. The quantity to be determined is still the rotation angle.and axis of rotation. Obviously the axis of rotation is the body diagonal of the block model. At this time, the body diagonal is equal toThe axes are parallel, but the transformation is still based on the local coordinate of the model, so the axis vector is, unitized into. When making interpolation animations, you can set four fixed rotation angles:、、、, so that the model is cyclically transformed in this order, and the duration of each interpolation isSecondgt。
byFor example, if the axis-angle formula is used, the data of right_rotation is
right_rotation: {angle: 90, axis: [0.58f, 0.58f, 0.58f]}Convert to quaternion form,Right now
right_rotation:[0.41f,0.41f,0.41f,0.71f]Same reason、、The data are respectively
right_rotation:[0.58f,0.58f,0.58f,0.0f]}right_rotation:[0.41f,0.41f,0.41f,-0.71f]}right_rotation:[0.0f,0.0f,0.0f,1.0f]}In order to smoothly transition the rotation angle of the model to, the command for interpolation animation is
data merge entity @n[type=block_display] {transformation:{right_rotation:[0.41f,0.41f,0.41f,0.71f]},interpolation_duration:20}After the command is executed, apply the command block circuit or function plan so that after 20gt, when the defined interpolation animation ends, the rotation angle of the model begins to smoothly transition to:
data merge entity @n[type=block_display] {transformation:{right_rotation:[0.58f,0.58f,0.58f,0.0f]},interpolation_duration:20}After 20gt, a smooth transition begins to:
data merge entity @n[type=block_display] {transformation:{right_rotation:[0.41f,0.41f,0.41f,-0.71f]},interpolation_duration:20}After 20gt, a smooth transition begins to:
data merge entity @n[type=block_display] {transformation:{right_rotation:[0.0f,0.0f,0.0f,1.0f]},interpolation_duration:20}After 20gt, a smooth transition begins to, forming a cycle. If the command is executed in the command block circuit, a clock circuit with a period of 80gt can be manufactured. There needs to be a delay of 20gt between each command block, and at least 5 repeaters need to be used. If the command is executed in the function, four functions 90.mcfunction, 180.mcfunction, 270.mcfunction, and 0.mcfunctioncan be created in the directorydata\minecraft\function\animation. For example, the content of function90.mcfunction could look like this:
data merge entity @n[type=block_display] {transformation:{right_rotation:[0.41f,0.41f,0.41f,0.71f]},interpolation_duration:20}
schedule function minecraft:animation/180 20tReferences
[1] https://zh.minecraft.wiki/w/展示实体
[2] https://krasjet.github.io/quaternion/quaternion.pdf
[3] https://blog.csdn.net/YiYeZhiNian/article/details/106750302
[4] https://zhuanlan.zhihu.com/p/45404840
[5] https://zhuanlan.zhihu.com/p/183973440
