Пример #1
0
    def generate(self, helperInfo):
        color = self.mapObject.getPropertyValue("_light",
                                                default=Vec4(
                                                    255, 255, 255, 255))
        color = LEGlobals.colorFromRGBScalar255(color)
        color = LEGlobals.vec3GammaToLinear(color)

        intensity = self.mapObject.getPropertyValue("_intensity", default=1.0)

        color[0] = color[0] * intensity
        color[1] = color[1] * intensity
        color[2] = color[2] * intensity

        csmDistance = self.mapObject.getPropertyValue("_csm_distance")
        sunDistance = self.mapObject.getPropertyValue("_sun_distance")
        csmLogFactor = self.mapObject.getPropertyValue("_csm_log_factor")
        csmBorderBias = self.mapObject.getPropertyValue("_csm_border_bias")
        csmFixedFilmSize = self.mapObject.getPropertyValue(
            "_csm_fixed_film_size")
        numCascades = self.mapObject.getPropertyValue("_num_cascades")
        depthBias = self.mapObject.getPropertyValue("_depth_bias")
        shadowSize = self.mapObject.getPropertyValue("_shadow_map_size")
        shadowCaster = self.mapObject.getPropertyValue("_shadow_caster")
        softnessFactor = self.mapObject.getPropertyValue("_softness_factor")
        normalOffsetScale = self.mapObject.getPropertyValue(
            "_normal_offset_scale")
        normalOffsetUvSpace = self.mapObject.getPropertyValue(
            "_normal_offset_uv_space")

        pl = CascadeLight("lightHelper-light")
        pl.setColor(Vec4(color[0], color[1], color[2], 1.0))
        if shadowCaster:
            pl.setSceneCamera(self.mapObject.doc.get3DViewport().cam)
            pl.setCameraMask(DirectRender.ShadowCameraBitmask)
            pl.setCsmDistance(csmDistance)
            pl.setSunDistance(sunDistance)
            pl.setLogFactor(csmLogFactor)
            pl.setBorderBias(csmBorderBias)
            pl.setUseFixedFilmSize(csmFixedFilmSize)
            pl.setNumCascades(numCascades)
            pl.setDepthBias(depthBias)
            pl.setSoftnessFactor(softnessFactor)
            pl.setNormalOffsetScale(normalOffsetScale)
            pl.setNormalOffsetUvSpace(normalOffsetUvSpace)
            pl.setShadowCaster(True, shadowSize, shadowSize)
        self.light = self.mapObject.helperRoot.attachNewNode(pl)
        if True:  #self.mapObject.doc.numlights < 128:
            self.mapObject.doc.render.setLight(self.light)
            self.mapObject.doc.numlights += 1
            self.hasLight = True
Пример #2
0
    def generate(self, helperInfo):
        MapHelper.generate(self)

        # Check for a color255 to tint the sprite
        color255Props = self.mapObject.getPropsWithValueType(
            ['color255', 'color1'])
        # If we have a color255 property, select the first one.
        color255Prop = color255Props[0] if len(color255Props) > 0 else None
        if color255Prop:
            color = self.mapObject.getPropertyValue(color255Prop)
            color = LEGlobals.colorFromRGBScalar255(color)
        else:
            color = Vec4(1)

        spritePath = helperInfo['args'][0].replace("\"", "")

        cm = CardMaker("sprite")
        cm.setFrame(-0.75, 0.75, -0.75, 0.75)
        np = NodePath(cm.generate())
        #np.setBSPMaterial(spritePath)
        np.setTexture(base.loader.loadTexture(spritePath))
        np.setColorScale(color)
        np.setLightOff(1)
        np.setFogOff(1)
        np.setBillboardPointEye()
        np.setTransparency(True)
        np.hide(~VIEWPORT_3D_MASK)
        np.reparentTo(self.mapObject.helperRoot)
        self.sprite = np
Пример #3
0
    def fromPlaneAndRadius(plane, radius=32768):
        normal = plane.getNormal()
        dist = -plane.getW()

        # Find the major axis
        x = plane.getClosestAxisToNormal()
        up = Vec3.unitX() if x == Vec3.unitZ() else Vec3.unitZ()

        v = up.dot(normal)
        up = LEGlobals.extrude(up, -v, normal)
        up.normalize()

        org = normal * dist

        right = up.cross(normal)

        up = up * radius
        right = right * radius

        # Project a really big axis aligned box onto the plane
        verts = [
            org - right + up, org + right + up, org + right - up,
            org - right - up
        ]

        poly = Winding(verts, plane)

        return poly
Пример #4
0
    def generate(self, helperInfo):
        color = self.mapObject.getPropertyValue("_light",
                                                default=Vec4(
                                                    255, 255, 255, 255))
        color = LEGlobals.colorFromRGBScalar255(color)
        color = LEGlobals.vec3GammaToLinear(color)

        intensity = self.mapObject.getPropertyValue("_intensity", default=1.0)

        color[0] = color[0] * intensity
        color[1] = color[1] * intensity
        color[2] = color[2] * intensity

        pl = AmbientLight("lightHelper-ambient-light")
        pl.setColor(Vec4(color[0], color[1], color[2], 1.0))
        self.light = self.mapObject.helperRoot.attachNewNode(pl)
        if True:  #self.mapObject.doc.numlights < 128:
            print("HI ambient light")
            self.mapObject.doc.render.setLight(self.light)
            self.mapObject.doc.numlights += 1
            self.hasLight = True
