Ejemplo n.º 1
0
    def generate(self, helperInfo):
        color = self.mapObject.getPropertyValue("_light",
                                                default=Vec4(
                                                    255, 255, 255, 255))
        color = CIGlobals.colorFromRGBScalar255(color)
        color = CIGlobals.vec3GammaToLinear(color)

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

        # Scale intensity for unit 100 distance
        ratio = (constant + 100 * linear + 100 * 100 * quadratic)
        if ratio > 0:
            color *= ratio

        pl = Spotlight("lightHelper-light_spot")
        pl.setColor(Vec4(color[0], color[1], color[2], 1.0))
        pl.setAttenuation(Vec3(constant, linear, quadratic))
        pl.setExponent(self.mapObject.getPropertyValue("_exponent"))
        pl.setMaxDistance(self.mapObject.getPropertyValue("_distance"))
        pl.getLens().setFov(self.mapObject.getPropertyValue("_cone"))
        pl.getLens().setViewHpr(0, -90, 0)
        self.light = self.mapObject.helperRoot.attachNewNode(pl)
        if self.mapObject.doc.numlights < 64:
            self.mapObject.doc.render.setLight(self.light)
            self.mapObject.doc.numlights += 1
            self.hasLight = True
Ejemplo n.º 2
0
	def __setSpotlight(self,props):
		light = Spotlight(props['name'])
		light.setShadowCaster(props['castShadows'],2048,2048)
		light.setAttenuation(props['attenuation'])		
		lens = PerspectiveLens()

		fov = math.degrees( props['range'].x )		
		lens.setFov(fov)
		lens.setFilmSize(5096); 
		light.setLens(lens)
		return self.__createLight(light,props)
Ejemplo n.º 3
0
    def __setSpotlight(self, props):
        light = Spotlight(props['name'])
        light.setShadowCaster(props['castShadows'], 2048, 2048)
        light.setAttenuation(props['attenuation'])
        lens = PerspectiveLens()

        fov = math.degrees(props['range'].x)
        lens.setFov(fov)
        lens.setFilmSize(5096)
        light.setLens(lens)
        return self.__createLight(light, props)
Ejemplo n.º 4
0
class Flashlight:

	POWER_MIN  = 0.06
	POWER_DRAIN_RATE = -1/240.0
	POWER_GAIN_RATE  =  1/480.0
	
	EXPONENET = 40 
	
	COLOR = Vec4(1.0, 1.0, 0.9, 1)
	
	ATT_CONST = 0
	ATT_LIN   = 0.3
	ATT_QUAD  = 0.05
	
	def __init__(self, name, owner, scene, pos, fov=60, near=0.01, far=100, resol=(1920, 1080)):
		self.owner = owner
		self.scene = scene
		
		self.last      = 0.0
		self.power     = 1.0
		self.on        = True
		self.powerrate = Flashlight.POWER_DRAIN_RATE 

		#TODO: Change lens parameters for a unique 'lens' parameter
		lens = PerspectiveLens()
		lens.setFov(fov)
		lens.setNearFar(near, far)
		lens.setFilmSize(resol[0], resol[1])

		self.light = Spotlight(name + '/wide')
		self.light.setLens(lens)
		#TODO: Shadows don't work well anymore
		#self.light.setShadowCaster(True, resol[0], resol[1])
		self.light.setColor(Flashlight.COLOR)
		self.light.setExponent(40)
		
		self.nodepath = owner.cam.attachNewNode(self.light)
		self.nodepath.setPos(pos)
		self.nodepath.setHpr((5,5,0))
		
		self.scene.setLight(self.nodepath)

	def __del__(self):
		self.nodepath.removeNode()

	def toggle(self):
		self.on = not self.on
		if self.on:
			self.scene.setLight(self.nodepath)
			self.powerrate = Flashlight.POWER_DRAIN_RATE
		else:
			self.scene.clearLight(self.nodepath)
			self.powerrate = Flashlight.POWER_GAIN_RATE

	def updatePower(self, task):
		elapsed = task.time - self.last
		
		self.last = task.time
		self.power = max(Flashlight.POWER_MIN, min(1.0, self.power + elapsed * self.powerrate))
		
		self.light.setAttenuation((Flashlight.ATT_CONST, Flashlight.ATT_LIN, Flashlight.ATT_QUAD / self.power))

		return task.cont
	
	def getNodePath(self):
		return self.nodepath
	
	def setHpr(self, vec):
		self.nodepath.setHpr(vec)

	def isOn(self):
		return self.on
