Created
January 2, 2020 17:03
-
-
Save jefvel/6e5b58cdbac74cbdc3d9b2a58bf377c5 to your computer and use it in GitHub Desktop.
ScreenFX pixelated shader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -lib heaps | |
| -lib hlsdl | |
| -D resourcesPath=. | |
| -main ScreenFXSample | |
| -hl hlboot.hl |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import h3d.mat.DepthBuffer; | |
| import hxd.Key; | |
| import hxd.Event.EventKind; | |
| import hxd.Res; | |
| import hxd.Window; | |
| import h3d.mat.Texture; | |
| class PixelatedShader extends h3d.shader.ScreenShader { | |
| static var SRC = { | |
| @param var texture : Sampler2D; | |
| @param var pixelSize : Vec2; | |
| function fragment() { | |
| pixelColor = texture.get(calculatedUV - calculatedUV % pixelSize + 0.0001); | |
| } | |
| }; | |
| } | |
| //PARAM=-lib heaps | |
| class ScreenFXSample extends hxd.App { | |
| public override function init() { | |
| var w = Window.getInstance(); | |
| w.addEventTarget(onEvent); | |
| w.addResizeEvent(onResizeEvent); | |
| // creates a new unit cube | |
| var prim = new h3d.prim.Cube(); | |
| // translate it so its center will be at the center of the cube | |
| prim.translate( -0.5, -0.5, -0.5); | |
| // unindex the faces to create hard edges normals | |
| prim.unindex(); | |
| // add face normals | |
| prim.addNormals(); | |
| // add texture coordinates | |
| prim.addUVs(); | |
| // creates another cube, this time with no texture | |
| var obj2 = new h3d.scene.Mesh(prim, s3d); | |
| // set the second cube color | |
| obj2.material.color.setColor(0xFFB280); | |
| // put it above the first cube | |
| obj2.z = 0.7; | |
| // scale it down to 60% | |
| obj2.scale(0.6); | |
| // set the ambient light to 30% | |
| s3d.lightSystem.ambientLight.set(0.3, 0.3, 0.3); | |
| // disable shadows | |
| obj2.material.shadows = false; | |
| pass = new h3d.pass.ScreenFx(new PixelatedShader()); | |
| onResizeEvent(); | |
| } | |
| final PIXEL_SIZE = 16; | |
| var pass : h3d.pass.ScreenFx<PixelatedShader>; | |
| var renderTarget : Texture; | |
| var upscaled = false; | |
| function onResizeEvent() { | |
| var s = hxd.Window.getInstance(); | |
| var w = Std.int(s.width / PIXEL_SIZE); | |
| var h = Std.int(s.height / PIXEL_SIZE); | |
| s2d.scaleMode = ScaleMode.Stretch(w, h); | |
| if (renderTarget != null) { | |
| renderTarget.dispose(); | |
| } | |
| w = s.width; | |
| h = s.height; | |
| renderTarget = new Texture(w, h, [ Target ]); | |
| renderTarget.filter = Nearest; | |
| renderTarget.depthBuffer = new DepthBuffer(w, h); | |
| } | |
| function onEvent(e : hxd.Event) { | |
| if (e.kind == EventKind.EKeyDown) { | |
| if (e.keyCode == Key.F10) { | |
| upscaled = !upscaled; | |
| } | |
| } | |
| } | |
| public override function render(e : h3d.Engine) { | |
| e.backgroundColor = s3d.lightSystem.ambientLight.toColor(); | |
| if (upscaled) { | |
| var s = hxd.Window.getInstance(); | |
| var w = (PIXEL_SIZE / s.width); | |
| var h = (PIXEL_SIZE / s.height); | |
| // Render the target first | |
| e.pushTarget(renderTarget); | |
| e.clear(e.backgroundColor, 1); | |
| s3d.render(e); | |
| e.popTarget(); | |
| pass.shader.pixelSize.set(w, h); | |
| pass.shader.texture = renderTarget; | |
| pass.render(); | |
| s2d.render(e); | |
| } else { | |
| super.render(e); | |
| } | |
| } | |
| static function main() { | |
| new ScreenFXSample(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment