ModelBlockRenderer.java

net.minecraft.client.renderer.block.ModelBlockRenderer

信息

  • 全限定名:net.minecraft.client.renderer.block.ModelBlockRenderer
  • 类型:public class
  • 包:net.minecraft.client.renderer.block
  • 源码路径:src/main/java/net/minecraft/client/renderer/block/ModelBlockRenderer.java
  • 起始行号:L25
  • 职责:

    TODO

字段/常量

  • DIRECTIONS

    • 类型: Direction[]
    • 修饰符: private static final
    • 源码定位: L26
    • 说明:

      TODO

  • lighter

    • 类型: BlockModelLighter
    • 修饰符: private final
    • 源码定位: L27
    • 说明:

      TODO

  • ambientOcclusion

    • 类型: boolean
    • 修饰符: private final
    • 源码定位: L28
    • 说明:

      TODO

  • cull

    • 类型: boolean
    • 修饰符: private final
    • 源码定位: L29
    • 说明:

      TODO

  • blockColors

    • 类型: BlockColors
    • 修饰符: private final
    • 源码定位: L30
    • 说明:

      TODO

  • random

    • 类型: RandomSource
    • 修饰符: private final
    • 源码定位: L31
    • 说明:

      TODO

  • parts

    • 类型: List<BlockStateModelPart>
    • 修饰符: private final
    • 源码定位: L32
    • 说明:

      TODO

  • scratchPos

    • 类型: BlockPos.MutableBlockPos
    • 修饰符: private final
    • 源码定位: L33
    • 说明:

      TODO

  • quadInstance

    • 类型: QuadInstance
    • 修饰符: private final
    • 源码定位: L34
    • 说明:

      TODO

  • tintCacheIndex

    • 类型: int
    • 修饰符: private
    • 源码定位: L35
    • 说明:

      TODO

  • tintCacheValue

    • 类型: int
    • 修饰符: private
    • 源码定位: L36
    • 说明:

      TODO

  • tintSourcesInitialized

    • 类型: boolean
    • 修饰符: private
    • 源码定位: L37
    • 说明:

      TODO

  • tintSources

    • 类型: List<BlockTintSource>
    • 修饰符: private final
    • 源码定位: L38
    • 说明:

      TODO

  • computedTintValues

    • 类型: IntList
    • 修饰符: private final
    • 源码定位: L39
    • 说明:

      TODO

内部类/嵌套类型

构造器

public ModelBlockRenderer(boolean ambientOcclusion, boolean cull, BlockColors blockColors) @ L41

  • 构造器名:ModelBlockRenderer
  • 源码定位:L41
  • 修饰符:public

参数:

  • ambientOcclusion: boolean
  • cull: boolean
  • blockColors: BlockColors

说明:

TODO

方法

下面的方法块按源码顺序生成。

public static boolean forceOpaque(boolean cutoutLeaves, BlockState blockState) @ L48

  • 方法名:forceOpaque
  • 源码定位:L48
  • 返回类型:boolean
  • 修饰符:public static

参数:

  • cutoutLeaves: boolean
  • blockState: BlockState

说明:

TODO

public void tesselateBlock(BlockQuadOutput output, float x, float y, float z, BlockAndTintGetter level, BlockPos pos, BlockState blockState, BlockStateModel model, long seed) @ L52

  • 方法名:tesselateBlock
  • 源码定位:L52
  • 返回类型:void
  • 修饰符:public

参数:

  • output: BlockQuadOutput
  • x: float
  • y: float
  • z: float
  • level: BlockAndTintGetter
  • pos: BlockPos
  • blockState: BlockState
  • model: BlockStateModel
  • seed: long

说明:

TODO

private void configureTintCache(BlockState blockState) @ L72

  • 方法名:configureTintCache
  • 源码定位:L72
  • 返回类型:void
  • 修饰符:private

参数:

  • blockState: BlockState

说明:

TODO

private void resetTintCache() @ L84

  • 方法名:resetTintCache
  • 源码定位:L84
  • 返回类型:void
  • 修饰符:private

参数:

说明:

TODO

private void tesselateAmbientOcclusion(BlockQuadOutput output, float x, float y, float z, List<BlockStateModelPart> parts, BlockAndTintGetter level, BlockState state, BlockPos pos) @ L93

  • 方法名:tesselateAmbientOcclusion
  • 源码定位:L93
  • 返回类型:void
  • 修饰符:private

参数:

  • output: BlockQuadOutput
  • x: float
  • y: float
  • z: float
  • parts: List
  • level: BlockAndTintGetter
  • state: BlockState
  • pos: BlockPos

说明:

TODO

private void tesselateFlat(BlockQuadOutput output, float x, float y, float z, List<BlockStateModelPart> parts, BlockAndTintGetter level, BlockState state, BlockPos pos) @ L132

  • 方法名:tesselateFlat
  • 源码定位:L132
  • 返回类型:void
  • 修饰符:private

参数:

  • output: BlockQuadOutput
  • x: float
  • y: float
  • z: float
  • parts: List
  • level: BlockAndTintGetter
  • state: BlockState
  • pos: BlockPos

说明:

TODO

private boolean shouldRenderFace(BlockAndTintGetter level, BlockState state, Direction direction, BlockPos neighborPos) @ L174

  • 方法名:shouldRenderFace
  • 源码定位:L174
  • 返回类型:boolean
  • 修饰符:private

参数:

  • level: BlockAndTintGetter
  • state: BlockState
  • direction: Direction
  • neighborPos: BlockPos

说明:

TODO

private void putQuadWithTint(BlockQuadOutput output, float x, float y, float z, BlockAndTintGetter level, BlockState state, BlockPos pos, BakedQuad quad) @ L183

  • 方法名:putQuadWithTint
  • 源码定位:L183
  • 返回类型:void
  • 修饰符:private

参数:

  • output: BlockQuadOutput
  • x: float
  • y: float
  • z: float
  • level: BlockAndTintGetter
  • state: BlockState
  • pos: BlockPos
  • quad: BakedQuad

说明:

TODO

private int getTintColor(BlockAndTintGetter level, BlockState state, BlockPos pos, int tintIndex) @ L192

  • 方法名:getTintColor
  • 源码定位:L192
  • 返回类型:int
  • 修饰符:private

参数:

  • level: BlockAndTintGetter
  • state: BlockState
  • pos: BlockPos
  • tintIndex: int

说明:

TODO

private int computeTintColor(BlockAndTintGetter level, BlockState state, BlockPos pos, int tintIndex) @ L203

  • 方法名:computeTintColor
  • 源码定位:L203
  • 返回类型:int
  • 修饰符:private

