Skip to content

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.

Cover

Research on displaying entity rendering transformations

Xu Muxian

Xu Muxian

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 a4×4The 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(x0,y0,z0), add a1To represent a point in affine space, that is(x0,y0,z0,1). Let the point undergo a certain affine transformationAlocated after(x,y,z,1), then it is written in the form of matrix multiplication:

[xyz1]=[a11a12a13a14a21a22a23a24a31a32a33a34a41a42a43a44][x0y0z01]

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 entity(x0,y0,z0,1)existxyzAxis translation respectivelyabcget points after(x,y,z,1),but

{x=x0+ay=y0+bz=z0+c1=1

Then the translation matrixTfor

T(a,b,c)=[100a010b001c0001]

rotate

There are three ways of rotation, namely aroundxaxis, aroundyaxis and windingzaxis rotation. to go aroundxaxis rotationαFor example, assume that the entity has a pointAand entity anchor pointOThe straight line formed byzThe angle between the axes isφ,makeOAThe modulus isl, then there is

{x=xy=lcosφz=lsinφ

OAaroundxaxis rotationαgetOA, at this time there is

{x=xy=lcos(φ+α)=lcosφcosαlsinφsinαz=lsin(φ+α)=lsinφcosα+lcosφsinα

So there is

{x=xy=ycosαzsinαz=ysinα+zcosα

Convert it to an affine matrix and get

Rx(α)=[10000cosαsinα00sinαcosα00001]

In the same way, aroundyaxis rotationβThe matrix form of

Ry(β)=[cosβ0sinβ00100sinβ0cosβ00001]

aroundzaxis rotationγThe matrix form of

Rz(γ)=[cosγsinγ00sinγcosγ0000100001]

Zoom

Assume that any point on the display entity(x0,y0,z0,1)alongxyzAxis scaled separatelymnpGet points after doubling(x,y,z,1),but

{x=mx0y=ny0z=pz01=1

Then the scaling matrixSfor

S(m,n,p)=[m0000n0000p00001]

likem=n=p, it is uniform scaling; otherwise, it is non-uniform scaling.

mirror

For a scaling matrix, in particular, ifmnpIf 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. \

Concave rendering caused by mirror transformation\

If you display any point on the entity(x0,y0,z0,1)alongxAxis mirroring, no changes in other directions, easy to get the mirror matrix

Mx(m)=[m000010000100001]

inm<0. The same principle can be followedyaxis mirror, alongzaxis mirror matrixMy(n)Mz(p). Mirror transformations in multiple directions also It is easy to derive, for example, inxaxis,yaxis, andzThe matrix required to apply mirror transformation simultaneously in the axis direction (m<0n<0p<0)for

Mx,y,z(m,n,p)=[m0000n0000p00001]

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.

Cut Transform

As shown in the figure, when the image is sheared in one direction, it actually has a shearing angle with the other direction.θi,j, subscript (i,j) represents theiCut in the direction and matchjThe direction is at a certain shear angle. If the horizontal direction in the figure isxaxis, the longitudinal direction isyaxis, the shear angle is recorded asθx,y, obviously there are

{x=x0+y0tanθx,yy=y0z=z01=1

butxMake shear in the axial direction and connect it withyThe matrix required for a certain shear angle in the axis directionHfor

H(θx,y)=[1tanθx,y00010000100001]

In the same way, the matrices required for the other six shear transformations can be derived. When the direction of the shear transformation isxaxis, elementtanθi,jmust be located in the first row of the matrix,yThe axis is the second row,zThe axis is the third row; the direction at a shear angle to the transformation direction isxaxis, elementtanθi,jMust be in the first column,yThe axis is the second column,zThe axis is the third column. For example, a clipping transformation inzaxis direction, andxThe axis direction is at a shear angle, thentanθz,xLocated 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

H(θx,y,θx,z,θy,x,θy,z,θz,x,θz,y)=[1tanθx,ytanθx,z0tanθy,x1tanθy,z0tanθz,xtanθz,y100001]

If the shear transformation in a certain direction is not used, the corresponding position in the matrix will betanθi,jwritten as0That’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 transformationsA1A2、……An, apply them to one point in turnx, then the point obtained after transformationxfor

x=AnAn1A2A1x

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

x=(AnAn1A2A1)x

makeA=AnAn1A2A1,butx=Ax,inAis 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 theyaxis rotation30, aroundxaxis rotation45, aroundzaxis rotation90。 Find the combined transformation matrix, paying attention to the calculation from right to left:

A=Rz(90)Rx(45)Ry(30)=[cos90sin9000sin90cos900000100001][10000cos45sin4500sin45cos4500001][cos300sin3000100sin300cos3000001]=[242264032012024226400001][0.350.710.6100.8700.500.350.710.6100001]

Therefore, command should be

mcfunction
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 these4×4affine transformation matrix of sizeA, whose elementsa41a42a43is always 0,a44is always 1, if not 1, the entire matrix is1a44scaling, so thata44is 1. It can be written in blocks as follows:

A=[a11a12a13a14a21a22a23a24a31a32a33a34a41a42a43a44]=[B3×3T3×1O1×3E1×1]

The block array in the formulaBIt's the upper left corner3×3Area, 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 arrayTThe three elements of are used only by translation transformations.
The transformation field in decomposed form is a block arrayBData used after singular value decomposition. For any square matrix of order 3B, there is always a third-order orthogonal square matrixUandV, 3rd order diagonal matrixΣ,have

B=UΣVT

In the formula:
VT--matrixVthe transposed matrix.
sayUis the left singular vector matrix,Vis the right singular vector matrix, diagonal matrixΣThe 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

BT=VΣUT

Because of the square matrixUandVis orthogonal, thereforeVTV=EUTU=E. then there is

BBT=UΣVTVΣUT=UΣ2UT

Transform the above formula:

UT(BBT)U=Σ2

phalanxBBTis a real symmetric matrix, obviously the above formula describes theBBTSimilar diagonalization process, whereΣ=[σ1σ2σ3], the orthogonal matrix used is the left singular vector matrixU. If you rememberλ1λ2λ3yesBBTThe three eigenvalues ​​of , these eigenvalues ​​are non-negative, readers can prove by themselves, so we have

Σ2=[λ1λ2λ3]=[σ12σ22σ32]

find outBBTThe diagonal matrix can be obtained by the three eigenvalues ​​ofΣ. therefore,ΣandUThe solution steps are as follows——
Step 1:
From the characteristic equation|λEBBT|=0begBBTAll eigenvalues ​​ofλi, and then find the diagonal matrixΣ=diag(σ1,σ2,σ3)=diag(λ1,λ2,λ3)
Step 2:
For each eigenvalueλi, by the system of equations(λiEBBT)x=0Find the corresponding feature vectorαi
Step 3:
If the obtained eigenvectors are not orthogonal to each other, then for the eigenvectorsαiPerform orthogonalization, and record the vector after orthogonalization asβi
Step 4:
If the vector obtainedβiIf there is no unitization, then unitize it asγi,makeU=[γ1,γ2,γ3]. Calculation completed.
For the right singular vector matrixV,have

BTB=VΣUTUΣVT=VΣ2VT

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, whereΣIt is the same matrix as above, so there is no need to repeat the calculation. likeBreversible, then

V=B1UΣ

In this way, the right singular vector matrix can be directly obtained without performing diagonalization calculations.V
The results of matrix singular value decomposition have geometric meaning, whereUVis 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, useVRepresents the initial rotation transformation, useΣRepresents scaling transformation, useURepresents another rotation transformation, and then introduces a translation vector on this basisT, then we can get the transformation matrixAThe decomposed form of , at this time the field transformation is a composite tag:

compoundtransformation:root tag
  • compoundhomolistright_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.
  • homolistscale:The scaling transformation of the model, related to ∑ in singular value decomposition. Use three-dimensional vectors.
  • compoundhomolistleft_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.
  • homolisttranslation: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 vectorvAround an axis of length 1 passing through the origin (i.e. the actual position of the entity)urotation angleθget vectorv. At this time there isu=1
Axis angle rotation diagram
For the convenience of analysis, the vectorvdecomposed into parallel to the axisuvector ofvand orthogonal to the axisuvector ofv, so there is

v=v+v

Decomposition of vector v
WillvUse containingvanduThe formula expression of , that is, calculatingvexistuProjection on:

v=vuu=(uv)uuu=(uv)u

So we can getvexpression

v=vv=v(uv)u

for vectorsv, which can also be decomposed to get

v=v+v

In fact, in the vectorvDuring the rotation process, the vectorvNo changes occurred, i.e.

v=v

Rotation of vector v⊥
Now consider the vectorvof rotation. It is not difficult to find that the rotation of the vector actually occurs on the circumference. At this time, it is orthogonal touThere are no other available axes in the plane of the axis, for which construction is simultaneously orthogonal touandvaxisw,have

w=u×v

Depend on

w=u×v=uvsin90=v

Knowwandvare equal, so the vectorvcan be decomposed into parallelwofvwand parallel tovofvv,have

v=vw+vv=wsinθ+vcosθ=(u×v)sinθ+vcosθ

so get

v=v+v=v+(u×v)sinθ+vcosθ=v+[u×(vv)]sinθ+vcosθ=v+(u×v)sinθ+vcosθ=(uv)u+(u×v)sinθ+[vv=v(uv)u]cosθ=(uv)u(1cosθ)+(u×v)sinθ+vcosθ

When using axial angle to represent rotation, the fields right_rotationandleft_rotation are composite tags:

compoundxxx_rotation:left_rotation or right_rotation
  • floatangle:The angle of rotation around the axis, that is, the θ angle, in the angle system.
  • homolistaxis: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:

homolistleft_rotation: or homolistright_rotation:: Represents the four elements of the quaternion, in order x, y, z, w.
  • float(list element)An element in a quaternion

All quaternions can be written in the following form:

q=w+xi+yj+zk

inwxyzR,sayxi+yj+zkis a quaternionqThe imaginary part ofwFor the real part. Generally, vectors can be usedq=(w,x,y,z)to represent a quaternion, or to(x,y,z)treated as a vectorv, representing quaternions in scalar and vector formq=(w,v). The modulus of a quaternion isq=w2+x2+y2+z2, stipulation: whenq=1, the quaternion is a unit quaternion. At the same time, there is also a provision: whenw=0When , 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 asu=(0,u)v=(0,v). So there are:

v=v+vv=v+v

vThe rotation can be expressed as

v=v

If(usinθ+cosθ)treated as a quaternionq,Right nowq=(cosθ,usinθ), then we can get

v=qv

Notice that the quaternion q above has the following properties:

q=cos2θ+usinθusinθ=cos2θ+u2sin2θ=1

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

v=v+v=v+qv

makeq=p2,inp=(cosθ2,usinθ2),but

v=v+qv=ppv+p2v=pvp+pvp=p(v+v)p=pvp

In the formula: p——QuaternionspThe conjugate ofp=(w,v),butp=(w,v)
As a result, the rotation formula expressed in quaternion form is obtained:

v=qvq

inq=(cosθ2,usinθ2). Each element in this quaternion isw=cosθ2x=uxsinθ2y=uysinθ2z=uzsinθ2
In the formula:
θ——Around the axisuThe angle of rotation, the direction is counterclockwise.
ui——Rotation axisuon the coordinate axision the weight.
For a rendering transformation, let the quaternion used for its initial rotation beqr, the quaternion used to rotate again isql, let the scaled datas=(sx,sy,sz), shift datat=(tx,ty,tz). Display any point on the entityA(x0,y0,z0)Construct quaternions

q0=x0i+y0j+z0k=(0,OA)

Perform the first rotation and get

q1=qrq0qr

Then applying the scaling transformation, we get

q2=sxq1xi+syq1yj+szq1zk

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 timeqr=(1,0)(no rotation occurs) or scale the datas=(1,1,1)(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

q3=qlq2ql

Finally, a translation transformation is applied to determine the final position of the model to obtain the pointAFinal position:

q=q3+t

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 toyThe 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 fromO(0,0,0)arriveA(1,1,1), now we need to make the model transform without deformingOAtransformed into(0,1,0)yaxis direction vector) parallel. It is now possible to directly determine the quaternion used to rotate againql, the quantity to be determined is the rotation angleθand axis of rotationu
Calculate the angle of rotation: convertOAUnitized, we get(13,13,13),therefore

θ=arccos[(13,13,13)(0,1,0)]=arccos1354.74

The axis of rotation is perpendicular to the vector before and after rotation, we have

u=(13,13,13)×(0,1,0)=(13,0,13)

convert it into units(12,0,12). If the axis-angle formula is used, the data of left_rotation is:

snbt
left_rotation:{angle: 54.74f, axis: [-0.71f, 0.0f, 0.71f]}

Compute rotated quaternion again

q=(cosθ2,uxsinθ2,uysinθ2,uzsinθ2)(0.89,0.33,0,0.33)

The model does not require initial rotation, scaling and translation, soqr=(1,0,0,0)s=(1,1,1)t=(0,0,0). The transformation field in decomposed form is:

snbt
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:

mcfunction
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 rotationu. Obviously the axis of rotation is the body diagonal of the block model. At this time, the body diagonal is equal toyThe axes are parallel, but the transformation is still based on the local coordinate of the model, so the axis vector is(1,1,1), unitized into(13,13,13). When making interpolation animations, you can set four fixed rotation angles:090180270, so that the model is cyclically transformed in this order, and the duration of each interpolation is4÷4=1Second=20gt。
byθ=90For example, if the axis-angle formula is used, the data of right_rotation is

snbt
right_rotation: {angle: 90, axis: [0.58f, 0.58f, 0.58f]}

Convert to quaternion formq(0.71,0.41,0.41,0.41),Right now

snbt
right_rotation:[0.41f,0.41f,0.41f,0.71f]

Same reasonθ=180θ=270θ=0The data are respectively

snbt
right_rotation:[0.58f,0.58f,0.58f,0.0f]}
snbt
right_rotation:[0.41f,0.41f,0.41f,-0.71f]}
snbt
right_rotation:[0.0f,0.0f,0.0f,1.0f]}

In order to smoothly transition the rotation angle of the model toθ=90, the command for interpolation animation is

mcfunction
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θ=180

mcfunction
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θ=270

mcfunction
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θ=0

mcfunction
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θ=90, 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:

mcfunction
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 20t

References

[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

Powered by VitePress and GitHub Pages