|
| 1 | + |
| 2 | +local portals |
| 3 | +local matView = CreateMaterial( |
| 4 | + "UnlitGeneric", |
| 5 | + "GMODScreenspace", |
| 6 | + { |
| 7 | + ["$basetexturetransform"] = "center .5 .5 scale -1 -1 rotate 0 translate 0 0", |
| 8 | + ["$texturealpha"] = "0", |
| 9 | + ["$vertexalpha"] = "1", |
| 10 | + } |
| 11 | +) |
| 12 | +local matDummy = Material( "debug/white" ) |
| 13 | + |
| 14 | + |
| 15 | +-- Render the portal views |
| 16 | +local drawing = false |
| 17 | + |
| 18 | +hook.Add( "RenderScene", "WorldPortalRenderHook", function( plyOrigin, plyAngles) |
| 19 | + |
| 20 | + if ( not portals ) then return end |
| 21 | + if ( drawing ) then return end |
| 22 | + |
| 23 | + local oldWepColor = LocalPlayer():GetWeaponColor() |
| 24 | + LocalPlayer():SetWeaponColor( Vector(0, 0, 0) ) --no more phys gun glaw or beam |
| 25 | + |
| 26 | + for _, portal in ipairs( portals ) do |
| 27 | + -- Render view from portal |
| 28 | + local oldRT = render.GetRenderTarget() |
| 29 | + render.SetRenderTarget( portal.texture ) |
| 30 | + render.Clear( 0, 0, 0, 255 ) |
| 31 | + render.ClearDepth() |
| 32 | + render.ClearStencil() |
| 33 | + |
| 34 | + render.EnableClipping(true) |
| 35 | + --render.PopCustomClipPlane() |
| 36 | + render.PushCustomClipPlane(portals[portal.exit].forward, portals[portal.exit].forward:Dot(portals[portal.exit].pos) ) |
| 37 | + |
| 38 | + local rotation = portals[portal.exit].forward:Angle() - portal.forward:Angle() |
| 39 | + rotation = rotation + Angle( 0, 180, 0) |
| 40 | + local offset = LocalPlayer():EyePos() - portal.pos |
| 41 | + --offset = calculateNewOffset( portal, offset ) |
| 42 | + offset:Rotate( rotation ) |
| 43 | + local camPos = portals[portal.exit].pos + offset |
| 44 | + |
| 45 | + local camAngles = plyAngles + rotation |
| 46 | + |
| 47 | + drawing = true |
| 48 | + render.RenderView( { |
| 49 | + x = 0, |
| 50 | + y = 0, |
| 51 | + w = ScrW(), |
| 52 | + h = ScrH(), |
| 53 | + origin = camPos, |
| 54 | + angles = camAngles, |
| 55 | + drawpostprocess = true, |
| 56 | + drawhud = false, |
| 57 | + drawmonitors = false, |
| 58 | + drawviewmodel = false, |
| 59 | + } ) |
| 60 | + drawing = false |
| 61 | + |
| 62 | + render.PopCustomClipPlane() |
| 63 | + render.EnableClipping(false) |
| 64 | + render.SetRenderTarget( oldRT ) |
| 65 | + end |
| 66 | + |
| 67 | + LocalPlayer():SetWeaponColor( oldWepColor ) |
| 68 | +end ) |
| 69 | +hook.Add( "PreDrawOpaqueRenderables", "WorldPortalRenderHook", function() |
| 70 | + |
| 71 | + render.UpdateScreenEffectTexture() |
| 72 | + |
| 73 | + if ( not portals ) then return end |
| 74 | + if ( drawing ) then return end |
| 75 | + |
| 76 | + for _, portal in ipairs( portals ) do |
| 77 | + |
| 78 | + -- Draw view over portal |
| 79 | + render.ClearStencil() |
| 80 | + render.SetStencilEnable( true ) |
| 81 | + |
| 82 | + render.SetStencilWriteMask( 1 ) |
| 83 | + render.SetStencilTestMask( 1 ) |
| 84 | + |
| 85 | + render.SetStencilFailOperation( STENCILOPERATION_KEEP ) |
| 86 | + render.SetStencilZFailOperation( STENCILOPERATION_KEEP ) |
| 87 | + render.SetStencilPassOperation( STENCILOPERATION_REPLACE ) |
| 88 | + render.SetStencilCompareFunction( STENCILCOMPARISONFUNCTION_ALWAYS ) |
| 89 | + render.SetStencilReferenceValue( 1 ) |
| 90 | + |
| 91 | + render.SetMaterial( matDummy ) |
| 92 | + render.SetColorModulation( 1, 1, 1 ) |
| 93 | + |
| 94 | + render.DrawQuadEasy( portal.pos, portal.forward, portal.width, portal.height, Color( 255, 255, 255, 255) ) |
| 95 | + |
| 96 | + render.SetStencilCompareFunction( STENCILCOMPARISONFUNCTION_EQUAL ) |
| 97 | + render.SetStencilPassOperation( STENCILOPERATION_REPLACE ) |
| 98 | + render.SetStencilReferenceValue( 1 ) |
| 99 | + |
| 100 | + matView:SetTexture( "$basetexture", portal.texture ) |
| 101 | + render.SetMaterial( matView ) |
| 102 | + render.DrawScreenQuad() |
| 103 | + |
| 104 | + render.SetStencilEnable( false ) |
| 105 | + end |
| 106 | +end ) |
| 107 | + |
| 108 | +-- Receive portal info |
| 109 | +net.Receive("WorldPortalUpdate", function() |
| 110 | + portals = {} |
| 111 | + |
| 112 | + for i = 1, net.ReadInt( 16 ) do |
| 113 | + portals[i] = {} |
| 114 | + portals[i].pos = net.ReadVector() |
| 115 | + portals[i].width = net.ReadInt( 16 ) |
| 116 | + portals[i].height = net.ReadInt( 16 ) |
| 117 | + portals[i].forward = net.ReadVector() |
| 118 | + portals[i].exit = net.ReadInt( 16 ) |
| 119 | + portals[i].texture = GetRenderTarget("portal" .. i, |
| 120 | + ScrW(), |
| 121 | + ScrH(), |
| 122 | + false |
| 123 | + ) |
| 124 | + end |
| 125 | + |
| 126 | +end ) |
| 127 | + |
| 128 | +-- Set it to draw the player while rendering portals |
| 129 | +-- Calling Start3D to fix this is incredibly hacky |
| 130 | + |
| 131 | +hook.Add( "PostDrawEffects", "WorldPortalPotentialFix", function() |
| 132 | + cam.Start3D( EyePos(), EyeAngles() ) |
| 133 | + cam.End3D() |
| 134 | +end) |
| 135 | + |
| 136 | +hook.Add( "ShouldDrawLocalPlayer", "WorldPortalRenderHook", function() |
| 137 | + return drawing |
| 138 | +end) |
0 commit comments