参数:

  • level: BlockAndTintGetter
  • state: BlockState
  • pos: BlockPos
  • tintIndex: int

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public class ModelBlockRenderer {
    private static final Direction[] DIRECTIONS = Direction.values();
    private final BlockModelLighter lighter;
    private final boolean ambientOcclusion;
    private final boolean cull;
    private final BlockColors blockColors;
    private final RandomSource random = RandomSource.createThreadLocalInstance(0L);
    private final List<BlockStateModelPart> parts = new ObjectArrayList<>();
    private final BlockPos.MutableBlockPos scratchPos = new BlockPos.MutableBlockPos();
    private final QuadInstance quadInstance = new QuadInstance();
    private int tintCacheIndex = -1;
    private int tintCacheValue;
    private boolean tintSourcesInitialized;
    private final List<@Nullable BlockTintSource> tintSources = new ObjectArrayList<>();
    private final IntList computedTintValues = new IntArrayList();
 
    public ModelBlockRenderer(boolean ambientOcclusion, boolean cull, BlockColors blockColors) {
        this.lighter = new BlockModelLighter();
        this.ambientOcclusion = ambientOcclusion;
        this.cull = cull;
        this.blockColors = blockColors;
    }
 
    public static boolean forceOpaque(boolean cutoutLeaves, BlockState blockState) {
        return !cutoutLeaves && blockState.getBlock() instanceof LeavesBlock;
    }
 
    public void tesselateBlock(
        BlockQuadOutput output, float x, float y, float z, BlockAndTintGetter level, BlockPos pos, BlockState blockState, BlockStateModel model, long seed
    ) {
        this.random.setSeed(seed);
        model.collectParts(this.random, this.parts);
        if (!this.parts.isEmpty()) {
            try {
                Vec3 offset = blockState.getOffset(pos);
                if (this.ambientOcclusion && blockState.getLightEmission() == 0 && this.parts.getFirst().useAmbientOcclusion()) {
                    this.tesselateAmbientOcclusion(output, x + (float)offset.x, y + (float)offset.y, z + (float)offset.z, this.parts, level, blockState, pos);
                } else {
                    this.tesselateFlat(output, x + (float)offset.x, y + (float)offset.y, z + (float)offset.z, this.parts, level, blockState, pos);
                }
            } finally {
                this.parts.clear();
                this.resetTintCache();
            }
        }
    }
 
    private void configureTintCache(BlockState blockState) {
        List<BlockTintSource> tintSources = this.blockColors.getTintSources(blockState);
        int tintSourceCount = tintSources.size();
        if (tintSourceCount > 0) {
            this.tintSources.addAll(tintSources);
 
            for (int i = 0; i < tintSourceCount; i++) {
                this.computedTintValues.add(-1);
            }
        }
    }
 
    private void resetTintCache() {
        this.tintCacheIndex = -1;
        if (this.tintSourcesInitialized) {
            this.tintSources.clear();
            this.computedTintValues.clear();
            this.tintSourcesInitialized = false;
        }
    }
 
    private void tesselateAmbientOcclusion(
        BlockQuadOutput output, float x, float y, float z, List<BlockStateModelPart> parts, BlockAndTintGetter level, BlockState state, BlockPos pos
    ) {
        int cacheValid = 0;
        int shouldRenderFaceCache = 0;
 
        for (BlockStateModelPart part : parts) {
            for (Direction direction : DIRECTIONS) {
                int cacheMask = 1 << direction.ordinal();
                boolean validCacheForDirection = (cacheValid & cacheMask) == 1;
                boolean shouldRenderFace = (shouldRenderFaceCache & cacheMask) == 1;
                if (!validCacheForDirection || shouldRenderFace) {
                    List<BakedQuad> culledQuads = part.getQuads(direction);
                    if (!culledQuads.isEmpty()) {
                        if (!validCacheForDirection) {
                            shouldRenderFace = this.shouldRenderFace(level, state, direction, this.scratchPos.setWithOffset(pos, direction));
                            cacheValid |= cacheMask;
                            if (shouldRenderFace) {
                                shouldRenderFaceCache |= cacheMask;
                            }
                        }
 
                        if (shouldRenderFace) {
                            for (BakedQuad quad : culledQuads) {
                                this.lighter.prepareQuadAmbientOcclusion(level, state, pos, quad, this.quadInstance);
                                this.putQuadWithTint(output, x, y, z, level, state, pos, quad);
                            }
                        }
                    }
                }
            }
 
            for (BakedQuad quad : part.getQuads(null)) {
                this.lighter.prepareQuadAmbientOcclusion(level, state, pos, quad, this.quadInstance);
                this.putQuadWithTint(output, x, y, z, level, state, pos, quad);
            }
        }
    }
 
    private void tesselateFlat(
        BlockQuadOutput output, float x, float y, float z, List<BlockStateModelPart> parts, BlockAndTintGetter level, BlockState state, BlockPos pos
    ) {
        int cacheValid = 0;
        int shouldRenderFaceCache = 0;
 
        for (BlockStateModelPart part : parts) {
            for (Direction direction : DIRECTIONS) {
                int cacheMask = 1 << direction.ordinal();
                boolean validCacheForDirection = (cacheValid & cacheMask) == 1;
                boolean shouldRenderFace = (shouldRenderFaceCache & cacheMask) == 1;
                if (!validCacheForDirection || shouldRenderFace) {
                    List<BakedQuad> culledQuads = part.getQuads(direction);
                    if (!culledQuads.isEmpty()) {
                        BlockPos relativePos = this.scratchPos.setWithOffset(pos, direction);
                        if (!validCacheForDirection) {
                            shouldRenderFace = this.shouldRenderFace(level, state, direction, relativePos);
                            cacheValid |= cacheMask;
                            if (shouldRenderFace) {
                                shouldRenderFaceCache |= cacheMask;
                            }
                        }
 
                        if (shouldRenderFace) {
                            int lightCoords = this.lighter.getLightCoords(state, level, relativePos);
 
                            for (BakedQuad quad : culledQuads) {
                                this.lighter.prepareQuadFlat(level, state, pos, lightCoords, quad, this.quadInstance);
                                this.putQuadWithTint(output, x, y, z, level, state, pos, quad);
                            }
                        }
                    }
                }
            }
 
            for (BakedQuad quad : part.getQuads(null)) {
                this.lighter.prepareQuadFlat(level, state, pos, -1, quad, this.quadInstance);
                this.putQuadWithTint(output, x, y, z, level, state, pos, quad);
            }
        }
    }
 
    private boolean shouldRenderFace(BlockAndTintGetter level, BlockState state, Direction direction, BlockPos neighborPos) {
        if (!this.cull) {
            return true;
        } else {
            BlockState neighborState = level.getBlockState(neighborPos);
            return Block.shouldRenderFace(state, neighborState, direction);
        }
    }
 
    private void putQuadWithTint(BlockQuadOutput output, float x, float y, float z, BlockAndTintGetter level, BlockState state, BlockPos pos, BakedQuad quad) {
        int tintIndex = quad.materialInfo().tintIndex();
        if (tintIndex != -1) {
            this.quadInstance.multiplyColor(this.getTintColor(level, state, pos, tintIndex));
        }
 
        output.put(x, y, z, quad, this.quadInstance);
    }
 
    private int getTintColor(BlockAndTintGetter level, BlockState state, BlockPos pos, int tintIndex) {
        if (this.tintCacheIndex == tintIndex) {
            return this.tintCacheValue;
        } else {
            int tintColor = this.computeTintColor(level, state, pos, tintIndex);
            this.tintCacheIndex = tintIndex;
            this.tintCacheValue = tintColor;
            return tintColor;
        }
    }
 
    private int computeTintColor(BlockAndTintGetter level, BlockState state, BlockPos pos, int tintIndex) {
        if (!this.tintSourcesInitialized) {
            this.configureTintCache(state);
            this.tintSourcesInitialized = true;
        }
 
        if (tintIndex >= this.tintSources.size()) {
            return -1;
        } else {
            BlockTintSource tintSource = this.tintSources.set(tintIndex, null);
            if (tintSource != null) {
                int computedTintValue = tintSource.colorInWorld(state, level, pos);
                this.computedTintValues.set(tintIndex, computedTintValue);
                return computedTintValue;
            } else {
                return this.computedTintValues.getInt(tintIndex);
            }
        }
    }
}

引用的其他类