@@ -29,33 +29,30 @@ toc_max_heading_level: 4
2929
3030MadParticle在` Particle ` 类中注入了类型为枚举` TakeOver.TickType ` 的` madparticleTickType ` 字段,以表示这个粒子是否支持并行计算。` TickType ` 的值可以是` SYNC ` 、` ASYNC ` 和` UNKNOWN ` 。对于没有主动赋值的第三方粒子,默认值是` UNKNOWN ` 。
3131
32- MadParticle依靠` Particle.getRenderType ` 方法来判断粒子是否支持实例化渲染。如果你返回传统的 ` PARTICLE_SHEET_TRANSLUCENT ` 或 ` PARTICLE_SHEET_OPAQUE ` ,MadParticle会自动根据用户设置来决定是否进行实例化渲染。如果返回 ` INSTANCED ` ,则是显式指定总是进行实例化渲染 。
32+ MadParticle依靠` getGroup ` 方法来判断粒子是否支持实例化渲染。如果方法的返回值为 ` ModParticleRenderTypes.INSTANCED ` ,则使用实例化渲染,并使用粒子Atlas材质;如果方法的返回值为 ` ModParticleRenderTypes.INSTANCED_TERRAIN ` ,则使用实例化渲染,并使用方块Atlas材质。返回其他的值,MadParticle便不会干涉粒子的渲染 。
3333
3434以下是完整的参考代码:
3535
3636``` java
3737public class YourParticle extends TextureSheetParticle {
38- private @NotNull ParticleRenderType particleRenderType = ParticleRenderType . PARTICLE_SHEET_TRANSLUCENT ;
38+ private @NotNull ParticleRenderType particleRenderType;
3939
4040 public YourParticle (ClientLevel pLevel , double pX , double pY , double pZ , ParticleRenderType particleRenderType ) {
4141 super (pLevel, pX, pY, pZ);
4242 if (ModList . get(). isLoaded(" madparticle" )) {
4343 try {
44- var madparticleTickType = this . getClass(). getDeclaredField(" madparticleTickType" );
45- madparticleTickType. setAccessible(true );
46- var takeOver = Class . forName(" cn.ussshenzhou.madparticle.particle.enums.TakeOver.TickType" );
47- var valueOf = takeOver. getMethod(" valueOf" , String . class);
44+ var tickType = Class . forName(" cn.ussshenzhou.madparticle.particle.enums.TakeOver.TickType" );
4845 // SYNC ASYNC UNKNOWN
49- var sync = valueOf. invoke(null , " SYNC" );
46+ var sync = tickType. getMethod(" valueOf" , String . class). invoke(null , " SYNC" );
47+ var madparticleTickType = this . getClass(). getField(" madparticleTickType" );
48+ madparticleTickType. setAccessible(true );
5049 madparticleTickType. set(this , sync);
51- } catch (NoSuchFieldException | ClassNotFoundException | NoSuchMethodException | IllegalAccessException |
52- InvocationTargetException ignored) {
53- }
54- try {
55- var madparticleModParticleRenderTypes = Class . forName(" cn.ussshenzhou.madparticle.particle.ModParticleRenderTypes" );
56- var instance = madparticleModParticleRenderTypes. getField(" INSTANCED" );
57- this . particleRenderType = (ParticleRenderType ) instance. get(null );
58- } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException ignored) {
50+
51+ var modParticleRenderTypes = Class . forName(" cn.ussshenzhou.madparticle.particle.render.ModParticleRenderTypes" );
52+ // INSTANCED INSTANCED_TERRAIN
53+ var instanced = modParticleRenderTypes. getDeclaredField(" INSTANCED" );
54+ this . particleRenderType = (ParticleRenderType ) instanced. get(null );
55+ } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException ignored) {
5956 }
6057 }
6158 }
@@ -64,12 +61,14 @@ public class YourParticle extends TextureSheetParticle {
6461 * Do NOT put reflection or other logic here. This method should be as quick as possible.
6562 */
6663 @Override
67- public @ NotNull ParticleRenderType getRenderType () {
64+ public ParticleRenderType getGroup () {
6865 return particleRenderType;
6966 }
7067}
7168```
7269
70+ 当然,示例简单起见,此处的反射类、方法、字段均未缓存。
71+
7372::: info
7473
7574这里利用了反射来避免在你的项目中引入MadParticle本身。如果你不太喜欢这样做,你也可以借助CurseMaven引入MadParticle。
0 commit comments