| 1 | package net.minecraft.client.renderer.rendertype;
|
| 2 |
|
| 3 | import com.google.common.base.Suppliers;
|
| 4 | import com.google.common.collect.ImmutableList;
|
| 5 | import com.google.common.collect.ImmutableList.Builder;
|
| 6 | import com.mojang.blaze3d.systems.SamplerCache;
|
| 7 | import com.mojang.renderpearl.api.pipeline.RenderPipeline;
|
| 8 | import com.mojang.renderpearl.api.textures.FilterMode;
|
| 9 | import com.mojang.renderpearl.api.textures.GpuSampler;
|
| 10 | import com.mojang.renderpearl.api.textures.GpuTextureView;
|
| 11 | import java.util.HashMap;
|
| 12 | import java.util.List;
|
| 13 | import java.util.Map;
|
| 14 | import java.util.Map.Entry;
|
| 15 | import java.util.function.Supplier;
|
| 16 | import net.fabricmc.api.EnvType;
|
| 17 | import net.fabricmc.api.Environment;
|
| 18 | import net.minecraft.client.renderer.oit.OitPipelineSet;
|
| 19 | import net.minecraft.client.renderer.texture.AbstractTexture;
|
| 20 | import net.minecraft.client.renderer.texture.TextureManager;
|
| 21 | import net.minecraft.resources.Identifier;
|
| 22 | import org.jspecify.annotations.Nullable;
|
| 23 |
|
| 24 | @Environment(EnvType.CLIENT)
|
| 25 | public final class RenderSetup {
|
| 26 | final RenderPipeline pipeline;
|
| 27 | final @Nullable OitPipelineSet oitPipelineSet;
|
| 28 | final Map<String, RenderSetup.TextureBinding> textures;
|
| 29 | final TextureTransform textureTransform;
|
| 30 | final RenderSetup.OutlineProperty outlineProperty;
|
| 31 | final @Nullable String outlineTextureName;
|
| 32 | final boolean useLightmap;
|
| 33 | final boolean useOverlay;
|
| 34 | final boolean affectsCrumbling;
|
| 35 | final boolean sortOnUpload;
|
| 36 | final LayeringTransform layeringTransform;
|
| 37 | final boolean forceSolidModelPhase;
|
| 38 |
|
| 39 | private RenderSetup(
|
| 40 | final RenderPipeline pipeline,
|
| 41 | final @Nullable OitPipelineSet oitPipelineSet,
|
| 42 | final Map<String, RenderSetup.TextureBinding> textures,
|
| 43 | final boolean useLightmap,
|
| 44 | final boolean useOverlay,
|
| 45 | final LayeringTransform layeringTransform,
|
| 46 | final TextureTransform textureTransform,
|
| 47 | final RenderSetup.OutlineProperty outlineProperty,
|
| 48 | final @Nullable String outlineTextureName,
|
| 49 | final boolean affectsCrumbling,
|
| 50 | final boolean sortOnUpload,
|
| 51 | final boolean forceSolidModelPhase
|
| 52 | ) {
|
| 53 | this.pipeline = pipeline;
|
| 54 | this.oitPipelineSet = oitPipelineSet;
|
| 55 | this.textures = textures;
|
| 56 | this.textureTransform = textureTransform;
|
| 57 | this.useLightmap = useLightmap;
|
| 58 | this.useOverlay = useOverlay;
|
| 59 | this.outlineProperty = outlineProperty;
|
| 60 | this.outlineTextureName = outlineTextureName;
|
| 61 | this.layeringTransform = layeringTransform;
|
| 62 | this.affectsCrumbling = affectsCrumbling;
|
| 63 | this.sortOnUpload = sortOnUpload;
|
| 64 | this.forceSolidModelPhase = forceSolidModelPhase;
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public String toString() {
|
| 69 | return "RenderSetup[layeringTransform="
|
| 70 | + this.layeringTransform
|
| 71 | + ", textureTransform="
|
| 72 | + this.textureTransform
|
| 73 | + ", textures="
|
| 74 | + this.textures
|
| 75 | + ", outlineProperty="
|
| 76 | + this.outlineProperty
|
| 77 | + ", useLightmap="
|
| 78 | + this.useLightmap
|
| 79 | + ", useOverlay="
|
| 80 | + this.useOverlay
|
| 81 | + "]";
|
| 82 | }
|
| 83 |
|
| 84 | public static RenderSetup.RenderSetupBuilder builder(final RenderPipeline pipeline) {
|
| 85 | return new RenderSetup.RenderSetupBuilder(pipeline);
|
| 86 | }
|
| 87 |
|
| 88 | public List<PreparedRenderType.Texture> prepareTextures(
|
| 89 | final TextureManager textureManager, final SamplerCache samplerCache, final GpuTextureView overlayTexture, final GpuTextureView lightmapTexture
|
| 90 | ) {
|
| 91 | if (this.textures.isEmpty() && !this.useOverlay && !this.useLightmap) {
|
| 92 | return List.of();
|
| 93 | }
|
| 94 |
|
| 95 | Builder<PreparedRenderType.Texture> textures = ImmutableList.builderWithExpectedSize(this.textures.size() + 2);
|
| 96 | if (this.useOverlay) {
|
| 97 | textures.add(new PreparedRenderType.Texture("Sampler1", overlayTexture, samplerCache.getClampToEdge(FilterMode.LINEAR)));
|
| 98 | }
|
| 99 |
|
| 100 | if (this.useLightmap) {
|
| 101 | textures.add(new PreparedRenderType.Texture("Sampler2", lightmapTexture, samplerCache.getClampToEdge(FilterMode.LINEAR)));
|
| 102 | }
|
| 103 |
|
| 104 | for (Entry<String, RenderSetup.TextureBinding> entry : this.textures.entrySet()) {
|
| 105 | AbstractTexture texture = textureManager.getTexture(entry.getValue().location);
|
| 106 | GpuSampler samplerOverride = entry.getValue().sampler().get();
|
| 107 | textures.add(new PreparedRenderType.Texture(entry.getKey(), texture.getTextureView(), samplerOverride != null ? samplerOverride : texture.getSampler()));
|
| 108 | }
|
| 109 |
|
| 110 | return textures.build();
|
| 111 | }
|
| 112 |
|
| 113 | @Environment(EnvType.CLIENT)
|
| 114 | public enum OutlineProperty {
|
| 115 | NONE("none"),
|
| 116 | IS_OUTLINE("is_outline"),
|
| 117 | AFFECTS_OUTLINE("affects_outline");
|
| 118 |
|
| 119 | private final String name;
|
| 120 |
|
| 121 | OutlineProperty(final String name) {
|
| 122 | this.name = name;
|
| 123 | }
|
| 124 |
|
| 125 | @Override
|
| 126 | public String toString() {
|
| 127 | return this.name;
|
| 128 | }
|
| 129 | }
|
| 130 |
|
| 131 | @Environment(EnvType.CLIENT)
|
| 132 | public static class RenderSetupBuilder {
|
| 133 | private final RenderPipeline pipeline;
|
| 134 | private @Nullable OitPipelineSet oitPipelineSet;
|
| 135 | private boolean useLightmap = false;
|
| 136 | private boolean useOverlay = false;
|
| 137 | private LayeringTransform layeringTransform = LayeringTransform.NO_LAYERING;
|
| 138 | private TextureTransform textureTransform = TextureTransform.DEFAULT_TEXTURING;
|
| 139 | private boolean affectsCrumbling = false;
|
| 140 | private boolean sortOnUpload = false;
|
| 141 | private RenderSetup.OutlineProperty outlineProperty = RenderSetup.OutlineProperty.NONE;
|
| 142 | private @Nullable String outlineTextureName;
|
| 143 | private final Map<String, RenderSetup.TextureBinding> textures = new HashMap<>();
|
| 144 | private boolean forceSolidModelPhase;
|
| 145 |
|
| 146 | private RenderSetupBuilder(final RenderPipeline pipeline) {
|
| 147 | this.pipeline = pipeline;
|
| 148 | }
|
| 149 |
|
| 150 | public RenderSetup.RenderSetupBuilder withTexture(final String name, final Identifier texture) {
|
| 151 | this.textures.put(name, new RenderSetup.TextureBinding(texture, () -> null));
|
| 152 | return this;
|
| 153 | }
|
| 154 |
|
| 155 | public RenderSetup.RenderSetupBuilder withTexture(final String name, final Identifier texture, final @Nullable Supplier<GpuSampler> sampler) {
|
| 156 | this.textures.put(name, new RenderSetup.TextureBinding(texture, Suppliers.memoize(() -> sampler == null ? null : sampler.get())));
|
| 157 | return this;
|
| 158 | }
|
| 159 |
|
| 160 | public RenderSetup.RenderSetupBuilder useLightmap() {
|
| 161 | this.useLightmap = true;
|
| 162 | return this;
|
| 163 | }
|
| 164 |
|
| 165 | public RenderSetup.RenderSetupBuilder useOverlay() {
|
| 166 | this.useOverlay = true;
|
| 167 | return this;
|
| 168 | }
|
| 169 |
|
| 170 | public RenderSetup.RenderSetupBuilder affectsCrumbling() {
|
| 171 | this.affectsCrumbling = true;
|
| 172 | return this;
|
| 173 | }
|
| 174 |
|
| 175 | public RenderSetup.RenderSetupBuilder sortOnUpload() {
|
| 176 | this.sortOnUpload = true;
|
| 177 | return this;
|
| 178 | }
|
| 179 |
|
| 180 | public RenderSetup.RenderSetupBuilder setLayeringTransform(final LayeringTransform layeringTransform) {
|
| 181 | this.layeringTransform = layeringTransform;
|
| 182 | return this;
|
| 183 | }
|
| 184 |
|
| 185 | public RenderSetup.RenderSetupBuilder setTextureTransform(final TextureTransform textureTransform) {
|
| 186 | this.textureTransform = textureTransform;
|
| 187 | return this;
|
| 188 | }
|
| 189 |
|
| 190 | public RenderSetup.RenderSetupBuilder setOutline(final RenderSetup.OutlineProperty outlineProperty) {
|
| 191 | this.outlineProperty = outlineProperty;
|
| 192 | this.outlineTextureName = null;
|
| 193 | return this;
|
| 194 | }
|
| 195 |
|
| 196 | public RenderSetup.RenderSetupBuilder setOutline(final RenderSetup.OutlineProperty outlineProperty, final String outlineTextureName) {
|
| 197 | this.outlineProperty = outlineProperty;
|
| 198 | this.outlineTextureName = outlineTextureName;
|
| 199 | return this;
|
| 200 | }
|
| 201 |
|
| 202 | public RenderSetup.RenderSetupBuilder setOitPipelines(final OitPipelineSet oitPipelineSet) {
|
| 203 | this.oitPipelineSet = oitPipelineSet;
|
| 204 | return this;
|
| 205 | }
|
| 206 |
|
| 207 | public RenderSetup.RenderSetupBuilder withForcedSolidModelPhase() {
|
| 208 | this.forceSolidModelPhase = true;
|
| 209 | return this;
|
| 210 | }
|
| 211 |
|
| 212 | public RenderSetup createRenderSetup() {
|
| 213 | return new RenderSetup(
|
| 214 | this.pipeline,
|
| 215 | this.oitPipelineSet,
|
| 216 | this.textures,
|
| 217 | this.useLightmap,
|
| 218 | this.useOverlay,
|
| 219 | this.layeringTransform,
|
| 220 | this.textureTransform,
|
| 221 | this.outlineProperty,
|
| 222 | this.outlineTextureName,
|
| 223 | this.affectsCrumbling,
|
| 224 | this.sortOnUpload,
|
| 225 | this.forceSolidModelPhase
|
| 226 | );
|
| 227 | }
|
| 228 | }
|
| 229 |
|
| 230 | @Environment(EnvType.CLIENT)
|
| 231 | record TextureBinding(Identifier location, Supplier<@Nullable GpuSampler> sampler) {
|
| 232 | }
|
| 233 | }
|
| 234 |
|