/
AbstractQuadRenderer.java
252 lines (206 loc) · 9.32 KB
/
AbstractQuadRenderer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.impl.client.indigo.renderer.render;
import static net.fabricmc.fabric.impl.client.indigo.renderer.helper.GeometryHelper.LIGHT_FACE_FLAG;
import java.util.function.Function;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.WorldRenderer;
import net.minecraft.util.math.Vec3f;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Matrix3f;
import net.minecraft.util.math.Matrix4f;
import net.fabricmc.fabric.api.renderer.v1.render.RenderContext.QuadTransform;
import net.fabricmc.fabric.impl.client.indigo.renderer.aocalc.AoCalculator;
import net.fabricmc.fabric.impl.client.indigo.renderer.helper.ColorHelper;
import net.fabricmc.fabric.impl.client.indigo.renderer.helper.GeometryHelper;
import net.fabricmc.fabric.impl.client.indigo.renderer.mesh.MutableQuadViewImpl;
/**
* Base quad-rendering class for fallback and mesh consumers.
* Has most of the actual buffer-time lighting and coloring logic.
*/
public abstract class AbstractQuadRenderer {
static final int FULL_BRIGHTNESS = 0xF000F0;
protected final Function<RenderLayer, VertexConsumer> bufferFunc;
protected final BlockRenderInfo blockInfo;
protected final AoCalculator aoCalc;
protected final QuadTransform transform;
protected final Vec3f normalVec = new Vec3f();
protected abstract Matrix4f matrix();
protected abstract Matrix3f normalMatrix();
protected abstract int overlay();
AbstractQuadRenderer(BlockRenderInfo blockInfo, Function<RenderLayer, VertexConsumer> bufferFunc, AoCalculator aoCalc, QuadTransform transform) {
this.blockInfo = blockInfo;
this.bufferFunc = bufferFunc;
this.aoCalc = aoCalc;
this.transform = transform;
}
/** handles block color and red-blue swizzle, common to all renders. */
private void colorizeQuad(MutableQuadViewImpl q, int blockColorIndex) {
if (blockColorIndex == -1) {
for (int i = 0; i < 4; i++) {
q.spriteColor(i, 0, ColorHelper.swapRedBlueIfNeeded(q.spriteColor(i, 0)));
}
} else {
final int blockColor = blockInfo.blockColor(blockColorIndex);
for (int i = 0; i < 4; i++) {
q.spriteColor(i, 0, ColorHelper.swapRedBlueIfNeeded(ColorHelper.multiplyColor(blockColor, q.spriteColor(i, 0))));
}
}
}
/** final output step, common to all renders. */
private void bufferQuad(MutableQuadViewImpl quad, RenderLayer renderLayer) {
bufferQuad(bufferFunc.apply(renderLayer), quad, matrix(), overlay(), normalMatrix(), normalVec);
}
public static void bufferQuad(VertexConsumer buff, MutableQuadViewImpl quad, Matrix4f matrix, int overlay, Matrix3f normalMatrix, Vec3f normalVec) {
final boolean useNormals = quad.hasVertexNormals();
if (useNormals) {
quad.populateMissingNormals();
} else {
final Vec3f faceNormal = quad.faceNormal();
normalVec.set(faceNormal.getX(), faceNormal.getY(), faceNormal.getZ());
normalVec.transform(normalMatrix);
}
for (int i = 0; i < 4; i++) {
buff.vertex(matrix, quad.x(i), quad.y(i), quad.z(i));
final int color = quad.spriteColor(i, 0);
buff.color(color & 0xFF, (color >> 8) & 0xFF, (color >> 16) & 0xFF, (color >> 24) & 0xFF);
buff.texture(quad.spriteU(i, 0), quad.spriteV(i, 0));
buff.overlay(overlay);
buff.light(quad.lightmap(i));
if (useNormals) {
normalVec.set(quad.normalX(i), quad.normalY(i), quad.normalZ(i));
normalVec.transform(normalMatrix);
}
buff.normal(normalVec.getX(), normalVec.getY(), normalVec.getZ());
buff.next();
}
}
// routines below have a bit of copy-paste code reuse to avoid conditional execution inside a hot loop
/** for non-emissive mesh quads and all fallback quads with smooth lighting. */
protected void tesselateSmooth(MutableQuadViewImpl q, RenderLayer renderLayer, int blockColorIndex) {
colorizeQuad(q, blockColorIndex);
for (int i = 0; i < 4; i++) {
q.spriteColor(i, 0, ColorHelper.multiplyRGB(q.spriteColor(i, 0), aoCalc.ao[i]));
q.lightmap(i, ColorHelper.maxBrightness(q.lightmap(i), aoCalc.light[i]));
}
bufferQuad(q, renderLayer);
}
/** for emissive mesh quads with smooth lighting. */
protected void tesselateSmoothEmissive(MutableQuadViewImpl q, RenderLayer renderLayer, int blockColorIndex) {
colorizeQuad(q, blockColorIndex);
for (int i = 0; i < 4; i++) {
q.spriteColor(i, 0, ColorHelper.multiplyRGB(q.spriteColor(i, 0), aoCalc.ao[i]));
q.lightmap(i, FULL_BRIGHTNESS);
}
bufferQuad(q, renderLayer);
}
/** for non-emissive mesh quads and all fallback quads with flat lighting. */
protected void tesselateFlat(MutableQuadViewImpl quad, RenderLayer renderLayer, int blockColorIndex) {
colorizeQuad(quad, blockColorIndex);
shadeFlatQuad(quad);
final int brightness = flatBrightness(quad, blockInfo.blockState, blockInfo.blockPos);
for (int i = 0; i < 4; i++) {
quad.lightmap(i, ColorHelper.maxBrightness(quad.lightmap(i), brightness));
}
bufferQuad(quad, renderLayer);
}
/** for emissive mesh quads with flat lighting. */
protected void tesselateFlatEmissive(MutableQuadViewImpl quad, RenderLayer renderLayer, int blockColorIndex) {
colorizeQuad(quad, blockColorIndex);
shadeFlatQuad(quad);
for (int i = 0; i < 4; i++) {
quad.lightmap(i, FULL_BRIGHTNESS);
}
bufferQuad(quad, renderLayer);
}
private final BlockPos.Mutable mpos = new BlockPos.Mutable();
/**
* Handles geometry-based check for using self brightness or neighbor brightness.
* That logic only applies in flat lighting.
*/
int flatBrightness(MutableQuadViewImpl quad, BlockState blockState, BlockPos pos) {
mpos.set(pos);
// To mirror Vanilla's behavior, if the face has a cull-face, always sample the light value
// offset in that direction. See net.minecraft.client.render.block.BlockModelRenderer.renderQuadsFlat
// for reference.
if (quad.cullFace() != null) {
mpos.move(quad.cullFace());
} else if ((quad.geometryFlags() & LIGHT_FACE_FLAG) != 0 || Block.isShapeFullCube(blockState.getCollisionShape(blockInfo.blockView, pos))) {
mpos.move(quad.lightFace());
}
// Unfortunately cannot use brightness cache here unless we implement one specifically for flat lighting. See #329
return WorldRenderer.getLightmapCoordinates(blockInfo.blockView, blockState, mpos);
}
/**
* Starting in 1.16 flat shading uses dimension-specific diffuse factors that can be < 1.0
* even for un-shaded quads. These are also applied with AO shading but that is done in AO calculator.
*/
private void shadeFlatQuad(MutableQuadViewImpl quad) {
if ((quad.geometryFlags() & GeometryHelper.AXIS_ALIGNED_FLAG) == 0 || quad.hasVertexNormals()) {
// Quads that aren't direction-aligned or that have vertex normals need to be shaded
// using interpolation - vanilla can't handle them. Generally only applies to modded models.
final float faceShade = blockInfo.blockView.getBrightness(quad.lightFace(), quad.hasShade());
for (int i = 0; i < 4; i++) {
quad.spriteColor(i, 0, ColorHelper.multiplyRGB(quad.spriteColor(i, 0), vertexShade(quad, i, faceShade)));
}
} else {
final float diffuseShade = blockInfo.blockView.getBrightness(quad.lightFace(), quad.hasShade());
if (diffuseShade != 1.0f) {
for (int i = 0; i < 4; i++) {
quad.spriteColor(i, 0, ColorHelper.multiplyRGB(quad.spriteColor(i, 0), diffuseShade));
}
}
}
}
private float vertexShade(MutableQuadViewImpl quad, int vertexIndex, float faceShade) {
return quad.hasNormal(vertexIndex) ? normalShade(quad.normalX(vertexIndex), quad.normalY(vertexIndex), quad.normalZ(vertexIndex), quad.hasShade()) : faceShade;
}
/**
* Finds mean of per-face shading factors weighted by normal components.
* Not how light actually works but the vanilla diffuse shading model is a hack to start with
* and this gives reasonable results for non-cubic surfaces in a vanilla-style renderer.
*/
private float normalShade(float normalX, float normalY, float normalZ, boolean hasShade) {
float sum = 0;
float div = 0;
if (normalX > 0) {
sum += normalX * blockInfo.blockView.getBrightness(Direction.EAST, hasShade);
div += normalX;
} else if (normalX < 0) {
sum += -normalX * blockInfo.blockView.getBrightness(Direction.WEST, hasShade);
div -= normalX;
}
if (normalY > 0) {
sum += normalY * blockInfo.blockView.getBrightness(Direction.UP, hasShade);
div += normalY;
} else if (normalY < 0) {
sum += -normalY * blockInfo.blockView.getBrightness(Direction.DOWN, hasShade);
div -= normalY;
}
if (normalZ > 0) {
sum += normalZ * blockInfo.blockView.getBrightness(Direction.SOUTH, hasShade);
div += normalZ;
} else if (normalZ < 0) {
sum += -normalZ * blockInfo.blockView.getBrightness(Direction.NORTH, hasShade);
div -= normalZ;
}
return sum / div;
}
}