Пример #5
0
    def generate(self, helperInfo):
        MapHelper.generate(self)

        args = helperInfo['args']
        modelPath = args[0] if len(args) > 0 else None
        if not modelPath:
            # Model wasn't specified in the class definition,
            # check for a property called "model"
            modelPath = self.mapObject.getPropertyValue(
                "model", default="models/smiley.egg.pz")
        else:
            # For some reason the fgd parser doesn't remove the quotes around the
            # model path string in the game class definition
            modelPath = modelPath.replace("\"", "")
        if not modelPath:
            return

        modelNp = base.loader.loadModel(modelPath, okMissing=True)
        if not modelNp:
            return

        colorScale = LEGlobals.colorFromRGBScalar255(
            self.mapObject.getPropertyValue("color_scale",
                                            default=Vec4(255, 255, 255, 255)))
        castShadows = self.mapObject.getPropertyValue("cast_shadows")
        reflect = self.mapObject.getPropertyValue("reflect")
        lighting = self.mapObject.getPropertyValue("receive_lighting")

        if colorScale != Vec4(1):
            modelNp.setColorScale(colorScale)
        if not lighting:
            modelNp.setLightOff(2)

        # Create a representation in each viewport
        for vp in base.viewportMgr.viewports:
            vpRoot = self.modelRoot.attachNewNode("vpRepr")
            showMask = vp.getViewportFullMask()
            if vp.is3D():
                if not castShadows:
                    showMask &= (~DirectRender.ShadowCameraBitmask)
                if not reflect:
                    showMask &= (~DirectRender.ReflectionCameraBitmask)
            vpRoot.hide(~showMask)
            self.vpRoots.append((vp, vpRoot))

            vpModel = modelNp.instanceTo(vpRoot)

        if self.mapObject.selected:
            self.setSelectedState()
        else:
            self.setUnselectedState()
Пример #6
0
    def update(self):
        if self.DotFade:
            camToAxis = self.getPos(base.render) - self.widget.vp.cam.getPos(
                base.render)
            camToAxis.normalize()

            dot = abs(camToAxis.dot(self.direction))
            inRange = dot >= self.DotRange[
                0] if not self.OppositeDot else dot <= self.DotRange[0]
            if inRange:
                alpha = LEGlobals.remapVal(dot, self.DotRange[0],
                                           self.DotRange[1], 1.0, 0.0)
                self.setAlphaScale(alpha)
            else:
                self.setAlphaScale(1)
Пример #7
0
    def __init__(self, parent):
        AssetBrowser.__init__(self, parent)

        self.currentLoadContext = None

        # Set up an offscreen buffer to render the thumbnails of our models.

        props = core.WindowProperties()
        props.setSize(96, 96)
        fbprops = core.FrameBufferProperties()
        fbprops.setSrgbColor(True)
        fbprops.setRgbaBits(8, 8, 8, 0)
        fbprops.setDepthBits(8)
        flags = (core.GraphicsPipe.BFRefuseWindow | core.GraphicsPipe.BFSizeSquare)
        self.buffer = base.graphicsEngine.makeOutput(base.pipe, "modelBrowserBuffer", 0,
            fbprops, props, flags, None, None)
        gsg = self.buffer.getGsg()
        self.buffer.setClearColor(LEGlobals.vec3GammaToLinear(core.Vec4(82 / 255.0, 82 / 255.0, 82 / 255.0, 1.0)))
        self.buffer.setActive(False)

        self.displayRegion = self.buffer.makeDisplayRegion()

        self.render = core.NodePath("modelBrowserRoot")
        self.render.setShaderAuto()

        camNode = core.Camera("modelBrowserRenderCam")
        lens = core.PerspectiveLens()
        lens.setFov(40)
        camNode.setLens(lens)
        self.lens = lens
        self.camera = self.render.attachNewNode(camNode)
        # Isometric camera angle
        self.camera.setHpr(225, -30, 0)

        self.displayRegion.setCamera(self.camera)

        #shgen = BSPShaderGenerator(self.buffer, gsg, self.camera, self.render)
        #gsg.setShaderGenerator(shgen)
        #for shader in ShaderGlobals.getShaders():
        #    shgen.addShader(shader)
        #self.shaderGenerator = shgen

        base.graphicsEngine.openWindows()
Пример #8
0
    def generate(self, helperInfo):
        color = self.mapObject.getPropertyValue("_light",
                                                default=Vec4(
                                                    255, 255, 255, 255))
        color = LEGlobals.colorFromRGBScalar255(color)
        color = LEGlobals.vec3GammaToLinear(color)

        intensity = self.mapObject.getPropertyValue("_intensity", default=1.0)
        innerRadius = self.mapObject.getPropertyValue("_inner_radius",
                                                      default=1.0)
        outerRadius = self.mapObject.getPropertyValue("_outer_radius",
                                                      default=2.0)

        color[0] = color[0] * intensity
        color[1] = color[1] * intensity
        color[2] = color[2] * intensity

        constant = self.mapObject.getPropertyValue("_constant_attn",
                                                   default=0.0)
        linear = self.mapObject.getPropertyValue("_linear_attn", default=0.0)
        quadratic = self.mapObject.getPropertyValue("_quadratic_attn",
                                                    default=1.0)

        depthBias = self.mapObject.getPropertyValue("_depth_bias")
        shadowSize = self.mapObject.getPropertyValue("_shadow_map_size")
        shadowCaster = self.mapObject.getPropertyValue("_shadow_caster")
        softnessFactor = self.mapObject.getPropertyValue("_softness_factor")
        normalOffsetScale = self.mapObject.getPropertyValue(
            "_normal_offset_scale")
        normalOffsetUvSpace = self.mapObject.getPropertyValue(
            "_normal_offset_uv_space")

        pl = PointLight("lightHelper-light")
        pl.setColor(Vec4(color[0], color[1], color[2], 1.0))
        pl.setFalloff(quadratic)
        pl.setInnerRadius(innerRadius)
        pl.setOuterRadius(outerRadius)
        if shadowCaster:
            pl.setCameraMask(DirectRender.ShadowCameraBitmask)
            pl.setShadowCaster(True, shadowSize, shadowSize)
            pl.setDepthBias(depthBias)
            pl.setSoftnessFactor(softnessFactor)
            pl.setNormalOffsetScale(normalOffsetScale)
            pl.setNormalOffsetUvSpace(normalOffsetUvSpace)
        self.light = self.mapObject.helperRoot.attachNewNode(pl)
        if True:  #self.mapObject.doc.numlights < 128:
            self.mapObject.doc.render.setLight(self.light)
            self.mapObject.doc.numlights += 1
            self.hasLight = True

        innerSphere = getUnitCircle().copyTo(self.light)
        innerSphere.setScale(innerRadius)
        innerSphere.setColorScale(InnerColor)
        outerSphere = getUnitCircle().copyTo(self.light)
        outerSphere.setScale(outerRadius)
        outerSphere.setColorScale(OuterColor)

        self.innerSphere = innerSphere
        self.outerSphere = outerSphere

        if not self.mapObject.selected:
            innerSphere.stash()
            outerSphere.stash()