Ejemplo n.º 5
0
    def getLightsForModel(self, modelId):
        lights = []
        if modelId in self.supportedModelIds:

            for n, lightData in enumerate(self.data[modelId]):

                attenuation = LVector3f(*lightData['attenuation'])

                #TODO: implement light power
                #power = float(lightData['power'])

                positionYup = LVector3f(*lightData['position'])
                yupTozupMat = LMatrix4f.convertMat(CS_yup_right, CS_zup_right)
                position = yupTozupMat.xformVec(positionYup)

                colorHtml = lightData['color']
                color = LVector3f(*[
                    int('0x' + colorHtml[i:i + 2], 16)
                    for i in range(1, len(colorHtml), 2)
                ]) / 255.0

                direction = None
                lightType = lightData['type']
                lightName = modelId + '-light-' + str(n)
                if lightType == 'SpotLight':
                    light = Spotlight(lightName)
                    light.setAttenuation(attenuation)
                    light.setColor(color)

                    cutoffAngle = float(lightData['cutoffAngle'])
                    lens = PerspectiveLens()
                    lens.setFov(cutoffAngle / np.pi * 180.0)
                    light.setLens(lens)

                    # NOTE: unused attributes
                    #dropoffRate = float(lightData['dropoffRate'])

                    directionYup = LVector3f(*lightData['direction'])
                    direction = yupTozupMat.xformVec(directionYup)

                elif lightType == 'PointLight':
                    light = PointLight(lightName)
                    light.setAttenuation(attenuation)
                    light.setColor(color)

                elif lightType == 'LineLight':
                    #XXX: we may wish to use RectangleLight from the devel branch of Panda3D
                    light = PointLight(lightName)
                    light.setAttenuation(attenuation)
                    light.setColor(color)

                    # NOTE: unused attributes
                    #dropoffRate = float(lightData['dropoffRate'])
                    #cutoffAngle = float(lightData['cutoffAngle'])

                    #position2Yup = LVector3f(*lightData['position2'])
                    #position2 = yupTozupMat.xformVec(position2Yup)

                    #directionYup = LVector3f(*lightData['direction'])
                    #direction = yupTozupMat.xformVec(directionYup)

                else:
                    raise Exception('Unsupported light type: %s' % (lightType))

                lightNp = NodePath(light)

                # Set position and direction of light
                lightNp.setPos(position)
                if direction is not None:
                    targetPos = position + direction
                    lightNp.look_at(targetPos, LVector3f.up())

                lights.append(lightNp)

        return lights
