I'm fairly new to PIXI and Shaders and although I have reached the effect I want but I'm struggling with a PIXI Renderer scale issue.
The image shows a CSS grid, over the top of a PIXI canvas. In the canvas I'm rendering sprites that do line up with the CSS grid perfectly. But when I apply my Shader Texture to the Sprit Container, the texture that gets rendered back to the canvas is scaled up and cropped.
I've not found any helping solution online and AI hasnt even found me an answer. Has anyone got an idea of how to fix this?
Project is in Next/React with Pixi v8
Layout.tsx
import type { Metadata } from "next";
import "./globals.css";
export const metadata: Metadata = {
title: "Pixel Demo",
description: "Next.js + Pixi.js",
};
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
{children}
);
}
Page.tsx
"use client";
import { PixiStage } from "./components/PixiStage";
import { useRef } from "react";
const GRID_SIZE = 800;
const rows = 8;
const columns = 10;
const gridSquarePixelSize = 91
export default function Home() {
const parentRef = useRef(null);
return (
{/* 800×800 CSS grid container */}
{/* Pixi canvas absolutely positioned over the grid */}
{Array.from({ length: rows * columns }, (_, index) => (
)
)}
);
}
PixiStage.tsx
"use client";
import { useEffect, useRef, useMemo } from 'react';
import * as PIXI from 'pixi.js';
import { Application, ApplicationRef, extend, useTick } from '@pixi/react';
import { defaultFilterVert, Filter, GlProgram, Sprite, Texture } from 'pixi.js';
extend({ Sprite, Texture });
const SIZE = 800;
const gridSquarePixelSize = 91
const fogFragmentShader = `#version 300 es
precision mediump float;
// Receive the texture (sprite map) and a mask indicating sprite presence.
uniform sampler2D uSpriteMask; // 1 if a sprite exists, 0 otherwise
in vec2 vTextureCoord;
out vec4 outColor;
void main() {
// Sample the sprite presence mask at this fragment's coordinates:
float hasSprite = texture(uSpriteMask, vTextureCoord).r;
// Only render color if there is a sprite, otherwise output fully transparent
if (hasSprite > 0.5) {
outColor = vec4(1.0, 0.5, 0.0, 1.0); // visible color (orange)
} else {
outColor = vec4(0.0, 0.0, 0.0, 0.0); // transparent
}
}
`;
export function PixiStage() {
const appRef = useRef(null);
const fogContainer = useMemo(() => new PIXI.Container(), []);
const spritesRef = useRef
Much appreciated.
