def from_trimesh_scene(triscene): import uuid import logging from editor.render.graphics import Scene, Mesh, Geometry, Material # read scene scene = Scene() # convert gltf for name, data in triscene.graph.transforms.nodes(data=True): # extract transform import glm transform = glm.mat4(triscene.graph[name][0]) # extract mesh hasGeometry = 'geometry' in data if hasGeometry: trigeo = triscene.geometry[data['geometry']] print("!!!!!!!!!!", name) print(transform) # geometry geometry = Geometry( positions=trigeo.vertices.astype(np.float32), indices=trigeo.faces.astype(np.uint), normals=trigeo.vertex_normals.astype(np.float32), uvs=trigeo.visual.uv.astype(np.float32)) # material print("!!!!!!!!!!", trigeo.visual.material.baseColorFactor) material = Material( albedo=glm.vec3( *trigeo.visual.material.baseColorFactor[:3] / 255), emission=trigeo.visual.material.emissiveFactor[:3] / 255, roughness=float(trigeo.visual.material.roughnessFactor), metallic=float(trigeo.visual.material.metallicFactor)) scene.add_child( Mesh(name=name, transform=glm.transpose(transform), geometry=geometry, material=material)) else: logging.warning("currently only supports mesh") # extract light return scene
from editor.render import puregl import trimesh from editor.render.graphics.window import Window # logging.basicConfig(level=logging.DEBUG) if __name__ == "__main__": import numpy as np import glm from editor.render import assets from editor.render.graphics import Scene from editor.render.graphics import PointLight, SpotLight, DirectionalLight from pathlib import Path path = Path("../../assets/test_scene.glb").resolve() triscene = trimesh.load(str(path)) scene = Scene.from_trimesh_scene(triscene) scene.plot() dirlight = DirectionalLight(direction=glm.vec3(1, -6, -2), color=glm.vec3(1.0), intensity=2.0, position=glm.vec3(-1, 6, 2), radius=5.0, near=1.0, far=30) scene.add_child(dirlight) spotlight = SpotLight(position=glm.vec3(-1, 0.5, -3), direction=glm.vec3(1, -0.5, 3), color=glm.vec3(0.04, 0.6, 1.0), intensity=150,
def render(self, input_texture): super().render() return self.texture if __name__ == "__main__": from OpenGL.GL import * import glm from editor.render.graphics.examples.viewer import Viewer from editor.render import glsl, puregl, imdraw from editor.render.graphics import Scene, Mesh, Geometry, Material, PointLight, SpotLight, DirectionalLight from pathlib import Path # Create Scene scene = Scene() mesh = Mesh(geometry=Geometry(*imdraw.geo.sphere()), transform=glm.translate(glm.mat4(1), (0, 0.5, 0)), material=Material(albedo=(0.7, 0.14, 0.14), roughness=0.3, metallic=1.0)) scene.add_child(mesh) plane = Mesh(geometry=Geometry(*imdraw.geo.plane()), material=Material(albedo=(0.9, 0.9, 0.9), roughness=0.7, metallic=0.0)) scene.add_child(plane) dirlight = DirectionalLight(direction=glm.vec3(1, -6, -2), color=glm.vec3(1.0),
from editor.render.graphics.passes import GeometryPass from editor.render.graphics.passes import EnvironmentPass from editor.render.graphics.passes import IrradiancePass, PrefilterPass, BRDFPass import glm from editor.render.graphics import Scene, Mesh, Geometry, Material from editor.render.graphics.examples.viewer import Viewer viewer = Viewer() # assets environment_image = assets.imread('hdri/Tropical_Beach_3k.hdr').astype( np.float32) # scene scene = Scene() scene.add_child( Mesh(transform=glm.translate(glm.mat4(1), (0.0, 0.5, 0.0)), geometry=Geometry(*imdraw.geo.sphere()), material=Material(albedo=glm.vec3(0.5), emission=(0, 0, 0), roughness=glm.pow(0.5, 2), metallic=0.0))) scene.add_child(Mesh(geometry=Geometry(*imdraw.geo.plane()))) dirlight = DirectionalLight(direction=glm.vec3(1, -6, -2), color=glm.vec3(1.0), intensity=1.0, position=-glm.vec3(1, -6, -2), radius=5, near=1,
# transform puregl.program.set_uniform(self.program, "model", mesh.transform) # geometry mesh.geometry._draw(self.program) return self.texture if __name__ == "__main__": import glm from editor.render.graphics.examples.viewer import Viewer from editor.render.graphics import Scene, Mesh, Geometry, Material import time, math scene = Scene.test_scene() viewer = Viewer(floating=True) cubedepth_pass = CubeDepthPass(1024,1024) @viewer.event def on_draw(): # render passes pos = glm.vec3(math.cos(time.time()*2)*3, 1, 3) camera360 = Camera360(transform=glm.translate(glm.mat4(1), pos), near=1, far=10) cubedepth_map = cubedepth_pass.render(scene.find_meshes(), camera360) # render passes to screen glDisable(GL_DEPTH_TEST)
imdraw.texture(self.lighting_pass.beauty, (0, 300, 90, 90)) # swap buffers # ------------ self.window.swap_buffers() GLFWViewer.poll_events() def start(self): with self.window: self.setup() while not self.window.should_close(): self.render() if __name__ == "__main__": scene = Scene() cube = Mesh(transform=glm.translate(glm.mat4(1), (-1, 0.5, 0)), material=Material(albedo=glm.vec3(0.9, 0.04, 0.04), roughness=0.6, metallic=0.0, ao=1.0), geometry=Geometry.cube()) sphere = Mesh(transform=glm.translate(glm.mat4(1), (1, 0.5, 0)), material=Material(albedo=glm.vec3(0.04, 0.7, 0.9), roughness=0.2, metallic=1.0, ao=1.0), geometry=Geometry.sphere()) plane = Mesh(transform=glm.translate(glm.mat4(1), (0, 0.0, 0)),
def render(self, scene, camera): super().render() gBuffer = self.geometry_pass.render(scene.find_all(lambda obj: isinstance(obj, Mesh)), camera) beauty = self.matcap_pass.render(self.matcap_texture, gBuffer[0], gBuffer[1], camera) return beauty if __name__ == "__main__": import glm from editor.render.graphics.examples.viewer import Viewer from editor.render.graphics import Scene, Mesh, Geometry, Material, PerspectiveCamera # create scene scene = Scene() mesh = Mesh(transform=glm.mat4(1), material=Material(albedo=(0, 0, 0), emission=(0, 0, 0), roughness=0.0, metallic=0.0), geometry=Geometry(*imdraw.geo.sphere())) scene.add_child(mesh) renderer = DeferredMatcapRenderer(1280, 720) camera = PerspectiveCamera(transform=glm.inverse(glm.lookAt(glm.vec3(2,2,4), glm.vec3(0,0,0), glm.vec3(0,1,0))), fovy=glm.radians(60), aspect=1280/720, near=0.1, far=30) viewer = Viewer()
def test_scene(): from .lights import PointLight, SpotLight, DirectionalLight scene = Scene() for j in range(2): for x, roughness in zip(np.linspace(-3, 3, 5), np.linspace(0, 1, 5)): sphere = Mesh(transform=glm.translate(glm.mat4(1), (x, 0.5, j * 3 - 1.5)), geometry=Geometry(*imdraw.geo.sphere()), material=Material(albedo=glm.vec3(0.5), emission=(0, 0, 0), roughness=roughness, metallic=float(j))) scene.add_child(sphere) dirlight = DirectionalLight(direction=glm.vec3(1, -6, -2), color=glm.vec3(1.0), intensity=1.0, position=-glm.vec3(1, -6, -2), radius=5, near=1, far=30) spotlight = SpotLight(position=glm.vec3(-1, 0.5, -3), direction=glm.vec3(1, -0.5, 3), color=glm.vec3(0.04, 0.6, 1.0), intensity=150.0, fov=60, near=1, far=15) pointlight = PointLight(position=glm.vec3(2.5, 1.3, 2.5), color=glm.vec3(1, 0.7, 0.1), intensity=17.5, near=0.1, far=10) scene.add_child(dirlight) scene.add_child(spotlight) scene.add_child(pointlight) # cube = Mesh(transform=glm.translate(glm.mat4(1), (1.0, 0.5, 0.0)) * glm.rotate(glm.mat4(1), glm.radians(30), (0,1,0)), # geometry=Geometry(*imdraw.geo.cube()), # material=Material(albedo=(1, 0, 0), # emission=(0,0,0), # roughness=0.7, # metallic=0.0)) # scene.add_child(cube) plane = Mesh(transform=glm.translate(glm.mat4(1), (0, 0.0, 0.0)), geometry=Geometry(*imdraw.geo.plane()), material=Material(albedo=(0.5, 0.5, 0.5), emission=(0, 0, 0), roughness=0.8, metallic=0.0)) scene.add_child(plane) return scene