Object info - material node: is it possible to map the object worldPosition with an UVmap?

all in the title :slight_smile:

There’s the MApping node but not sure how to use, nor to understand those 4 options (texture, point, vector, normal)

if im reading this right, you want to get a color value from a image using world position as a uv coord?

the way i get world position is get a vector transform node, set it to OBJECT > WORLD. plug an xyz combine on the IN and the texture to the OUT. you may need a mapping node after the transform and before the image to scale to fit enviro.

the xyz can be found by switch to blender render or cycles and adding. this used to get local position to be had in world. setting top (from) to CAMERA will get the location of the camera, super nice for planet atmosphere shaders.

also, you want to use POINT mode on everything since you dont want clipping values (normalized)

1 Like

sorry, i was not very clear, it’s more like i have my cube worldPosition and i want to know what it would be to travel on a texture given its uv map … it would looks like some Portal game if i dare the comparison :smile:
I made this yesterday. As you can see, i just map the worldLocation * Vector (to scale it to match the plane size) on a simple texture 2 space and the cube get’s in a whole the color of the single pixel he’s sat on (so the color switches , no transition)

… but i was wondering … can i use an “UV space” ? So basically instead of traveling on the texture 2d space to get its color , get its color by traveling in the real 3d space and get its color by matching his position with the UV map of the plane ?
In more simple words : can i make the cube get the color of the plane he’s laying on no matter the UV map of the plane ?

You can get the UV of a face with rayCast, use the UV to set maybe the color of the object and use a Object Data node to get the color and connect it to the UV Vector socket of the Texture node.

from bge import logic, render
from mathutils import Vector
from math import floor

cont = logic.getCurrentController()
own = cont.owner

prop = ""
xray = 0
range = 10.0

rayDir = own.worldPosition + Vector((0.0, 0.0, -1.0))
Ray = own.rayCast(rayDir, own, range, prop, 0, xray, 2)

if Ray[3]:
    u = Ray[4][0]
    v = Ray[4][1]
    # keep in range [0.0, 1.0]
    u -= floor(u)
    v -= floor(v)
    # the texture node uses uvs in the range [-1.0, 1.0]
    own.color = [u*2.0-1.0, v*2.0-1.0, 0.0, 1.0]
    own['u'] = u
    own['v'] = v
    render.drawLine(own.worldPosition, Ray[1], [0.0, 1.0, 0.0])

If it hits an object wihout uv, the uv will be (0.0, 0.0), so you may want to use a prop and xray.

rayCastUV.blend (92.0 KB)

1 Like

wow thanks ! :blush: BlueprintRandom and Lopas had gave me that solution with vertex color some time ago but this is even more easier since i can use just a simple texture . I was wondering if it would be possible to do it in a material setup with some world/uv mapping .

But at then end of the day, this would be fine enough since i already use a ground ray for other stuff … and even if it’s not point-shader based but affects the whole object, it’s already great for tuning an ambiant light response on a [0,1] scale

edit : wonderful it works on dirty uv too :slight_smile:

thanks a lot !

With some extra nodes shading can be easily achieved.

But the details on the textures become a flickering too noticeable, maybe with a blurred texture it would be less distracting, I tried using the Lod Bias in the texture node to access a lower level mipmap but it doesn’t appear to be working.
rayCastUV-shading.blend (106.7 KB)

1 Like

i was thinking about this : a 3d grid with baked AO on the vertex … with this, all i have to do is to vertex.getRBGA() on the 8 vertex around and make some average (since the vertex coord fit perfectly the world space , i dont even have to retrieve , ray or read an UV)

here for example with suzanne

so i can cover my whole map with that 3d grid and i have AO in a 3d space

yes just have to blur the texture to have it smooth as you said or made some temporal moving average. But the problem with that ray solution is when my character jump off a roof , there’s a sudden switch (ie : roof texture to grass ). If my character falls in very deep hole, he won’t get dark before hitting the bottom of the hole.

Looks great. Can you make something with a 3d grid and apply your material shading ?

I just extruded the default mesh grid 9 times , then deleted all the topping faces

for i in range(0,9) : 

I gonna use the RGB object color to compute an ambiant direction vector … and gonna use the Alpha channel to code the color (the H from the HSV)

the doc recommends to use a lib to unpack the RGB of the vertex but this is enough i guess

col = vert.getRGBA()
color = ( col % 256 , col // 256**1 % 256, col // 256**2 % 256 , col // 256**3 % 256 )

There’s no need to unpack, you can use the individual components of the vertex color:

color = (vert.r, vert.g, vert.b, vert.a)

Also, the color attribute is a Vector so it can take negative values.
I think a better option rather than the color attribute, would be a directional lamp (sun or hemi), with diffuse and specular disabled so it doesn’t impact the performance, this will give you get a direction vector and a color from the Lamp Data node.
The method for indirect lighting with Radiance Hints that I mentioned in another topic, uses 3d textures to store the direction of the indirect lighting and occlusion from the lamp. 3D textures are basically grids, unfortunatelly when baking the AO to the vertex color, it is not possible to bake the direction the occlusion is coming from.

yes, it’s indeed better since there’s more input provided

i was thinking to use the rgb object data node and center on 0.5 as you can see in the node setup … but having just the lamp data is better as you said