Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Rendering Pings


poopoodice
 Share

Recommended Posts

Hi, this article is more of a experience sharing instead of a tutorial, the code themselves should be self-explanatory, and the way I do things may not be the best practice, or most efficient.

Product:

zo1hIgh.png

 

I started with the textures, we need the textures/icons that are being rendered to the world, and the ones for the UI.

Kgqtirx.png

DFX7VXu.png

The blue textures are used when the option is hovered.

Then, we need to find out where the mouse is at and which one is selected, we can check the distance and angle from the screen center and decide which part it is on:

    private int getHoveringType(Minecraft minecraft)
    {
      double mX = minecraft.mouseHelper.getMouseX();
      double mY = minecraft.mouseHelper.getMouseY();
      double actualW = minecraft.getMainWindow().getWidth() / 2.0F;
      double actualH = minecraft.getMainWindow().getHeight() / 2.0F;
      double mX2 = mX - actualW;
      double mY2 = mY - actualH;
      double angle = AVAWeaponUtil.getAngleFromCoord(mX2, -mY2) + 30;
      if (Math.sqrt(mX2 * mX2 + mY2 * mY2) <= 15 * minecraft.getMainWindow().getGuiScaleFactor())
        return -1;
      if (angle > 360 || angle <= 60)
        return 0;
      return ((int) angle / 60);
    }

    public static double getAngleFromCoord(double x, double y)
    {
        double angle;
        angle = abs(atan2(x, y) * 180.0D / PI);
        if (x < 0)
            angle = 360 - angle;
        return angle;
    }

In this case the angle is 60 because 360 / 6 options = 60, and if the mouse is within radius of 15 from the screen center then no action is performed (-1)

And then the rendering can be done.

    @Override
      public void render(Minecraft minecraft, PlayerEntity player, MatrixStack stack, IPlayerAction capability, float screenWidth, float screenHeight)
    {
      float x = screenWidth / 2.0F;
      float y = screenHeight / 2.0F;
      float size = 60;
      int type = getHoveringType(minecraft);
      AVAClientUtil.blit(stack, type == -1 ? UI_BG_LIT : UI_BG, x - size, y - size, x + size, y + size);
      for (int i = 0; i < 6; i++)
      {
        stack.push();
        stack.translate(x, y, 0.0F);
        stack.rotate(Vector3f.ZP.rotationDegrees(i * 60));
        stack.translate(-x, -y, 0.0F);
        AVAClientUtil.blit(stack, type == i ? UI_BG_2_LIT : UI_BG_2, x - size, y - size, x + size, y + size);
        stack.pop();
      }
      AVAClientUtil.blit(stack, UI_BG_ICON_LAYER, x - size, y - size, x + size, y + size);
    }

    public static void blit(MatrixStack stack, @Nullable ResourceLocation texture, float x1, float y1, float x2, float y2)
    {
        if (texture != null)
            Minecraft.getInstance().getTextureManager().bindTexture(texture);
        Matrix4f matrix = stack.getLast().getMatrix();
        Tessellator tessellator = Tessellator.getInstance();
        BufferBuilder bufferbuilder = tessellator.getBuffer();
        bufferbuilder.begin(7, DefaultVertexFormats.POSITION_TEX);
        bufferbuilder.pos(matrix, x1, y2, 0.0F).tex(0.0F, 1.0F).endVertex();
        bufferbuilder.pos(matrix, x2, y2, 0.0F).tex(1.0F, 1.0F).endVertex();
        bufferbuilder.pos(matrix, x2, y1, 0.0F).tex(1.0F, 0.0F).endVertex();
        bufferbuilder.pos(matrix, x1, y1, 0.0F).tex(0.0F, 0.0F).endVertex();
        tessellator.draw();
    }

If the type is -1, then the circle at the center use the blue texture.

Otherwise if the type equals the index of the option, it uses the blue texture (or just use RenderSystem.color4f).