Пример #9
0
from panda3d.core import PointLight, Vec4, Vec3, CKeyValues, LineSegs, Quat, NodePath, AntialiasAttrib

from .MapHelper import MapHelper

from direct.foundry import LEGlobals, LEUtils
from direct.directbase import DirectRender

InnerColor = LEGlobals.vec3GammaToLinear(Vec4(1.0, 1.0, 0, 1))
OuterColor = LEGlobals.vec3GammaToLinear(Vec4(1.0, 0.5, 0, 1))

UnitCircle = None


def getUnitCircle():
    global UnitCircle
    if not UnitCircle:
        segs = LineSegs('unitCircle')
        vertices = LEUtils.circle(0, 0, 1, 64)
        angles = [Vec3(0, 0, 0), Vec3(90, 0, 0), Vec3(0, 90, 0)]
        for angle in angles:
            quat = Quat()
            quat.setHpr(angle)
            for i in range(len(vertices)):
                x1, y1 = vertices[i]
                x2, y2 = vertices[(i + 1) % len(vertices)]
                pFrom = quat.xform(Vec3(x1, 0, y1))
                pTo = quat.xform(Vec3(x2, 0, y2))
                segs.moveTo(pFrom)
                segs.drawTo(pTo)
        UnitCircle = NodePath(segs.create())
        UnitCircle.setAntialias(AntialiasAttrib.MLine)
