Screen Space Distortion for FX using render to texture

Camera 1 = aimed at plane

plane = video texture of camera 2 and also is distorted using kd tree to find points in radius

Camera 2 =aimed at main scene

when ‘Exposion’ happens in main scene, get screen position, use screen position to get screen vect in main camera, use to cast rays at plane, to use kd tree to put points on effect list, as well as set position.

Points are added to list [‘FX’] and is used with property ‘Run’ to run the script until all vertex are back to normal position.


ScreenSpaceDistortion.blend (711 KB)

Can you change the warp direction? I think having a swelling effect would look better for an explosion rather than pulling the texture inwards.

I am afk at the moment but I think scaling the plane to -1 on z will be a quick fix,

I will try and write it to do both later, and maybe a ripple wave,

edit: negative scale on screen +flip normals seems good. (have not checked to make sure orientations are correct) I will try and get to tinkering tonight*

I’m afk as well but is this in python or is it a shader

python :smiley:

I use render to texture on a plane, and use a KDtree to distort the screen,

Wait whats kd tree. Sorry if its a dumb question

mensional space. k-d trees are a useful data structure for several applications, such as searches involving a multidimensional search key (e.g. range searches and nearest neighbor searches). [I]k-d trees are a special case of binary space partitioning trees.)

Imagine building a list of all vertex that are in a radius, or finding the closest point, in python with a loop


for mesh in object.meshes:
    diff = ray.hitPoint-own.worldPosition
    localpoint = own.worldOrientation.inverted()*diff
    selectList =[]
    for m_index in range(len(mesh.materials)):
      for v_index in range(mesh.getVertexArrayLength(m_index)):
         vertex = mesh.getVertex(m_index, v_index)
         diffVect = vertex.XYZ-localpoint
         distance = diffVect.magnitude
         if distance < radius:


this is much slower than calling the kdtree that you build on frame zero, and then checking using that
(itterating using C, and also using an algorithm that does not traverse the whole point list*)

so it’s much faster, the more data there is to sort through that pertains to 3d space.

you can use a ‘worldKDTRee’ for LOD and physics LOD and Lamp LOD etc.
so long as the points are static that you are operating on (no need to rebalance the tree*)

in this example I build the tree with

#build kd tree

if 'KD' not in own:
    VLIST =[]
    for mesh in own.meshes:
        for m_index in range(len(mesh.materials)):
            for v_index in range(mesh.getVertexArrayLength(m_index)):
                vertex = mesh.getVertex(m_index, v_index)
    size = len(VLIST)
    kd = mathutils.kdtree.KDTree(size)
    index =0
    for Vert in VLIST:
        kd.insert(VLIST[index][1], index)



later any object can call that tree with target[‘KD’]
(so a vector can than be used with all these commands to access the data)

co = a vector
(like a ray hitPoint or a mouseOver hitPostion or a collision callback contact point location)

in this instance - I localize a point to the object that we then check using the local as the CO in the KDTree command

That’s very good use of the kdtree with the texture module, thanks

btw since you already know the object only have 1 mesh and 1 material you can extract the vertex faster by avoiding expensive function calls or attribute look up from inside the nested loops and better yet the loops


mesh = own.meshes[0]
mat_index = 0
vert_len = mesh.getVertexArrayLength(mat_index)
VLIST = [mesh.getVertex(mat_index, v_index) for v_index in range(vert_len)]

Great as usual, however the sudden FPS drop when monster is near is frustrating. Wait for a solution is developing best class Magento Extensions base on an great team.
Magento 2 Product Feed | Magento 2 Order Manager | Magento 2 Vtiger | Magento 2 SugarCRM | Magento 2 Odoo | Magento 2 One Step Checkout | Magento 2 Affiliate | Magento 2 Seo | Magento 2 Blog | Magento 2 Shop by Brand developed
Manual magento 2 : Magento 2 Tutorial