aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/features/misc/CustomCapes.kt
blob: a3725e5378daf06e5be04e5ab803a41aeaba882e (plain)
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
package moe.nea.firmament.features.misc

import com.mojang.blaze3d.buffers.GpuBuffer
import com.mojang.blaze3d.systems.RenderSystem
import java.nio.ByteBuffer
import java.util.OptionalDouble
import java.util.OptionalInt
import util.render.CustomRenderPipelines
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
import net.minecraft.client.network.AbstractClientPlayerEntity
import net.minecraft.client.render.BufferBuilder
import net.minecraft.client.render.RenderLayer
import net.minecraft.client.render.VertexConsumer
import net.minecraft.client.render.VertexConsumerProvider
import net.minecraft.client.render.entity.state.PlayerEntityRenderState
import net.minecraft.client.util.BufferAllocator
import net.minecraft.client.util.SkinTextures
import net.minecraft.util.Identifier
import moe.nea.firmament.Firmament
import moe.nea.firmament.features.FirmamentFeature
import moe.nea.firmament.gui.config.ManagedConfig
import moe.nea.firmament.util.MC
import moe.nea.firmament.util.TimeMark

object CustomCapes : FirmamentFeature {
	override val identifier: String
		get() = "developer-capes"

	object TConfig : ManagedConfig(identifier, Category.DEV) {
		val showCapes by toggle("show-cape") { true }
	}

	override val config: ManagedConfig
		get() = TConfig

	interface CustomCapeRenderer {
		fun replaceRender(
			renderLayer: RenderLayer,
			vertexConsumerProvider: VertexConsumerProvider,
			model: (VertexConsumer) -> Unit
		)
	}

	data class TexturedCapeRenderer(
		val location: Identifier
	) : CustomCapeRenderer {
		override fun replaceRender(
			renderLayer: RenderLayer,
			vertexConsumerProvider: VertexConsumerProvider,
			model: (VertexConsumer) -> Unit
		) {
			model(vertexConsumerProvider.getBuffer(RenderLayer.getEntitySolid(location)))
		}
	}

	data class ParallaxedHighlightCapeRenderer(
		val template: Identifier,
		val background: Identifier,
		val overlay: Identifier,
		val animationSpeed: Duration,
	) : CustomCapeRenderer {
		override fun replaceRender(
			renderLayer: RenderLayer,
			vertexConsumerProvider: VertexConsumerProvider,
			model: (VertexConsumer) -> Unit
		) {
			// TODO: figure out how exactly the rotation abstraction works
			val animationValue = (startTime.passedTime() / animationSpeed).mod(1F)
			val commandEncoder = RenderSystem.getDevice().createCommandEncoder()
			val animationBufSource = ByteBuffer.allocateDirect(8)
			animationBufSource.putFloat(animationValue.toFloat())
			animationBufSource.flip()
			BufferAllocator(2048).use { allocator ->
				val bufferBuilder = BufferBuilder(allocator, renderLayer.drawMode, renderLayer.vertexFormat)
				model(bufferBuilder)
				bufferBuilder.end().use { buffer ->
					val vertexBuffer = renderLayer.vertexFormat.uploadImmediateVertexBuffer(buffer.buffer)
					val indexBufferConstructor = RenderSystem.getSequentialBuffer(renderLayer.drawMode)
					val indexBuffer = indexBufferConstructor.getIndexBuffer(buffer.drawParameters.indexCount)
					val templateTexture = MC.textureManager.getTexture(template)
					val backgroundTexture = MC.textureManager.getTexture(background)
					val foregroundTexture = MC.textureManager.getTexture(overlay)
					RenderSystem.getDevice()
						.createBuffer({ "Firm Cape Animation" }, GpuBuffer.USAGE_UNIFORM, animationBufSource)
						.use { animationUniformBuf ->
							commandEncoder.createRenderPass(
								{ "FirmamentCustomCape" },
								MC.instance.framebuffer.colorAttachmentView,
								OptionalInt.empty(),
								MC.instance.framebuffer.depthAttachmentView,
								OptionalDouble.empty(),
							).use { renderPass ->
								// TODO: account for lighting
								renderPass.setPipeline(CustomRenderPipelines.PARALLAX_CAPE_SHADER)
								renderPass.bindSampler("Sampler0", templateTexture.glTextureView)
								renderPass.bindSampler("Sampler1", backgroundTexture.glTextureView)
								renderPass.bindSampler("Sampler3", foregroundTexture.glTextureView)
								renderPass.setUniform("Animation", animationUniformBuf)
								renderPass.setIndexBuffer(indexBuffer, indexBufferConstructor.indexType)
								renderPass.setVertexBuffer(0, vertexBuffer)
								renderPass.drawIndexed(0, 0, buffer.drawParameters.indexCount, 1)
							}

						}
				}
			}
		}
	}

