0
$\begingroup$

I want to get a unique random value for every face using direction of mesh coordinates. Like a derivative, position change, a unit vector based on current face's vertices. (I guess, something similar to dFdx() in OpenGL.)

I tried to find the answer on Google, but got no results, though as a software developer I think this function isn't difficult to create. Maybe some Blender experts here know the solution?

$\endgroup$
  • $\begingroup$ Bake world position to a texture image, then do a two lookups on that texture, one at a UV offset, to get ddx(UV)/ddx(worldpos). $\endgroup$ – Nathan Jul 17 '18 at 17:24
1
$\begingroup$

Not strictly an answer to your question, but..

To get a random value per face to be read by Cycles, you could use an adaptation of Chebhou's answer to this question.

Run this script on the active object:

import bpy
import random

obj = bpy.context.object
obj_data = obj.data
group_name = 'face_ID'

#check for existing group with the same name
if None == obj_data.vertex_colors.get(group_name): 
    obj_data.vertex_colors.new(name=group_name)
color_map =  obj_data.vertex_colors[group_name]

for poly in obj_data.polygons:
    rnd=random.random()
    color = (rnd, rnd, rnd,0)
    for loop_ind in poly.loop_indices:
        color_map.data[loop_ind].color = color 

and then use the Cycles 'Attribute' node to retrieve the generated vertex color layer by name:

enter image description here

result of this example on a subdivided plane:

enter image description here

I've moved the randomization into the script - I was having trouble getting a good pseudo-random number out of a 0.0-1.0 input range using nodes. If nobody points out something obvious in a comment, I'll post that as another question.

| improve this answer | |
$\endgroup$
  • $\begingroup$ I haven't yet worked with Python scripts in Blender, but will start it with your code :) But my answer (use normals) is closer to geometry, and I will use it if won't find an answer to my other question. And thanks for your reply! $\endgroup$ – AivanF. Jul 17 '18 at 19:24
0
$\begingroup$

I just realised that I can use normal to achieve almost the same behaviour – it is perpendicular to the direction of a face, so, it is an almost isomorphic value that gives some unique value for a face. But I will appreciate any better answer.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.