perf(web): eliminate putImageData via WebGL2 quad, sync frame pacing, and add PWA/Service Worker
Replaces `ctx2d.putImageData` with a WebGL2 texture quad in both 117HD and SD modes, aligns the TeaVM game loop with `requestAnimationFrame`, and adds PWA manifest and Service Worker asset caching for mobile/iOS Safari. ### WebGL2 Compositing & SD/HD Rendering - `WebHd117Renderer.java`, `WebGL2.java`: - Decoupled `ensureOverlayContext()` / `ensureOverlayReady()` so the WebGL2 2D overlay quad initializes without compiling 3D shadow/scene shaders when 117HD is off. - Added `drawHudPixels` and `drawHudCanvas` compositing onto `#gl-canvas` with `useBlend` control (disabling `GL_BLEND` for opaque SD frames). - `RSImageProducer.java`, `DesktopHooks.java`: - Routed `RSImageProducer.drawGraphics` and modern HUD draws (`drawModernMinimap`, `drawModernChat`, `drawModernTab`) through the WebGL2 quad whenever `ensureOverlayReady()` succeeds, clearing `#game-canvas` once to avoid `putImageData` overhead. ### Frame Pacing & Mobile PWA - `RSApplet.java`: - Added TeaVM `@Async` `awaitAnimationFrame()` backed by `window.requestAnimationFrame` for `delay <= 16` and capped catch-up ticks per frame (`loopTicks >= 2`). - `index.html`, `manifest.webmanifest`, `sw.js`: - Added Apple Web App meta tags, `manifest.webmanifest`, and `sw.js` Service Worker registration with shell precaching, network-first navigation, and stale-while-revalidate static asset caching.
This commit is contained in:
1 parent
3331ab745b
commit
33235cec55
8 files changed
+412
-41
No files matched your search
@@ -164,11 +164,14 @@ public final class DesktopHooks {
|
||||
HTMLCanvasElement canvas = RSImageProducer.getCanvas();
|
||||
if (canvas == null) return;
|
||||
Uint8ClampedArray rgba = toRgbaOpaque(minimapPixels, 172 * 156);
|
||||
jsDrawModernMinimap(canvas, rgba, windowWidth);
|
||||
boolean toGpu = WebHd117Renderer.ensureOverlayReady();
|
||||
HTMLCanvasElement hudC = jsDrawModernMinimap(canvas, rgba, windowWidth, !toGpu);
|
||||
if (toGpu && hudC != null) {
|
||||
WebHd117Renderer.drawHudCanvas(hudC, windowWidth - 249, 0, 249, 168);
|
||||
}
|
||||
}
|
||||
|
||||
public static void drawModernChat(Object graphics, int[] chatContentPixels, int[] chatButtonsPixels, int windowHeight) {
|
||||
ensureHudAssets();
|
||||
ensureHudAssets();
|
||||
HTMLCanvasElement canvas = RSImageProducer.getCanvas();
|
||||
if (canvas == null) return;
|
||||
@@ -176,7 +179,11 @@ public final class DesktopHooks {
|
||||
? toRgbaOpaque(chatContentPixels, 479 * 96) : null;
|
||||
Uint8ClampedArray buttonsRgba = (chatButtonsPixels != null && chatButtonsPixels.length >= 496 * 50)
|
||||
? toRgbaOpaque(chatButtonsPixels, 496 * 50) : null;
|
||||
jsDrawModernChat(canvas, contentRgba, buttonsRgba, windowHeight);
|
||||
boolean toGpu = WebHd117Renderer.ensureOverlayReady();
|
||||
HTMLCanvasElement hudC = jsDrawModernChat(canvas, contentRgba, buttonsRgba, windowHeight, !toGpu);
|
||||
if (toGpu && hudC != null) {
|
||||
WebHd117Renderer.drawHudCanvas(hudC, 0, windowHeight - 165, 519, 165);
|
||||
}
|
||||
}
|
||||
|
||||
public static void drawModernTab(Object graphics, int[] topTabPixels, int[] leftPillarTopPixels,
|
||||
@@ -241,7 +248,11 @@ public final class DesktopHooks {
|
||||
rgba.set(j + 3, (short) 0);
|
||||
}
|
||||
}
|
||||
jsDrawModernTab(canvas, rgba, windowWidth - 249, windowHeight - 343);
|
||||
boolean toGpu = WebHd117Renderer.ensureOverlayReady();
|
||||
HTMLCanvasElement hudC = jsDrawModernTab(canvas, rgba, windowWidth - 249, windowHeight - 343, !toGpu);
|
||||
if (toGpu && hudC != null) {
|
||||
WebHd117Renderer.drawHudCanvas(hudC, windowWidth - 249, windowHeight - 343, 249, 343);
|
||||
}
|
||||
}
|
||||
|
||||
public static Object getRenderGraphics(Object screenGraphics) { return screenGraphics; }
|
||||
@@ -384,6 +395,7 @@ public final class DesktopHooks {
|
||||
}
|
||||
|
||||
@JSBody(script =
|
||||
"if (typeof window.scapeStageW === 'number' && window.scapeStageW > 0) return window.scapeStageW;\n" +
|
||||
"var s = document.getElementById('stage');\n" +
|
||||
"var w = s ? s.clientWidth : window.innerWidth;\n" +
|
||||
"if (window.visualViewport) w = Math.min(w, Math.floor(window.visualViewport.width));\n" +
|
||||
@@ -391,6 +403,7 @@ public final class DesktopHooks {
|
||||
private static native int getStageWidth();
|
||||
|
||||
@JSBody(script =
|
||||
"if (typeof window.scapeStageH === 'number' && window.scapeStageH > 0) return window.scapeStageH;\n" +
|
||||
"var s = document.getElementById('stage');\n" +
|
||||
"var h = s ? s.clientHeight : window.innerHeight;\n" +
|
||||
"if (window.visualViewport) h = Math.min(h, Math.floor(window.visualViewport.height));\n" +
|
||||
@@ -420,30 +433,33 @@ public final class DesktopHooks {
|
||||
"};")
|
||||
private static native void jsInitHudAssets();
|
||||
|
||||
@JSBody(params = {"canvas", "rgba", "winW"}, script =
|
||||
"var h = window.scapeHud; if (!h || !h.minimapBezel.complete || !h.minimapHole.complete) return;\n" +
|
||||
@JSBody(params = {"canvas", "rgba", "winW", "draw2d"}, script =
|
||||
"var h = window.scapeHud; if (!h || !h.minimapBezel.complete || !h.minimapHole.complete) return null;\n" +
|
||||
"var id = h.mContentCtx.createImageData(172, 156); id.data.set(rgba); h.mContentCtx.putImageData(id, 0, 0);\n" +
|
||||
"var g = h.mcCtx; g.clearRect(0, 0, 249, 168);\n" +
|
||||
"g.globalCompositeOperation = 'source-over'; g.drawImage(h.mContent, 34, 4);\n" +
|
||||
"g.globalCompositeOperation = 'destination-in'; g.drawImage(h.minimapHole, 0, 0);\n" +
|
||||
"g.globalCompositeOperation = 'source-over'; g.drawImage(h.minimapBezel, 0, 0);\n" +
|
||||
"canvas.getContext('2d').drawImage(h.mc, winW - 249, 0);")
|
||||
private static native void jsDrawModernMinimap(HTMLCanvasElement canvas, Uint8ClampedArray rgba, int winW);
|
||||
"if (draw2d) canvas.getContext('2d').drawImage(h.mc, winW - 249, 0);\n" +
|
||||
"return h.mc;")
|
||||
private static native HTMLCanvasElement jsDrawModernMinimap(HTMLCanvasElement canvas, Uint8ClampedArray rgba, int winW, boolean draw2d);
|
||||
|
||||
@JSBody(params = {"canvas", "contentRgba", "buttonsRgba", "winH"}, script =
|
||||
"var h = window.scapeHud; if (!h || !h.chatFrame.complete) return;\n" +
|
||||
@JSBody(params = {"canvas", "contentRgba", "buttonsRgba", "winH", "draw2d"}, script =
|
||||
"var h = window.scapeHud; if (!h || !h.chatFrame.complete) return null;\n" +
|
||||
"var g = h.ccCtx; g.clearRect(0, 0, 519, 165);\n" +
|
||||
"g.globalCompositeOperation = 'source-over'; g.drawImage(h.chatFrame, 0, 0);\n" +
|
||||
"if (contentRgba) { var id1 = h.cContentCtx.createImageData(479, 96); id1.data.set(contentRgba); h.cContentCtx.putImageData(id1, 0, 0); g.drawImage(h.cContent, 17, 19); }\n" +
|
||||
"if (buttonsRgba) { var id2 = h.cButtonsCtx.createImageData(496, 50); id2.data.set(buttonsRgba); h.cButtonsCtx.putImageData(id2, 0, 0); g.drawImage(h.cButtons, 0, 115); }\n" +
|
||||
"canvas.getContext('2d').drawImage(h.cc, 0, winH - 165);")
|
||||
private static native void jsDrawModernChat(HTMLCanvasElement canvas, Uint8ClampedArray contentRgba, Uint8ClampedArray buttonsRgba, int winH);
|
||||
"if (draw2d) canvas.getContext('2d').drawImage(h.cc, 0, winH - 165);\n" +
|
||||
"return h.cc;")
|
||||
private static native HTMLCanvasElement jsDrawModernChat(HTMLCanvasElement canvas, Uint8ClampedArray contentRgba, Uint8ClampedArray buttonsRgba, int winH, boolean draw2d);
|
||||
|
||||
@JSBody(params = {"canvas", "rgba", "x", "y"}, script =
|
||||
"var hh = window.scapeHud; if (!hh) return;\n" +
|
||||
@JSBody(params = {"canvas", "rgba", "x", "y", "draw2d"}, script =
|
||||
"var hh = window.scapeHud; if (!hh) return null;\n" +
|
||||
"var id = hh.tcCtx.createImageData(249, 343); id.data.set(rgba); hh.tcCtx.putImageData(id, 0, 0);\n" +
|
||||
"canvas.getContext('2d').drawImage(hh.tc, x, y);")
|
||||
private static native void jsDrawModernTab(HTMLCanvasElement canvas, Uint8ClampedArray rgba, int x, int y);
|
||||
"if (draw2d) canvas.getContext('2d').drawImage(hh.tc, x, y);\n" +
|
||||
"return hh.tc;")
|
||||
private static native HTMLCanvasElement jsDrawModernTab(HTMLCanvasElement canvas, Uint8ClampedArray rgba, int x, int y, boolean draw2d);
|
||||
|
||||
@JSBody(params = {"rgba", "fx", "fy", "w", "h"}, script =
|
||||
"if (!window.scapeLoginBuf) {\n" +
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package org.scape2006.web;
|
||||
|
||||
import org.teavm.interop.Async;
|
||||
import org.teavm.interop.AsyncCallback;
|
||||
import org.teavm.jso.JSBody;
|
||||
import org.teavm.jso.JSExceptions;
|
||||
import org.teavm.jso.JSFunctor;
|
||||
@@ -346,6 +348,23 @@ public class RSApplet implements Runnable {
|
||||
@JSBody(params = {"cb"}, script = "setTimeout(function() { cb(); }, 40);")
|
||||
private static native void jsScheduleTouchUp(TouchUpCallback cb);
|
||||
|
||||
@JSFunctor
|
||||
public interface RafCallback extends JSObject {
|
||||
void onFrame();
|
||||
}
|
||||
|
||||
@Async
|
||||
private static native void awaitAnimationFrame();
|
||||
|
||||
private static void awaitAnimationFrame(AsyncCallback<Void> callback) {
|
||||
jsRequestFrame(() -> callback.complete(null));
|
||||
}
|
||||
|
||||
@JSBody(params = {"cb"}, script =
|
||||
"if (window.requestAnimationFrame) { window.requestAnimationFrame(function() { cb(); }); }\n" +
|
||||
"else { setTimeout(function() { cb(); }, 16); }")
|
||||
private static native void jsRequestFrame(RafCallback cb);
|
||||
|
||||
private void checkShowMobileKeyboard(Game g, int gx, int gy) {
|
||||
if (PluginPanelUI.isSearchFocused()) {
|
||||
jsFocusMobileKeyboard();
|
||||
@@ -616,11 +635,20 @@ public class RSApplet implements Runnable {
|
||||
|
||||
if (delay < minDelay) delay = minDelay;
|
||||
|
||||
try {
|
||||
Thread.sleep(delay);
|
||||
} catch (InterruptedException ignored) {}
|
||||
if (delay <= 16) {
|
||||
awaitAnimationFrame();
|
||||
} else {
|
||||
try {
|
||||
Thread.sleep(delay);
|
||||
} catch (InterruptedException ignored) {}
|
||||
}
|
||||
|
||||
int loopTicks = 0;
|
||||
for (; count < 256; count += ratio) {
|
||||
if (loopTicks++ >= 2) {
|
||||
count = 0;
|
||||
break;
|
||||
}
|
||||
clickMode3 = clickMode1;
|
||||
saveClickX = clickX;
|
||||
saveClickY = clickY;
|
||||
|
||||
@@ -23,7 +23,9 @@ public final class RSImageProducer {
|
||||
|
||||
private static HTMLCanvasElement canvas;
|
||||
private static CanvasRenderingContext2D ctx2d;
|
||||
private static boolean cleared2dForGpu = false;
|
||||
private ImageData imageData;
|
||||
private org.teavm.jso.JSObject imageDataU32;
|
||||
|
||||
public RSImageProducer(int width, int height, Object component) {
|
||||
this.anInt316 = width;
|
||||
@@ -32,6 +34,7 @@ public final class RSImageProducer {
|
||||
ensureCanvas();
|
||||
if (ctx2d != null) {
|
||||
this.imageData = ctx2d.createImageData(width, height);
|
||||
this.imageDataU32 = wrapImageDataU32(this.imageData);
|
||||
}
|
||||
initDrawingArea();
|
||||
}
|
||||
@@ -46,11 +49,19 @@ public final class RSImageProducer {
|
||||
HTMLDocument doc = HTMLDocument.current();
|
||||
canvas = (HTMLCanvasElement) doc.getElementById("game-canvas");
|
||||
if (canvas != null) {
|
||||
ctx2d = (CanvasRenderingContext2D) canvas.getContext("2d");
|
||||
ctx2d = create2dContext(canvas);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@JSBody(params = {"canvas"}, script =
|
||||
"try { return canvas.getContext('2d', { alpha: true, desynchronized: true }) || canvas.getContext('2d'); }\n" +
|
||||
"catch (_) { return canvas.getContext('2d'); }")
|
||||
private static native CanvasRenderingContext2D create2dContext(HTMLCanvasElement canvas);
|
||||
|
||||
@JSBody(params = {"imgData"}, script = "return new Uint32Array(imgData.data.buffer);")
|
||||
private static native org.teavm.jso.JSObject wrapImageDataU32(ImageData imgData);
|
||||
|
||||
public void initDrawingArea() {
|
||||
DrawingArea.initDrawingArea(anInt317, anInt316, anIntArray315);
|
||||
Texture.textureInt1 = anInt316 / 2;
|
||||
@@ -64,38 +75,47 @@ public final class RSImageProducer {
|
||||
public void drawGraphics(int y, Object graphics, int x) {
|
||||
if (anIntArray315 == null) return;
|
||||
if (ClientSettings.isDesktopModern() && !ModernLayout.inGame) {
|
||||
cleared2dForGpu = false;
|
||||
DesktopHooks.drawScaledLoginTile(graphics, anIntArray315, x, y, anInt316, anInt317);
|
||||
return;
|
||||
}
|
||||
ensureCanvas();
|
||||
if (ctx2d == null) return;
|
||||
boolean gpuViewport = HD117Pipeline.isGpuActive() && anInt316 == ClientSettings.VIEWPORT_WIDTH && anInt317 == ClientSettings.VIEWPORT_HEIGHT;
|
||||
if (WebHd117Renderer.ensureOverlayReady()) {
|
||||
if (!cleared2dForGpu && canvas != null) {
|
||||
ctx2d.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
|
||||
cleared2dForGpu = true;
|
||||
}
|
||||
WebHd117Renderer.drawHudPixels(anIntArray315, x, y, anInt316, anInt317, gpuViewport);
|
||||
return;
|
||||
}
|
||||
cleared2dForGpu = false;
|
||||
if (imageData == null || imageData.getWidth() != anInt316 || imageData.getHeight() != anInt317) {
|
||||
imageData = ctx2d.createImageData(anInt316, anInt317);
|
||||
imageDataU32 = wrapImageDataU32(imageData);
|
||||
}
|
||||
boolean gpuViewport = HD117Pipeline.isGpuActive() && anInt316 == ClientSettings.VIEWPORT_WIDTH && anInt317 == ClientSettings.VIEWPORT_HEIGHT;
|
||||
if (gpuViewport) {
|
||||
blitOverlayPixelsToImageData(anIntArray315, imageData);
|
||||
blitOverlayPixelsToImageData(anIntArray315, imageDataU32);
|
||||
} else {
|
||||
blitPixelsToImageData(anIntArray315, imageData);
|
||||
blitPixelsToImageData(anIntArray315, imageDataU32);
|
||||
}
|
||||
ctx2d.putImageData(imageData, x, y);
|
||||
}
|
||||
|
||||
@JSBody(params = {"pixels", "imgData"}, script =
|
||||
"var dst = new Uint32Array(imgData.data.buffer);\n" +
|
||||
@JSBody(params = {"pixels", "dst"}, script =
|
||||
"var len = pixels.length;\n" +
|
||||
"for (var i = 0; i < len; i++) {\n" +
|
||||
" var c = pixels[i];\n" +
|
||||
" dst[i] = -16777216 | ((c & 255) << 16) | (c & 65280) | ((c >> 16) & 255);\n" +
|
||||
"}")
|
||||
private static native void blitPixelsToImageData(@JSByRef int[] pixels, ImageData imgData);
|
||||
private static native void blitPixelsToImageData(@JSByRef int[] pixels, org.teavm.jso.JSObject dst);
|
||||
|
||||
@JSBody(params = {"pixels", "imgData"}, script =
|
||||
"var dst = new Uint32Array(imgData.data.buffer);\n" +
|
||||
@JSBody(params = {"pixels", "dst"}, script =
|
||||
"var len = pixels.length;\n" +
|
||||
"for (var i = 0; i < len; i++) {\n" +
|
||||
" var c = pixels[i];\n" +
|
||||
" dst[i] = (c === 66051) ? 0 : (-16777216 | ((c & 255) << 16) | (c & 65280) | ((c >> 16) & 255));\n" +
|
||||
"}")
|
||||
private static native void blitOverlayPixelsToImageData(@JSByRef int[] pixels, ImageData imgData);
|
||||
private static native void blitOverlayPixelsToImageData(@JSByRef int[] pixels, org.teavm.jso.JSObject dst);
|
||||
}
|
||||
@@ -14,7 +14,9 @@ public interface WebGL2 extends JSObject {
|
||||
int COLOR_BUFFER_BIT = 0x00004000;
|
||||
int DEPTH_BUFFER_BIT = 0x00000100;
|
||||
int TRIANGLES = 0x0004;
|
||||
int TRIANGLE_STRIP = 0x0005;
|
||||
int LEQUAL = 0x0203;
|
||||
int ONE = 1;
|
||||
int SRC_ALPHA = 0x0302;
|
||||
int ONE_MINUS_SRC_ALPHA = 0x0303;
|
||||
int DEPTH_TEST = 0x0B71;
|
||||
@@ -63,7 +65,7 @@ public interface WebGL2 extends JSObject {
|
||||
interface UniformLocation extends JSObject {}
|
||||
|
||||
@JSBody(params = {"canvas"}, script =
|
||||
"return canvas.getContext('webgl2', { alpha: true, antialias: false, preserveDrawingBuffer: false });")
|
||||
"return canvas.getContext('webgl2', { alpha: false, antialias: false, preserveDrawingBuffer: false, powerPreference: 'high-performance', desynchronized: true });")
|
||||
static WebGL2 getContext(HTMLCanvasElement canvas) {
|
||||
return null;
|
||||
}
|
||||
@@ -114,6 +116,7 @@ public interface WebGL2 extends JSObject {
|
||||
void bindTexture(int target, Texture texture);
|
||||
void texParameteri(int target, int pname, int param);
|
||||
void texImage2D(int target, int level, int internalformat, int width, int height, int border, int format, int type, Uint8Array pixels);
|
||||
void texImage2D(int target, int level, int internalformat, int format, int type, HTMLCanvasElement canvas);
|
||||
void texImage3D(int target, int level, int internalformat, int width, int height, int depth, int border, int format, int type, Uint8Array pixels);
|
||||
void generateMipmap(int target);
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.scape2006.web;
|
||||
|
||||
import org.teavm.jso.JSBody;
|
||||
import org.teavm.jso.JSByRef;
|
||||
import org.teavm.jso.JSObject;
|
||||
import org.teavm.jso.dom.html.HTMLCanvasElement;
|
||||
import org.teavm.jso.dom.html.HTMLDocument;
|
||||
import org.teavm.jso.typedarrays.Float32Array;
|
||||
@@ -18,16 +19,48 @@ import java.util.Arrays;
|
||||
*/
|
||||
public final class WebHd117Renderer implements HD117Pipeline.HdRendererBackend {
|
||||
|
||||
private static final String OVERLAY_VERT =
|
||||
"#version 300 es\n" +
|
||||
"layout(location = 0) in vec2 aPos;\n" +
|
||||
"uniform vec4 uRect;\n" +
|
||||
"out vec2 vUv;\n" +
|
||||
"void main() {\n" +
|
||||
" gl_Position = vec4(mix(uRect.x, uRect.z, aPos.x), mix(uRect.y, uRect.w, aPos.y), 0.0, 1.0);\n" +
|
||||
" vUv = aPos;\n" +
|
||||
"}\n";
|
||||
|
||||
private static final String OVERLAY_FRAG =
|
||||
"#version 300 es\n" +
|
||||
"precision mediump float;\n" +
|
||||
"in vec2 vUv;\n" +
|
||||
"uniform sampler2D uTex;\n" +
|
||||
"out vec4 fragColor;\n" +
|
||||
"void main() {\n" +
|
||||
" fragColor = texture(uTex, vUv);\n" +
|
||||
"}\n";
|
||||
|
||||
private static WebHd117Renderer instance = null;
|
||||
private int shadowMapSize = 1024;
|
||||
private static final boolean IS_IOS = jsIsIos();
|
||||
private final HdProceduralGenerator procGen = new HdProceduralGenerator();
|
||||
private final HdSceneUploader sceneUploader = new HdSceneUploader();
|
||||
|
||||
private WebGL2 gl = null;
|
||||
private boolean glInitialized = false;
|
||||
private boolean glFailed = false;
|
||||
private boolean overlayInitialized = false;
|
||||
private boolean overlayFailed = false;
|
||||
|
||||
private WebGL2.Program shadowProgram = null;
|
||||
private WebGL2.Program sceneProgram = null;
|
||||
private WebGL2.Program overlayProgram = null;
|
||||
private WebGL2.UniformLocation uOverlayRect = null;
|
||||
private WebGL2.UniformLocation uOverlayTex = null;
|
||||
private WebGL2.Buffer overlayVbo = null;
|
||||
private WebGL2.Texture overlayTex = null;
|
||||
private final Float32Array overlayRectF32 = Float32Array.create(4);
|
||||
private Uint8Array hudScratchU8 = Uint8Array.create(0);
|
||||
private JSObject hudScratchU32 = null;
|
||||
|
||||
private WebGL2.UniformLocation uShadowLightViewProj;
|
||||
private WebGL2.UniformLocation uShadowDiffuseArray;
|
||||
@@ -112,6 +145,7 @@ public final class WebHd117Renderer implements HD117Pipeline.HdRendererBackend {
|
||||
private WorldController lastWorldController = null;
|
||||
|
||||
public WebHd117Renderer() {
|
||||
instance = this;
|
||||
float len = (float) Math.sqrt(sunDir[0] * sunDir[0] + sunDir[1] * sunDir[1] + sunDir[2] * sunDir[2]);
|
||||
sunDir[0] /= len;
|
||||
sunDir[1] /= len;
|
||||
@@ -238,8 +272,8 @@ public final class WebHd117Renderer implements HD117Pipeline.HdRendererBackend {
|
||||
computeCameraAndLightMatrices(width, height);
|
||||
selectActiveLights();
|
||||
|
||||
boolean shadowsOn = HD117Pipeline.cfgShadowsEnabled;
|
||||
int desiredShadowSize = HD117Pipeline.cfgShadowResolution;
|
||||
boolean shadowsOn = HD117Pipeline.cfgShadowsEnabled && !IS_IOS;
|
||||
int desiredShadowSize = IS_IOS ? 512 : HD117Pipeline.cfgShadowResolution;
|
||||
if (shadowsOn && desiredShadowSize != shadowMapSize &&
|
||||
(desiredShadowSize == 512 || desiredShadowSize == 1024 || desiredShadowSize == 2048)) {
|
||||
shadowMapSize = desiredShadowSize;
|
||||
@@ -541,16 +575,57 @@ public final class WebHd117Renderer implements HD117Pipeline.HdRendererBackend {
|
||||
return tex;
|
||||
}
|
||||
|
||||
private boolean ensureOverlayContext() {
|
||||
if (overlayInitialized) return true;
|
||||
if (overlayFailed) return false;
|
||||
try {
|
||||
if (glCanvas == null) {
|
||||
glCanvas = (HTMLCanvasElement) HTMLDocument.current().getElementById("gl-canvas");
|
||||
}
|
||||
if (glCanvas == null) {
|
||||
overlayFailed = true;
|
||||
return false;
|
||||
}
|
||||
if (gl == null) {
|
||||
gl = WebGL2.getContext(glCanvas);
|
||||
}
|
||||
if (gl == null) {
|
||||
overlayFailed = true;
|
||||
return false;
|
||||
}
|
||||
overlayProgram = createProgram(OVERLAY_VERT, OVERLAY_FRAG);
|
||||
if (overlayProgram == null) {
|
||||
overlayFailed = true;
|
||||
return false;
|
||||
}
|
||||
uOverlayRect = gl.getUniformLocation(overlayProgram, "uRect");
|
||||
uOverlayTex = gl.getUniformLocation(overlayProgram, "uTex");
|
||||
overlayVbo = gl.createBuffer();
|
||||
gl.bindBuffer(WebGL2.ARRAY_BUFFER, overlayVbo);
|
||||
Float32Array quad = Float32Array.create(8);
|
||||
quad.set(0, 0.0f); quad.set(1, 0.0f);
|
||||
quad.set(2, 0.0f); quad.set(3, 1.0f);
|
||||
quad.set(4, 1.0f); quad.set(5, 0.0f);
|
||||
quad.set(6, 1.0f); quad.set(7, 1.0f);
|
||||
gl.bufferData(WebGL2.ARRAY_BUFFER, quad, WebGL2.STATIC_DRAW);
|
||||
overlayTex = gl.createTexture();
|
||||
gl.bindTexture(WebGL2.TEXTURE_2D, overlayTex);
|
||||
gl.texParameteri(WebGL2.TEXTURE_2D, WebGL2.TEXTURE_MIN_FILTER, WebGL2.NEAREST);
|
||||
gl.texParameteri(WebGL2.TEXTURE_2D, WebGL2.TEXTURE_MAG_FILTER, WebGL2.NEAREST);
|
||||
gl.texParameteri(WebGL2.TEXTURE_2D, WebGL2.TEXTURE_WRAP_S, WebGL2.CLAMP_TO_EDGE);
|
||||
gl.texParameteri(WebGL2.TEXTURE_2D, WebGL2.TEXTURE_WRAP_T, WebGL2.CLAMP_TO_EDGE);
|
||||
overlayInitialized = true;
|
||||
return true;
|
||||
} catch (Throwable t) {
|
||||
overlayFailed = true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean ensureGlContext() {
|
||||
if (glInitialized) return true;
|
||||
try {
|
||||
glCanvas = (HTMLCanvasElement) HTMLDocument.current().getElementById("gl-canvas");
|
||||
if (glCanvas == null) {
|
||||
glFailed = true;
|
||||
return false;
|
||||
}
|
||||
gl = WebGL2.getContext(glCanvas);
|
||||
if (gl == null) {
|
||||
if (!ensureOverlayContext()) {
|
||||
glFailed = true;
|
||||
return false;
|
||||
}
|
||||
@@ -629,12 +704,112 @@ public final class WebHd117Renderer implements HD117Pipeline.HdRendererBackend {
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isOverlayReady() {
|
||||
return instance != null && instance.overlayInitialized && instance.overlayProgram != null && instance.glCanvas != null;
|
||||
}
|
||||
|
||||
public static boolean ensureOverlayReady() {
|
||||
if (isOverlayReady()) return true;
|
||||
if (instance == null) {
|
||||
new WebHd117Renderer();
|
||||
}
|
||||
return instance.ensureOverlayContext();
|
||||
}
|
||||
|
||||
public static void drawHudPixels(int[] pixels, int x, int y, int w, int h, boolean transparentSentinel) {
|
||||
if (!isOverlayReady() || pixels == null || w <= 0 || h <= 0) return;
|
||||
instance.drawHudPixelsImpl(pixels, x, y, w, h, transparentSentinel);
|
||||
}
|
||||
|
||||
public static void drawHudCanvas(HTMLCanvasElement srcCanvas, int x, int y, int w, int h) {
|
||||
if (!isOverlayReady() || srcCanvas == null || w <= 0 || h <= 0) return;
|
||||
instance.drawHudCanvasImpl(srcCanvas, x, y, w, h);
|
||||
}
|
||||
|
||||
private void drawHudPixelsImpl(int[] pixels, int x, int y, int w, int h, boolean transparentSentinel) {
|
||||
int neededBytes = w * h * 4;
|
||||
if (hudScratchU8.getLength() < neededBytes) {
|
||||
hudScratchU8 = Uint8Array.create(neededBytes);
|
||||
hudScratchU32 = wrapUint32View(hudScratchU8);
|
||||
}
|
||||
packHudPixels(pixels, hudScratchU32, w * h, transparentSentinel);
|
||||
gl.activeTexture(WebGL2.TEXTURE0);
|
||||
gl.bindTexture(WebGL2.TEXTURE_2D, overlayTex);
|
||||
gl.texImage2D(WebGL2.TEXTURE_2D, 0, WebGL2.RGBA, w, h, 0, WebGL2.RGBA, WebGL2.UNSIGNED_BYTE, uint8Subarray(hudScratchU8, 0, neededBytes));
|
||||
drawOverlayQuad(x, y, w, h, false, transparentSentinel);
|
||||
}
|
||||
|
||||
private void drawHudCanvasImpl(HTMLCanvasElement srcCanvas, int x, int y, int w, int h) {
|
||||
gl.activeTexture(WebGL2.TEXTURE0);
|
||||
gl.bindTexture(WebGL2.TEXTURE_2D, overlayTex);
|
||||
gl.texImage2D(WebGL2.TEXTURE_2D, 0, WebGL2.RGBA, WebGL2.RGBA, WebGL2.UNSIGNED_BYTE, srcCanvas);
|
||||
drawOverlayQuad(x, y, w, h, true, true);
|
||||
}
|
||||
|
||||
private void drawOverlayQuad(int x, int y, int w, int h, boolean premultiplied, boolean useBlend) {
|
||||
int cw = Math.max(1, glCanvas.getWidth());
|
||||
int ch = Math.max(1, glCanvas.getHeight());
|
||||
overlayRectF32.set(0, (2.0f * x) / cw - 1.0f);
|
||||
overlayRectF32.set(1, 1.0f - (2.0f * y) / ch);
|
||||
overlayRectF32.set(2, (2.0f * (x + w)) / cw - 1.0f);
|
||||
overlayRectF32.set(3, 1.0f - (2.0f * (y + h)) / ch);
|
||||
|
||||
gl.bindFramebuffer(WebGL2.FRAMEBUFFER, null);
|
||||
gl.viewport(0, 0, cw, ch);
|
||||
gl.disable(WebGL2.DEPTH_TEST);
|
||||
gl.depthMask(false);
|
||||
if (useBlend) {
|
||||
gl.enable(WebGL2.BLEND);
|
||||
gl.blendFunc(premultiplied ? WebGL2.ONE : WebGL2.SRC_ALPHA, WebGL2.ONE_MINUS_SRC_ALPHA);
|
||||
} else {
|
||||
gl.disable(WebGL2.BLEND);
|
||||
}
|
||||
|
||||
gl.useProgram(overlayProgram);
|
||||
gl.uniform1i(uOverlayTex, 0);
|
||||
gl.uniform4fv(uOverlayRect, overlayRectF32);
|
||||
gl.bindBuffer(WebGL2.ARRAY_BUFFER, overlayVbo);
|
||||
gl.enableVertexAttribArray(0);
|
||||
gl.vertexAttribPointer(0, 2, WebGL2.FLOAT, false, 0, 0);
|
||||
gl.drawArrays(WebGL2.TRIANGLE_STRIP, 0, 4);
|
||||
gl.disableVertexAttribArray(0);
|
||||
|
||||
gl.blendFunc(WebGL2.SRC_ALPHA, WebGL2.ONE_MINUS_SRC_ALPHA);
|
||||
gl.depthMask(true);
|
||||
gl.enable(WebGL2.DEPTH_TEST);
|
||||
}
|
||||
|
||||
@JSBody(params = {"u8"}, script = "return new Uint32Array(u8.buffer);")
|
||||
private static native JSObject wrapUint32View(Uint8Array u8);
|
||||
|
||||
@JSBody(params = {"arr", "start", "end"}, script = "return arr.subarray(start, end);")
|
||||
private static native Uint8Array uint8Subarray(Uint8Array arr, int start, int end);
|
||||
|
||||
@JSBody(params = {"pixels", "dst", "len", "sentinel"}, script =
|
||||
"if (sentinel) {\n" +
|
||||
" for (var i = 0; i < len; i++) {\n" +
|
||||
" var c = pixels[i];\n" +
|
||||
" dst[i] = (c === 66051) ? 0 : (-16777216 | ((c & 255) << 16) | (c & 65280) | ((c >> 16) & 255));\n" +
|
||||
" }\n" +
|
||||
"} else {\n" +
|
||||
" for (var i = 0; i < len; i++) {\n" +
|
||||
" var c = pixels[i];\n" +
|
||||
" dst[i] = -16777216 | ((c & 255) << 16) | (c & 65280) | ((c >> 16) & 255);\n" +
|
||||
" }\n" +
|
||||
"}")
|
||||
private static native void packHudPixels(@JSByRef int[] pixels, JSObject dst, int len, boolean sentinel);
|
||||
|
||||
@JSBody(params = {"gl"}, script = "gl.drawBuffers([0]);")
|
||||
private static native void drawBuffersNone(WebGL2 gl);
|
||||
|
||||
@JSBody(params = {"gl"}, script = "return gl.getError();")
|
||||
private static native int getGlError(WebGL2 gl);
|
||||
|
||||
@JSBody(script =
|
||||
"var ua = navigator.userAgent || '';\n" +
|
||||
"return /iPad|iPhone|iPod/.test(ua) || (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1);")
|
||||
private static native boolean jsIsIos();
|
||||
|
||||
private WebGL2.Program createProgram(String vertSrc, String fragSrc) {
|
||||
WebGL2.Shader vs = compileShader(WebGL2.VERTEX_SHADER, vertSrc);
|
||||
WebGL2.Shader fs = compileShader(WebGL2.FRAGMENT_SHADER, fragSrc);
|
||||
|
||||
@@ -2,13 +2,21 @@
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, interactive-widget=overlays-content" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover, interactive-widget=overlays-content" />
|
||||
<meta name="apple-mobile-web-app-capable" content="yes" />
|
||||
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
|
||||
<meta name="apple-mobile-web-app-title" content="2006Scape" />
|
||||
<meta name="mobile-web-app-capable" content="yes" />
|
||||
<meta name="theme-color" content="#000000" />
|
||||
<title>RuneScape - Free Multiplayer Online Game</title>
|
||||
<link rel="manifest" href="manifest.webmanifest" />
|
||||
<link rel="apple-touch-icon" href="navbar_mainmenu.gif" />
|
||||
<link rel="icon" href="navbar_mainmenu.gif" type="image/gif" />
|
||||
<style>
|
||||
html, body {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
|
||||
box-sizing: border-box;
|
||||
width: 100%;
|
||||
max-width: 100vw;
|
||||
height: 100%;
|
||||
@@ -606,6 +614,20 @@
|
||||
if (btnSaveReload) btnSaveReload.addEventListener('click', () => { saveOpts(false); window.location.reload(); });
|
||||
})();
|
||||
|
||||
function updateCachedStageSize() {
|
||||
const s = document.getElementById('stage');
|
||||
const nav = document.getElementById('rs-navbar');
|
||||
let w = s ? s.clientWidth : window.innerWidth;
|
||||
let h = s ? s.clientHeight : window.innerHeight;
|
||||
if (window.visualViewport) {
|
||||
w = Math.min(w, Math.floor(window.visualViewport.width));
|
||||
h = Math.min(h, Math.floor(window.visualViewport.height));
|
||||
}
|
||||
if (nav && nav.offsetHeight > 0) h -= 25;
|
||||
window.scapeStageW = Math.max(1, w);
|
||||
window.scapeStageH = Math.max(1, h);
|
||||
}
|
||||
|
||||
function fitFixedCanvasToViewport() {
|
||||
const footer = document.getElementById('rs-footer');
|
||||
const frame = document.getElementById('rs-frame');
|
||||
@@ -617,6 +639,7 @@
|
||||
frame.style.height = '100%';
|
||||
}
|
||||
if (navInner) navInner.style.transform = '';
|
||||
updateCachedStageSize();
|
||||
return;
|
||||
}
|
||||
if (frame) frame.style.height = '';
|
||||
@@ -650,6 +673,7 @@
|
||||
if (nav) nav.style.width = '765px';
|
||||
if (navInner) navInner.style.transform = '';
|
||||
}
|
||||
updateCachedStageSize();
|
||||
}
|
||||
window.addEventListener('resize', fitFixedCanvasToViewport);
|
||||
if (window.visualViewport) window.visualViewport.addEventListener('resize', fitFixedCanvasToViewport);
|
||||
@@ -681,5 +705,12 @@
|
||||
})();
|
||||
</script>
|
||||
<script type="text/javascript" charset="utf-8" src="js/client.js" onload="main()"></script>
|
||||
<script>
|
||||
if ('serviceWorker' in navigator) {
|
||||
window.addEventListener('load', () => {
|
||||
navigator.serviceWorker.register('sw.js').catch(() => {});
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "2006Scape",
|
||||
"short_name": "2006Scape",
|
||||
"description": "2006Scape 377 Web Client",
|
||||
"start_url": ".",
|
||||
"display": "standalone",
|
||||
"orientation": "any",
|
||||
"background_color": "#000000",
|
||||
"theme_color": "#000000",
|
||||
"icons": [
|
||||
{
|
||||
"src": "navbar_mainmenu.gif",
|
||||
"sizes": "64x64",
|
||||
"type": "image/gif"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
const CACHE_VERSION = '2006scape-web-v1';
|
||||
const SHELL_ASSETS = [
|
||||
'./',
|
||||
'index.html',
|
||||
'manifest.webmanifest',
|
||||
'js/client.js',
|
||||
'hud_minimap_bezel.png',
|
||||
'hud_minimap_hole.png',
|
||||
'hud_chat_frame.png',
|
||||
'navbar.gif',
|
||||
'navbar_classic.gif',
|
||||
'navbar_modern.gif'
|
||||
];
|
||||
|
||||
self.addEventListener('install', (event) => {
|
||||
event.waitUntil((async () => {
|
||||
const cache = await caches.open(CACHE_VERSION);
|
||||
await Promise.allSettled(
|
||||
SHELL_ASSETS.map(async (asset) => {
|
||||
try {
|
||||
const resp = await fetch(asset, { cache: 'no-cache' });
|
||||
if (resp && resp.ok) {
|
||||
await cache.put(asset, resp);
|
||||
}
|
||||
} catch (_) {
|
||||
// Ignore optional assets that are not present in this deployment
|
||||
}
|
||||
})
|
||||
);
|
||||
await self.skipWaiting();
|
||||
})());
|
||||
});
|
||||
|
||||
self.addEventListener('activate', (event) => {
|
||||
event.waitUntil((async () => {
|
||||
const keys = await caches.keys();
|
||||
await Promise.all(
|
||||
keys.filter((k) => k.startsWith('2006scape-web-') && k !== CACHE_VERSION)
|
||||
.map((k) => caches.delete(k))
|
||||
);
|
||||
await self.clients.claim();
|
||||
})());
|
||||
});
|
||||
|
||||
self.addEventListener('fetch', (event) => {
|
||||
const req = event.request;
|
||||
if (req.method !== 'GET') return;
|
||||
const url = new URL(req.url);
|
||||
if (url.origin !== self.location.origin) return;
|
||||
|
||||
if (req.mode === 'navigate' || url.pathname.endsWith('/index.html') || url.pathname === '/') {
|
||||
event.respondWith((async () => {
|
||||
try {
|
||||
const fresh = await fetch(req);
|
||||
if (fresh && fresh.ok) {
|
||||
const cache = await caches.open(CACHE_VERSION);
|
||||
cache.put(req, fresh.clone());
|
||||
}
|
||||
return fresh;
|
||||
} catch (_) {
|
||||
const cached = await caches.match(req);
|
||||
return cached || caches.match('index.html');
|
||||
}
|
||||
})());
|
||||
return;
|
||||
}
|
||||
|
||||
if (/\.(js|png|gif|jpg|jpeg|webp|svg|ico|css|webmanifest|dat|idx\d*)$/i.test(url.pathname)) {
|
||||
event.respondWith((async () => {
|
||||
const cache = await caches.open(CACHE_VERSION);
|
||||
const cached = await cache.match(req);
|
||||
const networkPromise = fetch(req).then((resp) => {
|
||||
if (resp && resp.ok) {
|
||||
cache.put(req, resp.clone());
|
||||
}
|
||||
return resp;
|
||||
}).catch(() => null);
|
||||
return cached || (await networkPromise) || Response.error();
|
||||
})());
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user