Spaces:
Sleeping
Sleeping
File size: 6,076 Bytes
16682ee |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
from panda3d.core import *
from direct.showbase.ShowBase import ShowBase
import cv2
from floorplan import Floorplan
import numpy as np
import random
import math
class Renderer(ShowBase):
def __init__(self):
#self.scene = self.loader.loadModel("floorplan_1.txt-floor.obj")
loadPrcFileData("", "window-type offscreen")
loadPrcFileData("", "win-size 128 128")
ShowBase.__init__(self)
self.scene = NodePath("Scene")
self.scene.reparentTo(self.render)
self.scene.setScale(1, 1, 1)
self.scene.setTwoSided(True)
self.scene.setPos(0, 0, 0)
self.scene.setHpr(0, 0, 0)
self.near_plane = 0.1
self.far_plane = 5.0
self.resolution = 128
self.max_16bit_val = 65535
self.light_sources = []
self.light_nodes = []
self.alight = AmbientLight('alight')
self.alight.setColor(VBase4(0.2, 0.2, 0.2, 1))
self.alnp = self.render.attachNewNode(self.alight)
self.render.setLight(self.alnp)
self.attenuation = False
base.camLens.setNear(self.near_plane)
base.camLens.setFar(self.far_plane)
self.generate_depth = True
if self.generate_depth is True:
self.depth_tex = Texture()
self.depth_tex.setFormat(Texture.FDepthComponent)
self.depth_buffer = base.win.makeTextureBuffer('depthmap', self.resolution, self.resolution, self.depth_tex, to_ram=True)
self.depth_cam = self.makeCamera(self.depth_buffer, lens = base.camLens)
print(self.depth_cam.node().getLens().getFilmSize())
self.depth_cam.reparentTo(base.render)
pass
self.models = []
self.backgrounds = []
self.model = None
self.createLightSources()
return
def delete(self):
self.alnp.removeNode()
for n in self.light_nodes:
n.removeNode()
continue
for m in self.models:
self.loader.unloadModel(m)
continue
base.destroy()
return
def selectModel(self, model_ind):
self.model = self.models[model_ind]
self.model.reparentTo(self.scene)
def unselectModel(self, model_ind):
self.model.detachNode()
self.model = None
def loadModels(self, filenames):
self.models = []
for filename in filenames:
floorplan = Floorplan(filename)
floorplan.read()
floorplan.segmentRooms()
exit(1)
self.models.append(floorplan.generateEggModel())
continue
return
def createLightSources(self):
for i in range(0, 7):
plight = PointLight('plight')
if self.attenuation is True:
plight.setAttenuation((1, 0, 1))
pass
plight.setColor(VBase4(0, 0, 0, 0))
self.light_sources.append(plight)
plnp = self.render.attachNewNode(plight)
plnp.setPos(3, 3, 3)
render.setLight(plnp)
self.light_nodes.append(plnp)
continue
return
def activateLightSources(self, light_sources, spher=True):
i = 0
for lght in light_sources:
lp_rad = lght[0]
lp_el = lght[1]
lp_az = lght[2]
lp_int = lght[3]
if spher:
self.light_nodes[i].setPos(
lp_rad*math.cos(lp_el)*math.cos(lp_az),
lp_rad*math.cos(lp_el)*math.sin(lp_az),
lp_rad*math.sin(lp_el))
else:
self.light_nodes[i].setPos(lp_rad, lp_el, lp_az)
pass
self.light_sources[i].setColor(VBase4(lp_int, lp_int, lp_int, 1))
i += 1
continue
return
def deactivateLightSources(self):
for i in range(0, 7):
self.light_sources[i].setColor(VBase4(0, 0, 0, 0))
continue
return
def textureToImage(self, texture):
im = texture.getRamImageAs("RGB")
strim = im.getData()
image = np.fromstring(strim, dtype='uint8')
#image = image.reshape(1200, 1200)
#cv2.imwrite('test/test.png', image.astype(np.uint8))
image = image.reshape(self.resolution, self.resolution, 3)
image = np.flipud(image)
return image
def setCameraPosition(self, pos, target):
self.camera.setPos(pos[0], pos[1], pos[2])
self.camera.lookAt(target[0], target[1], target[2])
if self.generate_depth is True:
self.depth_cam.setPos(pos[0], pos[1], pos[2])
self.depth_cam.lookAt(target[0], target[1], target[2])
pass
return
def renderView(self, camera_pos, light_sources):
angle = math.radians(random.randint(0, 360))
target = (camera_pos[0] + math.sin(angle), camera_pos[1] + math.cos(angle), camera_pos[2])
self.setCameraPosition(camera_pos, target)
self.activateLightSources(light_sources)
base.graphicsEngine.renderFrame()
tex = base.win.getScreenshot()
im = self.textureToImage(tex)
dm_uint = False
if self.generate_depth is True:
depth_im = PNMImage()
self.depth_tex.store(depth_im)
depth_map = np.zeros([self.resolution, self.resolution], dtype='float')
for i in range(0, self.resolution):
for j in range(0, self.resolution):
depth_val = depth_im.getGray(j, i)
depth_map[i, j] = self.far_plane * self.near_plane / (self.far_plane - depth_val * (self.far_plane - self.near_plane))
depth_map[i, j] = depth_map[i, j] / self.far_plane
continue
continue
dm_uint = np.round(depth_map * self.max_16bit_val).astype('uint16')
pass
im = im.astype(dtype=np.uint8)
self.deactivateLightSources()
return im, dm_uint
renderer = Renderer()
renderer.loadModels(['test/floorplan_2', ])
renderer.selectModel(0)
num_light = random.randint(2, 4)
lights = []
for nl in range(0, num_light):
light_pos = [random.random()*2. + 2.5,
random.randint(-90, 90),
random.randint(0, 360),
random.randint(10, 15)]
lights.append(light_pos)
continue
for im_num in range(0, 20):
x = random.random()
y = random.random()
z = 0.15
im, dm = renderer.renderView([x, y, z], lights)
cv2.imwrite('test/rendering_' + str(im_num) + '.png', (np.asarray(im)).astype(np.uint8))
cv2.imwrite('test/depth_' + str(im_num) + '.png', (np.asarray(1 / (dm / 65535.0) * 255)).astype(np.uint8))
continue
exit(1)
|