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
def _set_general_lights(self, train_np): """Set initial Sun lights. Args: train_np (panda3d.core.NodePath): Train node. Returns: panda3d.core.AmbientLight: Sun ambient light. panda3d.core.DirectionalLight: Sun directional light. panda3d.core.NodePath: NodePath of the Sun. """ amb_light = AmbientLight("sun_amb") amb_light.setColor(self._color["amb"]) render.setLight(render.attachNewNode(amb_light)) # noqa: F821 lens = PerspectiveLens() lens.setNearFar(1, 100) lens.setFov(20, 20) sun_light = Spotlight("sun_dir") sun_light.setColor(self._color["dir"]) sun_light.setShadowCaster(True, 8192, 8192, sort=-2000) sun_light.setLens(lens) sun_light.setExponent(0.5) sun_light.setCameraMask(0b0001) sun_np = train_np.attachNewNode(sun_light) render.setLight(sun_np) # noqa: F821 return amb_light, sun_light, sun_np
def set_spotlight(self, col, exp, cutoff, pos, look_at): lgt = Spotlight('spotlight') lgt.setColor(col) lgt.setExponent(exp) lgt.getLens().setFov(cutoff, cutoff) self.__set_lgt(lgt) self.lights[-1].setPos(*pos) self.lights[-1].lookAt(*look_at)
def __init__(self): self._train_pos = base.train.root_node.getPos() # noqa: F821 self._mod = loader.loadModel(address("city_hangar")) # noqa: F821 base.camera_ctrl.set_hangar_pos(self._mod) # noqa: F821 self._mod.reparentTo(base.train.model) # noqa: F821 lens = PerspectiveLens() lens.setNearFar(0, 10) lens.setFov(100, 100) lighter = Spotlight("hangar_light") lighter.setColor((1, 1, 1, 1)) lighter.setLens(lens) self._lighter_np = self._mod.attachNewNode(lighter) self._lighter_np.setPos(-0.3, 0.65, 4) self._lighter_np.lookAt(base.train.model) # noqa: F821 render.setLight(self._lighter_np) # noqa: F821 flies = ParticleEffect() flies.loadConfig("effects/flies.ptf") flies.setPos(-0.465, 0.11, 0.17) flies.start(self._mod, render) # noqa: F821 lens = PerspectiveLens() lens.setNearFar(0, 50) lens.setFov(80, 80) lamp = Spotlight("hangar_light") lamp.setColor((0.7, 0.7, 0.7, 1)) lamp.setLens(lens) lamp.setExponent(0.002) self._lamp_np = self._mod.attachNewNode(lamp) self._lamp_np.setPos(-0.47, 0.11, 0.195) self._lamp_np.setHpr(-180, -60, 0) render.setLight(self._lamp_np) # noqa: F821
def _set_lights(self): """Configure the locomotive lights. Sets the main locomotive lighter and lights above the doors. Returns: list: NodePath's of the Train lights. """ lens = PerspectiveLens() lens.setNearFar(0, 50) lens.setFov(60, 60) floodlight = Spotlight("train_main_lighter") floodlight.setColor((0.5, 0.5, 0.5, 1)) floodlight.setLens(lens) floodlight.setExponent(0.4) floodlight_np = self.model.attachNewNode(floodlight) floodlight_np.setPos(0, 0.34, 50) render.setLight(floodlight_np) # noqa: F821 train_lights = [floodlight_np] for name, coors in ( ("train_right_door_light", (0.073, -0.17, 50)), ("train_left_door_light", (-0.073, -0.17, 50)), ("train_back_door_light", (0, -0.63, 50)), ): lamp = PointLight(name) lamp.setColor((0.89, 0.81, 0.55, 1)) lamp.setAttenuation(3) lamp_np = self.model.attachNewNode(lamp) lamp_np.setPos(*coors) render.setLight(lamp_np) # noqa: F821 train_lights.append(lamp_np) return train_lights
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
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()
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()
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()