6
6
import net .fabricmc .api .EnvType ;
7
7
import net .fabricmc .api .Environment ;
8
8
import net .minecraft .client .MinecraftClient ;
9
- import net .minecraft .client .gl .ShaderProgramKeys ;
9
+ import net .minecraft .client .gl .GlUsage ;
10
+ import net .minecraft .client .gl .VertexBuffer ;
10
11
import net .minecraft .client .render .*;
12
+ import net .minecraft .client .texture .TextureManager ;
13
+ import net .minecraft .client .util .BufferAllocator ;
11
14
import net .minecraft .util .Identifier ;
12
15
import net .minecraft .util .math .RotationAxis ;
13
16
import org .joml .Matrix4f ;
@@ -19,6 +22,7 @@ public class VistasCubemapRenderer {
19
22
20
23
private static final int FACES_COUNT = 6 ;
21
24
private final Identifier [] faces = new Identifier [FACES_COUNT ];
25
+ private final VertexBuffer [] vertexBuffers = new VertexBuffer [FACES_COUNT ];
22
26
23
27
private final Cubemap cubemap ;
24
28
@@ -32,90 +36,116 @@ public VistasCubemapRenderer(Cubemap cubemap) {
32
36
}
33
37
34
38
public void draw (MinecraftClient client , float alpha ) {
35
- Tessellator tessellator = Tessellator .getInstance ();
36
- Matrix4f matrix4f = new Matrix4f ().setPerspective ((float ) Math .toRadians (this .cubemap .getVisualControl ().getFov ()), (float ) client .getWindow ().getFramebufferWidth () / (float ) client .getWindow ().getFramebufferHeight (), 0.05F , 100.0F );
39
+ if (this .vertexBuffers [0 ] == null ) {
40
+ this .computeVertexBuffers (alpha );
41
+ }
42
+
43
+ Matrix4f matrix4f = new Matrix4f ().setPerspective ((float ) Math .toRadians (this .cubemap .getVisualControl ().getFov ()), (float ) client .getWindow ().getFramebufferWidth () / (float ) client .getWindow ().getFramebufferHeight (), 0.05F , 10.0F );
37
44
RenderSystem .backupProjectionMatrix ();
38
45
RenderSystem .setProjectionMatrix (matrix4f , ProjectionType .PERSPECTIVE );
39
46
Matrix4fStack matrixStack = RenderSystem .getModelViewStack ();
40
47
matrixStack .pushMatrix ();
41
48
matrixStack .rotationX ((float ) Math .PI );
42
- RenderSystem .setShader (ShaderProgramKeys .POSITION_TEX_COLOR );
43
- RenderSystem .enableBlend ();
44
- RenderSystem .disableCull ();
45
- RenderSystem .depthMask (false );
46
-
47
- int r = Math .round ((float ) this .cubemap .getVisualControl ().getColorR ());
48
- int g = Math .round ((float ) this .cubemap .getVisualControl ().getColorG ());
49
- int b = Math .round ((float ) this .cubemap .getVisualControl ().getColorB ());
50
- int a = Math .round ((float ) this .cubemap .getVisualControl ().getColorA () * alpha );
51
-
52
- float w = (float ) this .cubemap .getVisualControl ().getWidth () / 2.0f ;
53
- float h = (float ) this .cubemap .getVisualControl ().getHeight () / 2.0f ;
54
- float d = (float ) this .cubemap .getVisualControl ().getDepth () / 2.0f ;
55
49
56
50
matrixStack .pushMatrix ();
57
51
matrixStack .translate ((float ) this .cubemap .getVisualControl ().getAddedX (), (float ) this .cubemap .getVisualControl ().getAddedY (), (float ) this .cubemap .getVisualControl ().getAddedZ ());
58
52
matrixStack .rotate (RotationAxis .POSITIVE_X .rotationDegrees ((float ) this .cubemap .getRotationControl ().getPitch (cubemap .getRotationControl ().isFrozen () ? 0.0D : time )));
59
53
matrixStack .rotate (RotationAxis .POSITIVE_Y .rotationDegrees ((float ) this .cubemap .getRotationControl ().getYaw (cubemap .getRotationControl ().isFrozen () ? 0.0D : time )));
60
54
matrixStack .rotate (RotationAxis .POSITIVE_Z .rotationDegrees ((float ) this .cubemap .getRotationControl ().getRoll (cubemap .getRotationControl ().isFrozen () ? 0.0D : time )));
61
55
62
- for (int n = 0 ; n < 6 ; ++n ) {
63
- RenderSystem .setShaderTexture (0 , this .faces [n ]);
64
- BufferBuilder bufferBuilder = tessellator .begin (VertexFormat .DrawMode .QUADS , VertexFormats .POSITION_TEXTURE_COLOR );
65
-
66
- if (n == 0 ) {
67
- bufferBuilder .vertex (-w , -h , d ).texture (0.0F , 0.0F ).color (r , g , b , a );
68
- bufferBuilder .vertex (-w , h , d ).texture (0.0F , 1.0F ).color (r , g , b , a );
69
- bufferBuilder .vertex (w , h , d ).texture (1.0F , 1.0F ).color (r , g , b , a );
70
- bufferBuilder .vertex (w , -h , d ).texture (1.0F , 0.0F ).color (r , g , b , a );
71
- }
72
-
73
- if (n == 1 ) {
74
- bufferBuilder .vertex (w , -h , d ).texture (0.0F , 0.0F ).color (r , g , b , a );
75
- bufferBuilder .vertex (w , h , d ).texture (0.0F , 1.0F ).color (r , g , b , a );
76
- bufferBuilder .vertex (w , h , -d ).texture (1.0F , 1.0F ).color (r , g , b , a );
77
- bufferBuilder .vertex (w , -h , -d ).texture (1.0F , 0.0F ).color (r , g , b , a );
78
- }
79
-
80
- if (n == 2 ) {
81
- bufferBuilder .vertex (w , -h , -d ).texture (0.0F , 0.0F ).color (r , g , b , a );
82
- bufferBuilder .vertex (w , h , -d ).texture (0.0F , 1.0F ).color (r , g , b , a );
83
- bufferBuilder .vertex (-w , h , -d ).texture (1.0F , 1.0F ).color (r , g , b , a );
84
- bufferBuilder .vertex (-w , -h , -d ).texture (1.0F , 0.0F ).color (r , g , b , a );
85
- }
86
-
87
- if (n == 3 ) {
88
- bufferBuilder .vertex (-w , -h , -d ).texture (0.0F , 0.0F ).color (r , g , b , a );
89
- bufferBuilder .vertex (-w , h , -d ).texture (0.0F , 1.0F ).color (r , g , b , a );
90
- bufferBuilder .vertex (-w , h , d ).texture (1.0F , 1.0F ).color (r , g , b , a );
91
- bufferBuilder .vertex (-w , -h , d ).texture (1.0F , 0.0F ).color (r , g , b , a );
92
- }
56
+ for (int pass = 0 ; pass < 4 ; ++pass ) {
57
+ matrixStack .pushMatrix ();
58
+ float xOffset = ((float )(pass % 2 ) / 2.0F - 0.5F ) / 256.0F ;
59
+ float yOffset = ((float )(pass / 2 ) / 2.0F - 0.5F ) / 256.0F ;
60
+ matrixStack .translate (xOffset , yOffset , 0.0F );
61
+ RenderSystem .setShaderColor (1.0F , 1.0F , 1.0F , alpha / (float )(pass + 1 ));
93
62
94
- if (n == 4 ) {
95
- bufferBuilder .vertex (-w , -h , -d ).texture (0.0F , 0.0F ).color (r , g , b , a );
96
- bufferBuilder .vertex (-w , -h , d ).texture (0.0F , 1.0F ).color (r , g , b , a );
97
- bufferBuilder .vertex (w , -h , d ).texture (1.0F , 1.0F ).color (r , g , b , a );
98
- bufferBuilder .vertex (w , -h , -d ).texture (1.0F , 0.0F ).color (r , g , b , a );
63
+ for (int face = 0 ; face < FACES_COUNT ; ++face ) {
64
+ this .vertexBuffers [face ].bind ();
65
+ this .vertexBuffers [face ].draw (RenderLayer .getPanorama (this .faces [face ]));
99
66
}
100
67
101
- if (n == 5 ) {
102
- bufferBuilder .vertex (-w , h , d ).texture (0.0F , 0.0F ).color (r , g , b , a );
103
- bufferBuilder .vertex (-w , h , -d ).texture (0.0F , 1.0F ).color (r , g , b , a );
104
- bufferBuilder .vertex (w , h , -d ).texture (1.0F , 1.0F ).color (r , g , b , a );
105
- bufferBuilder .vertex (w , h , d ).texture (1.0F , 0.0F ).color (r , g , b , a );
106
- }
107
-
108
- BufferRenderer .drawWithGlobalProgram (bufferBuilder .end ());
68
+ VertexBuffer .unbind ();
69
+ matrixStack .popMatrix ();
70
+ RenderSystem .colorMask (true , true , true , false );
109
71
}
110
72
111
73
matrixStack .popMatrix ();
112
74
113
75
RenderSystem .colorMask (true , true , true , true );
76
+ RenderSystem .setShaderColor (1.0F , 1.0F , 1.0F , 1.0F );
114
77
RenderSystem .restoreProjectionMatrix ();
115
78
matrixStack .popMatrix ();
116
- RenderSystem .depthMask (true );
117
- RenderSystem .enableCull ();
118
- RenderSystem .enableDepthTest ();
79
+ }
80
+
81
+ private void computeVertexBuffers (float alpha ) {
82
+ int r = Math .round ((float ) this .cubemap .getVisualControl ().getColorR ());
83
+ int g = Math .round ((float ) this .cubemap .getVisualControl ().getColorG ());
84
+ int b = Math .round ((float ) this .cubemap .getVisualControl ().getColorB ());
85
+ int a = Math .round ((float ) this .cubemap .getVisualControl ().getColorA () * alpha );
86
+
87
+ float w = (float ) this .cubemap .getVisualControl ().getWidth () / 2.0f ;
88
+ float h = (float ) this .cubemap .getVisualControl ().getHeight () / 2.0f ;
89
+ float d = (float ) this .cubemap .getVisualControl ().getDepth () / 2.0f ;
90
+
91
+ try (BufferAllocator bufferAllocator = new BufferAllocator (VertexFormats .POSITION_TEXTURE_COLOR .getVertexSizeByte () * 4 )) {
92
+ for (int face = 0 ; face < FACES_COUNT ; ++face ) {
93
+ BufferBuilder bufferBuilder = new BufferBuilder (bufferAllocator , VertexFormat .DrawMode .QUADS , VertexFormats .POSITION_TEXTURE_COLOR );
94
+
95
+ if (face == 0 ) {
96
+ bufferBuilder .vertex (-w , -h , d ).texture (0.0F , 0.0F ).color (r , g , b , a );
97
+ bufferBuilder .vertex (-w , h , d ).texture (0.0F , 1.0F ).color (r , g , b , a );
98
+ bufferBuilder .vertex (w , h , d ).texture (1.0F , 1.0F ).color (r , g , b , a );
99
+ bufferBuilder .vertex (w , -h , d ).texture (1.0F , 0.0F ).color (r , g , b , a );
100
+ }
101
+
102
+ if (face == 1 ) {
103
+ bufferBuilder .vertex (w , -h , d ).texture (0.0F , 0.0F ).color (r , g , b , a );
104
+ bufferBuilder .vertex (w , h , d ).texture (0.0F , 1.0F ).color (r , g , b , a );
105
+ bufferBuilder .vertex (w , h , -d ).texture (1.0F , 1.0F ).color (r , g , b , a );
106
+ bufferBuilder .vertex (w , -h , -d ).texture (1.0F , 0.0F ).color (r , g , b , a );
107
+ }
108
+
109
+ if (face == 2 ) {
110
+ bufferBuilder .vertex (w , -h , -d ).texture (0.0F , 0.0F ).color (r , g , b , a );
111
+ bufferBuilder .vertex (w , h , -d ).texture (0.0F , 1.0F ).color (r , g , b , a );
112
+ bufferBuilder .vertex (-w , h , -d ).texture (1.0F , 1.0F ).color (r , g , b , a );
113
+ bufferBuilder .vertex (-w , -h , -d ).texture (1.0F , 0.0F ).color (r , g , b , a );
114
+ }
115
+
116
+ if (face == 3 ) {
117
+ bufferBuilder .vertex (-w , -h , -d ).texture (0.0F , 0.0F ).color (r , g , b , a );
118
+ bufferBuilder .vertex (-w , h , -d ).texture (0.0F , 1.0F ).color (r , g , b , a );
119
+ bufferBuilder .vertex (-w , h , d ).texture (1.0F , 1.0F ).color (r , g , b , a );
120
+ bufferBuilder .vertex (-w , -h , d ).texture (1.0F , 0.0F ).color (r , g , b , a );
121
+ }
122
+
123
+ if (face == 4 ) {
124
+ bufferBuilder .vertex (-w , -h , -d ).texture (0.0F , 0.0F ).color (r , g , b , a );
125
+ bufferBuilder .vertex (-w , -h , d ).texture (0.0F , 1.0F ).color (r , g , b , a );
126
+ bufferBuilder .vertex (w , -h , d ).texture (1.0F , 1.0F ).color (r , g , b , a );
127
+ bufferBuilder .vertex (w , -h , -d ).texture (1.0F , 0.0F ).color (r , g , b , a );
128
+ }
129
+
130
+ if (face == 5 ) {
131
+ bufferBuilder .vertex (-w , h , d ).texture (0.0F , 0.0F ).color (r , g , b , a );
132
+ bufferBuilder .vertex (-w , h , -d ).texture (0.0F , 1.0F ).color (r , g , b , a );
133
+ bufferBuilder .vertex (w , h , -d ).texture (1.0F , 1.0F ).color (r , g , b , a );
134
+ bufferBuilder .vertex (w , h , d ).texture (1.0F , 0.0F ).color (r , g , b , a );
135
+ }
136
+
137
+ this .vertexBuffers [face ] = new VertexBuffer (GlUsage .STATIC_WRITE );
138
+ this .vertexBuffers [face ].bind ();
139
+ this .vertexBuffers [face ].upload (bufferBuilder .end ());
140
+ VertexBuffer .unbind ();
141
+ }
142
+ }
143
+ }
144
+
145
+ public void registerTextures (TextureManager textureManager ) {
146
+ for (Identifier identifier : this .faces ) {
147
+ textureManager .registerTexture (identifier );
148
+ }
119
149
}
120
150
121
151
public Cubemap getCubemap () {
0 commit comments