By rotating an identical texture we don't need to have 6 different textures for each option.

 

 

        public static boolean ACTIVE = false;
        public static Vector3d VEC = null;
        @Override
        public void tick(Minecraft minecraft, PlayerEntity player)
        {
            if (minecraft.isGameFocused() && minecraft.currentScreen == null && AVAClientConfig.ENABLE_PING_HOTKEY.get())
            {
                if (AVAClientUtil.middleMouseDown())
                {
                    if (!ACTIVE)
                    {
                        Vector3d eye = AVAWeaponUtil.getEyePositionFor(player);
                        BlockRayTraceResult result = player.world.rayTraceBlocks(new RayTraceContext(eye, eye.add(player.getLookVec().scale(100.0F)), RayTraceContext.BlockMode.VISUAL, RayTraceContext.FluidMode.NONE, player));
                        if (result.getType() != RayTraceResult.Type.MISS)
                        {
                            ACTIVE = true;
                            minecraft.mouseHelper.ungrabMouse();
                            if (minecraft.gameSettings.keyBindPickBlock.getKey().getKeyCode() == GLFW.GLFW_MOUSE_BUTTON_MIDDLE)
                                AVAClientUtil.unpressKeybind(minecraft.gameSettings.keyBindPickBlock);
                            VEC = result.getHitVec();
                        }
                    }
                }
                else
                {
                    int type = getHoveringType(minecraft);
                    if (ACTIVE && VEC != null && type != -1)
                        AVAPackets.INSTANCE.sendToServer(new PingMessage(VEC, ActivePingEffect.Type.values()[type]));
                    reset(minecraft);
                }
            }
            else reset(minecraft);
        }

If player's focusing, the middle mouse is down, then we check if there's terrain (block) in player's sight within 100 blocks, if not, reset the vec, regrab the mouse and set active to false.

MouseHelper#ungrabMouse call allows players to move mouse around the screen.

 

Once we have the VEC set (where the ping will occur), we can render a line between the center of the screen and the ping location. (White line)

AxLrJ4e.png

{
          ActiveRenderInfo info = minecraft.gameRenderer.getActiveRenderInfo();
          Vector3f view = info.getViewVector();
          Vector3d vec = info.getProjectedView().add(view.getX(), view.getY(), view.getZ());
          drawLine(stack, (float) vec.x, (float) vec.y, (float) vec.z, (float) pingVec.x, (float) pingVec.y, (float) pingVec.z, 255, 255, 255, 1.0F)
  }

    public static void drawLine(MatrixStack stack, float x1, float y1, float z1, float x2, float y2, float z2, int r, int g, int b, float a)
    {
        AVAClientUtil.drawTransparent(true);
        IRenderTypeBuffer.Impl impl = IRenderTypeBuffer.getImpl(Tessellator.getInstance().getBuffer());
        IVertexBuilder builder = impl.getBuffer(RenderType.LINES);
        Vector3d view = Minecraft.getInstance().gameRenderer.getActiveRenderInfo().getProjectedView();
        stack.push();
        stack.translate(-view.x, -view.y, -view.z);
        Matrix4f matrix = stack.getLast().getMatrix();
        builder.pos(matrix, x1, y1, z1).color(r, g, b, (int) (a * 255.0F)).endVertex();
        builder.pos(matrix, x2, y2, z2).color(r, g, b, (int) (a * 255.0F)).endVertex();
        stack.pop();
        impl.finish();
        AVAClientUtil.drawTransparent(false);
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
    }
                      
                      
                      

If you did not add the look offset to the camera position it will not be visible.

 

Then we send a packet to the server to notify all other players that I've ping the location.