def runViewer(mesh):
    scene_members = getSceneMembers(mesh)
    
    loadPrcFileData('', 'win-size 300 300')
    base = ShowBase()
    globNode = GeomNode("collada")
    nodePath = base.render.attachNewNode(globNode)
    
    rotateNode = GeomNode("rotater")
    rotatePath = nodePath.attachNewNode(rotateNode)
    matrix = numpy.identity(4)
    if mesh.assetInfo.upaxis == collada.asset.UP_AXIS.X_UP:
        r = collada.scene.RotateTransform(0,1,0,90)
        matrix = r.matrix
    elif mesh.assetInfo.upaxis == collada.asset.UP_AXIS.Y_UP:
        r = collada.scene.RotateTransform(1,0,0,90)
        matrix = r.matrix
    rotatePath.setMat(Mat4(*matrix.T.flatten().tolist()))
    
    basecollada = GeomNode("basecollada")
    basecolladaNP = rotatePath.attachNewNode(basecollada)
    for geom, renderstate, mat4 in scene_members:
        node = GeomNode("primitive")
        node.addGeom(geom)
        if renderstate is not None:
            node.setGeomState(0, renderstate)
        geomPath = basecolladaNP.attachNewNode(node)
        geomPath.setMat(mat4)

    for boundlight in mesh.scene.objects('light'):
        
        if len(boundlight.color) == 3:
            color = (boundlight.color[0], boundlight.color[1], boundlight.color[2], 1)
        else:
            color = boundlight.color
        
        if isinstance(boundlight, collada.light.BoundDirectionalLight):
            dl = DirectionalLight('dirLight')
            dl.setColor(Vec4(color[0], color[1], color[2], color[3]))
            lightNP = rotatePath.attachNewNode(dl)
            lightNP.lookAt(Point3(boundlight.direction[0],boundlight.direction[1],boundlight.direction[2]))
        elif isinstance(boundlight, collada.light.BoundAmbientLight):
            ambientLight = AmbientLight('ambientLight')
            ambientLight.setColor(Vec4(color[0], color[1], color[2], color[3]))
            lightNP = rotatePath.attachNewNode(ambientLight)
        elif isinstance(boundlight, collada.light.BoundPointLight):
            pointLight = PointLight('pointLight')
            pointLight.setColor(Vec4(color[0], color[1], color[2], color[3]))
            pointLight.setAttenuation(Vec3(boundlight.constant_att, boundlight.linear_att, boundlight.quad_att))
            lightNP = rotatePath.attachNewNode(pointLight)
            lightNP.setPos(Vec3(boundlight.position[0], boundlight.position[1], boundlight.position[2]))
        elif isinstance(boundlight, collada.light.BoundSpotLight):
            spotLight = Spotlight('spotLight')
            spotLight.setColor(Vec4(color[0], color[1], color[2], color[3]))
            spotLight.setAttenuation(Vec3(boundlight.constant_att, boundlight.linear_att, boundlight.quad_att))
            spotLight.setExponent(boundlight.falloff_exp)
            lightNP = rotatePath.attachNewNode(spotLight)
            lightNP.setPos(Vec3(boundlight.position[0], boundlight.position[1], boundlight.position[2]))
            lightNP.lookAt(Point3(boundlight.direction[0], boundlight.direction[1], boundlight.direction[2]),
                               Vec3(boundlight.up[0], boundlight.up[1], boundlight.up[2]))
        else:
            print('Unknown light type', boundlight)
            continue
            
        base.render.setLight(lightNP)

    for boundcam in mesh.scene.objects('camera'):
        if isinstance(boundcam, collada.camera.BoundPerspectiveCamera):
            base.camera.reparentTo(rotatePath)
            base.camLens.setNear(boundcam.znear)
            base.camLens.setFar(boundcam.zfar)
            
            if boundcam.xfov is not None and boundcam.yfov is not None:
                #xfov + yfov
                base.camLens.setFov(boundcam.xfov, boundcam.yfov)
            elif boundcam.xfov is not None and boundcam.aspect_ratio is not None:
                #xfov + aspect_ratio
                base.camLens.setFov(boundcam.xfov)
                base.camLens.setAspectRatio(boundcam.aspect_ratio)
            elif boundcam.yfov is not None and boundcam.aspect_ratio is not None:
                #yfov + aspect_ratio
                #aspect_ratio = tan(0.5*xfov) / tan(0.5*yfov)
                xfov = math.degrees(2.0 * math.atan(boundcam.aspect_ratio * math.tan(math.radians(0.5 * boundcam.yfov))))
                base.camLens.setFov(xfov, boundcam.yfov)
            elif boundcam.yfov is not None:
                #yfov only
                #aspect_ratio = tan(0.5*xfov) / tan(0.5*yfov)
                xfov = math.degrees(2.0 * math.atan(base.camLens.getAspectRatio() * math.tan(math.radians(0.5 * boundcam.yfov))))
                base.camLens.setFov(xfov, boundcam.yfov)
            elif boundcam.xfov is not None:
                base.camLens.setFov(boundcam.xfov)
            
            base.camera.setPos(Vec3(boundcam.position[0], boundcam.position[1], boundcam.position[2]))
            base.camera.lookAt(Point3(boundcam.direction[0], boundcam.direction[1], boundcam.direction[2]),
                               Vec3(boundcam.up[0], boundcam.up[1], boundcam.up[2]))
        elif isinstance(boundcam, collada.camera.BoundOrthographicCamera):
            
            lens = OrthographicLens()
            base.cam.node().setLens(lens)
            base.camLens = lens
            base.camera.reparentTo(rotatePath)
            base.camLens.setNear(boundcam.znear)
            base.camLens.setFar(boundcam.zfar)
            
            if boundcam.xmag is not None and boundcam.ymag is not None:
                #xmag + ymag
                base.camLens.setFilmSize(boundcam.xmag, boundcam.ymag)
            elif boundcam.xmag is not None and boundcam.aspect_ratio is not None:
                #xmag + aspect_ratio
                base.camLens.setFilmSize(boundcam.xmag)
                base.camLens.setAspectRatio(boundcam.aspect_ratio)
            elif boundcam.ymag is not None and boundcam.aspect_ratio is not None:
                #ymag + aspect_ratio
                xmag = boundcam.aspect_ratio * boundcam.ymag
                base.camLens.setFilmSize(xmag, boundcam.ymag)
            elif boundcam.ymag is not None:
                #ymag only
                xmag = base.camLens.getAspectRatio() * boundcam.ymag
                base.camLens.setFilmSize(xmag, boundcam.ymag)
            elif boundcam.xmag is not None:
                base.camLens.setFilmSize(boundcam.xmag)
            
            base.camera.setPos(Vec3(boundcam.position[0], boundcam.position[1], boundcam.position[2]))
            base.camera.lookAt(Point3(boundcam.direction[0], boundcam.direction[1], boundcam.direction[2]),
                               Vec3(boundcam.up[0], boundcam.up[1], boundcam.up[2]))
            
        else:
            print('Unknown camera type', boundcam)
            continue

    base.disableMouse()
    base.render.setShaderAuto()
    base.render.setTransparency(TransparencyAttrib.MDual, 1)
    
    KeyboardMovement()
    MouseDrag(basecolladaNP)
    MouseScaleZoom(basecolladaNP)
    
    base.run()
