import { SceneContext } from "../../SceneBase" ; import { GfxClipSpaceNearZ , GfxDevice } from "../platform/GfxPlatform" ; import * as GX from '../../gx/gx_enum' ; import { GfxRenderInst , GfxRenderInstManager } from "../render/GfxRenderInstManager" ; import { fillMatrix4x3 } from "./UniformBufferHelpers" ; import { mat4 , vec3 , vec4 } from "gl-matrix" ; import { projectionMatrixForCuboid , MathConstants } from "../../MathHelpers" ; import { colorCopy , colorNewCopy , OpaqueBlack , White } from "../../Color" ; import { CharWriter , parseBRFNT , ResFont } from "../../Common/NW4R/lyt/Font" ; import { decompress } from "../../Common/Compression/Yaz0" ; import * as JKRArchive from "../../Common/JSYSTEM/JKRArchive" ; import { TDDraw } from "../../SuperMarioGalaxy/DDraw" ; import { GX_Program } from "../../gx/gx_material" ; import { fillSceneParamsData , gxBindingLayouts , SceneParams , ub_SceneParamsBufferSize } from "../../gx/gx_render" ; import { projectionMatrixConvertClipSpaceNearZ } from "./ProjectionHelpers" ; const scratchMatrix = mat4 . create ( ) ; const scratchVec4 = vec4 . create ( ) ; const sceneParams = new SceneParams ( ) ; export class DebugTextDrawer { private charWriter = new CharWriter ( ) ; private ddraw = new TDDraw ( ) ; public textColor = colorNewCopy ( White ) ; public strokeColor = colorNewCopy ( OpaqueBlack ) ; constructor ( private fontData ) { this . charWriter . setFont ( fontData , 0 , 0 ) ; const ddraw = this . ddraw ; ddraw . setVtxAttrFmt ( GX . VtxFmt . VTXFMT0 , GX . Attr . POS , GX . CompCnt . POS_XYZ ) ; ddraw . setVtxDesc ( GX . Attr . POS , true ) ; ddraw . setVtxAttrFmt ( GX . VtxFmt . VTXFMT0 , GX . Attr . CLR0 , GX . CompCnt . CLR_RGBA ) ; ddraw . setVtxDesc ( GX . Attr . CLR0 , true ) ; ddraw . setVtxAttrFmt ( GX . VtxFmt . VTXFMT0 , GX . Attr . TEX0 , GX . CompCnt . TEX_ST ) ; ddraw . setVtxDesc ( GX . Attr . TEX0 , true ) ; } private setSceneParams ( renderInst , w , h , clipSpaceNearZ ) { let offs = renderInst . allocateUniformBuffer ( GX_Program . ub_SceneParams , ub_SceneParamsBufferSize ) ; const d = renderInst . mapUniformBufferF32 ( GX_Program . ub_SceneParams ) ; projectionMatrixForCuboid ( sceneParams . u_Projection , 0 , w , 0 , h , - 10000.0 , 10000.0 ) ; projectionMatrixConvertClipSpaceNearZ ( sceneParams . u_Projection , clipSpaceNearZ , GfxClipSpaceNearZ . NegativeOne ) ; fillSceneParamsData ( d , offs , sceneParams ) ; } private setPacketParams ( renderInst ) { let offs = renderInst . allocateUniformBuffer ( GX_Program . ub_DrawParams , 16 ) ; const d = renderInst . mapUniformBufferF32 ( GX_Program . ub_DrawParams ) ; mat4 . identity ( scratchMatrix ) ; offs += fillMatrix4x3 ( d , offs , scratchMatrix ) ; } public beginDraw ( ) { this . ddraw . beginDraw ( ) ; } public endDraw ( renderInstManager ) { this . ddraw . endAndUpload ( renderInstManager ) ; } public setFontScale ( scale ) { this . charWriter . scale [ 0 ] = scale ; this . charWriter . scale [ 1 ] = scale ; } public getScaledLineHeight ( ) { return this . charWriter . getScaledLineHeight ( ) ; } public drawString ( renderInstManager , vw , vh , str , x , y , strokeWidth = 1 , strokeNum = 4 ) { this . charWriter . calcRect ( scratchVec4 , str ) ; const rx0 = scratchVec4 [ 0 ] , rx1 = scratchVec4 [ 2 ] ; const w = rx1 - rx0 ; x -= w / 2 ; const template = renderInstManager . pushTemplateRenderInst ( ) ; template . setBindingLayouts ( gxBindingLayouts ) ; const clipSpaceNearZ = renderInstManager . gfxRenderCache . device . queryVendorInfo ( ) . clipSpaceNearZ ; this . setSceneParams ( template , vw , vh , clipSpaceNearZ ) ; this . setPacketParams ( template ) ; colorCopy ( this . charWriter . color1 , this . strokeColor ) ; for ( let i = 0 ; i < strokeNum ; i ++ ) { const theta = i * MathConstants . TAU / strokeNum ; const sy = strokeWidth * Math . sin ( theta ) , sx = strokeWidth * Math . cos ( theta ) ; vec3 . set ( this . charWriter . cursor , x + sx , y + sy , 0 ) ; this . charWriter . drawString ( renderInstManager , this . ddraw , str ) ; } colorCopy ( this . charWriter . color1 , this . textColor ) ; vec3 . set ( this . charWriter . cursor , x , y , 0 ) ; this . charWriter . drawString ( renderInstManager , this . ddraw , str ) ; renderInstManager . popTemplateRenderInst ( ) ; } public destroy ( device ) { this . fontData . destroy ( device ) ; this . ddraw . destroy ( device ) ; } } export async function makeDebugTextDrawer ( context ) { return context . dataShare . ensureObject < DebugTextDrawer > ( `DebugTextDrawer` , async ( ) => { const fontArcData = await context . dataFetcher . fetchData ( `SuperMarioGalaxy/LayoutData/Font.arc` ) ; const fontArc = JKRArchive . parse ( await decompress ( fontArcData ) ) ; const fontData = new ResFont ( context . device , parseBRFNT ( fontArc . findFileData ( `messagefont26.brfnt` ) ! ) ) ; return new DebugTextDrawer ( fontData ) ; } ) ; }