The Virtual Node Problem in the Fog of War

Hello everyone, this is an example of setting the fog effect in our community, but I am particularly confused about the coordinates and scaling settings of the virtual nodes in it. It has been bothering me for a day. Does anyone know this function? Can you answer it for me? I would greatly appreciate it!

from direct.directbase import DirectStart
from pandac.PandaModules import *
import random, sys

terrain = loader.loadModel('../samples/Roaming-Ralph/models/world')
terrain.reparentTo(render)
terrain.showTightBounds()

veilColor = .2 # the veil/fog color
imgSize = 32.
img = PNMImage(imgSize,imgSize)
img.fill(veilColor) 
brush = PNMBrush.makeSpot(VBase4D(1),3,True)
painter = PNMPainter(img)
painter.setPen(brush)
tex = Texture()
tex.load(img) 

minb, maxb = terrain.getTightBounds()
dim = maxb-minb
maxDim = max(dim[0],dim[1])
scale = 1./maxDim
center = (minb+maxb)*.5
veilTS = TextureStage('')
terrain.setTexture(veilTS,tex)
terrain.setTexGen(veilTS, RenderAttrib.MWorldPosition)
terrain.setTexScale(veilTS, scale)
terrain.setTexOffset(veilTS, -.5-center[0]*scale, -.5-center[1]*scale)

# a dummy node to hold size & position of terrain to texture transform  
texOrigin = render.attachNewNode('')
texOrigin.setPos(center[0]-.5*maxDim, center[1]+.5*maxDim, 0)
texOrigin.setScale(maxDim/imgSize,-maxDim/imgSize,1)

ls = LineSegs('')
ls.setColor(Vec4(1,0,0,1))
ls.moveTo(0,0,100)
ls.drawTo(0,0,-20)
unit = render.attachNewNode(ls.create())
unit.setTransparency(1)
ls.setVertexColor(0,Vec4(1,0,0,0))

cm = CardMaker('')
cm.setFrame(-.4,0,0,.4)
card = base.a2dBottomRight.attachNewNode(cm.generate())
card.setTexture(tex)

def move(task):
    dt = globalClock.getDt()
    task.updateDt += dt
    unit.setH(unit, random.uniform(-15,15))
    unit.setY(unit, 20*dt)
    if not (minb[0]<unit.getX()<maxb[0] and minb[1]<unit.getY()<maxb[1]):
       unit.setH(unit,180)
       unit.setY(unit, 40*dt)
    if task.updateDt>.2: # fog update interval, in second
       task.updateDt=0
       x = unit.getX(texOrigin)
       y = unit.getY(texOrigin)
       painter.drawPoint(x, y)
       tex.load(img) 
    return task.cont
moveTask = taskMgr.add(move,'move')
moveTask.updateDt = 0

camera.setPos(-29.66, -216.88, 255.92)
camera.setHpr(-0.47, -50.13, -0.36)
camera.lookAt(render,Point3(center))
mat = Mat4(camera.getMat())
mat.invertInPlace()
base.mouseInterfaceNode.setMat(mat)
base.accept('escape', sys.exit)
base.accept('r', img.fill, [veilColor])
run()