Ejemplo n.º 7
0
def runViewer(mesh):
    scene_members = getSceneMembers(mesh)

    loadPrcFileData('', 'win-size 300 300')
    base = ShowBase()
    globNode = GeomNode("collada")
    nodePath = base.render.attachNewNode(globNode)

    rotateNode = GeomNode("rotater")
    rotatePath = nodePath.attachNewNode(rotateNode)
    matrix = numpy.identity(4)
    if mesh.assetInfo.upaxis == collada.asset.UP_AXIS.X_UP:
        r = collada.scene.RotateTransform(0, 1, 0, 90)
        matrix = r.matrix
    elif mesh.assetInfo.upaxis == collada.asset.UP_AXIS.Y_UP:
        r = collada.scene.RotateTransform(1, 0, 0, 90)
        matrix = r.matrix
    rotatePath.setMat(Mat4(*matrix.T.flatten().tolist()))

    basecollada = GeomNode("basecollada")
    basecolladaNP = rotatePath.attachNewNode(basecollada)
    for geom, renderstate, mat4 in scene_members:
        node = GeomNode("primitive")
        node.addGeom(geom)
        if renderstate is not None:
            node.setGeomState(0, renderstate)
        geomPath = basecolladaNP.attachNewNode(node)
        geomPath.setMat(mat4)

    for boundlight in mesh.scene.objects('light'):

        if len(boundlight.color) == 3:
            color = (boundlight.color[0], boundlight.color[1],
                     boundlight.color[2], 1)
        else:
            color = boundlight.color

        if isinstance(boundlight, collada.light.BoundDirectionalLight):
            dl = DirectionalLight('dirLight')
            dl.setColor(Vec4(color[0], color[1], color[2], color[3]))
            lightNP = rotatePath.attachNewNode(dl)
            lightNP.lookAt(
                Point3(boundlight.direction[0], boundlight.direction[1],
                       boundlight.direction[2]))
        elif isinstance(boundlight, collada.light.BoundAmbientLight):
            ambientLight = AmbientLight('ambientLight')
            ambientLight.setColor(Vec4(color[0], color[1], color[2], color[3]))
            lightNP = rotatePath.attachNewNode(ambientLight)
        elif isinstance(boundlight, collada.light.BoundPointLight):
            pointLight = PointLight('pointLight')
            pointLight.setColor(Vec4(color[0], color[1], color[2], color[3]))
            pointLight.setAttenuation(
                Vec3(boundlight.constant_att, boundlight.linear_att,
                     boundlight.quad_att))
            lightNP = rotatePath.attachNewNode(pointLight)
            lightNP.setPos(
                Vec3(boundlight.position[0], boundlight.position[1],
                     boundlight.position[2]))
        elif isinstance(boundlight, collada.light.BoundSpotLight):
            spotLight = Spotlight('spotLight')
            spotLight.setColor(Vec4(color[0], color[1], color[2], color[3]))
            spotLight.setAttenuation(
                Vec3(boundlight.constant_att, boundlight.linear_att,
                     boundlight.quad_att))
            spotLight.setExponent(boundlight.falloff_exp)
            lightNP = rotatePath.attachNewNode(spotLight)
            lightNP.setPos(
                Vec3(boundlight.position[0], boundlight.position[1],
                     boundlight.position[2]))
            lightNP.lookAt(
                Point3(boundlight.direction[0], boundlight.direction[1],
                       boundlight.direction[2]),
                Vec3(boundlight.up[0], boundlight.up[1], boundlight.up[2]))
        else:
            print 'Unknown light type', boundlight
            continue

        base.render.setLight(lightNP)

    for boundcam in mesh.scene.objects('camera'):
        if isinstance(boundcam, collada.camera.BoundPerspectiveCamera):
            base.camera.reparentTo(rotatePath)
            base.camLens.setNear(boundcam.znear)
            base.camLens.setFar(boundcam.zfar)

            if boundcam.xfov is not None and boundcam.yfov is not None:
                #xfov + yfov
                base.camLens.setFov(boundcam.xfov, boundcam.yfov)
            elif boundcam.xfov is not None and boundcam.aspect_ratio is not None:
                #xfov + aspect_ratio
                base.camLens.setFov(boundcam.xfov)
                base.camLens.setAspectRatio(boundcam.aspect_ratio)
            elif boundcam.yfov is not None and boundcam.aspect_ratio is not None:
                #yfov + aspect_ratio
                #aspect_ratio = tan(0.5*xfov) / tan(0.5*yfov)
                xfov = math.degrees(
                    2.0 *
                    math.atan(boundcam.aspect_ratio *
                              math.tan(math.radians(0.5 * boundcam.yfov))))
                base.camLens.setFov(xfov, boundcam.yfov)
            elif boundcam.yfov is not None:
                #yfov only
                #aspect_ratio = tan(0.5*xfov) / tan(0.5*yfov)
                xfov = math.degrees(
                    2.0 *
                    math.atan(base.camLens.getAspectRatio() *
                              math.tan(math.radians(0.5 * boundcam.yfov))))
                base.camLens.setFov(xfov, boundcam.yfov)
            elif boundcam.xfov is not None:
                base.camLens.setFov(boundcam.xfov)

            base.camera.setPos(
                Vec3(boundcam.position[0], boundcam.position[1],
                     boundcam.position[2]))
            base.camera.lookAt(
                Point3(boundcam.direction[0], boundcam.direction[1],
                       boundcam.direction[2]),
                Vec3(boundcam.up[0], boundcam.up[1], boundcam.up[2]))
        elif isinstance(boundcam, collada.camera.BoundOrthographicCamera):

            lens = OrthographicLens()
            base.cam.node().setLens(lens)
            base.camLens = lens
            base.camera.reparentTo(rotatePath)
            base.camLens.setNear(boundcam.znear)
            base.camLens.setFar(boundcam.zfar)

            if boundcam.xmag is not None and boundcam.ymag is not None:
                #xmag + ymag
                base.camLens.setFilmSize(boundcam.xmag, boundcam.ymag)
            elif boundcam.xmag is not None and boundcam.aspect_ratio is not None:
                #xmag + aspect_ratio
                base.camLens.setFilmSize(boundcam.xmag)
                base.camLens.setAspectRatio(boundcam.aspect_ratio)
            elif boundcam.ymag is not None and boundcam.aspect_ratio is not None:
                #ymag + aspect_ratio
                xmag = boundcam.aspect_ratio * boundcam.ymag
                base.camLens.setFilmSize(xmag, boundcam.ymag)
            elif boundcam.ymag is not None:
                #ymag only
                xmag = base.camLens.getAspectRatio() * boundcam.ymag
                base.camLens.setFilmSize(xmag, boundcam.ymag)
            elif boundcam.xmag is not None:
                base.camLens.setFilmSize(boundcam.xmag)

            base.camera.setPos(
                Vec3(boundcam.position[0], boundcam.position[1],
                     boundcam.position[2]))
            base.camera.lookAt(
                Point3(boundcam.direction[0], boundcam.direction[1],
                       boundcam.direction[2]),
                Vec3(boundcam.up[0], boundcam.up[1], boundcam.up[2]))

        else:
            print 'Unknown camera type', boundcam
            continue

    base.disableMouse()
    base.render.setShaderAuto()
    base.render.setTransparency(TransparencyAttrib.MDual, 1)

    KeyboardMovement()
    MouseDrag(basecolladaNP)
    MouseScaleZoom(basecolladaNP)

    base.run()