Пример #10
0
class Viewport(QtWidgets.QWidget, DirectObject):

    ClearColor = LEGlobals.vec3GammaToLinear(Vec4(0.361, 0.361, 0.361, 1.0))

    def __init__(self, vpType, window, doc):
        DirectObject.__init__(self)
        QtWidgets.QWidget.__init__(self, window)
        self.doc = doc
        self.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.setMouseTracking(True)

        self.qtWindow = None
        self.qtWidget = None

        self.window = window
        self.type = vpType

        self.spec = VIEWPORT_SPECS[self.type]

        self.lens = None
        self.camNode = None
        self.camera = None
        self.cam = None
        self.win = None
        self.displayRegion = None
        self.mouseWatcher = None
        self.mouseWatcherNp = None
        self.buttonThrower = None
        self.clickRay = None
        self.clickNode = None
        self.clickNp = None
        self.clickQueue = None
        self.tickTask = None
        self.zoom = 1.0
        self.gizmo = None
        self.inputDevice = None
        self.mouseAndKeyboard = None
        self.lastRenderTime = 0.0
        self.enabled = False
        self.needsUpdate = True

        # 2D stuff copied from ShowBase :(
        self.camera2d = None
        self.cam2d = None
        self.render2d = None
        self.aspect2d = None
        self.a2dBackground = None
        self.a2dTop = None
        self.a2dBottom = None
        self.a2dLeft = None
        self.a2dRight = None
        self.a2dTopCenter = None
        self.a2dTopCenterNs = None
        self.a2dBottomCenter = None
        self.a2dBottomCenterNs = None
        self.a2dRightCenter = None
        self.a2dRightCenterNs = None
        self.a2dTopLeft = None
        self.a2dTopLeftNs = None
        self.a2dTopRight = None
        self.a2dTopRightNs = None
        self.a2dBottomLeft = None
        self.a2dBottomLeftNs = None
        self.a2dBottomRight = None
        self.a2dBottomRightNs = None
        self.__oldAspectRatio = None

        self.gridRoot = self.doc.render.attachNewNode("gridRoot")
        self.gridRoot.setLightOff(1)
        #self.gridRoot.setBSPMaterial("phase_14/materials/unlit.mat")
        #self.gridRoot.setDepthWrite(False)
        self.gridRoot.setBin("background", 0)
        self.gridRoot.hide(~self.getViewportMask())

        self.grid = None

    def updateView(self, now=False):
        if now:
            self.renderView()
        else:
            self.needsUpdate = True

    def getGizmoAxes(self):
        raise NotImplementedError

    def getMouseRay(self, collRay=False):
        ray = CollisionRay()
        ray.setFromLens(self.camNode, self.getMouse())
        if collRay:
            return ray
        else:
            return Ray(ray.getOrigin(), ray.getDirection())

    def hasMouse(self):
        return self.mouseWatcher.hasMouse()

    def getMouse(self):
        if self.mouseWatcher.hasMouse():
            return self.mouseWatcher.getMouse()
        return Point2(0, 0)

    def is3D(self):
        return self.type == VIEWPORT_3D

    def is2D(self):
        return self.type != VIEWPORT_3D

    def makeGrid(self):
        raise NotImplementedError

    def getViewportMask(self):
        return BitMask32.bit(self.type)

    def getViewportFullMask(self):
        return self.getViewportMask()

    def makeLens(self):
        raise NotImplementedError

    def getGridAxes(self):
        raise NotImplementedError

    def expand(self, point):
        return point

    def initialize(self):
        self.lens = self.makeLens()
        self.camera = self.doc.render.attachNewNode(
            ModelNode("viewportCameraParent"))
        self.camNode = Camera("viewportCamera")
        self.camNode.setLens(self.lens)
        self.camNode.setCameraMask(self.getViewportMask())
        self.cam = self.camera.attachNewNode(self.camNode)

        winprops = WindowProperties.getDefault()
        winprops.setParentWindow(int(self.winId()))
        winprops.setForeground(False)
        winprops.setUndecorated(True)

        gsg = self.doc.gsg

        output = base.graphicsEngine.makeOutput(
            base.pipe, "viewportOutput", 0, FrameBufferProperties.getDefault(),
            winprops,
            (GraphicsPipe.BFFbPropsOptional | GraphicsPipe.BFRequireWindow),
            gsg)

        self.qtWindow = QtGui.QWindow.fromWinId(
            output.getWindowHandle().getIntHandle())
        self.qtWidget = QtWidgets.QWidget.createWindowContainer(
            self.qtWindow, self, QtCore.Qt.WindowDoesNotAcceptFocus
            | QtCore.Qt.WindowTransparentForInput
            | QtCore.Qt.WindowStaysOnBottomHint
            | QtCore.Qt.BypassWindowManagerHint | QtCore.Qt.SubWindow)  #,
        #(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowDoesNotAcceptFocus
        #| QtCore.Qt.WindowTransparentForInput | QtCore.Qt.BypassWindowManagerHint
        #| QtCore.Qt.SubWindow | QtCore.Qt.WindowStaysOnBottomHint))
        self.qtWidget.setFocusPolicy(QtCore.Qt.NoFocus)

        self.inputDevice = output.getInputDevice(0)

        assert output is not None, "Unable to create viewport output!"

        dr = output.makeDisplayRegion()
        dr.disableClears()
        dr.setCamera(self.cam)
        self.displayRegion = dr

        output.disableClears()
        output.setClearColor(Viewport.ClearColor)
        output.setClearColorActive(True)
        output.setClearDepthActive(True)
        output.setActive(True)

        self.win = output

        # keep track of the mouse in this viewport
        mak = MouseAndKeyboard(self.win, 0, "mouse")
        mouse = base.dataRoot.attachNewNode(mak)
        self.mouseAndKeyboard = mouse
        self.mouseWatcher = MouseWatcher()
        self.mouseWatcher.setDisplayRegion(self.displayRegion)
        mw = mouse.attachNewNode(self.mouseWatcher)
        self.mouseWatcherNp = mw

        # listen for keyboard and mouse events in this viewport
        bt = ButtonThrower("kbEvents")
        bt.setButtonDownEvent("btndown")
        bt.setButtonUpEvent("btnup")
        mods = ModifierButtons()
        mods.addButton(KeyboardButton.shift())
        mods.addButton(KeyboardButton.control())
        mods.addButton(KeyboardButton.alt())
        mods.addButton(KeyboardButton.meta())
        bt.setModifierButtons(mods)
        self.buttonThrower = mouse.attachNewNode(bt)

        # collision objects for clicking on objects from this viewport
        self.clickRay = CollisionRay()
        self.clickNode = CollisionNode("viewportClickRay")
        self.clickNode.addSolid(self.clickRay)
        self.clickNp = NodePath(self.clickNode)
        self.clickQueue = CollisionHandlerQueue()

        self.setupRender2d()
        self.setupCamera2d()

        self.gizmo = ViewportGizmo(self)

        self.doc.viewportMgr.addViewport(self)

        self.makeGrid()

    def cleanup(self):
        self.grid.cleanup()
        self.grid = None
        self.gridRoot.removeNode()
        self.gridRoot = None

        self.lens = None
        self.camNode = None
        self.cam.removeNode()
        self.cam = None
        self.camera.removeNode()
        self.camera = None
        self.spec = None
        self.doc = None
        self.type = None
        self.window = None
        self.zoom = None
        self.gizmo.cleanup()
        self.gizmo = None
        self.clickNp.removeNode()
        self.clickNp = None
        self.clickQueue.clearEntries()
        self.clickQueue = None
        self.clickNode = None
        self.clickRay = None
        self.buttonThrower.removeNode()
        self.buttonThrower = None
        self.inputDevice = None
        self.mouseWatcherNp.removeNode()
        self.mouseWatcherNp = None
        self.mouseWatcher = None
        self.mouseAndKeyboard.removeNode()
        self.mouseAndKeyboard = None
        self.win.removeAllDisplayRegions()
        self.displayRegion = None
        base.graphicsEngine.removeWindow(self.win)
        self.win = None

        self.camera2d.removeNode()
        self.camera2d = None
        self.cam2d = None

        self.render2d.removeNode()
        self.render2d = None

        self.a2dBackground = None
        self.a2dTop = None
        self.a2dBottom = None
        self.a2dLeft = None
        self.a2dRight = None
        self.aspect2d = None
        self.a2dTopCenter = None
        self.a2dTopCenterNs = None
        self.a2dBottomCenter = None
        self.a2dBottomCenterNs = None
        self.a2dLeftCenter = None
        self.a2dLeftCenterNs = None
        self.a2dRightCenter = None
        self.a2dRightCenterNs = None

        self.a2dTopLeft = None
        self.a2dTopLeftNs = None
        self.a2dTopRight = None
        self.a2dTopRightNs = None
        self.a2dBottomLeft = None
        self.a2dBottomLeftNs = None
        self.a2dBottomRight = None
        self.a2dBottomRightNs = None
        self.__oldAspectRatio = None

        self.qtWindow.deleteLater()
        self.qtWidget.deleteLater()
        self.qtWindow = None
        self.qtWidget = None

        self.deleteLater()

    def keyPressEvent(self, event):
        button = LEUtils.keyboardButtonFromQtKey(event.key())
        if button:
            self.inputDevice.buttonDown(button)

    def keyReleaseEvent(self, event):
        button = LEUtils.keyboardButtonFromQtKey(event.key())
        if button:
            self.inputDevice.buttonUp(button)

    def enterEvent(self, event):
        # Give ourselves focus.
        self.setFocus()
        QtWidgets.QWidget.enterEvent(self, event)

    def mouseMoveEvent(self, event):
        self.inputDevice.setPointerInWindow(event.pos().x(), event.pos().y())
        QtWidgets.QWidget.mouseMoveEvent(self, event)

    def leaveEvent(self, event):
        self.clearFocus()
        self.inputDevice.setPointerOutOfWindow()
        self.inputDevice.focusLost()
        QtWidgets.QWidget.leaveEvent(self, event)

    def mousePressEvent(self, event):
        btn = event.button()
        if btn == QtCore.Qt.LeftButton:
            self.inputDevice.buttonDown(MouseButton.one())
        elif btn == QtCore.Qt.MiddleButton:
            self.inputDevice.buttonDown(MouseButton.two())
        elif btn == QtCore.Qt.RightButton:
            self.inputDevice.buttonDown(MouseButton.three())
        QtWidgets.QWidget.mousePressEvent(self, event)

    def mouseReleaseEvent(self, event):
        btn = event.button()
        if btn == QtCore.Qt.LeftButton:
            self.inputDevice.buttonUp(MouseButton.one())
        elif btn == QtCore.Qt.MiddleButton:
            self.inputDevice.buttonUp(MouseButton.two())
        elif btn == QtCore.Qt.RightButton:
            self.inputDevice.buttonUp(MouseButton.three())
        QtWidgets.QWidget.mouseReleaseEvent(self, event)

    def wheelEvent(self, event):
        ang = event.angleDelta().y()
        if ang > 0:
            self.inputDevice.buttonDown(MouseButton.wheelUp())
            self.inputDevice.buttonUp(MouseButton.wheelUp())
        else:
            self.inputDevice.buttonDown(MouseButton.wheelDown())
            self.inputDevice.buttonUp(MouseButton.wheelDown())
        QtWidgets.QWidget.wheelEvent(self, event)

    def getAspectRatio(self):
        return self.win.getXSize() / self.win.getYSize()

    def setupRender2d(self):
        ## This is the root of the 2-D scene graph.
        self.render2d = NodePath("viewport-render2d")

        # Set up some overrides to turn off certain properties which
        # we probably won't need for 2-d objects.

        # It's probably important to turn off the depth test, since
        # many 2-d objects will be drawn over each other without
        # regard to depth position.

        # We used to avoid clearing the depth buffer before drawing
        # render2d, but nowadays we clear it anyway, since we
        # occasionally want to put 3-d geometry under render2d, and
        # it's simplest (and seems to be easier on graphics drivers)
        # if the 2-d scene has been cleared first.

        self.render2d.setDepthTest(0)
        self.render2d.setDepthWrite(0)
        self.render2d.setMaterialOff(1)
        self.render2d.setTwoSided(1)

        self.aspect2d = self.render2d.attachNewNode("viewport-aspect2d")

        aspectRatio = self.getAspectRatio()
        self.aspect2d.setScale(1.0 / aspectRatio, 1.0, 1.0)

        self.a2dBackground = self.aspect2d.attachNewNode("a2dBackground")

        ## The Z position of the top border of the aspect2d screen.
        self.a2dTop = 1.0
        ## The Z position of the bottom border of the aspect2d screen.
        self.a2dBottom = -1.0
        ## The X position of the left border of the aspect2d screen.
        self.a2dLeft = -aspectRatio
        ## The X position of the right border of the aspect2d screen.
        self.a2dRight = aspectRatio

        self.a2dTopCenter = self.aspect2d.attachNewNode("a2dTopCenter")
        self.a2dTopCenterNs = self.aspect2d.attachNewNode("a2dTopCenterNS")
        self.a2dBottomCenter = self.aspect2d.attachNewNode("a2dBottomCenter")
        self.a2dBottomCenterNs = self.aspect2d.attachNewNode(
            "a2dBottomCenterNS")
        self.a2dLeftCenter = self.aspect2d.attachNewNode("a2dLeftCenter")
        self.a2dLeftCenterNs = self.aspect2d.attachNewNode("a2dLeftCenterNS")
        self.a2dRightCenter = self.aspect2d.attachNewNode("a2dRightCenter")
        self.a2dRightCenterNs = self.aspect2d.attachNewNode("a2dRightCenterNS")

        self.a2dTopLeft = self.aspect2d.attachNewNode("a2dTopLeft")
        self.a2dTopLeftNs = self.aspect2d.attachNewNode("a2dTopLeftNS")
        self.a2dTopRight = self.aspect2d.attachNewNode("a2dTopRight")
        self.a2dTopRightNs = self.aspect2d.attachNewNode("a2dTopRightNS")
        self.a2dBottomLeft = self.aspect2d.attachNewNode("a2dBottomLeft")
        self.a2dBottomLeftNs = self.aspect2d.attachNewNode("a2dBottomLeftNS")
        self.a2dBottomRight = self.aspect2d.attachNewNode("a2dBottomRight")
        self.a2dBottomRightNs = self.aspect2d.attachNewNode("a2dBottomRightNS")

        # Put the nodes in their places
        self.a2dTopCenter.setPos(0, 0, self.a2dTop)
        self.a2dTopCenterNs.setPos(0, 0, self.a2dTop)
        self.a2dBottomCenter.setPos(0, 0, self.a2dBottom)
        self.a2dBottomCenterNs.setPos(0, 0, self.a2dBottom)
        self.a2dLeftCenter.setPos(self.a2dLeft, 0, 0)
        self.a2dLeftCenterNs.setPos(self.a2dLeft, 0, 0)
        self.a2dRightCenter.setPos(self.a2dRight, 0, 0)
        self.a2dRightCenterNs.setPos(self.a2dRight, 0, 0)

        self.a2dTopLeft.setPos(self.a2dLeft, 0, self.a2dTop)
        self.a2dTopLeftNs.setPos(self.a2dLeft, 0, self.a2dTop)
        self.a2dTopRight.setPos(self.a2dRight, 0, self.a2dTop)
        self.a2dTopRightNs.setPos(self.a2dRight, 0, self.a2dTop)
        self.a2dBottomLeft.setPos(self.a2dLeft, 0, self.a2dBottom)
        self.a2dBottomLeftNs.setPos(self.a2dLeft, 0, self.a2dBottom)
        self.a2dBottomRight.setPos(self.a2dRight, 0, self.a2dBottom)
        self.a2dBottomRightNs.setPos(self.a2dRight, 0, self.a2dBottom)

    def setupCamera2d(self,
                      sort=10,
                      displayRegion=(0, 1, 0, 1),
                      coords=(-1, 1, -1, 1)):
        dr = self.win.makeMonoDisplayRegion(*displayRegion)
        dr.setSort(10)

        # Enable clearing of the depth buffer on this new display
        # region (see the comment in setupRender2d, above).
        dr.setClearDepthActive(1)

        # Make any texture reloads on the gui come up immediately.
        dr.setIncompleteRender(False)

        left, right, bottom, top = coords

        # Now make a new Camera node.
        cam2dNode = Camera('cam2d')

        lens = OrthographicLens()
        lens.setFilmSize(right - left, top - bottom)
        lens.setFilmOffset((right + left) * 0.5, (top + bottom) * 0.5)
        lens.setNearFar(-1000, 1000)
        cam2dNode.setLens(lens)

        # self.camera2d is the analog of self.camera, although it's
        # not as clear how useful it is.
        self.camera2d = self.render2d.attachNewNode('camera2d')

        camera2d = self.camera2d.attachNewNode(cam2dNode)
        dr.setCamera(camera2d)

        self.cam2d = camera2d

        return camera2d

    def mouse1Up(self):
        pass

    def mouse1Down(self):
        pass

    def mouse2Up(self):
        pass

    def mouse2Down(self):
        pass

    def mouse3Up(self):
        pass

    def mouse3Down(self):
        pass

    def mouseEnter(self):
        self.updateView()

    def mouseExit(self):
        pass

    def mouseMove(self):
        pass

    def wheelUp(self):
        pass

    def wheelDown(self):
        pass

    def shouldRender(self):
        if not self.enabled:
            return False

        now = globalClock.getRealTime()
        if self.lastRenderTime != 0:
            elapsed = now - self.lastRenderTime
            if elapsed <= 0:
                return False

            frameRate = 1 / elapsed
            if frameRate > 100.0:
                # Never render faster than 100Hz
                return False

        return self.needsUpdate

    def renderView(self):
        self.lastRenderTime = globalClock.getRealTime()
        self.needsUpdate = False
        #self.win.setActive(1)
        base.requestRender()

    def tick(self):
        if self.shouldRender():
            self.renderView()
        else:
            pass
            #self.win.setActive(0)

    def getViewportName(self):
        return self.spec.name

    def getViewportCenterPixels(self):
        return LPoint2i(self.win.getXSize() // 2, self.win.getYSize() // 2)

    def centerCursor(self, cursor):
        center = self.getViewportCenterPixels()
        cursor.setPos(
            self.mapToGlobal(QtCore.QPoint(self.width() / 2,
                                           self.height() / 2)))
        self.inputDevice.setPointerInWindow(center.x, center.y)

    def viewportToWorld(self, viewport, vec=False):
        front = Point3()
        back = Point3()
        self.lens.extrude(viewport, front, back)
        world = (front + back) / 2

        worldMat = self.cam.getMat(render)
        if vec:
            world = worldMat.xformVec(world)
        else:
            world = worldMat.xformPoint(world)

        return world

    def worldToViewport(self, world):
        # move into local camera space
        invMat = Mat4(self.cam.getMat(render))
        invMat.invertInPlace()

        local = invMat.xformPoint(world)

        point = Point2()
        self.lens.project(local, point)

        return point

    def zeroUnusedCoordinate(self, vec):
        pass

    def click(self, mask, queue=None, traverser=None, root=None):
        if not self.mouseWatcher.hasMouse():
            return None

        if not queue:
            queue = self.clickQueue

        self.clickRay.setFromLens(self.camNode, self.mouseWatcher.getMouse())
        self.clickNode.setFromCollideMask(mask)
        self.clickNode.setIntoCollideMask(BitMask32.allOff())
        self.clickNp.reparentTo(self.cam)
        queue.clearEntries()
        if not traverser:
            base.clickTraverse(self.clickNp, queue)
        else:
            if not root:
                root = self.doc.render
            traverser.addCollider(self.clickNp, queue)
            traverser.traverse(root)
            traverser.removeCollider(self.clickNp)
        queue.sortEntries()
        self.clickNp.reparentTo(NodePath())

        return queue.getEntries()

    def fixRatio(self, size=None):
        if not self.lens:
            return

        if size is None:
            aspectRatio = self.win.getXSize() / self.win.getYSize()
        else:
            if size.y > 0:
                aspectRatio = size.x / size.y
            else:
                aspectRatio = 1.0

        if self.is2D():
            zoomFactor = (1.0 / self.zoom) * 100.0
            self.lens.setFilmSize(zoomFactor * aspectRatio, zoomFactor)
        else:
            self.lens.setAspectRatio(aspectRatio)

        if aspectRatio != self.__oldAspectRatio:
            self.__oldAspectRatio = aspectRatio
            # Fix up some anything that depends on the aspectRatio
            if aspectRatio < 1:
                # If the window is TALL, lets expand the top and bottom
                self.aspect2d.setScale(1.0, aspectRatio, aspectRatio)
                self.a2dTop = 1.0 / aspectRatio
                self.a2dBottom = -1.0 / aspectRatio
                self.a2dLeft = -1
                self.a2dRight = 1.0
            else:
                # If the window is WIDE, lets expand the left and right
                self.aspect2d.setScale(1.0 / aspectRatio, 1.0, 1.0)
                self.a2dTop = 1.0
                self.a2dBottom = -1.0
                self.a2dLeft = -aspectRatio
                self.a2dRight = aspectRatio

            # Reposition the aspect2d marker nodes
            self.a2dTopCenter.setPos(0, 0, self.a2dTop)
            self.a2dTopCenterNs.setPos(0, 0, self.a2dTop)
            self.a2dBottomCenter.setPos(0, 0, self.a2dBottom)
            self.a2dBottomCenterNs.setPos(0, 0, self.a2dBottom)
            self.a2dLeftCenter.setPos(self.a2dLeft, 0, 0)
            self.a2dLeftCenterNs.setPos(self.a2dLeft, 0, 0)
            self.a2dRightCenter.setPos(self.a2dRight, 0, 0)
            self.a2dRightCenterNs.setPos(self.a2dRight, 0, 0)

            self.a2dTopLeft.setPos(self.a2dLeft, 0, self.a2dTop)
            self.a2dTopLeftNs.setPos(self.a2dLeft, 0, self.a2dTop)
            self.a2dTopRight.setPos(self.a2dRight, 0, self.a2dTop)
            self.a2dTopRightNs.setPos(self.a2dRight, 0, self.a2dTop)
            self.a2dBottomLeft.setPos(self.a2dLeft, 0, self.a2dBottom)
            self.a2dBottomLeftNs.setPos(self.a2dLeft, 0, self.a2dBottom)
            self.a2dBottomRight.setPos(self.a2dRight, 0, self.a2dBottom)
            self.a2dBottomRightNs.setPos(self.a2dRight, 0, self.a2dBottom)

    def resizeEvent(self, event):
        if not self.win:
            return

        newsize = LVector2i(event.size().width(), event.size().height())
        self.qtWidget.resize(newsize[0], newsize[1])
        self.qtWidget.move(0, 0)

        #props = WindowProperties()
        #props.setSize(newsize)
        #props.setOrigin(0, 0)
        #self.win.requestProperties(props)

        self.fixRatio(newsize)

        self.onResize(newsize)

        self.updateView()

    def onResize(self, newsize):
        pass

    def draw(self):
        pass

    def enable(self):
        # Render to the viewport
        self.win.setActive(True)
        self.enabled = True

    def disable(self):
        # Don't render to the viewport
        self.win.setActive(False)
        self.enabled = False
Пример #11
0
    def generate(self, helperInfo):
        color = self.mapObject.getPropertyValue("_light",
                                                default=Vec4(
                                                    255, 255, 255, 255))
        color = LEGlobals.colorFromRGBScalar255(color)
        color = LEGlobals.vec3GammaToLinear(color)

        intensity = self.mapObject.getPropertyValue("_intensity", default=1.0)
        innerRadius = self.mapObject.getPropertyValue("_inner_radius",
                                                      default=1.0)
        outerRadius = self.mapObject.getPropertyValue("_outer_radius",
                                                      default=2.0)

        color[0] = color[0] * intensity
        color[1] = color[1] * intensity
        color[2] = color[2] * intensity

        constant = self.mapObject.getPropertyValue("_constant_attn",
                                                   default=0.0)
        linear = self.mapObject.getPropertyValue("_linear_attn", default=0.0)
        quadratic = self.mapObject.getPropertyValue("_quadratic_attn",
                                                    default=1.0)

        innerConeDeg = self.mapObject.getPropertyValue("_inner_cone")
        innerConeRad = deg2Rad(innerConeDeg)
        outerConeDeg = self.mapObject.getPropertyValue("_cone")
        outerConeRad = deg2Rad(outerConeDeg)

        depthBias = self.mapObject.getPropertyValue("_depth_bias")
        shadowSize = self.mapObject.getPropertyValue("_shadow_map_size")
        shadowCaster = self.mapObject.getPropertyValue("_shadow_caster")
        softnessFactor = self.mapObject.getPropertyValue("_softness_factor")
        normalOffsetScale = self.mapObject.getPropertyValue(
            "_normal_offset_scale")
        normalOffsetUvSpace = self.mapObject.getPropertyValue(
            "_normal_offset_uv_space")

        pl = Spotlight("lightHelper-light_spot")
        pl.setColor(Vec4(color[0], color[1], color[2], 1.0))
        pl.setFalloff(quadratic)
        pl.setInnerRadius(innerRadius)
        pl.setOuterRadius(outerRadius)
        pl.setExponent(self.mapObject.getPropertyValue("_exponent"))
        pl.setInnerCone(innerConeDeg)
        pl.setOuterCone(outerConeDeg)
        if shadowCaster:
            pl.setCameraMask(DirectRender.ShadowCameraBitmask)
            pl.setShadowCaster(True, shadowSize, shadowSize)
            pl.setDepthBias(depthBias)
            pl.setSoftnessFactor(softnessFactor)
            pl.setNormalOffsetScale(normalOffsetScale)
            pl.setNormalOffsetUvSpace(normalOffsetUvSpace)
        self.light = self.mapObject.helperRoot.attachNewNode(pl)
        if True:  #self.mapObject.doc.numlights < 64:
            self.mapObject.doc.render.setLight(self.light)
            self.mapObject.doc.numlights += 1
            self.hasLight = True

        self.spotlightMdl = base.loader.loadModel(
            "models/misc/spotlight-editor.bam")
        #self.spotlightMdl.setState("materials/spotlight-editor.mat")
        #state = self.spotlightMdl.getState()
        #params = state.getAttrib(ShaderParamAttrib)
        #params = params.setParam("selfillumtint", CKeyValues.toString(color.getXyz()))
        #print(params)
        #self.spotlightMdl.setState(state.setAttrib(params))
        self.spotlightMdl.reparentTo(self.light)
        self.spotlightMdl.setScale(0.5)
        self.spotlightMdl.setH(180)
        self.spotlightMdl.setLightOff(1)
        self.spotlightMdl.setRenderModeWireframe(1)
        self.spotlightMdl.setTextureOff(1)
        self.spotlightMdl.setColor(Vec4(0, 0, 0, 1))
        #self.spotlightMdl.setLightOff(self.light, 1)
        #self.spotlightMdl.ls()

        innerCone = getUnitCone().copyTo(self.light)
        innerCone.setSy(innerRadius)
        innerCone.setSx((innerConeRad / 2) * innerRadius)
        innerCone.setSz((innerConeRad / 2) * innerRadius)
        innerCone.setColorScale(InnerColor)
        self.innerCone = innerCone

        outerCone = getUnitCone().copyTo(self.light)
        outerCone.setSy(outerRadius)
        outerCone.setSx((outerConeRad / 2) * outerRadius)
        outerCone.setSz((outerConeRad / 2) * outerRadius)
        outerCone.setColorScale(OuterColor)
        self.outerCone = outerCone

        if not self.mapObject.selected:
            innerCone.stash()
            outerCone.stash()
Пример #12
0
 def calcZoom(self):
     z = max(int(abs(self.viewport.camera.getZ() * 16)), 0.001)
     return LEGlobals.clamp(10000 / z, 0.001 * 16, 256 * 16)
Пример #13
0
    def gotModel(self, mdl, filename, context):
        if context.destroyed:
            return

        self.currentLoadContext = None

        if not mdl or mdl.isEmpty():
            context.createNextAsset()
            return

        # If there's no geomnode, there is no model!
        if mdl.find("**/+GeomNode").isEmpty():
            context.createNextAsset()
            return

        mdl.reparentTo(self.render)

        # Determine a good offset point to take the thumbnail snapshot
        mins = core.Point3()
        maxs = core.Point3()
        mdl.calcTightBounds(mins, maxs)
        size = maxs - mins
        center = (mins + maxs) / 2.0
        # Choose the longest axis as the radius
        radius = size.length() / 2

        fov = self.lens.getFov()
        distance = (radius / float(math.tan(core.deg2Rad(min(fov[0], fov[1]) / 2.0))))

        # Ensure the far plane is far enough back to see the entire object.
        idealFarPlane = distance + radius * 1.5
        self.lens.setFar(max(self.lens.getDefaultFar(), idealFarPlane))

        # And that the near plane is far enough forward.
        idealNearPlane = distance - radius
        self.lens.setNear(min(self.lens.getDefaultNear(), idealNearPlane))

        self.camera.setPos(center + self.camera.getQuat().xform(core.Vec3.forward() * -distance))

        # Render the model to the back buffer
        self.buffer.setActive(True)
        base.graphicsEngine.renderFrame()
        base.graphicsEngine.syncFrame()

        # Fetch the pixels into a PNMImage
        image = core.PNMImage()
        self.buffer.getScreenshot(image)

        self.buffer.setActive(False)

        mdl.removeNode()

        # Store the pixels in a QPixmap
        qimage = QtGui.QImage(image.getXSize(), image.getYSize(), QtGui.QImage.Format_RGB888)
        for x in range(image.getXSize()):
            for y in range(image.getYSize()):
                col = LEGlobals.vec3LinearToGamma(image.getXelA(x, y))
                qimage.setPixelColor(x, y,
                    QtGui.QColor(int(col[0] * 255), int(col[1] * 255), int(col[2] * 255), int(col[3] * 255)))

        pixmap = QtGui.QPixmap.fromImage(qimage)
        icon = QtGui.QIcon(pixmap)
        self.modelThumbnails[filename] = icon

        context.addAssetItem(icon, filename)

        context.createNextAsset()