	interface CapeStorage {
		companion object {
			@JvmStatic
			fun cast(playerEntityRenderState: PlayerEntityRenderState) =
				playerEntityRenderState as CapeStorage

		}

		var cape_firmament: CustomCape?
	}

	data class CustomCape(
		val id: String,
		val label: String,
		val render: CustomCapeRenderer,
	)

	enum class AllCapes(val label: String, val render: CustomCapeRenderer) {
		FIRMAMENT_ANIMATED(
			"Animated Firmament", ParallaxedHighlightCapeRenderer(
				Firmament.identifier("textures/cape/parallax_template.png"),
				Firmament.identifier("textures/cape/parallax_background.png"),
				Firmament.identifier("textures/cape/firmament_star.png"),
				110.seconds
			)
		),

		FURFSKY_STATIC(
			"FurfSky",
			TexturedCapeRenderer(Firmament.identifier("textures/cape/fsr_static.png"))
		),

		FIRMAMENT_STATIC(
			"Firmament",
			TexturedCapeRenderer(Firmament.identifier("textures/cape/firm_static.png"))
		)
		;

		val cape = CustomCape(name, label, render)
	}

	val byId = AllCapes.entries.associateBy { it.cape.id }
	val byUuid =
		listOf(
			listOf(
				Devs.nea to AllCapes.FIRMAMENT_ANIMATED,
				Devs.kath to AllCapes.FIRMAMENT_STATIC,
				Devs.jani to AllCapes.FIRMAMENT_ANIMATED,
			),
			Devs.FurfSky.all.map { it to AllCapes.FURFSKY_STATIC },
		).flatten().flatMap { (dev, cape) -> dev.uuids.map { it to cape.cape } }.toMap()

	@JvmStatic
	fun render(
		playerEntityRenderState: PlayerEntityRenderState,
		vertexConsumer: VertexConsumer,
		renderLayer: RenderLayer,
		vertexConsumerProvider: VertexConsumerProvider,
		model: (VertexConsumer) -> Unit
	) {
		val capeStorage = CapeStorage.cast(playerEntityRenderState)
		val firmCape = capeStorage.cape_firmament
		if (firmCape != null) {
			firmCape.render.replaceRender(renderLayer, vertexConsumerProvider, model)
		} else {
			model(vertexConsumer)
		}
	}

	@JvmStatic
	fun addCapeData(
		player: AbstractClientPlayerEntity,
		playerEntityRenderState: PlayerEntityRenderState
	) {
		val cape = if (TConfig.showCapes) byUuid[player.uuid] else null
		val capeStorage = CapeStorage.cast(playerEntityRenderState)
		if (cape == null) {
			capeStorage.cape_firmament = null
		} else {
			capeStorage.cape_firmament = cape
			playerEntityRenderState.skinTextures = SkinTextures(
				playerEntityRenderState.skinTextures.texture,
				playerEntityRenderState.skinTextures.textureUrl,
				Firmament.identifier("placeholder/fake_cape"),
				playerEntityRenderState.skinTextures.elytraTexture,
				playerEntityRenderState.skinTextures.model,
				playerEntityRenderState.skinTextures.secure,
			)
			playerEntityRenderState.capeVisible = true
		}
	}

	val startTime = TimeMark.now()
}