Guest User

Untitled

a guest
Mar 18th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. from __future__ import print_function
  2.  
  3. import math
  4.  
  5. from panda3d.core import *
  6. from direct.interval.IntervalGlobal import Sequence
  7.  
  8. from RenderTarget import RenderTarget
  9.  
  10.  
  11. loadPrcFileData("", "gl-coordinate-system default")
  12. loadPrcFileData("", "win-size 1920 1080")
  13. # loadPrcFileData("", "win-size 1600 900")
  14. loadPrcFileData("", "win-fixed-size 1")
  15. # loadPrcFileData("", "show-buffers #t")
  16. loadPrcFileData("", "sync-video #f")
  17. loadPrcFileData("", "show-frame-rate-meter #t")
  18. loadPrcFileData("", "textures-power-2 none")
  19.  
  20. import direct.directbase.DirectStart
  21.  
  22. scene = loader.loadModel("panda")
  23. scene.reparentTo(render)
  24. scene.setPos(0, 0, -3)
  25. scene.setH(90)
  26.  
  27.  
  28. # Remove default camera
  29. base.disableMouse()
  30. base.camera.removeNode()
  31.  
  32.  
  33. targetW, targetH = 480, 854
  34.  
  35.  
  36. # Create the buffer
  37. buffW, buffH = targetW * 4, targetH * 6
  38. print("Creating offscreen buffer of size", buffW, buffH)
  39.  
  40. target = RenderTarget("scene")
  41. target.setSize(buffW, buffH)
  42. target.addColorTexture()
  43. target.addDepthTexture()
  44. target.prepareSceneRender()
  45.  
  46.  
  47. # When moving the camera, move the camera rig instead of base.cam
  48. cameraRig = render.attachNewNode("Camera Rig")
  49. cameras = []
  50. regions = []
  51.  
  52. # Create the 24 display regions and cameras
  53.  
  54. for i in range(24):
  55.  
  56. x = int(i % 4)
  57. y = int(i / 4)
  58.  
  59. # Create the camera
  60. angleDegree = i / 24.0 * 180.0
  61. angleRad = angleDegree / 180.0 * math.pi
  62.  
  63. camNode = Camera("Camera-" + str(i))
  64. camNode.setLens(base.camLens)
  65. cam = cameraRig.attachNewNode(camNode)
  66. cam.setPos(math.sin(angleRad) * 30.0, math.cos(angleRad) * 30.0, 20.0)
  67. cam.lookAt(0, 0, 0)
  68. cameras.append(cam)
  69.  
  70. # Create the region
  71. dr = target.getInternalBuffer().makeDisplayRegion()
  72. dr.setSort(1000)
  73. dr.setDimensions(x / 4.0, x / 4.0 + 0.25, y / 6.0, y / 6.0 + 1.0 / 6.0)
  74. dr.setClearDepthActive(True)
  75. dr.setClearDepth(1.0)
  76. dr.setCamera(cam)
  77.  
  78. regions.append(dr)
  79.  
  80. target.getInternalBuffer().setSort(10000)
  81. target.getInternalRegion().setSort(100)
  82.  
  83. # Create the shader which merges the 24 frames
  84. combineVertex = """
  85. #version 400
  86.  
  87. uniform mat4 p3d_ModelViewProjectionMatrix;
  88.  
  89. in vec4 p3d_Vertex;
  90. out vec2 texcoord;
  91.  
  92. void main() {
  93. gl_Position = vec4(p3d_Vertex.x, p3d_Vertex.z, 0, 1);
  94. texcoord = sign(p3d_Vertex.xz * 0.5 + 0.5);
  95. }
  96. """
  97.  
  98. combineFragment = """
  99. #version 400
  100.  
  101. in vec2 texcoord;
  102. out vec4 result;
  103. uniform sampler2D colorTex;
  104. uniform sampler2D ditherTex;
  105.  
  106. uniform ivec2 imageDimensions;
  107.  
  108. // Dither
  109. bool find_closest(int x, int y, float c0)
  110. {
  111. float limit = texelFetch(ditherTex, ivec2(x, y), 0).x;
  112.  
  113. if(c0 < limit) {
  114. return false;
  115. }
  116. return true;
  117. }
  118.  
  119. void main() {
  120. result = vec4(0);
  121.  
  122. int flags = 0;
  123.  
  124. int colorChannel = int(int(gl_FragCoord.x) / imageDimensions.x);
  125.  
  126. // "Rest of the area of the FullHD frame is unused"
  127. if (colorChannel >= 3 || gl_FragCoord.y > imageDimensions.y) {
  128. result = vec4(1, 1, 0, 1);
  129. return;
  130. }
  131.  
  132. vec3 color_mask = vec3(
  133. colorChannel == 0 ? 1 : 0,
  134. colorChannel == 1 ? 1 : 0,
  135. colorChannel == 2 ? 1 : 0
  136. );
  137.  
  138. ivec2 subtex = ivec2(gl_FragCoord.xy) % imageDimensions;
  139.  
  140.  
  141. for (int i = 0; i < 24; i++) {
  142. int x = i % 4;
  143. int y = i / 4;
  144.  
  145. ivec2 transformedCoord = subtex + ivec2(x, y) * imageDimensions;
  146.  
  147. int fractX = int(mod(int(gl_FragCoord.x), 4));
  148. int fractY = int(mod(int(gl_FragCoord.y), 4));
  149.  
  150. vec4 colorSample = texelFetch(colorTex, transformedCoord, 0);
  151. float luminance = dot(colorSample.xyz, color_mask);
  152.  
  153. bool dithered = find_closest(fractX, fractY, luminance);
  154.  
  155. if (dithered) {
  156. flags |= 1 << i;
  157. }
  158. }
  159.  
  160. // Convert flags to color
  161. vec4 color = vec4( vec3( (flags >> 16) & 0xFF, (flags >> 8) & 0xFF, flags & 0xFF) / 256.0 , 1);
  162. result = color;
  163.  
  164. }
  165.  
  166. """
  167.  
  168. flipFragment = """
  169. #version 400
  170.  
  171. in vec2 texcoord;
  172. out vec4 result;
  173. uniform sampler2D sourceTex;
  174.  
  175. void main() {
  176. ivec2 coord = ivec2(gl_FragCoord.xy);
  177. result = texelFetch(sourceTex, ivec2(coord.x, 1080 - coord.y), 0);
  178. result.w = 1.0;
  179. }
  180. """
  181.  
  182. combineShader = Shader.make(Shader.SLGLSL, combineVertex, combineFragment)
  183. flipShader = Shader.make(Shader.SLGLSL, combineVertex, flipFragment)
  184.  
  185.  
  186. # Process
  187. targetDither = RenderTarget("process")
  188. targetDither.setSize(base.win.getXSize(), base.win.getYSize())
  189. targetDither.addColorTexture()
  190. targetDither.prepareOffscreenBuffer()
  191. targetDither.setShader(combineShader)
  192. targetDither.setShaderInput("colorTex", target.getColorTexture())
  193. targetDither.setShaderInput("ditherTex", loader.loadTexture("dither.png"))
  194. targetDither.setShaderInput("imageDimensions", LVecBase2i(targetW, targetH))
  195.  
  196.  
  197. # Flip vertically
  198. target.setShader(flipShader)
  199. target.setShaderInput("sourceTex", targetDither.getColorTexture())
  200.  
  201.  
  202. # Animate camera rig
  203.  
  204.  
  205. lerpTop = cameraRig.posInterval(1.2, Vec3(0, 0, 7), startPos=Vec3(0,0,2))
  206. lerpBot = cameraRig.posInterval(1.2, Vec3(0, 0, 2), startPos=Vec3(0,0,7))
  207. sequence = Sequence(lerpTop, lerpBot)
  208. sequence.loop()
  209.  
  210. run()
Add Comment
Please, Sign In to add comment