Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Odilkhan Yakubov's PR for Blender 4.1+ #934

Merged
merged 26 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a5626b2
b4 api adaptation - 2D_ ; UNDO ; blf.size dpi
CGThoughts Nov 10, 2023
b860354
Fix viewport display of LOL library
neo2068 Dec 1, 2023
fd6bf84
Merge branch 'for_blender_4' of https://github.com/LuxCoreRender/Blen…
neo2068 Dec 1, 2023
d3afbe8
Fix missing merge edit in last commit
neo2068 Dec 1, 2023
a5a81eb
Fix nodes, use changed procedure for access to node sockets
neo2068 Dec 27, 2023
168028c
Add missing texture node tree for e.g. light texture
neo2068 Dec 27, 2023
039bd64
Fix for auto smooth option (PR #866 from pbrt4bounty)
neo2068 Dec 28, 2023
dd4d3be
Initial support for curve hair
neo2068 Dec 30, 2023
2279d29
set BlendLuxCore version to 2.8 alpha1
neo2068 Dec 30, 2023
ebae228
Fix light group operator
neo2068 Dec 30, 2023
c9f71f1
Fix for cycles node reader and glossy coating node
neo2068 Jan 4, 2024
43244a7
fix custom normals export
neo2068 Jan 5, 2024
97bfb3e
Adjust sheen socket in disney material
neo2068 Jan 5, 2024
3cb5435
Fix car paint material with preset parameters
neo2068 Jan 13, 2024
ea6b067
Fix auto smooth normals
neo2068 Jan 30, 2024
0ef3018
Improvement: scene statistics during viewport rendering
odil24 Mar 28, 2024
fc6ac86
deactivate auto smooth and normal handing until implementation is ready
neo2068 Jun 6, 2024
9650306
Merge branch 'LuxCoreRender:for_blender_4.1' into for_blender_4.1
odil24 Jun 6, 2024
0c5d87a
Fix: Viewport rendering restart when Color Management Display settings
odil24 Aug 21, 2024
8d289db
Update viewport.py
odil24 Aug 23, 2024
e9362db
Fix: Color management display settings changes restart issue
odil24 Aug 25, 2024
53aa11c
Update viewport.py
odil24 Aug 25, 2024
455f6e3
Update viewport.py
odil24 Aug 25, 2024
9195c15
Fix: LOL does not work in Blender 4.1
odil24 Aug 26, 2024
ab95170
Fixed Finally: Viewport rendering does not response to Color manageme…
odil24 Sep 1, 2024
103fd26
Minor fix for previous commit
odil24 Sep 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ def get_bin_directory():
bl_info = {
"name": "LuxCoreRender",
"author": "Simon Wendsche (B.Y.O.B.), Michael Klemm (neo2068), Odilkhan Yakubov (odil24), acasta69, u3dreal, Philstix",
"version": (2, 7),
"blender": (3, 6, 0),
"version": (2, 8),
"blender": (4, 0, 0),
"category": "Render",
"description": "LuxCoreRender integration for Blender",
"warning": "beta2",
"warning": "alpha1",
"wiki_url": "https://wiki.luxcorerender.org/",
"tracker_url": "https://github.com/LuxCoreRender/BlendLuxCore/issues/new",
}
Expand Down
57 changes: 24 additions & 33 deletions draw/lol/viewport.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
#
# #####

import bgl, blf
import blf
import bpy
import gpu
import math
import random
from mathutils import Vector
import mathutils


from bpy_extras import view3d_utils
from gpu_extras.batch import batch_for_shader
from ...utils import get_addon_preferences
Expand All @@ -49,6 +50,7 @@ def draw_progress(x, y, text='', percent=None, color=(0, 1, 0, 1)):


def draw_rect(x, y, width, height, color):
gpu.state.blend_set('ALPHA')
xmax = x + width
ymax = y + height
points = [[x, y], # [x, y]
Expand All @@ -58,49 +60,49 @@ def draw_rect(x, y, width, height, color):
]
indices = ((0, 1, 2), (2, 3, 0))

shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR')
shader = gpu.shader.from_builtin('UNIFORM_COLOR')
batch = batch_for_shader(shader, 'TRIS', {"pos": points}, indices=indices)

shader.bind()
shader.uniform_float("color", color)
bgl.glEnable(bgl.GL_BLEND)
batch.draw(shader)
gpu.state.blend_set('NONE')


def draw_line2d(x1, y1, x2, y2, width, color):
coords = (
(x1, y1), (x2, y2))

indices = (
(0, 1),)
bgl.glEnable(bgl.GL_BLEND)
bgl.glEnable(bgl.GL_LINE_SMOOTH)
indices = ((0, 1),)


shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR')
gpu.state.blend_set('ALPHA')
shader = gpu.shader.from_builtin('UNIFORM_COLOR')
batch = batch_for_shader(shader, 'LINES', {"pos": coords}, indices=indices)
shader.bind()
shader.uniform_float("color", color)
batch.draw(shader)
gpu.state.blend_set('NONE')


def draw_lines(vertices, indices, color):
bgl.glEnable(bgl.GL_BLEND)
bgl.glEnable(bgl.GL_LINE_SMOOTH)
bgl.glLineWidth(2)

shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
gpu.state.blend_set('ALPHA')
shader = gpu.shader.from_builtin('UNIFORM_COLOR')
batch = batch_for_shader(shader, 'LINES', {"pos": vertices}, indices=indices)
shader.bind()
shader.uniform_float("color", color)
batch.draw(shader)
gpu.state.blend_set('NONE')


def draw_rect_3d(coords, color):
indices = [(0, 1, 2), (2, 3, 0)]
shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
gpu.state.blend_set('ALPHA')
shader = gpu.shader.from_builtin('UNIFORM_COLOR')
batch = batch_for_shader(shader, 'TRIS', {"pos": coords}, indices=indices)
shader.uniform_float("color", color)
batch.draw(shader)
gpu.state.blend_set('NONE')


def draw_bbox(location, rotation, bbox_min, bbox_max, progress=None, color=(0, 1, 0, 1)):
Expand Down Expand Up @@ -129,6 +131,7 @@ def draw_bbox(location, rotation, bbox_min, bbox_max, progress=None, color=(0, 1
lines = [[0, 1], [1, 2], [2, 3], [3, 0], [4, 5], [5, 6], [6, 7], [7, 4], [0, 4], [1, 5],
[2, 6], [3, 7], [0, 8], [1, 8]]
draw_lines(vertices, lines, color)

if progress != None:
color = (color[0], color[1], color[2], .2)
progress = progress * .01
Expand All @@ -146,7 +149,6 @@ def draw_bbox(location, rotation, bbox_min, bbox_max, progress=None, color=(0, 1


def draw_image(x, y, width, height, image, transparency, crop=(0, 0, 1, 1)):
# draw_rect(x,y, width, height, (.5,0,0,.5))

coords = [
(x, y), (x + width, y),
Expand All @@ -160,32 +162,21 @@ def draw_image(x, y, width, height, image, transparency, crop=(0, 0, 1, 1)):

indices = [(0, 1, 2), (2, 1, 3)]

shader = gpu.shader.from_builtin('2D_IMAGE')
gpu.state.blend_set('ALPHA')
shader = gpu.shader.from_builtin('IMAGE')

batch = batch_for_shader(shader, 'TRIS',
{"pos": coords,
"texCoord": uvs},
indices=indices)

# send image to gpu if it isn't there already
if image.gl_load():
raise Exception()

# texture identifier on gpu
texture_id = image.bindcode

# in case someone disabled it before
bgl.glEnable(bgl.GL_BLEND)

# bind texture to image unit 0
bgl.glActiveTexture(bgl.GL_TEXTURE0)
bgl.glBindTexture(bgl.GL_TEXTURE_2D, texture_id)
texture = gpu.texture.from_image(image)

shader.bind()
# tell shader to use the image that is bound to image unit 0
shader.uniform_int("image", 0)
shader.uniform_sampler("image", texture)
batch.draw(shader)

bgl.glDisable(bgl.GL_TEXTURE_2D)
gpu.state.blend_set('NONE')


def draw_downloader(x, y, percent=0, img=None):
Expand All @@ -205,7 +196,7 @@ def draw_text(text, x, y, size, color=(1, 1, 1, 0.5)):
# bgl.glColor4f(*color)
blf.color(font_id, color[0], color[1], color[2], color[3])
blf.position(font_id, x, y, 0)
blf.size(font_id, size, 72)
blf.size(font_id, size)
blf.draw(font_id, text)


Expand Down
56 changes: 19 additions & 37 deletions draw/viewport.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import bgl
import gpu
from gpu_extras.batch import batch_for_shader

import math
import os
import numpy
Expand All @@ -15,16 +14,12 @@

NULL = 0


class TempfileManager:
_paths = {}

@classmethod
def track(cls, key, path):
try:
cls._paths[key].add(path)
except KeyError:
cls._paths[key] = {path}
cls._paths.setdefault(key, set()).add(path)

@classmethod
def delete_files(cls, key):
Expand All @@ -33,6 +28,7 @@ def delete_files(cls, key):
for path in cls._paths[key]:
if os.path.exists(path):
os.remove(path)
cls._paths.pop(key, None)

@classmethod
def cleanup(cls):
Expand All @@ -43,9 +39,7 @@ def cleanup(cls):
cls._paths.clear()


class FrameBuffer(object):
""" FrameBuffer used for viewport render """

class FrameBuffer:
def __init__(self, engine, context, scene):
filmsize = utils.calc_filmsize(scene, context)
self._width, self._height = filmsize
Expand Down Expand Up @@ -78,8 +72,7 @@ def __init__(self, engine, context, scene):
self._denoised_file_path = self._make_denoiser_filepath("denoised")
current_dir = dirname(os.path.realpath(__file__))
addon_dir = dirname(current_dir) # Go up one level
self._denoiser_path = which("oidnDenoise",
path=os.path.join(addon_dir, "bin") + os.pathsep + os.environ["PATH"])
self._denoiser_path = which("oidnDenoise", path=os.path.join(addon_dir, "bin") + os.pathsep + os.environ["PATH"])
self._denoiser_process = None
self.denoiser_result_cached = False

Expand All @@ -91,17 +84,16 @@ def _init_opengl(self):
(self._offset_x, self._offset_y),
(self._offset_x + width, self._offset_y),
(self._offset_x + width, self._offset_y + height),
(self._offset_x, self._offset_y + height)
(self._offset_x, self._offset_y + height),
(self._offset_x, self._offset_y),
(self._offset_x + width, self._offset_y + height)
)

self.shader = gpu.shader.from_builtin('2D_IMAGE')
self.batch = batch_for_shader(
self.shader, 'TRI_FAN',
{
"pos": position,
"texCoord": ((0, 0), (1, 0), (1, 1), (0, 1)),
},
)
self.shader = gpu.shader.from_builtin('IMAGE')
self.batch = batch_for_shader(self.shader, 'TRIS', {
"pos": position,
"texCoord": ((0, 0), (1, 0), (1, 1), (0, 1), (0, 0), (1, 1)),
})

def __del__(self):
del self.buffer
Expand All @@ -114,7 +106,6 @@ def needs_replacement(self, context, scene):
if self._transparent != scene.camera.data.luxcore.imagepipeline.transparent_film:
return True
elif self._transparent:
# By default (if no camera is available), the film is not transparent
return True
new_border = utils.calc_blender_border(scene, context)
if self._border != new_border:
Expand All @@ -126,20 +117,18 @@ def needs_replacement(self, context, scene):
return False

def _make_denoiser_filepath(self, name):
return os.path.join(tempfile.gettempdir(), str(id(self)) + "_" + name + ".pfm")
return os.path.join(tempfile.gettempdir(), f"{id(self)}_{name}.pfm")

def _save_denoiser_AOV(self, luxcore_session, film_output_type, path):
# Bufferdepth always 3 because denoiser can't handle alpha anyway (maybe copy over alpha channel in the future)
np_buffer = numpy.zeros((self._height, self._width, 3), dtype="float32")
luxcore_session.GetFilm().GetOutputFloat(film_output_type, np_buffer)
TempfileManager.track(id(self), path)
with open(path, "w+b") as f:
utils.pfm.save_pfm(f, np_buffer)
with open(path, "wb") as f:
pfm.save_pfm(f, np_buffer)

def start_denoiser(self, luxcore_session):
if not os.path.exists(self._denoiser_path):
raise Exception("Binary not found. Download it from "
"https://github.com/OpenImageDenoise/oidn/releases")
raise Exception("Binary not found. Download it from https://github.com/OpenImageDenoise/oidn/releases")
if self._transparent:
self._alpha = numpy.zeros((self._height, self._width, 1), dtype="float32")
luxcore_session.GetFilm().GetOutputFloat(pyluxcore.FilmOutputType.ALPHA, self._alpha)
Expand Down Expand Up @@ -169,15 +158,15 @@ def load_denoiser_result(self, scene):
shape = (self._height * self._width * 3)
try:
with open(self._denoised_file_path, "rb") as f:
data, scale = utils.pfm.load_pfm(f)
data, scale = pfm.load_pfm(f)
TempfileManager.delete_files(id(self))
except FileNotFoundError:
TempfileManager.delete_files(id(self))
raise Exception("Denoising failed, check console for details")

if self._transparent:
shape = (self._height * self._width * 4)
data = numpy.concatenate((data,self._alpha), axis=2)
data = numpy.concatenate((data, self._alpha), axis=2)

data = numpy.resize(data, shape)
self.buffer[:] = data
Expand All @@ -197,11 +186,7 @@ def update(self, luxcore_session, scene):
luxcore_session.GetFilm().GetOutputFloat(self._output_type, self.buffer)

def draw(self, engine, context, scene):
if self._transparent:
format = 'RGBA16F'
else:
format = 'RGB16F'

format = 'RGBA16F' if self._transparent else 'RGB16F'
image = gpu.types.GPUTexture(size=(self._width, self._height), layers=0, is_cubemap=False, format=format,
data=self.buffer)
self.shader.uniform_sampler("image", image)
Expand All @@ -218,9 +203,7 @@ def _calc_offset(self, context, scene, border):
border_min_x, border_max_x, border_min_y, border_max_y = border

if context.region_data.view_perspective == "CAMERA" and render.use_border:
# Offset is only needed if viewport is in camera mode and uses border rendering
sensor_fit = scene.camera.data.sensor_fit

aspectratio, aspect_x, aspect_y = utils.calc_aspect(
render.resolution_x * render.pixel_aspect_x,
render.resolution_y * render.pixel_aspect_y,
Expand All @@ -241,7 +224,6 @@ def _calc_offset(self, context, scene, border):
offset_x = region_width * border_min_x + 1
offset_y = region_height * border_min_y + 1

# offset_x, offset_y are in pixels
return int(offset_x), int(offset_y)

def _cam_border_offset(self, aspect, base, border_min, region_width, view_camera_offset, zoom):
Expand Down
Loading
Loading