Once players receives the location of the pings, it's time to render them.

                RenderSystem.disableDepthTest();
                AVAClientUtil.drawTransparent(true);
                Vector3d view = Minecraft.getInstance().gameRenderer.getActiveRenderInfo().getProjectedView();
                Vector3d vec = activePing.getVec();
                double distance = activePing.getVec().distanceTo(view);
                if (distance > 100.0F)
                    continue;
                renderObjectAt(minecraft, activePing, world, stack, (float) (distance / 20.0F), 0.0F, activePing.getTexture());
                double x = vec.x - view.getX();
                double y = vec.y - view.getY();
                double z = vec.z - view.getZ();
                stack.push();
                stack.translate(x, y, z);
                stack.rotate(minecraft.getRenderManager().getCameraOrientation());
                stack.rotate(Vector3f.ZP.rotationDegrees(180.0F));
                float size = (float) (distance / 200.0F);
                stack.scale(size, size, size);
                IRenderTypeBuffer.Impl impl = IRenderTypeBuffer.getImpl(Tessellator.getInstance().getBuffer());
                String text = AVACommonUtil.round(distance, 2) + "m";
                minecraft.fontRenderer.func_243247_a(new StringTextComponent(text), -minecraft.fontRenderer.getStringWidth(text) / 2.0F, 10, AVAConstants.AVA_HUD_TEXT_WHITE, false, stack.getLast().getMatrix(), impl, true, 0, 15728880);
                impl.finish();
                stack.pop();

    private static void renderObjectAt(Minecraft minecraft, EnvironmentObjectEffect object, World world, MatrixStack stack, float size, float offsetScale, ResourceLocation texture)
    {
        Vector3d vec = object.getVec();
        stack.push();
        Vector3d view = Minecraft.getInstance().gameRenderer.getActiveRenderInfo().getProjectedView();
        double x = vec.x - view.getX();
        double y = vec.y - view.getY();
        double z = vec.z - view.getZ();
        if (Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2)) > 100.0F)
            return;
        stack.translate(x, y, z);
        Direction direction = object.getDirection();
        if (direction != null)
        {
            Vector3i offset = direction.getDirectionVec();
            stack.translate(offset.getX() * offsetScale, offset.getY() * offsetScale, offset.getZ() * offsetScale);
            rotateByDirection(stack, direction);
        }
        else
        {
            stack.rotate(minecraft.getRenderManager().getCameraOrientation());
            stack.rotate(Vector3f.ZP.rotationDegrees(180.0F));
        }
        if (object.doBlend())
        {
            Color colour = new Color(world.getBlockState(object.getPos()).getMaterialColor(world, object.getPos()).colorValue);
            RenderSystem.color4f(colour.getRed() / 255.0F, colour.getGreen() / 255.0F, colour.getBlue() / 255.0F, object.getTransparency());
        }
        AVAClientUtil.blit(stack, texture, -size, -size, size, size);
        RenderSystem.color4f(1.0F, 1.0F, 1.0f, 1.0F);
        stack.pop();
    }

QTSIc8A.png

T1IxILB.png

 

In the code I change the size of the object according to the distance ( (distance / 20.0F) ), so the object will stay at similar size no matter the distance between them, and so does the text size.

The direction is always null, and doBlend is false. They are used in my other renderings.

 

 

 

