nortikin / sverchok

Sverchok
http://nortikin.github.io/sverchok/
GNU General Public License v3.0
2.23k stars 232 forks source link

Viewer Draw Extensions #4364

Open zeffii opened 2 years ago

zeffii commented 2 years ago

The object viewers are a versatile way to present Sverchok-controlled geometry to the Blender render systems. Cycles and Eevee are fantastic, but they require a relevant amount of computation to produce interactive and -ultimately- good-looking results.

The goal of Viewer Draw nodes, as i see it, is several-fold.

At the moment each VD triggers its own bgl/shader callback and does geometry "metadata" preprocessing, this has advantages and disadvantages. I won't enumerate these points exhaustively, some of the points are subjective and may appear on both sides depending your perspective.

After https://github.com/nortikin/sverchok/issues/4354 i had a couple of weeks to think about what can be done, and i keep coming back to the same ideas.

Anyone who's read the source of the VD's knows that essentially the batch/shader is relatively simple, and that by far most of the work is done ahead of rendering and that involves a lot of code to flow through of which only a small bit gets executed depending on the circumstances. It evolved as a function of imposed requirements, and i'll forgive anyone who thinks it's difficult to read - it is.

thoughts?

vicdoval commented 2 years ago

I like the ideas that this issue opens

vicdoval commented 2 years ago

Rendering edges from camera view. By the moment only edges

Generated Image

import bpy
import gpu
import bgl
import random
from mathutils import Matrix
from gpu_extras.presets import draw_circle_2d
from gpu.types import (
        GPUBatch,
        GPUVertBuf,
        GPUIndexBuf,
        GPUVertFormat,
    )
IMAGE_NAME = "Generated Image"
context = bpy.context
scene = context.scene

WIDTH = scene.render.resolution_x
HEIGHT = scene.render.resolution_y
offscreen = gpu.types.GPUOffScreen(WIDTH, HEIGHT)
verts = bpy.data.node_groups["NodeTree"].nodes["Viewer Draw.001"].inputs[0].sv_get()[0]
edges = bpy.data.node_groups["NodeTree"].nodes["Viewer Draw.001"].inputs[1].sv_get()[0]

with offscreen.bind():
    background_color = (0.0, 0.0, 0.0, 1.0)
    bgl.glClearColor(background_color)
    bgl.glClear(bgl.GL_COLOR_BUFFER_BIT)
    with gpu.matrix.push_pop():
        # reset matrices -> use normalized device coordinates [-1, 1]

        view_matrix = scene.camera.matrix_world.inverted()

        projection_matrix = scene.camera.calc_matrix_camera(
            context.evaluated_depsgraph_get(), x=WIDTH, y=HEIGHT)
        gpu.matrix.load_matrix(view_matrix)
        gpu.matrix.load_projection_matrix(projection_matrix)
        fmt = GPUVertFormat()
        pos_id = fmt.attr_add(id="pos", comp_type='F32', len=3, fetch_mode='FLOAT')
        vbo = GPUVertBuf(len=len(verts), format=fmt)
        eds= GPUIndexBuf(type='LINES',seq=edges)
        vbo.attr_fill(id=pos_id, data=verts)
        batch = GPUBatch(type='LINES', buf=vbo, elem=eds)
        shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
        batch.program_set(shader)
        edges_color= (1, 1, 1, 1)
        shader.uniform_float("color", edges_color)
        batch.draw()

    buffer = bgl.Buffer(bgl.GL_BYTE, WIDTH * HEIGHT * 4)
    bgl.glReadBuffer(bgl.GL_BACK)
    bgl.glReadPixels(0, 0, WIDTH, HEIGHT, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, buffer)

offscreen.free()

if not IMAGE_NAME in bpy.data.images:
    bpy.data.images.new(IMAGE_NAME, WIDTH, HEIGHT)
image = bpy.data.images[IMAGE_NAME]
image.scale(WIDTH, HEIGHT)
image.pixels = [v / 255 for v in buffer]

Now, how to get some AA? Best way to integrate it with the UI? and to export animations? But it is already usable!! Generated Image

zeffii commented 2 years ago

i didn't see this post until just now. How to get AA indeed!

zeffii commented 2 years ago

@vicdoval haha

image

no live updating yet..and a lot of things are not implemented..

code

vicdoval commented 2 years ago

Are you doing something like the SVG document node but with three.js???? 😮

zeffii commented 2 years ago

lol. yes :)

parking some techniques here.. https://r105.threejsfundamentals.org/threejs/lessons/threejs-cleanup.html