Skip to content
This repository was archived by the owner on Mar 22, 2026. It is now read-only.

Commit 0192401

Browse files
committed
完善动画内的 molang 运算支持
1 parent aec5bae commit 0192401

37 files changed

+909
-623
lines changed

MODULE.bazel

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ maven.install(
6464
"org.lwjgl:lwjgl-assimp:jar:natives-linux:3.3.3",
6565
"org.lwjgl:lwjgl-assimp:jar:natives-macos:3.3.3",
6666
"org.lwjgl:lwjgl-assimp:jar:natives-macos-arm64:3.3.3",
67-
"team.unnamed:mocha:3.0.0",
6867
"org.javassist:javassist:3.30.2-GA",
6968

7069
# test dependencies
@@ -102,6 +101,17 @@ http_jar(
102101
downloaded_file_name = "iris.jar",
103102
)
104103
http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
104+
http_archive(
105+
name = "mocha",
106+
url = "https://repo.maven.apache.org/maven2/team/unnamed/mocha/3.0.1/mocha-3.0.1-sources.jar",
107+
sha256 = "325319bfb53aa83bdb8367b03fced9b32fc285e12ec1b9d60134bc16d8170119",
108+
build_file = "@//third_party/mocha:BUILD.mocha.bazel",
109+
patch_strip = 1,
110+
patches = [
111+
"@//third_party/mocha/patches:entity_object_property.patch",
112+
"@//third_party/mocha/patches:mutable_entity.patch",
113+
],
114+
)
105115
# Download YSM for its bundled model
106116
http_archive(
107117
name = "yes_steve_model",

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ Apache 2.0 许可证授权。
6565

6666
捆绑了 [mocha](https://github.com/unnamed/mocha)(MIT)项目作为 molang 执行引擎,在此感谢。
6767

68-
捆绑了 [Javassist](https://github.com/jboss-javassist/javassist)(MPL 1.1 或 LGPL 2.1-or-later 或 Apache 2.0)作为 mocha 的代码生成引擎,在此感谢。
69-
7068
## 许可证
7169

7270
本 mod 以 LGPL 3.0 及以上版本授权,在发布和修改时请遵守许可证要求。
@@ -154,9 +152,6 @@ rendering.
154152

155153
Bundled [mocha](https://github.com/unnamed/mocha)(MIT) project as executing engine of molang, thanks to them.
156154

157-
Bundled [Javassist](https://github.com/jboss-javassist/javassist)(MPL 1.1, LGPL 2.1-or-later or Apache 2.0) as code
158-
generation engine of mocha, thanks to them.
159-
160155
## License
161156

162157
The mod is licensed under the LGPL 3.0 or later. Please comply with the license requirements when distributing and

blazerod/model/model-base/src/main/kotlin/top/fifthlight/blazerod/model/animation/AnimationContext.kt

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
package top.fifthlight.blazerod.model.animation
22

3-
import org.joml.Vector3fc
4-
import top.fifthlight.blazerod.model.util.BooleanWrapper
5-
import top.fifthlight.blazerod.model.util.FloatWrapper
6-
import top.fifthlight.blazerod.model.util.IntWrapper
3+
import org.joml.Vector3d
4+
import top.fifthlight.blazerod.model.util.*
75

86
interface AnimationContext {
97
companion object {
108
const val SECONDS_PER_TICK = 1f / 20f
119
}
1210

11+
enum class RenderingTargetType {
12+
PLAYER,
13+
MAID, // Reserved for TouhouLittleMaid
14+
ENTITY,
15+
BLOCK,
16+
}
17+
1318
interface Property<T> {
14-
object GameTick: Property<IntWrapper>
15-
object DeltaTick: Property<FloatWrapper>
19+
object GameTick : Property<LongWrapper>
20+
object DeltaTick : Property<FloatWrapper>
21+
22+
object RenderTarget : Property<RenderingTargetType>
1623

1724
// Entity
18-
object EntityPosition : Property<Vector3fc>
19-
object EntityPositionDelta : Property<Vector3fc>
25+
object EntityPosition : Property<Vector3d>
26+
object EntityPositionDelta : Property<Vector3d>
2027
object EntityHorizontalFacing : Property<IntWrapper>
21-
object EntityGroundSpeed : Property<FloatWrapper>
22-
object EntityVerticalSpeed : Property<FloatWrapper>
28+
object EntityGroundSpeed : Property<DoubleWrapper>
29+
object EntityVerticalSpeed : Property<DoubleWrapper>
2330
object EntityHasRider : Property<BooleanWrapper>
2431
object EntityIsRiding : Property<BooleanWrapper>
2532
object EntityIsInWater : Property<BooleanWrapper>
33+
object EntityIsInWaterOrRain : Property<BooleanWrapper>
2634
object EntityIsInFire : Property<BooleanWrapper>
2735
object EntityIsOnGround : Property<BooleanWrapper>
2836

@@ -36,24 +44,34 @@ interface AnimationContext {
3644
// Player
3745
object PlayerHeadXRotation : Property<FloatWrapper>
3846
object PlayerHeadYRotation : Property<FloatWrapper>
47+
object PlayerBodyXRotation : Property<FloatWrapper>
48+
object PlayerBodyYRotation : Property<FloatWrapper>
3949
object PlayerIsFirstPerson : Property<BooleanWrapper>
50+
object PlayerPersonView : Property<IntWrapper>
4051
object PlayerIsSpectator : Property<BooleanWrapper>
4152
object PlayerIsSneaking : Property<BooleanWrapper>
4253
object PlayerIsSprinting : Property<BooleanWrapper>
4354
object PlayerIsSwimming : Property<BooleanWrapper>
44-
object PlayerBodyXRotation : Property<FloatWrapper>
45-
object PlayerBodyYRotation : Property<FloatWrapper>
4655
object PlayerIsEating : Property<BooleanWrapper>
4756
object PlayerIsUsingItem : Property<BooleanWrapper>
48-
object PlayerLevel : Property<IntWrapper>
4957
object PlayerIsJumping : Property<BooleanWrapper>
58+
object PlayerIsSleeping : Property<BooleanWrapper>
59+
object PlayerLevel : Property<IntWrapper>
5060

5161
// World
5262
object WorldMoonPhase : Property<IntWrapper>
63+
object WorldTimeOfDay : Property<FloatWrapper>
64+
object WorldTimeStamp : Property<IntWrapper>
65+
object WorldWeather : Property<IntWrapper>
66+
object WorldDimension : Property<String>
67+
68+
// Game
69+
object GameFps : Property<IntWrapper>
5370
}
5471

5572
// game ticks
5673
fun getGameTick(): Long
74+
5775
// 0 ~ 1
5876
fun getDeltaTick(): Float
5977

blazerod/model/model-base/src/main/kotlin/top/fifthlight/blazerod/model/animation/AnimationInterpolator.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ abstract class AnimationInterpolation(val elements: Int) {
1010
}
1111

1212
abstract fun interpolateVector3f(
13+
context: AnimationContext,
14+
state: AnimationState,
1315
delta: Float,
1416
startFrame: Int,
1517
endFrame: Int,
@@ -19,6 +21,8 @@ abstract class AnimationInterpolation(val elements: Int) {
1921
)
2022

2123
abstract fun interpolateQuaternionf(
24+
context: AnimationContext,
25+
state: AnimationState,
2226
delta: Float,
2327
startFrame: Int,
2428
endFrame: Int,
@@ -28,6 +32,8 @@ abstract class AnimationInterpolation(val elements: Int) {
2832
)
2933

3034
abstract fun interpolateFloat(
35+
context: AnimationContext,
36+
state: AnimationState,
3137
delta: Float,
3238
startFrame: Int,
3339
endFrame: Int,
@@ -41,6 +47,8 @@ abstract class AnimationInterpolation(val elements: Int) {
4147

4248
val linear = object : AnimationInterpolation(1) {
4349
override fun interpolateVector3f(
50+
context: AnimationContext,
51+
state: AnimationState,
4452
delta: Float,
4553
startFrame: Int,
4654
endFrame: Int,
@@ -52,6 +60,8 @@ abstract class AnimationInterpolation(val elements: Int) {
5260
}
5361

5462
override fun interpolateQuaternionf(
63+
context: AnimationContext,
64+
state: AnimationState,
5565
delta: Float,
5666
startFrame: Int,
5767
endFrame: Int,
@@ -63,6 +73,8 @@ abstract class AnimationInterpolation(val elements: Int) {
6373
}
6474

6575
override fun interpolateFloat(
76+
context: AnimationContext,
77+
state: AnimationState,
6678
delta: Float,
6779
startFrame: Int,
6880
endFrame: Int,
@@ -76,6 +88,8 @@ abstract class AnimationInterpolation(val elements: Int) {
7688

7789
val step = object : AnimationInterpolation(1) {
7890
override fun interpolateVector3f(
91+
context: AnimationContext,
92+
state: AnimationState,
7993
delta: Float,
8094
startFrame: Int,
8195
endFrame: Int,
@@ -87,6 +101,8 @@ abstract class AnimationInterpolation(val elements: Int) {
87101
}
88102

89103
override fun interpolateQuaternionf(
104+
context: AnimationContext,
105+
state: AnimationState,
90106
delta: Float,
91107
startFrame: Int,
92108
endFrame: Int,
@@ -98,6 +114,8 @@ abstract class AnimationInterpolation(val elements: Int) {
98114
}
99115

100116
override fun interpolateFloat(
117+
context: AnimationContext,
118+
state: AnimationState,
101119
delta: Float,
102120
startFrame: Int,
103121
endFrame: Int,
@@ -111,6 +129,8 @@ abstract class AnimationInterpolation(val elements: Int) {
111129

112130
val cubicSpline = object : AnimationInterpolation(3) {
113131
override fun interpolateVector3f(
132+
context: AnimationContext,
133+
state: AnimationState,
114134
delta: Float,
115135
startFrame: Int,
116136
endFrame: Int,
@@ -136,6 +156,8 @@ abstract class AnimationInterpolation(val elements: Int) {
136156

137157
private val tempQuaternion = Quaternionf()
138158
override fun interpolateQuaternionf(
159+
context: AnimationContext,
160+
state: AnimationState,
139161
delta: Float,
140162
startFrame: Int,
141163
endFrame: Int,
@@ -163,6 +185,8 @@ abstract class AnimationInterpolation(val elements: Int) {
163185
}
164186

165187
override fun interpolateFloat(
188+
context: AnimationContext,
189+
state: AnimationState,
166190
delta: Float,
167191
startFrame: Int,
168192
endFrame: Int,
@@ -193,6 +217,8 @@ abstract class AnimationInterpolation(val elements: Int) {
193217

194218
interface AnimationInterpolator<T> {
195219
fun interpolate(
220+
context: AnimationContext,
221+
state: AnimationState,
196222
delta: Float,
197223
startFrame: Int,
198224
endFrame: Int,
@@ -204,13 +230,17 @@ interface AnimationInterpolator<T> {
204230

205231
class Vector3AnimationInterpolator(val type: AnimationInterpolation) : AnimationInterpolator<Vector3f> {
206232
override fun interpolate(
233+
context: AnimationContext,
234+
state: AnimationState,
207235
delta: Float,
208236
startFrame: Int,
209237
endFrame: Int,
210238
startValue: List<Vector3f>,
211239
endValue: List<Vector3f>,
212240
result: Vector3f,
213241
) = type.interpolateVector3f(
242+
context = context,
243+
state = state,
214244
delta = delta,
215245
startFrame = startFrame,
216246
endFrame = endFrame,
@@ -222,13 +252,17 @@ class Vector3AnimationInterpolator(val type: AnimationInterpolation) : Animation
222252

223253
class QuaternionAnimationInterpolator(val type: AnimationInterpolation) : AnimationInterpolator<Quaternionf> {
224254
override fun interpolate(
255+
context: AnimationContext,
256+
state: AnimationState,
225257
delta: Float,
226258
startFrame: Int,
227259
endFrame: Int,
228260
startValue: List<Quaternionf>,
229261
endValue: List<Quaternionf>,
230262
result: Quaternionf,
231263
) = type.interpolateQuaternionf(
264+
context = context,
265+
state = state,
232266
delta = delta,
233267
startFrame = startFrame,
234268
endFrame = endFrame,
@@ -240,6 +274,8 @@ class QuaternionAnimationInterpolator(val type: AnimationInterpolation) : Animat
240274

241275
class FloatAnimationInterpolator(val type: AnimationInterpolation) : AnimationInterpolator<MutableFloat> {
242276
override fun interpolate(
277+
context: AnimationContext,
278+
state: AnimationState,
243279
delta: Float,
244280
startFrame: Int,
245281
endFrame: Int,
@@ -248,6 +284,8 @@ class FloatAnimationInterpolator(val type: AnimationInterpolation) : AnimationIn
248284
result: MutableFloat,
249285
) {
250286
type.interpolateFloat(
287+
context = context,
288+
state = state,
251289
delta = delta,
252290
startFrame = startFrame,
253291
endFrame = endFrame,

blazerod/model/model-base/src/main/kotlin/top/fifthlight/blazerod/model/animation/AnimationKeyFrameData.kt

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ import java.nio.ByteOrder
1111
interface AnimationKeyFrameData<T> {
1212
val frames: Int
1313
val elements: Int
14-
fun get(index: Int, data: List<T>, post: Boolean)
14+
fun get(
15+
context: AnimationContext,
16+
state: AnimationState,
17+
index: Int,
18+
data: List<T>,
19+
post: Boolean,
20+
)
1521

1622
companion object
1723
}
@@ -34,7 +40,13 @@ class FloatListAnimationKeyFrameData<T>(
3440

3541
override val frames = values.size / valueSize
3642

37-
override fun get(index: Int, data: List<T>, post: Boolean) {
43+
override fun get(
44+
context: AnimationContext,
45+
state: AnimationState,
46+
index: Int,
47+
data: List<T>,
48+
post: Boolean,
49+
) {
3850
val baseOffset = index * valueSize + if (splitPrePost && post) {
3951
elements * componentCount
4052
} else {
@@ -123,7 +135,13 @@ class AccessorAnimationKeyFrameData<T>(
123135
.order(ByteOrder.LITTLE_ENDIAN)
124136
}
125137

126-
override fun get(index: Int, data: List<T>, post: Boolean) {
138+
override fun get(
139+
context: AnimationContext,
140+
state: AnimationState,
141+
index: Int,
142+
data: List<T>,
143+
post: Boolean,
144+
) {
127145
var position = index * elementStride
128146
for (i in 0 until elements) {
129147
if (isZeroFilled) {
@@ -153,8 +171,14 @@ fun <T, R> AnimationKeyFrameData<T>.map(
153171

154172
private val tempOriginalData = List(original.elements) { defaultValue() }
155173

156-
override fun get(index: Int, data: List<R>, post: Boolean) {
157-
original.get(index, tempOriginalData, post)
174+
override fun get(
175+
context: AnimationContext,
176+
state: AnimationState,
177+
index: Int,
178+
data: List<R>,
179+
post: Boolean,
180+
) {
181+
original.get(context, state, index, tempOriginalData, post)
158182

159183
for (i in 0 until elements) {
160184
transform(tempOriginalData[i], data[i])

blazerod/model/model-base/src/main/kotlin/top/fifthlight/blazerod/model/animation/KeyFrameAnimationChannel.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,21 @@ data class KeyFrameAnimationChannel<T : Any, D>(
4242
val time = state.getTime()
4343
indexer.findKeyFrames(time, indexResult)
4444
if (indexResult.startFrame == indexResult.endFrame || time < indexResult.startTime) {
45-
keyframeData.get(indexResult.startFrame, startValues, post = false)
45+
keyframeData.get(context, state, indexResult.startFrame, startValues, post = false)
4646
valueSetter(startValues, result)
4747
return
4848
}
4949
if (indexResult.endTime < time) {
50-
keyframeData.get(indexResult.endFrame, endValues, post = true)
50+
keyframeData.get(context, state, indexResult.endFrame, endValues, post = true)
5151
valueSetter(endValues, result)
5252
return
5353
}
5454
val delta = (time - indexResult.startTime) / (indexResult.endTime - indexResult.startTime)
55-
keyframeData.get(indexResult.startFrame, startValues, post = false)
56-
keyframeData.get(indexResult.endFrame, endValues, post = true)
55+
keyframeData.get(context, state, indexResult.startFrame, startValues, post = false)
56+
keyframeData.get(context, state, indexResult.endFrame, endValues, post = true)
5757
interpolator.interpolate(
58+
context = context,
59+
state = state,
5860
delta = delta,
5961
startFrame = indexResult.startFrame,
6062
endFrame = indexResult.endFrame,

0 commit comments

Comments
 (0)