Edited by poopoodice
  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • I have my structure in .nbt format. When I click with my custom item on the ground it should place the structure. However, it only places one block from the bottom corner of the structure. When I try to walk, the player moves very slowly, like the wall structure is there. For some reason it's just not visible. After reopening the world, the structure is visible. My code:  if (world instanceof ServerLevel _serverworld) { StructureTemplate template = _serverworld.getStructureManager().getOrCreate(new ResourceLocation(TransportMod.MOD_ID, name)); if (template != null) { template.placeInWorld(_serverworld, new BlockPos((int) x, (int) y, (int) z), new BlockPos((int) x, (int) y, (int) z), new StructurePlaceSettings().setRotation(Rotation.NONE).setMirror(Mirror.NONE).setIgnoreEntities(true), _serverworld.random, 3); } } How make the structure visible right after placing? Or am I doing something wrong? Thanks in advance!
    • Hey im trying to make server side mod with some basic server commands currently im stuck getting text colored after loading it from a txt file i got the text show up fine but how would i go about getting it recognize color codes like §b or \u00A7b
    • do you mean going to java 7 or java 8 update 320?
    • I downloaded the server files for a 1.16.5 forge server server (forge-1.16.5-36.2.20) but when i run the .jar file it doesn't start up.   I also tried running it with the following .bat script : "C:\Program Files\Java\jre1.8.0_321\bin\javaw.exe" -Xmx4096M -Xms4096M -jar forgeserver.jar pause when I run the server it gives this log: [25jan2022 14:41:46.874] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher running: args [--gameDir, ., --launchTarget, fmlserver, --fml.forgeVersion, 36.2.20, --fml.mcpVersion, 20210115.111550, --fml.mcVersion, 1.16.5, --fml.forgeGroup, net.minecraftforge] [25jan2022 14:41:46.876] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher 8.0.9+86+master.3cf110c starting: java version 1.8.0_321 by Oracle Corporation [25jan2022 14:41:47.215] [main/INFO] [net.minecraftforge.fml.loading.FixSSL/CORE]: Added Lets Encrypt root certificates as additional trust [25jan2022 14:41:47.248] [main/INFO] [mixin/]: SpongePowered MIXIN Subsystem Version=0.8.4 Source=file:/C:/Users/USER/Desktop/folder%20folder/servers/servers/lege%20folder/libraries/org/spongepowered/mixin/0.8.4/mixin-0.8.4.jar Service=ModLauncher Env=SERVER [25jan2022 14:41:48.005] [main/INFO] [cpw.mods.modlauncher.LaunchServiceHandler/MODLAUNCHER]: Launching target 'fmlserver' with arguments [--gameDir, .] [25jan2022 14:41:48.063] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: java.lang.NoSuchMethodError: sun.security.util.ManifestEntryVerifier.<init>(Ljava/util/jar/Manifest;)V [25jan2022 14:41:48.064] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:     at cpw.mods.modlauncher.SecureJarHandler.createCodeSource(SecureJarHandler.java:66) [25jan2022 14:41:48.064] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:     at cpw.mods.modlauncher.TransformingClassLoader$DelegatedClassLoader.findClass(TransformingClassLoader.java:275) [25jan2022 14:41:48.065] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:     at cpw.mods.modlauncher.TransformingClassLoader.loadClass(TransformingClassLoader.java:136) [25jan2022 14:41:48.065] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:     at cpw.mods.modlauncher.TransformingClassLoader.loadClass(TransformingClassLoader.java:98) [25jan2022 14:41:48.065] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:     at java.lang.ClassLoader.loadClass(Unknown Source) [25jan2022 14:41:48.066] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:     at java.lang.Class.forName0(Native Method) [25jan2022 14:41:48.066] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:     at java.lang.Class.forName(Unknown Source) [25jan2022 14:41:48.066] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:     at net.minecraftforge.fml.loading.FMLServerLaunchProvider.lambda$launchService$0(FMLServerLaunchProvider.java:51) [25jan2022 14:41:48.067] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:     at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:37) [25jan2022 14:41:48.067] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:54) [25jan2022 14:41:48.067] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:72) [25jan2022 14:41:48.068] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:     at cpw.mods.modlauncher.Launcher.run(Launcher.java:82) [25jan2022 14:41:48.068] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:     at cpw.mods.modlauncher.Launcher.main(Launcher.java:66) [25jan2022 14:41:48.068] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:     at net.minecraftforge.server.ServerMain$Runner.runLauncher(ServerMain.java:63) [25jan2022 14:41:48.068] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:     at net.minecraftforge.server.ServerMain$Runner.access$100(ServerMain.java:60) [25jan2022 14:41:48.069] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:     at net.minecraftforge.server.ServerMain.main(ServerMain.java:57)    
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.