def apply(self): winprops = WindowProperties.size(2048, 2048) props = FrameBufferProperties() props.setRgbColor(1) props.setAlphaBits(1) props.setDepthBits(1) lbuffer = base.graphicsEngine.makeOutput(base.pipe, 'offscreen buffer', -2, props, winprops, GraphicsPipe.BFRefuseWindow, base.win.getGsg(), base.win) self.buffer = lbuffer ldepthmap = Texture() lbuffer.addRenderTexture(ldepthmap, GraphicsOutput.RTMBindOrCopy, GraphicsOutput.RTPDepthStencil) ldepthmap.setMinfilter(Texture.FTShadow) ldepthmap.setMagfilter(Texture.FTShadow) base.camLens.setNearFar(1.0, 10000) base.camLens.setFov(75) self.lcam = base.makeCamera(lbuffer) self.lcam.node().setScene(render) self.lcam.node().getLens().setFov(45) self.lcam.node().getLens().setNearFar(1, 100) render.setShaderInput('light', self.lcam) render.setShaderInput('depthmap', ldepthmap) render.setShaderInput('ambient', .15, .15, .15, 1.0) lci = NodePath(PandaNode('light camera initializer')) with open('yyagl/assets/shaders/caster.vert') as f: vert = f.read() with open('yyagl/assets/shaders/caster.frag') as f: frag = f.read() lci.setShader(Shader.make(Shader.SLGLSL, vert, frag)) self.lcam.node().setInitialState(lci.getState()) mci = NodePath(PandaNode('main camera initializer')) with open('yyagl/assets/shaders/main.vert') as f: vert = f.read() with open('yyagl/assets/shaders/main.frag') as f: frag = f.read() frag = frag.replace('<LIGHTS>', str(len(self.lights))) # use PTALVecBaseX instead # setShaderInput('vec3argname', PTALVecBase3(((0, 0, 0), (1, 1, 1)))) render.setShader(Shader.make(Shader.SLGLSL, vert, frag)) render.setShaderInput('num_lights', len(self.lights)) map(lambda lgt: self.set_lgt_args(*lgt), enumerate(self.lights)) mci.setShader(Shader.make(Shader.SLGLSL, vert, frag)) base.cam.node().setInitialState(mci.getState()) self.lcam.setPos(15, 30, 45) self.lcam.lookAt(0, 15, 0) self.lcam.node().getLens().setNearFar(1, 100)
def __init__(self): # The basics ShowBase.__init__(self) base.disableMouse() base.setBackgroundColor(0.1, 0.1, 0.1) base.setFrameRateMeter(True) self.accept("escape", sys.exit) # Camera self.camera_orbit = base.render.attach_new_node("Camera orbit") self.camera_pitch = self.camera_orbit.attach_new_node("Camera pitch") base.camera.reparent_to(self.camera_pitch) base.camera.set_pos(0, -5, 0) # Camera control self.camera_movement = (0, 0) self.accept("arrow_up", self.change_camera_movement, [0, -1]) self.accept("arrow_up-up", self.change_camera_movement, [0, 1]) self.accept("arrow_down", self.change_camera_movement, [0, 1]) self.accept("arrow_down-up", self.change_camera_movement, [0, -1]) self.accept("arrow_left", self.change_camera_movement, [-1, 0]) self.accept("arrow_left-up", self.change_camera_movement, [1, 0]) self.accept("arrow_right", self.change_camera_movement, [1, 0]) self.accept("arrow_right-up", self.change_camera_movement, [-1, 0]) base.taskMgr.add(self.move_camera, "Move camera") # Object self.model = loader.loadModel("models/smiley") self.model.reparent_to(base.render) shader = Shader.make( Shader.SL_GLSL, vertex=vertex_shader, fragment=fragment_shader, ) self.model.set_shader(shader)
def compile(self): self._shader = Panda3dShader.make(self.language, self.vertex, self.fragment, self.geometry) self.compiled = True if not self in imported_shaders: imported_shaders.append(self)
def _recompile_pbr(self): gles = ConfigVariableString("load-display").getValue() if gles == "pandagles2": pbr_defines = { 'MAX_LIGHTS': self.max_lights, } if self.use_normal_maps: pbr_defines['USE_NORMAL_MAP'] = '' if self.use_emission_maps: pbr_defines['USE_EMISSION_MAP'] = '' if self.enable_shadows: pbr_defines['ENABLE_SHADOWS'] = '' if self.enable_fog: pbr_defines['ENABLE_FOG'] = '' if self.use_occlusion_maps: pbr_defines['USE_OCCLUSION_MAP'] = '' pbr_vert_str = _load_shader_str('simplepbr_gles.vert', pbr_defines) pbr_frag_str = _load_shader_str('simplepbr_gles.frag', pbr_defines) pbrshader = Shader.make( Shader.SL_GLSL, vertex=pbr_vert_str, fragment=pbr_frag_str, ) self.render_node.set_shader(pbrshader) else: super(OurPipeline, self)._recompile_pbr()
def _makeShader(shaderEffects, baseShader=None): """ Generates shader source from effects and converts it into a Panda3D shader object baseShader is source to inject effects into """ source = shadereffects.makeSource(shaderEffects, baseShader) key = source if key not in builtEffectsShaders: if useShaderFiles: # preflatten should not impact anything much, but lets us get the full names shaderEffects = [s.flatten() for s in shaderEffects] name = 'debug(' + ','.join([e.name for e in shaderEffects]) + ')' outLoc = 'ShadersOut/' + name + '.sha' print 'Making Shader: ' + outLoc builtEffectsShaders[key] = Shader.make(source) if useShaderFiles: fOut = open(outLoc, 'w') fOut.write(source) fOut.close() return builtEffectsShaders[key]
def load(self, *args): """ Loads a shader in the order: vertex, fragment, geometry, tesseval, tesscontrol """ newArgs = [] toHash = "" for arg in args: if len(arg) < 1: newArgs.append("") continue content = self._handleIncludes(arg) newArgs.append(content) toHash += content self._writeDebugShader("Shader-" + str(arg), content) self._clearIncludeStack() # Check if we already have the result cached hashed = hash(toHash) if hashed in self._ShaderCache: # Cache entry found return self._ShaderCache[hashed] shaderName = args[1].replace("Shader", "").split(".")[0].lstrip("/") result = Shader.make(Shader.SLGLSL, *newArgs) self._ShaderCache[hashed] = result return result
def _makeFontShader(self): self.fontShader = Shader.make(Shader.SLGLSL, """ #version 150 uniform mat4 p3d_ModelViewProjectionMatrix; in vec4 p3d_Vertex; in vec2 p3d_MultiTexCoord0; uniform float displData[100]; out vec2 texcoord; uniform vec2 pos; uniform vec2 size; void main() { int rawDispl = int(displData[gl_InstanceID]); ivec2 offsetDispl = ivec2( rawDispl % 16, rawDispl / 16); vec2 offsetCoordReal = vec2(offsetDispl.x / 16.0, (5.0 - offsetDispl.y) / 6.0); texcoord = p3d_MultiTexCoord0 / vec2(16,6) + offsetCoordReal; vec4 offset = vec4(gl_InstanceID*size.x*0.55 , 0, 0, 0) + vec4(pos.x, 0, pos.y, 0); vec4 finalPos = p3d_Vertex * vec4(size.x, size.x, size.x, 1.0) + offset; gl_Position = p3d_ModelViewProjectionMatrix * finalPos; } """, """ #version 150 #pragma file FastText.fragment in vec2 texcoord; uniform sampler2D font; uniform vec3 color; out vec4 result; void main() { float textFactor = texture(font, texcoord).x; result = vec4(color, textFactor); } """)
def _makeShader(shaderEffects, baseShader=None): """ Generates shader source from effects and converts it into a Panda3D shader object baseShader is source to inject effects into """ source=shadereffects.makeSource(shaderEffects, baseShader) key=source if key not in builtEffectsShaders: if useShaderFiles: # preflatten should not impact anything much, but lets us get the full names shaderEffects=[s.flatten() for s in shaderEffects] name='debug('+','.join([e.name for e in shaderEffects])+')' outLoc='ShadersOut/'+name+'.sha' print 'Making Shader: '+outLoc builtEffectsShaders[key]=Shader.make(source) if useShaderFiles: fOut=open(outLoc, 'w') fOut.write(source) fOut.close() return builtEffectsShaders[key]
def load(self, *args): """ Loads a shader in the order: vertex, fragment, geometry, tesseval, tesscontrol """ newArgs = [] toHash = "" for arg in args: if len(arg) < 1: newArgs.append("") continue content = self._handleIncludes(arg) newArgs.append(content) toHash += content self._writeDebugShader("Shader-" + str(arg), content) self._clearIncludeStack() # Check if we already have the result cached hashed = hash(toHash) if hashed in self._ShaderCache: # Cache entry found return self._ShaderCache[hashed] shaderName = args[1].replace("Shader", "").split(".")[0].lstrip("/") result = Shader.make(Shader.SLGLSL, *newArgs) self._ShaderCache[hashed] = result return result
def __init__(self): # The basics ShowBase.__init__(self) base.disableMouse() base.setBackgroundColor(0.1, 0.1, 0.1) base.setFrameRateMeter(True) self.accept("escape", sys.exit) # Camera self.camera_orbit = base.render.attach_new_node("Camera orbit") self.camera_pitch = self.camera_orbit.attach_new_node("Camera pitch") base.camera.reparent_to(self.camera_pitch) base.camera.set_pos(0, -5, 0) # Camera control self.camera_movement = (0, 0) self.accept("arrow_up", self.change_camera_movement, [ 0, -1]) self.accept("arrow_up-up", self.change_camera_movement, [ 0, 1]) self.accept("arrow_down", self.change_camera_movement, [ 0, 1]) self.accept("arrow_down-up", self.change_camera_movement, [ 0, -1]) self.accept("arrow_left", self.change_camera_movement, [-1, 0]) self.accept("arrow_left-up", self.change_camera_movement, [ 1, 0]) self.accept("arrow_right", self.change_camera_movement, [ 1, 0]) self.accept("arrow_right-up", self.change_camera_movement, [-1, 0]) base.taskMgr.add(self.move_camera, "Move camera") # Object self.model = loader.loadModel("models/smiley") self.model.reparent_to(base.render) shader = Shader.make( Shader.SL_GLSL, vertex = vertex_shader, fragment = fragment_shader, ) self.model.set_shader(shader)
def setup(self, bloom=True, hdr=True): self.cleanup() textures = {"sceneColorSampler": self.getSceneColorTexture()} if hdr: self.hdr = HDREffect(self) self.hdr.getHdrPass().setExposureOutput( base.shaderGenerator.getExposureAdjustment()) self.addEffect(self.hdr) if bloom: self.bloom = BloomEffect(self) self.addEffect(self.bloom) textures["bloomSampler"] = self.bloom.getFinalTexture() finalQuad = self.getScenePass().getQuad() vtext = "#version 330\n" vtext += "uniform mat4 p3d_ModelViewProjectionMatrix;\n" vtext += "in vec4 p3d_Vertex;\n" vtext += "in vec4 texcoord;\n" vtext += "out vec2 l_texcoord;\n" vtext += "void main()\n" vtext += "{\n" vtext += " gl_Position = p3d_ModelViewProjectionMatrix * p3d_Vertex;\n" vtext += " l_texcoord = texcoord.xy;\n" vtext += "}\n" ptext = "#version 330\n" ptext += "out vec4 outputColor;\n" ptext += "in vec2 l_texcoord;\n" for sampler in textures.keys(): ptext += "uniform sampler2D " + sampler + ";\n" if self.flashEnabled: ptext += "uniform vec3 flashColor[1];\n" ptext += "void main()\n" ptext += "{\n" ptext += " outputColor = texture(sceneColorSampler, l_texcoord);\n" if bloom: ptext += " outputColor.rgb += texture(bloomSampler, l_texcoord).rgb;\n" if self.flashEnabled: ptext += " outputColor.rgb += pow(flashColor[0], vec3(2.2));\n" ptext += "}\n" shader = Shader.make(Shader.SL_GLSL, vtext, ptext) if not shader: return finalQuad.setShader(shader) for sampler, texture in textures.items(): finalQuad.setShaderInput(sampler, texture) if self.flashEnabled: finalQuad.setShaderInput("flashColor", self.flashColor)
def refreshBuildings(self): nodePath = self.buildingsNodePath nodePath = render.find("scene.dae").find("Scene").find("Buildings") nodePath.setShader( Shader.make(Shader.SL_GLSL, open("shaders/buildings.vert").read(), open("shaders/buildings.frag").read()))
def setup_post_fx(self): self.filter_mgr = FilterManager(base.win, base.cam) col_tex = Texture() final_tex = Texture() final_quad = self.filter_mgr.renderSceneInto(colortex=col_tex) inter_quad = self.filter_mgr.renderQuadInto(colortex=final_tex) with open('yyagl/assets/shaders/filter.vert') as f: vert = f.read() with open('yyagl/assets/shaders/filter.frag') as f: frag = f.read() inter_quad.setShader(Shader.make(Shader.SLGLSL, vert, frag)) inter_quad.setShaderInput('in_tex', col_tex) with open('yyagl/assets/shaders/pass.frag') as f: frag = f.read() final_quad.setShader(Shader.make(Shader.SLGLSL, vert, frag)) final_quad.set_shader_input('gamma', self.gamma) final_quad.setShaderInput('in_tex', final_tex)
def __set_material(nodepath): # mat = Material() # mat.set_ambient((.35, .35, .35, .5)) # mat.set_diffuse((.35, .35, .35, .5)) # mat.set_specular((.35, .35, .35, .5)) # mat.set_shininess(12.5) # nodepath.set_material(mat) nodepath.node.set_shader(Shader.make(Shader.SL_GLSL, vert, frag)) nodepath.node.set_shader_input('alpha', .5)
def load(self, *args): # print "Loading shader",args[-1] newArgs = [] for arg in args: content = self._handleIncludes(arg) newArgs.append(content) self._writeDebugShader("Shader-" + str(arg), content) self._clearIncludeStack() result = Shader.make(Shader.SLGLSL, *newArgs) return result
def __init__(self, language=Panda3dShader.SL_GLSL, vertex=default_vertex_shader, fragment=default_fragment_shader, geometry='', **kwargs): self._shader = Panda3dShader.make(language, vertex, fragment, geometry) self.entity = None self.default_input = dict() for key, value in kwargs.items(): setattr(self, key, value)
def __init__(self, font): self.txt_node = TextNode('sdf_text_node') self.txt_node.set_font(font) self.geom = NodePath(self.txt_node.get_internal_geom()) self.__txt_color = Vec4(1.0, 1.0, 1.0, 1.0) self.__outline_color = Vec4(0.0, 0.0, 0.0, 0.0) self.__outline_offset = Vec2(0.0) self.__outline_power = 1.0 self.parent = None self.__pos = Vec3(0.0) self.__hpr = Vec3(0.0) self.__scale = Vec3(1.0) self.__shader = Shader.make(Shader.SL_GLSL, v_shader, f_shader) self._make_geom()
def setupTargetClip(self): """ The target is fed in to the typewriter but until we invent "geom curling", it shouldn't be visible under the typewriter under the desk. The @underDeskClip node has a world-relative bounding box, which we can convert to the target-relative bounding box, and pass to a shader that can clip the nodes. """ shader = Shader.make( Shader.SLGLSL, """ #version 120 attribute vec4 p3d_MultiTexCoord0; attribute vec4 p3d_MultiTexCoord1; void main() { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; gl_TexCoord[0] = p3d_MultiTexCoord0; gl_TexCoord[1] = p3d_MultiTexCoord1; } """, """ #version 120 uniform sampler2D baseTex; uniform sampler2D charTex; const vec4 zero = vec4(0, 0, 0, 0); const vec4 one = vec4(1, 1, 1, 1); const vec4 half = vec4(0.5, 0.5, 0.5, 0); void main() { vec4 baseColor = texture2D(baseTex, gl_TexCoord[0].st); vec4 typeColor = texture2D(charTex, gl_TexCoord[1].st); gl_FragColor = baseColor * typeColor; }""" ) self.target.setShader(shader) baseTex = self.targetRoot.getTexture() print "Base Texture:",baseTex self.target.setShaderInput("baseTex", baseTex) self.target.setShaderInput("charTex", self.tex)
def _makeFontShader(self): self.fontShader = Shader.make(Shader.SLGLSL, """ #version 150 uniform mat4 p3d_ModelViewProjectionMatrix; in vec4 p3d_Vertex; in vec2 p3d_MultiTexCoord0; uniform float displData[100]; out vec2 texcoord; uniform vec2 pos; uniform sampler2D font; uniform vec2 size; void main() { int rawDispl = int(displData[gl_InstanceID]); ivec2 offsetDispl = ivec2( rawDispl % 16, rawDispl / 16); vec2 offsetCoordReal = vec2(offsetDispl.x / 16.0, (5.0 - offsetDispl.y) / 6.0); vec2 halfOffset = 1.0 / textureSize(font, 0); texcoord = clamp(p3d_MultiTexCoord0, halfOffset, 1.0 - halfOffset) / vec2(16,6) + offsetCoordReal; vec4 offset = vec4(gl_InstanceID*size.x*0.56 , 0, 0, 0) + vec4(pos.x, 0, pos.y, 0); vec4 finalPos = p3d_Vertex * vec4(size.xxx, 1.0) + offset; gl_Position = p3d_ModelViewProjectionMatrix * finalPos; } """, """ #version 150 in vec2 texcoord; uniform sampler2D font; uniform vec3 color; out vec4 result; void main() { float textFactor = texture(font, texcoord).x; vec2 texsize = textureSize(font, 0); ivec2 icoord = ivec2(texcoord * texsize); ivec2 cidx = icoord / ivec2(16, 6); vec2 minCoord = (cidx * vec2(16, 6) + vec2(1.0)) / texsize; vec2 maxCoord = ((cidx + 1) * vec2(16, 6) - vec2(1.0)) / texsize; float outlineSize = 0.75; float textShadow = clamp( texture(font, clamp(texcoord + vec2(-outlineSize, -outlineSize) / texsize, minCoord, maxCoord)).x + texture(font, clamp(texcoord + vec2(outlineSize, -outlineSize) / texsize, minCoord, maxCoord)).x + texture(font, clamp(texcoord + vec2(-outlineSize, outlineSize) / texsize, minCoord, maxCoord)).x + texture(font, clamp(texcoord + vec2(outlineSize, outlineSize) / texsize, minCoord, maxCoord)).x, 0, 1) * 0.7; result = vec4( mix(vec3(0), color, textFactor), textFactor + textShadow); } """)
def load(self, *args): """ Loads a shader in the order: vertex, fragment, geometry, tesseval, tesscontrol """ newArgs = [] for arg in args: if len(arg) < 1: newArgs.append("") continue content = self._handleIncludes(arg) newArgs.append(content) self._writeDebugShader("Shader-" + str(arg), content) self._clearIncludeStack() result = Shader.make(Shader.SLGLSL, *newArgs) return result
def setupTargetClip(self): """ The target is fed in to the typewriter but until we invent "geom curling", it shouldn't be visible under the typewriter under the desk. The @underDeskClip node has a world-relative bounding box, which we can convert to the target-relative bounding box, and pass to a shader that can clip the nodes. """ shader = Shader.make( Shader.SLGLSL, """ #version 120 attribute vec4 p3d_MultiTexCoord0; attribute vec4 p3d_MultiTexCoord1; void main() { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; gl_TexCoord[0] = p3d_MultiTexCoord0; gl_TexCoord[1] = p3d_MultiTexCoord1; } """, """ #version 120 uniform sampler2D baseTex; uniform sampler2D charTex; const vec4 zero = vec4(0, 0, 0, 0); const vec4 one = vec4(1, 1, 1, 1); const vec4 half = vec4(0.5, 0.5, 0.5, 0); void main() { vec4 baseColor = texture2D(baseTex, gl_TexCoord[0].st); vec4 typeColor = texture2D(charTex, gl_TexCoord[1].st); gl_FragColor = baseColor * typeColor; }""") self.target.setShader(shader) baseTex = self.targetRoot.getTexture() print "Base Texture:", baseTex self.target.setShaderInput("baseTex", baseTex) self.target.setShaderInput("charTex", self.tex)
def __setShader(self): shader = Shader.make(Shader.SL_GLSL, vert, frag) # Apply the shader to the node, but set a special flag indicating that # the point size is controlled bythe shader. attrib = ShaderAttrib.make(shader) attrib = attrib.setFlag(ShaderAttrib.F_shader_point_size, True) self.__nodepath.setAttrib(attrib) self.__nodepath.setShaderInputs( positions=self.__texPos, emitter_pos=self.__emitter.getPos(render), start_vel=self.__texStartVel, velocities=self.__texCurrVel, accel=(0, 0, self.__gravity), start_time=base.clock.getFrameTime(), emission_times=self.__texTimes, part_duration=self.__partDuration, image=loader.loadTexture(self.__texture))
def __init__(self): ShowBase.__init__(self) # 初期化 vs = open('vs_water.glsl', "r", encoding="UTF-8").read() fs = open('fs_water.glsl', "r", encoding="UTF-8").read() props = WindowProperties() props.setTitle('Fragment') Win_size = (1920, 1080) # ウィンドウサイズ props.setSize(Win_size) # ウィンドウのサイズを設定 props.setCursorHidden(True) # マウスカーソルを隠す base.win.requestProperties(props) self.AspectRaito = float(Win_size[0] / Win_size[1]) # アスペクト比を計算 self.accept("escape", sys.exit) # 終了 self.disableMouse() # マウスカーソルを非表示 self.setFrameRateMeter(True) # FPSを表示 camera.setPos(0, -2, 0) # カメラを配置 format = GeomVertexFormat.getV3() mesh = GeomVertexData('', format, Geom.UHDynamic) # メッシュを用意 vertex = GeomVertexWriter(mesh, 'vertex') # 頂点オブジェクト vertex.addData3(-1, 0, -1) # 頂点を追加 vertex.addData3(-1, 0, 1) vertex.addData3(1, 0, 1) vertex.addData3(1, 0, -1) tri = GeomTriangles(Geom.UHDynamic) # 面オブジェクト tri.addVertices(0, 2, 1) # 面のインデックスを追加 tri.addVertices(0, 3, 2) node = GeomNode('') geo = Geom(mesh) # ジオメトリオブジェクト geo.addPrimitive(tri) # プリミティブにする node.addGeom(geo) self.panel = render.attachNewNode(node) # レンダーオブジェクトとして追加 shader = Shader.make(Shader.SL_GLSL, vs, fs) self.panel.setShader(shader) resolution = self.win.getXSize(), self.win.getYSize() self.panel.setShaderInput('resolution', resolution) taskMgr.add(self.Loop, 'update') # メインループ
def get_ham_shader(self): """ Return a trivial shader that will directly place the mesh in the clip space """ if self.ham_shader is None: self.ham_shader = Shader.make(Shader.SL_GLSL, vertex="""#version 330 in vec4 p3d_Vertex; void main() { gl_Position = p3d_Vertex; } """, fragment="""#version 330 out vec4 frag_color; void main() { frag_color = vec4(0, 0, 0, 1); } """) return self.ham_shader
def set_ranking(self): items = game.logic.season.logic.ranking.logic.ranking.items() sorted_ranking = reversed(sorted(items, key=lambda el: el[1])) txt = OnscreenText(text=_('Ranking'), scale=.1, pos=(0, .3), font=self.font, fg=self.text_bg) self.widgets += [txt] for i, car in enumerate(sorted_ranking): idx, name, _car, skills = next(driver for driver in self.props.drivers if driver[3] == car[0]) is_car = car[0] == self.props.car_path txt = OnscreenText(text=str(car[1]) + ' ' + name, align=TextNode.A_left, scale=.072, pos=(-.2, .1 - i * .16), font=self.font, fg=self.text_fg if is_car else self.text_bg) self.widgets += [txt] img = OnscreenImage(self.props.cars_path % car[0], pos=(-.36, 1, .12 - i * .16), scale=.074) self.widgets += [img] with open(eng.curr_path + 'yyagl/assets/shaders/filter.vert') as ffilter: vert = ffilter.read() with open(eng.curr_path + 'yyagl/assets/shaders/drv_car.frag') as ffilter: frag = ffilter.read() shader = Shader.make(Shader.SL_GLSL, vert, frag) img.setShader(shader) img.setTransparency(True) ts = TextureStage('ts') ts.setMode(TextureStage.MDecal) txt_path = self.props.drivers_path % idx img.setTexture(ts, loader.loadTexture(txt_path))
def _makeFontShader(self): self.fontShader = Shader.make( Shader.SLGLSL, """ #version 150 uniform mat4 p3d_ModelViewProjectionMatrix; in vec4 p3d_Vertex; in vec2 p3d_MultiTexCoord0; uniform float displData[100]; out vec2 texcoord; uniform vec2 pos; uniform vec2 size; void main() { int rawDispl = int(displData[gl_InstanceID]); ivec2 offsetDispl = ivec2( rawDispl % 16, rawDispl / 16); vec2 offsetCoordReal = vec2(offsetDispl.x / 16.0, (5.0 - offsetDispl.y) / 6.0); texcoord = p3d_MultiTexCoord0 / vec2(16,6) + offsetCoordReal; vec4 offset = vec4(gl_InstanceID*size.x*0.55 , 0, 0, 0) + vec4(pos.x, 0, pos.y, 0); vec4 finalPos = p3d_Vertex * vec4(size.x, size.x, size.x, 1.0) + offset; gl_Position = p3d_ModelViewProjectionMatrix * finalPos; } """, """ #version 150 #pragma file FastText.fragment in vec2 texcoord; uniform sampler2D font; uniform vec3 color; out vec4 result; void main() { float textFactor = texture(font, texcoord).x; result = vec4(color, textFactor); } """)
def set_grid(self): txt = OnscreenText(text=_('Starting grid'), scale=.1, pos=(-1.0, .3), font=self.font, fg=self.text_bg) self.widgets += [txt] for i, car in enumerate(self.props.grid): idx, name, _car, skills = next(driver for driver in self.props.drivers if driver[3] == car) is_car = car == self.props.car_path txt = OnscreenText(text=str(i + 1) + '. ' + name, align=TextNode.A_left, scale=.072, pos=(-1.28, .1 - i * .16), font=self.font, fg=self.text_fg if is_car else self.text_bg) self.widgets += [txt] img = OnscreenImage(self.props.cars_path % car, pos=(-1.42, 1, .12 - i * .16), scale=.074) self.widgets += [img] with open(eng.curr_path + 'yyagl/assets/shaders/filter.vert') as ffilter: vert = ffilter.read() with open(eng.curr_path + 'yyagl/assets/shaders/drv_car.frag') as f: frag = f.read() shader = Shader.make(Shader.SL_GLSL, vert, frag) img.setShader(shader) img.setTransparency(True) ts = TextureStage('ts') ts.setMode(TextureStage.MDecal) txt_path = self.props.drivers_path % idx img.setTexture(ts, loader.loadTexture(txt_path))
def compile(self): self._shader = Panda3dShader.make(self.language, self.vertex, self.fragment, self.geometry) self.compiled = True
def loadShader(self): """Textures based on altitude and slope.""" logging.info( "loading textures...") ### texture scaling texScale = self.terrain.tileSize / 32 * self.terrain.horizontalScale self.texScale = Vec4(texScale, texScale, texScale, 1.0) ### Load textures self.normalMap = self.loadTexture("Detail_NRM.png") self.displacementMap = self.loadTexture("Detail_DISP.png") self.testOn = False self.detailTex = self.loadTexture("Detail_COLOR.png") self.detailTex2 = self.loadTexture("Detail_COLOR2.png") self.tex1 = self.loadTexture("dirt.jpg") self.tex2 = self.loadTexture("grass.jpg") self.tex3 = self.loadTexture("rock.jpg") self.tex4 = self.loadTexture("snow.jpg") self.normalTS = TextureStage('normalMap') #self.normalTS2 = TextureStage('normalMap') self.detailTS = TextureStage('detailMap') self.ts1 = TextureStage('textures') ### Load the boundries for each texture # regionLimits ( min height, max height, min slope, max slope ) self.textureMapper = TextureMapper(self.terrain) self.textureMapper.addTexture(self.tex1) self.textureMapper.addRegionToTex(Vec4(-9999.0, self.indexToHeight(0.1), -001, 1.001)) self.textureMapper.addTexture(self.tex2) self.textureMapper.addRegionToTex(Vec4(self.indexToHeight(-0.15), self.indexToHeight(0.75), -0.001, 0.30)) self.textureMapper.addTexture(self.tex3) self.textureMapper.addRegionToTex(Vec4(self.indexToHeight(0.1), self.indexToHeight(0.95), 0.10, 1.001)) #second region forces tex 2 and 4 to blend a bit at their boundries regardless of slope self.textureMapper.addRegionToTex(Vec4(self.indexToHeight(0.4), self.indexToHeight(0.9), -0.001, 1.001)) self.textureMapper.addTexture(self.tex4) self.textureMapper.addRegionToTex(Vec4(self.indexToHeight(0.72), 9999.0, -0.001, 1.001)) logging.info( "initializing terrain shader generator...") file = 'shaders/terrain.sha' if SAVED_TEXTURE_MAPS: self.shaderGenerator = BakedTerrainShaderGenerator(self.terrain, self, self.textureMapper) file = 'shaders/bakedTerrain.sha' else: self.shaderGenerator = FullTerrainShaderGenerator(self.terrain, self, self.textureMapper) file = 'shaders/fullTerrain.sha' logging.info( "terrain shader generator initialized...") if RUNTYPE == 'python': self.shaderGenerator.saveShader(file) self.shader = Shader.load(file, Shader.SLCg) else: self.shader = Shader.make(self.shaderGenerator.createShader(), Shader.SLCg); self.terrain.setShaderInput("normalMap", self.normalMap) self.terrain.setShaderInput("displacementMap", self.displacementMap) self.terrain.setShaderInput("detailTex", self.detailTex) self.terrain.setShaderInput('tscale', self.texScale) self.terrain.setShaderInput("fogColor", Vec4(1.0, 1.0, 1.0, 1.0)) self.terrain.setShaderInput("camPos", base.camera.getPos())
def reconfigure(self, fullrebuild, changed): """ Reconfigure is called whenever any configuration change is made. """ configuration = self.configuration if (fullrebuild): self.cleanup() if (len(configuration) == 0): return auxbits = 0 needtex = set(["color"]) needtexcoord = set(["color"]) if ("CartoonInk" in configuration): needtex.add("aux") auxbits |= AuxBitplaneAttrib.ABOAuxNormal needtexcoord.add("aux") if ("AmbientOcclusion" in configuration): needtex.add("depth") needtex.add("ssao0") needtex.add("ssao1") needtex.add("ssao2") needtex.add("aux") auxbits |= AuxBitplaneAttrib.ABOAuxNormal needtexcoord.add("ssao2") if ("BlurSharpen" in configuration): needtex.add("blur0") needtex.add("blur1") needtexcoord.add("blur1") if ("Bloom" in configuration): needtex.add("bloom0") needtex.add("bloom1") needtex.add("bloom2") needtex.add("bloom3") auxbits |= AuxBitplaneAttrib.ABOGlow needtexcoord.add("bloom3") if ("ViewGlow" in configuration): auxbits |= AuxBitplaneAttrib.ABOGlow if ("VolumetricLighting" in configuration): needtex.add(configuration["VolumetricLighting"].source) for tex in needtex: self.textures[tex] = Texture("scene-" + tex) self.textures[tex].setWrapU(Texture.WMClamp) self.textures[tex].setWrapV(Texture.WMClamp) self.finalQuad = self.manager.renderSceneInto(textures = self.textures, auxbits=auxbits) if (self.finalQuad == None): self.cleanup() return False if ("BlurSharpen" in configuration): blur0=self.textures["blur0"] blur1=self.textures["blur1"] self.blur.append(self.manager.renderQuadInto(colortex=blur0,div=2)) self.blur.append(self.manager.renderQuadInto(colortex=blur1)) self.blur[0].setShaderInput("src", self.textures["color"]) self.blur[0].setShader(self.loadShader("filter-blurx.sha")) self.blur[1].setShaderInput("src", blur0) self.blur[1].setShader(self.loadShader("filter-blury.sha")) if ("AmbientOcclusion" in configuration): ssao0=self.textures["ssao0"] ssao1=self.textures["ssao1"] ssao2=self.textures["ssao2"] self.ssao.append(self.manager.renderQuadInto(colortex=ssao0)) self.ssao.append(self.manager.renderQuadInto(colortex=ssao1,div=2)) self.ssao.append(self.manager.renderQuadInto(colortex=ssao2)) self.ssao[0].setShaderInput("depth", self.textures["depth"]) self.ssao[0].setShaderInput("normal", self.textures["aux"]) self.ssao[0].setShaderInput("random", loader.loadTexture("maps/random.rgb")) self.ssao[0].setShader(Shader.make(SSAO_BODY % configuration["AmbientOcclusion"].numsamples, Shader.SL_Cg)) self.ssao[1].setShaderInput("src", ssao0) self.ssao[1].setShader(self.loadShader("filter-blurx.sha")) self.ssao[2].setShaderInput("src", ssao1) self.ssao[2].setShader(self.loadShader("filter-blury.sha")) if ("Bloom" in configuration): bloomconf = configuration["Bloom"] bloom0=self.textures["bloom0"] bloom1=self.textures["bloom1"] bloom2=self.textures["bloom2"] bloom3=self.textures["bloom3"] if (bloomconf.size == "large"): scale=8 downsampler="filter-down4.sha" elif (bloomconf.size == "medium"): scale=4 downsampler="filter-copy.sha" else: scale=2 downsampler="filter-copy.sha" self.bloom.append(self.manager.renderQuadInto(colortex=bloom0, div=2, align=scale)) self.bloom.append(self.manager.renderQuadInto(colortex=bloom1, div=scale, align=scale)) self.bloom.append(self.manager.renderQuadInto(colortex=bloom2, div=scale, align=scale)) self.bloom.append(self.manager.renderQuadInto(colortex=bloom3, div=scale, align=scale)) self.bloom[0].setShaderInput("src", self.textures["color"]) self.bloom[0].setShader(self.loadShader("filter-bloomi.sha")) self.bloom[1].setShaderInput("src", bloom0) self.bloom[1].setShader(self.loadShader(downsampler)) self.bloom[2].setShaderInput("src", bloom1) self.bloom[2].setShader(self.loadShader("filter-bloomx.sha")) self.bloom[3].setShaderInput("src", bloom2) self.bloom[3].setShader(self.loadShader("filter-bloomy.sha")) texcoords = {} texcoordPadding = {} for tex in needtexcoord: if self.textures[tex].getAutoTextureScale() != ATSNone or \ "HalfPixelShift" in configuration: texcoords[tex] = "l_texcoord_" + tex texcoordPadding["l_texcoord_" + tex] = tex else: # Share unpadded texture coordinates. texcoords[tex] = "l_texcoord" texcoordPadding["l_texcoord"] = None texcoordSets = list(enumerate(texcoordPadding.keys())) text = "//Cg\n" text += "void vshader(float4 vtx_position : POSITION,\n" text += " out float4 l_position : POSITION,\n" for texcoord, padTex in texcoordPadding.items(): if padTex is not None: text += " uniform float4 texpad_tx%s,\n" % (padTex) if ("HalfPixelShift" in configuration): text += " uniform float4 texpix_tx%s,\n" % (padTex) for i, name in texcoordSets: text += " out float2 %s : TEXCOORD%d,\n" % (name, i) text += " uniform float4x4 mat_modelproj)\n" text += "{\n" text += " l_position = mul(mat_modelproj, vtx_position);\n" for texcoord, padTex in texcoordPadding.items(): if padTex is None: text += " %s = vtx_position.xz * float2(0.5, 0.5) + float2(0.5, 0.5);\n" % (texcoord) else: text += " %s = (vtx_position.xz * texpad_tx%s.xy) + texpad_tx%s.xy;\n" % (texcoord, padTex, padTex) if ("HalfPixelShift" in configuration): text += " %s += texpix_tx%s.xy * 0.5;\n" % (texcoord, padTex) text += "}\n" text += "void fshader(\n" for i, name in texcoordSets: text += " float2 %s : TEXCOORD%d,\n" % (name, i) for key in self.textures: text += " uniform sampler2D k_tx" + key + ",\n" if ("CartoonInk" in configuration): text += " uniform float4 k_cartoonseparation,\n" text += " uniform float4 k_cartooncolor,\n" text += " uniform float4 texpix_txaux,\n" if ("BlurSharpen" in configuration): text += " uniform float4 k_blurval,\n" if ("VolumetricLighting" in configuration): text += " uniform float4 k_casterpos,\n" text += " uniform float4 k_vlparams,\n" text += " out float4 o_color : COLOR)\n" text += "{\n" text += " o_color = tex2D(k_txcolor, %s);\n" % (texcoords["color"]) if ("CartoonInk" in configuration): text += CARTOON_BODY % {"texcoord" : texcoords["aux"]} if ("AmbientOcclusion" in configuration): text += " o_color *= tex2D(k_txssao2, %s).r;\n" % (texcoords["ssao2"]) if ("BlurSharpen" in configuration): text += " o_color = lerp(tex2D(k_txblur1, %s), o_color, k_blurval.x);\n" % (texcoords["blur1"]) if ("Bloom" in configuration): text += " o_color = saturate(o_color);\n"; text += " float4 bloom = 0.5 * tex2D(k_txbloom3, %s);\n" % (texcoords["bloom3"]) text += " o_color = 1-((1-bloom)*(1-o_color));\n" if ("ViewGlow" in configuration): text += " o_color.r = o_color.a;\n" if ("VolumetricLighting" in configuration): text += " float decay = 1.0f;\n" text += " float2 curcoord = %s;\n" % (texcoords["color"]) text += " float2 lightdir = curcoord - k_casterpos.xy;\n" text += " lightdir *= k_vlparams.x;\n" text += " half4 sample = tex2D(k_txcolor, curcoord);\n" text += " float3 vlcolor = sample.rgb * sample.a;\n" text += " for (int i = 0; i < %s; i++) {\n" % (int(configuration["VolumetricLighting"].numsamples)) text += " curcoord -= lightdir;\n" text += " sample = tex2D(k_tx%s, curcoord);\n" % (configuration["VolumetricLighting"].source) text += " sample *= sample.a * decay;//*weight\n" text += " vlcolor += sample.rgb;\n" text += " decay *= k_vlparams.y;\n" text += " }\n" text += " o_color += float4(vlcolor * k_vlparams.z, 1);\n" if ("GammaAdjust" in configuration): gamma = configuration["GammaAdjust"] if gamma == 0.5: text += " o_color.rgb = sqrt(o_color.rgb);\n" elif gamma == 2.0: text += " o_color.rgb *= o_color.rgb;\n" elif gamma != 1.0: text += " o_color.rgb = pow(o_color.rgb, %ff);\n" % (gamma) if ("Inverted" in configuration): text += " o_color = float4(1, 1, 1, 1) - o_color;\n" text += "}\n" self.finalQuad.setShader(Shader.make(text, Shader.SL_Cg)) for tex in self.textures: self.finalQuad.setShaderInput("tx"+tex, self.textures[tex]) self.task = taskMgr.add(self.update, "common-filters-update") if (changed == "CartoonInk") or fullrebuild: if ("CartoonInk" in configuration): c = configuration["CartoonInk"] self.finalQuad.setShaderInput("cartoonseparation", LVecBase4(c.separation, 0, c.separation, 0)) self.finalQuad.setShaderInput("cartooncolor", c.color) if (changed == "BlurSharpen") or fullrebuild: if ("BlurSharpen" in configuration): blurval = configuration["BlurSharpen"] self.finalQuad.setShaderInput("blurval", LVecBase4(blurval, blurval, blurval, blurval)) if (changed == "Bloom") or fullrebuild: if ("Bloom" in configuration): bloomconf = configuration["Bloom"] intensity = bloomconf.intensity * 3.0 self.bloom[0].setShaderInput("blend", bloomconf.blendx, bloomconf.blendy, bloomconf.blendz, bloomconf.blendw * 2.0) self.bloom[0].setShaderInput("trigger", bloomconf.mintrigger, 1.0/(bloomconf.maxtrigger-bloomconf.mintrigger), 0.0, 0.0) self.bloom[0].setShaderInput("desat", bloomconf.desat) self.bloom[3].setShaderInput("intensity", intensity, intensity, intensity, intensity) if (changed == "VolumetricLighting") or fullrebuild: if ("VolumetricLighting" in configuration): config = configuration["VolumetricLighting"] tcparam = config.density / float(config.numsamples) self.finalQuad.setShaderInput("vlparams", tcparam, config.decay, config.exposure, 0.0) if (changed == "AmbientOcclusion") or fullrebuild: if ("AmbientOcclusion" in configuration): config = configuration["AmbientOcclusion"] self.ssao[0].setShaderInput("params1", config.numsamples, -float(config.amount) / config.numsamples, config.radius, 0) self.ssao[0].setShaderInput("params2", config.strength, config.falloff, 0, 0) self.update() return True
def __init__(self, name, folder): file=folder+'maps/'+name+'.txt' d=parseFile(file) #inputMapNames, tex2DNames, shaderSource if "Input" in d: self.inputMapNames=d["Input"] else: self.inputMapNames=[] if "Tex2D" in d: tex2D=d["Tex2D"] else: tex2D=[] shaderSource='\n'.join(d["Shader"]) self.name=name texLines=[] paramsStrs=[] paramsDefStrs=[] for i in xrange(len(self.inputMapNames)): texLines.append(' in uniform sampler2D tex_'+str(i)+': TEXUNIT'+str(i)+',') paramsStrs.append('tex2D(tex_'+str(i)+', l_tex)') paramsDefStrs.append('float4 map_'+self.inputMapNames[i]) self.shaderTex=[] for t in tex2D: i=len(texLines) texName='tex_'+str(i) texLines.append(' in uniform sampler2D '+texName+': TEXUNIT'+str(i)+',') paramsStrs.append('tex_'+str(i)) paramsDefStrs.append('sampler2D tex2D_'+t) tex=loadTex(folder+'textures/'+t) texStage=TextureStage(t+"stage") self.shaderTex.append((texStage,tex)) texText='\n'.join(texLines) paramsText=', '.join(paramsStrs) paramsDef=', '.join(paramsDefStrs) if len(paramsDef)>0: paramsDef=", "+paramsDef paramsText=", "+paramsText self.source=mapMakerShaderSource.replace('#tex#',texText) self.source=self.source.replace('#params#',paramsText) self.source=self.source.replace('#source#',shaderSource) self.source=self.source.replace('#paramsDef#',paramsDef) if useShaderFiles: outLoc='ShadersOut/'+name+'.sha' fOut=open(outLoc, 'w') fOut.write(self.source) fOut.close() self.shader=loader.loadShader(outLoc) else: self.shader=Shader.make(self.source) self.resolutionScale=1 self.addPixels=0 if "Settings" in d: for s in d["Settings"]: t=s.split() v=t[1] m=t[0] if m=='resolutionScale': self.resolutionScale=float(v) elif m=='addPixels': self.addPixels=int(v)
def show(self, race_ranking, lap_times, drivers, player_car_name): track = self.props.track_path self.result_frm = DirectFrame(frameColor=(.8, .8, .8, .64), frameSize=(-2, 2, -1, 1)) laps = len(lap_times) text_bg = self.props.menu_args.text_bg pars = {'scale': .1, 'fg': text_bg, 'font': self.props.menu_args.font} # ref into race self.__res_txts = [ OnscreenText(str(round(lap_times[i], 2)), pos=(0, .47 - .2 * (i + 1)), **pars) for i in range(laps) ] self.__res_txts += [OnscreenText(_('LAP'), pos=(-.6, .6), **pars)] self.__res_txts += [OnscreenText(_('TIME'), pos=(0, .6), **pars)] self.__res_txts += [ OnscreenText(_('RANKING'), pos=(.5, .6), align=TextNode.A_left, **pars) ] self.__res_txts += [ OnscreenText(str(i), pos=(-.6, .47 - .2 * i), **pars) for i in range(1, 4) ] race_ranking_sorted = sorted(race_ranking.items(), key=lambda x: x[1]) race_ranking_sorted = reversed([el[0] for el in race_ranking_sorted]) for i, car in enumerate(race_ranking_sorted): carinfo = next(drv for drv in drivers if drv[3] == car) idx, name, skills, _car = carinfo is_car = car == player_car_name fgc = self.props.menu_args.text_fg if is_car else text_bg txt = OnscreenText(text=str(i + 1) + '. ' + name, align=TextNode.A_left, scale=.072, pos=(.68, .44 - .16 * (i + 1)), font=self.props.menu_args.font, fg=fgc) img = OnscreenImage(self.props.cars_imgs % car, pos=(.58, 1, .47 - (i + 1) * .16), scale=.074) with open(eng.curr_path + 'yyagl/assets/shaders/filter.vert') as f: vert = f.read() with open(eng.curr_path + 'yyagl/assets/shaders/drv_car.frag') as f: frag = f.read() shader = Shader.make(Shader.SL_GLSL, vert, frag) img.setShader(shader) img.setTransparency(True) ts = TextureStage('ts') ts.setMode(TextureStage.MDecal) txt_path = self.props.drivers_imgs % idx img.setTexture(ts, loader.loadTexture(txt_path)) self.__res_txts += [txt, img] self.__res_txts += [ OnscreenText(_('share:'), pos=(-.1, -.82), align=TextNode.A_right, **pars) ] self.__buttons = [] curr_time = min(game.player_car.logic.lap_times or [0]) facebook_url = self.props.share_urls[0] #TODO: find a way to share the time on Facebook twitter_url = self.props.share_urls[1] twitter_url = twitter_url.format(time=round(curr_time, 2), track=track) plus_url = self.props.share_urls[2] #TODO: find a way to share the time on Google Plus tumblr_url = self.props.share_urls[3] #TODO: find a way to share the time on Tumblr sites = [('facebook', facebook_url), ('twitter', twitter_url), ('google_plus', plus_url), ('tumblr', tumblr_url)] self.__buttons += [ ImageButton(scale=.078, pos=(.02 + i * .18, 1, -.79), frameColor=(0, 0, 0, 0), image=self.props.share_imgs % site[0], command=eng.open_browser, extraArgs=[site[1]], rolloverSound=self.props.menu_args.rollover, clickSound=self.props.menu_args.click) for i, site in enumerate(sites) ] def step(): self.notify('on_race_step', race_ranking) self.destroy() Subject.destroy(self) cont_btn = DirectButton(text=_('Continue'), pos=(0, 1, -.6), command=step, **self.props.menu_args.btn_args) self.__buttons += [cont_btn]
def loadShader(self): """Textures based on altitude and slope.""" logging.info("loading textures...") ### texture scaling texScale = self.terrain.tileSize / 32 * self.terrain.horizontalScale self.texScale = Vec4(texScale, texScale, texScale, 1.0) ### Load textures self.normalMap = self.loadTexture("Detail_NRM.png") self.displacementMap = self.loadTexture("Detail_DISP.png") self.testOn = False self.detailTex = self.loadTexture("Detail_COLOR.png") self.detailTex2 = self.loadTexture("Detail_COLOR2.png") self.tex1 = self.loadTexture("dirt.jpg") self.tex2 = self.loadTexture("grass.jpg") self.tex3 = self.loadTexture("rock.jpg") self.tex4 = self.loadTexture("snow.jpg") self.normalTS = TextureStage('normalMap') #self.normalTS2 = TextureStage('normalMap2') self.detailTS = TextureStage('detailMap') self.ts1 = TextureStage('textures') ### Load the boundries for each texture # regionLimits ( min height, max height, min slope, max slope ) self.textureMapper = TextureMapper(self.terrain) self.textureMapper.addTexture(self.tex1) self.textureMapper.addRegionToTex( Vec4(-9999.0, self.indexToHeight(0.1), -0.001, 1.001)) self.textureMapper.addTexture(self.tex2) self.textureMapper.addRegionToTex( Vec4(self.indexToHeight(-0.15), self.indexToHeight(0.75), -0.001, 0.30)) self.textureMapper.addTexture(self.tex3) self.textureMapper.addRegionToTex( Vec4(self.indexToHeight(0.1), self.indexToHeight(0.95), 0.10, 1.001)) #second region forces tex 2 and 4 to blend a bit at their boundries regardless of slope self.textureMapper.addRegionToTex( Vec4(self.indexToHeight(0.4), self.indexToHeight(0.9), -0.001, 1.001)) self.textureMapper.addTexture(self.tex4) self.textureMapper.addRegionToTex( Vec4(self.indexToHeight(0.72), 9999.0, -0.001, 1.001)) logging.info("intializing terrain shader generator...") file = 'shaders/terrain.sha' if SAVED_TEXTURE_MAPS: self.shaderGenerator = BakedTerrainShaderGenerator( self.terrain, self, self.textureMapper) file = 'shaders/bakedTerrain.sha' else: self.shaderGenerator = FullTerrainShaderGenerator( self.terrain, self, self.textureMapper) file = 'shaders/fullTerrain.sha' logging.info("terrain shader generator initialized...") if RUNTYPE == 'python': self.shaderGenerator.saveShader(file) self.shader = Shader.load(file, Shader.SLCg) else: self.shader = Shader.make(self.shaderGenerator.createShader(), Shader.SLCg) self.terrain.setShaderInput("normalMap", self.normalMap) self.terrain.setShaderInput("displacementMap", self.displacementMap) self.terrain.setShaderInput("detailTex", self.detailTex) self.terrain.setShaderInput('tscale', self.texScale) self.terrain.setShaderInput("fogColor", Vec4(1.0, 1.0, 1.0, 1.0)) self.terrain.setShaderInput("camPos", base.camera.getPos())
class Shaders: region_sel_mask = Shader.make(Shader.SL_GLSL, region_sel.VERT_SHADER_MASK, region_sel.FRAG_SHADER_MASK) grid = Shader.make(Shader.SL_GLSL, grid.VERT_SHADER, grid.FRAG_SHADER, grid.GEOM_SHADER) normal = Shader.make(Shader.SL_GLSL, normal.VERT_SHADER, normal.FRAG_SHADER, normal.GEOM_SHADER) surface_normal = Shader.make(Shader.SL_GLSL, surface_normal.VERT_SHADER, surface_normal.FRAG_SHADER) extrusion_inset = Shader.make(Shader.SL_GLSL, extrusion_inset.VERT_SHADER, extrusion_inset.FRAG_SHADER, extrusion_inset.GEOM_SHADER) solidify = Shader.make(Shader.SL_GLSL, solidify.VERT_SHADER, solidify.FRAG_SHADER, solidify.GEOM_SHADER) cone_shaded = Shader.make(Shader.SL_GLSL, cone.VERT_SHADER, prim.FRAG_SHADER, cone.GEOM_SHADER) cone_wire = Shader.make(Shader.SL_GLSL, cone.VERT_SHADER_WIRE, locked_wireframe.FRAG_SHADER, locked_wireframe.GEOM_SHADER) torus_shaded = Shader.make(Shader.SL_GLSL, torus.VERT_SHADER, prim.FRAG_SHADER) torus_wire = Shader.make(Shader.SL_GLSL, torus.VERT_SHADER_WIRE, locked_wireframe.FRAG_SHADER, locked_wireframe.GEOM_SHADER) locked_wireframe = Shader.make(Shader.SL_GLSL, locked_wireframe.VERT_SHADER, locked_wireframe.FRAG_SHADER, locked_wireframe.GEOM_SHADER) snap = { "vert": Shader.make(Shader.SL_GLSL, snap.VERT_SHADER_V, snap.FRAG_SHADER, snap.GEOM_SHADER_V), "edge": Shader.make(Shader.SL_GLSL, snap.VERT_SHADER_E, snap.FRAG_SHADER, snap.GEOM_SHADER_E), "poly": Shader.make(Shader.SL_GLSL, snap.VERT_SHADER_P, snap.FRAG_SHADER, snap.GEOM_SHADER_P) }
camera_vertical_blur_shader = Shader.make( ''' void vshader(float4 vtx_position : POSITION, // float2 vtx_texcoord0 : TEXCOORD0, out float4 l_position : POSITION, out float2 l_texcoord0 : TEXCOORD0, uniform float4 texpad_tex, uniform float4x4 mat_modelproj) { l_position=mul(mat_modelproj, vtx_position); l_texcoord0 = vtx_position.xz * texpad_tex.xy + texpad_tex.xy; } void fshader(float2 l_texcoord0 : TEXCOORD0, out float4 o_color : COLOR, uniform sampler2D k_tex : TEXUNIT0, uniform float k_blur_size) { float4 c = tex2D(k_tex, l_texcoord0); float4 col = 0; for(float index=0;index<10;index++){ //add color at position to color float2 uv = l_texcoord0 + float2(0, (index/9 - 0.5) * k_blur_size); col += tex2D(k_tex, uv); } col = col / 10; // o_color = col; float r = 1-((1-col[0])*(1-c[0])); float g = 1-((1-col[1])*(1-c[1])); float b = 1-((1-col[2])*(1-c[2])); // 1-((1-c)*(1-col)) o_color = float4(r,g,b,1); // basic black and white effet // float moyenne = (c.x + c.y + c.z)/3; // o_color = float4(moyenne, moyenne, moyenne, 1); } ''', Shader.SL_Cg)
empty_shader = Shader.make(Shader.SL_GLSL, vertex=''' #version 130 uniform mat4 p3d_ModelViewProjectionMatrix; uniform mat4 transform_matrix; in vec4 p3d_Vertex; in vec2 p3d_MultiTexCoord0; in vec3 p3d_Normal; out vec2 texcoord; out vec3 world_space_normal; void main() { gl_Position = p3d_ModelViewProjectionMatrix * p3d_Vertex; texcoord = p3d_MultiTexCoord0; vec4 invcamx = transform_matrix[0]; vec4 invcamy = transform_matrix[1]; vec4 invcamz = transform_matrix[2]; vec4 invcamw = transform_matrix[3]; mat3 invcam = mat3( invcamx[0], invcamx[1], invcamx[2], invcamy[0], invcamy[1], invcamy[2], invcamz[0], invcamz[1], invcamz[2] ); world_space_normal = normalize(p3d_Normal * invcam); } ''', fragment=''' #version 130 uniform sampler2D p3d_Texture0; uniform vec4 p3d_ColorScale; in vec2 texcoord; in vec3 world_space_normal; out vec4 fragColor; void main() { vec4 norm = vec4(world_space_normal*0.5+0.5, 1); float grey = 0.21 * norm.r + 0.71 * norm.g + 0.07 * norm.b; norm = vec4(grey, grey, grey, 1); vec4 color = texture(p3d_Texture0, texcoord) * p3d_ColorScale * norm; // float m = (color.r + color.g + color.b) / 3; // color = vec4(grey, grey, grey, 1); fragColor = color.rgba; } ''', geometry='')
def getShader(self, renderState, debugFile=None, noChache=False): """ returns a shader appropriate for the passed RenderState will generate or fetch from cache as needed noChache forces the generation of the shader (but it will still get cached). Useful for use with debugFile if you need to see the source, but it may be cached """ shader = self.cache.get(renderState) if shader and not noChache: print "Shader is cached. Skipping generating shader to: " + debugFile return shader # process from top down to see what part of graph is active, and produce active graph # nodes are only processed when all nodes above them have been processed. # which will then be processed bottom up to generate shader activeOutputs = set() # set of activeNodes that might be needed activeOutLinks = set() linkStatus = {} linkToSource = {} toProcess = set(self.topNodes) while toProcess: n = toProcess.pop() a = n.getActiveNode(renderState, linkStatus) activeOutLinks.update(a.getOutLinks()) outLinks = a.getOutLinks() for link in outLinks: linkStatus[link] = link in activeOutLinks linkToSource[link] = a for link in outLinks: for dst in self.linkToDst[link]: inLinks = dst.getInLinks() for l in inLinks: if l not in linkStatus: break else: toProcess.add(dst) if a.isOutPut(): activeOutputs.add(a) # scan of active nodes to find bottoms that are needed for activeOutputs # walk upward from all activeOutputs marking visited nodes # and removing any activeOutputs hit from set of bottomActiveOutputs as they will be generated # as a result of others bottomActiveOutputs = set(activeOutputs) visited = set() toVisit = set(activeOutputs) linkToActiveDst = collections.defaultdict(set) while toVisit: n = toVisit.pop() if n not in visited: visited.add(n) inLinks = n.getInLinks() for link in inLinks: linkToActiveDst[link].add(n) a = linkToSource[link] if a not in visited: bottomActiveOutputs.discard(a) toVisit.add(a) # some sanity checks for link, dsts in linkToActiveDst.iteritems(): for dst in dsts: assert link in dst.getInLinks() for link, source in linkToSource.iteritems(): assert link in source.getOutLinks() # generate shader upwards from bottomActiveOutputs # this will generate the minimal part of the graph needed to provide the inputs toProcess = set(bottomActiveOutputs) neededNodes = [ ] # nodes needed from bottom up. The reverse of this is an ok order to compute them in. processed = set() while toProcess: n = toProcess.pop() neededNodes.append(n) #print neededNodes processed.add(n) # see if nodes providing input should be processed yet inLinks = n.getInLinks() for inLink in inLinks: # for all inputs to n source = linkToSource[inLink] assert source not in processed assert source not in toProcess outLinks = source.getOutLinks() # check if all uses of outputs of source are already processed sourceReady = True for link in outLinks: if not sourceReady: break for dst in linkToActiveDst[link]: if dst not in processed: sourceReady = False break if sourceReady: # if all outputs already processed, process it toProcess.add(source) # check casheByNodes. # Many different RenderStates may produce the same ActiveNode graph, so this second cache is useful. neededSet = frozenset(neededNodes) shader = self.casheByNodes.get(neededSet) if shader and not noChache: print "Shader is cached. Skipping generating shader to: " + debugFile return shader # no shader cached, so produce the source and shader stages = collections.defaultdict(StageBuilder) functions = AutoNamer("__f") for n in neededNodes: # process n s = stages[n.getStage()] s.addNode(n, functions) # TODO : Auto generate/match unspecified semantics here funcs = "\n\n".join(functions.getItems()) stageCode = "\n\n".join( stage.generateSource(name) for name, stage in stages.iteritems()) source = self.header + funcs + "\n\n" + stageCode + self.footer if debugFile: print 'Making Shader: ' + debugFile if debugFile: fOut = open(debugFile, 'w') fOut.write(source) fOut.close() shader = Shader.make(source) self.cache[renderState] = shader self.casheByNodes[neededSet] = shader return shader
camera_contrast_shader = Shader.make( ''' void vshader(float4 vtx_position : POSITION, float2 vtx_texcoord0 : TEXCOORD0, out float4 l_position : POSITION, out float2 l_texcoord0 : TEXCOORD0, uniform float4 texpad_tex, uniform float4x4 mat_modelproj) { l_position=mul(mat_modelproj, vtx_position); l_texcoord0 = vtx_position.xz * texpad_tex.xy + texpad_tex.xy; } half3 AdjustContrast(half3 color, half contrast) { return saturate(lerp(half3(0.5, 0.5, 0.5), color, contrast)); } void fshader(float2 l_texcoord0 : TEXCOORD0, out float4 o_color : COLOR, uniform sampler2D k_tex : TEXUNIT0, uniform float k_contrast) { float4 c = tex2D(k_tex, l_texcoord0); half3 d = half3(c); d = AdjustContrast(d, k_contrast); c.rgb = d.rgb; o_color = c; } ''', Shader.SL_Cg)
def reconfigure(self, fullrebuild, changed): """ Reconfigure is called whenever any configuration change is made. """ configuration = self.configuration if (fullrebuild): self.cleanup() if (len(configuration) == 0): return auxbits = 0 needtex = set(["color"]) needtexcoord = set(["color"]) if ("CartoonInk" in configuration): needtex.add("aux") auxbits |= AuxBitplaneAttrib.ABOAuxNormal needtexcoord.add("aux") if ("AmbientOcclusion" in configuration): needtex.add("depth") needtex.add("ssao0") needtex.add("ssao1") needtex.add("ssao2") needtex.add("aux") auxbits |= AuxBitplaneAttrib.ABOAuxNormal needtexcoord.add("ssao2") if ("BlurSharpen" in configuration): needtex.add("blur0") needtex.add("blur1") needtexcoord.add("blur1") if ("Bloom" in configuration): needtex.add("bloom0") needtex.add("bloom1") needtex.add("bloom2") needtex.add("bloom3") auxbits |= AuxBitplaneAttrib.ABOGlow needtexcoord.add("bloom3") if ("ViewGlow" in configuration): auxbits |= AuxBitplaneAttrib.ABOGlow if ("VolumetricLighting" in configuration): needtex.add(configuration["VolumetricLighting"].source) for tex in needtex: self.textures[tex] = Texture("scene-" + tex) self.textures[tex].setWrapU(Texture.WMClamp) self.textures[tex].setWrapV(Texture.WMClamp) self.finalQuad = self.manager.renderSceneInto( textures=self.textures, auxbits=auxbits) if (self.finalQuad == None): self.cleanup() return False if ("BlurSharpen" in configuration): blur0 = self.textures["blur0"] blur1 = self.textures["blur1"] self.blur.append( self.manager.renderQuadInto(colortex=blur0, div=2)) self.blur.append(self.manager.renderQuadInto(colortex=blur1)) self.blur[0].setShaderInput("src", self.textures["color"]) self.blur[0].setShader(self.loadShader("filter-blurx.sha")) self.blur[1].setShaderInput("src", blur0) self.blur[1].setShader(self.loadShader("filter-blury.sha")) if ("AmbientOcclusion" in configuration): ssao0 = self.textures["ssao0"] ssao1 = self.textures["ssao1"] ssao2 = self.textures["ssao2"] self.ssao.append(self.manager.renderQuadInto(colortex=ssao0)) self.ssao.append( self.manager.renderQuadInto(colortex=ssao1, div=2)) self.ssao.append(self.manager.renderQuadInto(colortex=ssao2)) self.ssao[0].setShaderInput("depth", self.textures["depth"]) self.ssao[0].setShaderInput("normal", self.textures["aux"]) self.ssao[0].setShaderInput( "random", loader.loadTexture("maps/random.rgb")) self.ssao[0].setShader( Shader.make( SSAO_BODY % configuration["AmbientOcclusion"].numsamples, Shader.SL_Cg)) self.ssao[1].setShaderInput("src", ssao0) self.ssao[1].setShader(self.loadShader("filter-blurx.sha")) self.ssao[2].setShaderInput("src", ssao1) self.ssao[2].setShader(self.loadShader("filter-blury.sha")) if ("Bloom" in configuration): bloomconf = configuration["Bloom"] bloom0 = self.textures["bloom0"] bloom1 = self.textures["bloom1"] bloom2 = self.textures["bloom2"] bloom3 = self.textures["bloom3"] if (bloomconf.size == "large"): scale = 8 downsampler = "filter-down4.sha" elif (bloomconf.size == "medium"): scale = 4 downsampler = "filter-copy.sha" else: scale = 2 downsampler = "filter-copy.sha" self.bloom.append( self.manager.renderQuadInto(colortex=bloom0, div=2, align=scale)) self.bloom.append( self.manager.renderQuadInto(colortex=bloom1, div=scale, align=scale)) self.bloom.append( self.manager.renderQuadInto(colortex=bloom2, div=scale, align=scale)) self.bloom.append( self.manager.renderQuadInto(colortex=bloom3, div=scale, align=scale)) self.bloom[0].setShaderInput("src", self.textures["color"]) self.bloom[0].setShader(self.loadShader("filter-bloomi.sha")) self.bloom[1].setShaderInput("src", bloom0) self.bloom[1].setShader(self.loadShader(downsampler)) self.bloom[2].setShaderInput("src", bloom1) self.bloom[2].setShader(self.loadShader("filter-bloomx.sha")) self.bloom[3].setShaderInput("src", bloom2) self.bloom[3].setShader(self.loadShader("filter-bloomy.sha")) texcoords = {} texcoordPadding = {} for tex in needtexcoord: if self.textures[tex].getAutoTextureScale() != ATSNone or \ "HalfPixelShift" in configuration: texcoords[tex] = "l_texcoord_" + tex texcoordPadding["l_texcoord_" + tex] = tex else: # Share unpadded texture coordinates. texcoords[tex] = "l_texcoord" texcoordPadding["l_texcoord"] = None texcoordSets = list(enumerate(texcoordPadding.keys())) text = "//Cg\n" text += "void vshader(float4 vtx_position : POSITION,\n" text += " out float4 l_position : POSITION,\n" for texcoord, padTex in texcoordPadding.items(): if padTex is not None: text += " uniform float4 texpad_tx%s,\n" % (padTex) if ("HalfPixelShift" in configuration): text += " uniform float4 texpix_tx%s,\n" % (padTex) for i, name in texcoordSets: text += " out float2 %s : TEXCOORD%d,\n" % (name, i) text += " uniform float4x4 mat_modelproj)\n" text += "{\n" text += " l_position = mul(mat_modelproj, vtx_position);\n" for texcoord, padTex in texcoordPadding.items(): if padTex is None: text += " %s = vtx_position.xz * float2(0.5, 0.5) + float2(0.5, 0.5);\n" % ( texcoord) else: text += " %s = (vtx_position.xz * texpad_tx%s.xy) + texpad_tx%s.xy;\n" % ( texcoord, padTex, padTex) if ("HalfPixelShift" in configuration): text += " %s += texpix_tx%s.xy * 0.5;\n" % (texcoord, padTex) text += "}\n" text += "void fshader(\n" for i, name in texcoordSets: text += " float2 %s : TEXCOORD%d,\n" % (name, i) for key in self.textures: text += " uniform sampler2D k_tx" + key + ",\n" if ("CartoonInk" in configuration): text += " uniform float4 k_cartoonseparation,\n" text += " uniform float4 k_cartooncolor,\n" text += " uniform float4 texpix_txaux,\n" if ("BlurSharpen" in configuration): text += " uniform float4 k_blurval,\n" if ("VolumetricLighting" in configuration): text += " uniform float4 k_casterpos,\n" text += " uniform float4 k_vlparams,\n" text += " out float4 o_color : COLOR)\n" text += "{\n" text += " o_color = tex2D(k_txcolor, %s);\n" % ( texcoords["color"]) if ("CartoonInk" in configuration): text += CARTOON_BODY % {"texcoord": texcoords["aux"]} if ("AmbientOcclusion" in configuration): text += " o_color *= tex2D(k_txssao2, %s).r;\n" % ( texcoords["ssao2"]) if ("BlurSharpen" in configuration): text += " o_color = lerp(tex2D(k_txblur1, %s), o_color, k_blurval.x);\n" % ( texcoords["blur1"]) if ("Bloom" in configuration): text += " o_color = saturate(o_color);\n" text += " float4 bloom = 0.5 * tex2D(k_txbloom3, %s);\n" % ( texcoords["bloom3"]) text += " o_color = 1-((1-bloom)*(1-o_color));\n" if ("ViewGlow" in configuration): text += " o_color.r = o_color.a;\n" if ("VolumetricLighting" in configuration): text += " float decay = 1.0f;\n" text += " float2 curcoord = %s;\n" % (texcoords["color"]) text += " float2 lightdir = curcoord - k_casterpos.xy;\n" text += " lightdir *= k_vlparams.x;\n" text += " half4 sample = tex2D(k_txcolor, curcoord);\n" text += " float3 vlcolor = sample.rgb * sample.a;\n" text += " for (int i = 0; i < %s; i++) {\n" % (int( configuration["VolumetricLighting"].numsamples)) text += " curcoord -= lightdir;\n" text += " sample = tex2D(k_tx%s, curcoord);\n" % ( configuration["VolumetricLighting"].source) text += " sample *= sample.a * decay;//*weight\n" text += " vlcolor += sample.rgb;\n" text += " decay *= k_vlparams.y;\n" text += " }\n" text += " o_color += float4(vlcolor * k_vlparams.z, 1);\n" if ("GammaAdjust" in configuration): gamma = configuration["GammaAdjust"] if gamma == 0.5: text += " o_color.rgb = sqrt(o_color.rgb);\n" elif gamma == 2.0: text += " o_color.rgb *= o_color.rgb;\n" elif gamma != 1.0: text += " o_color.rgb = pow(o_color.rgb, %ff);\n" % ( gamma) if ("Inverted" in configuration): text += " o_color = float4(1, 1, 1, 1) - o_color;\n" text += "}\n" self.finalQuad.setShader(Shader.make(text, Shader.SL_Cg)) for tex in self.textures: self.finalQuad.setShaderInput("tx" + tex, self.textures[tex]) self.task = taskMgr.add(self.update, "common-filters-update") if (changed == "CartoonInk") or fullrebuild: if ("CartoonInk" in configuration): c = configuration["CartoonInk"] self.finalQuad.setShaderInput( "cartoonseparation", LVecBase4(c.separation, 0, c.separation, 0)) self.finalQuad.setShaderInput("cartooncolor", c.color) if (changed == "BlurSharpen") or fullrebuild: if ("BlurSharpen" in configuration): blurval = configuration["BlurSharpen"] self.finalQuad.setShaderInput( "blurval", LVecBase4(blurval, blurval, blurval, blurval)) if (changed == "Bloom") or fullrebuild: if ("Bloom" in configuration): bloomconf = configuration["Bloom"] intensity = bloomconf.intensity * 3.0 self.bloom[0].setShaderInput("blend", bloomconf.blendx, bloomconf.blendy, bloomconf.blendz, bloomconf.blendw * 2.0) self.bloom[0].setShaderInput( "trigger", bloomconf.mintrigger, 1.0 / (bloomconf.maxtrigger - bloomconf.mintrigger), 0.0, 0.0) self.bloom[0].setShaderInput("desat", bloomconf.desat) self.bloom[3].setShaderInput("intensity", intensity, intensity, intensity, intensity) if (changed == "VolumetricLighting") or fullrebuild: if ("VolumetricLighting" in configuration): config = configuration["VolumetricLighting"] tcparam = config.density / float(config.numsamples) self.finalQuad.setShaderInput("vlparams", tcparam, config.decay, config.exposure, 0.0) if (changed == "AmbientOcclusion") or fullrebuild: if ("AmbientOcclusion" in configuration): config = configuration["AmbientOcclusion"] self.ssao[0].setShaderInput( "params1", config.numsamples, -float(config.amount) / config.numsamples, config.radius, 0) self.ssao[0].setShaderInput("params2", config.strength, config.falloff, 0, 0) self.update() return True