Geometry Nodes: Add Random Rotation node #109846

Closed
Colin Basnett wants to merge 5 commits from cmbasnett/bdk-blender:geometry-nodes-random-value-quaternion into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
Member

This adds a Random Rotation node as part of task: #92967.

image

The algorithm used generates uniformly distributed unit quaternions and is based on this paper:

Effective Sampling and Distance Metrics for 3D Rigid Body Path Planning (2004) - James J. Kuffner
https://www.ri.cmu.edu/pub_files/pub4/kuffner_james_2004_1/kuffner_james_2004_1.pdf

This adds a `Random Rotation` node as part of task: https://projects.blender.org/blender/blender/issues/92967. ![image](/attachments/1a6deeef-e677-4260-9cd1-bd9dd47f645f) The algorithm used generates uniformly distributed unit quaternions and is based on this paper: Effective Sampling and Distance Metrics for 3D Rigid Body Path Planning (2004) - James J. Kuffner https://www.ri.cmu.edu/pub_files/pub4/kuffner_james_2004_1/kuffner_james_2004_1.pdf
Colin Basnett added the
Module
Nodes & Physics
label 2023-07-08 05:47:14 +02:00
Colin Basnett requested review from Hans Goudey 2023-07-08 06:25:52 +02:00
Iliya Katushenock added this to the Nodes & Physics project 2023-07-08 14:12:38 +02:00
Iliya Katushenock added the
Interest
Geometry Nodes
label 2023-07-08 14:12:44 +02:00

While the rotation socket is an experimental feature, it shouldn't be visible in the item list unless the feature is enabled. It seems that for now your socket is available without regard to experimentation. You can find examples of how to implement this in #109084 / #109517.

While the rotation socket is an experimental feature, it shouldn't be visible in the item list unless the feature is enabled. It seems that for now your socket is available without regard to experimentation. You can find examples of how to implement this in https://projects.blender.org/blender/blender/pulls/109084 / https://projects.blender.org/blender/blender/pulls/109517.
Iliya Katushenock added the
Interest
Animation & Rigging
label 2023-07-08 15:32:11 +02:00
Colin Basnett added 17 commits 2023-07-09 01:01:46 +02:00
Colin Basnett force-pushed geometry-nodes-random-value-quaternion from a575570de5 to 1154803026 2023-07-09 01:15:43 +02:00 Compare
Author
Member

While the rotation socket is an experimental feature, it shouldn't be visible in the item list unless the feature is enabled. It seems that for now your socket is available without regard to experimentation. You can find examples of how to implement this in #109084 / #109517.

I have added handling to respect the experimental flag.

> While the rotation socket is an experimental feature, it shouldn't be visible in the item list unless the feature is enabled. It seems that for now your socket is available without regard to experimentation. You can find examples of how to implement this in https://projects.blender.org/blender/blender/pulls/109084 / https://projects.blender.org/blender/blender/pulls/109517. I have added handling to respect the experimental flag.
Iliya Katushenock reviewed 2023-07-09 01:21:52 +02:00
@ -2220,0 +2217,4 @@
const eCustomDataType data_type = eCustomDataType(item->value);
if (U.experimental.use_rotation_socket && data_type == CD_PROP_QUATERNION) {
const bNodeTree* tree = reinterpret_cast<const bNodeTree*>(ptr->owner_id);
if (tree->type == NTREE_GEOMETRY) {

This check is a bit redundant, this node is not used outside of geometry nodes (Mix node used and in shaders...).

This check is a bit redundant, this node is not used outside of geometry nodes (Mix node used and in shaders...).
Colin Basnett added 1 commit 2023-07-09 02:16:42 +02:00
Member

We talked about this in the recent module meeting. There was more to it than I expected, sorry for misleading you a bit earlier.

It’s not clear that the random value node handle this at all, since it’s relatively different from the more basic “random value” types. There are likely multiple ways to generate a random rotation, and we didn’t add colors to the node for that reason.

A node group that does the same thing would be helpful to see. This would be an easy thing to ship as an “Essentials” asset. Adding assets one at a time is okay in cases like this, especially when we would add it as a builtin node otherwise.

So, I think to move forward with this feature, it might be best to port the math to nodes and see how this works as a "Random Rotation" node group asset. Unfortunately the essentials repository is still in SVN, so we can't make pull requests for it yet.

We talked about this in the recent [module meeting](https://devtalk.blender.org/t/2023-07-25-nodes-physics-module-meeting/30443). There was more to it than I expected, sorry for misleading you a bit earlier. >It’s not clear that the random value node handle this at all, since it’s relatively different from the more basic “random value” types. There are likely multiple ways to generate a random rotation, and we didn’t add colors to the node for that reason. >A node group that does the same thing would be helpful to see. This would be an easy thing to ship as an “Essentials” asset. Adding assets one at a time is okay in cases like this, especially when we would add it as a builtin node otherwise. So, I think to move forward with this feature, it might be best to port the math to nodes and see how this works as a "Random Rotation" node group asset. Unfortunately the essentials repository is still in SVN, so we can't make pull requests for it yet.
Author
Member

So, I think to move forward with this feature, it might be best to port the math to nodes and see how this works as a "Random Rotation" node group asset. Unfortunately the essentials repository is still in SVN, so we can't make pull requests for it yet.

I don't think that, in this case at least, a basic operation like getting a random rotation should be an asset that has to be linked in. How is a script author expected to add these nodes? I am doing a lot of heavy node scripting at the moment and I would consider it very inconvenient to have to go and load basic math functions from an asset bundle.

> So, I think to move forward with this feature, it might be best to port the math to nodes and see how this works as a "Random Rotation" node group asset. Unfortunately the essentials repository is still in SVN, so we can't make pull requests for it yet. I don't think that, in this case at least, a basic operation like getting a random rotation should be an asset that has to be linked in. How is a script author expected to add these nodes? I am doing a lot of heavy node scripting at the moment and I would consider it very inconvenient to have to go and load basic math functions from an asset bundle.
Member

The convenience of adding an asset through a script is fairly tangential to the decision here-- we can always make that more convenient, in fact this may be a good reason to look into that.

Other than that, I don't think there's any reason to avoid using assets even for "basic operations" (though I'd argue this isn't that basic, since there are potentially many ways of generating a random rotation). The asset bundle is called "Essentials" after all.

The convenience of adding an asset through a script is fairly tangential to the decision here-- we can always make that more convenient, in fact this may be a good reason to look into that. Other than that, I don't think there's any reason to avoid using assets even for "basic operations" (though I'd argue this isn't _that_ basic, since there are potentially many ways of generating a random rotation). The asset bundle is called "Essentials" after all.
Member

I agree with @HooglyBoogly that baking a single "blessed" approach for this into geometry nodes maybe isn't the best idea. More thoughts below.

In the world of path tracing, sampling is generally split into two separate concepts:

  1. Generating uniform samples in the unit hypercube.
  2. Mapping that unit hypercube to the space you actually want the samples in.

So, for example, generating samples inside a disc is broken into two steps: 1. generating uniform samples in the unit square, and 2. remapping those samples to the unit disc.

The reason for this split is two-fold:

  • There are multiple ways you might want to generate samples. For example, a random number generator vs a low discrepancy sequence vs a blue noise distribution, etc. Being able to mix-and-match those sample generating techniques with any mapping is really useful.
  • There are often multiple competing mappings with different trade offs. For example, even for the seemingly simple case of mapping from a square to a disc there are radial, conformal, and many other mappings. And which one is appropriate depends to some extent on what you're doing.

The paper you based your implementation on provides one possible way of mapping the unit cube to the surface of the 4D unit hypersphere. But there are certainly other mappings that may be more/less appropriate depending on circumstances.

Having said all of that, I don't know if I necessarily agree that these should be entirely left up to node groups. (Although, admittedly, I'm not part of the geonodes module, so feel free to ignore.) But I do think that it should at least be split up into smaller components, like various sample generators and various mappings so that people can mix and match appropriately. And if those components were built-ins, then it would be at most two nodes to get the same result as the single node in this PR.

I agree with @HooglyBoogly that baking a single "blessed" approach for this into geometry nodes maybe isn't the best idea. More thoughts below. In the world of path tracing, sampling is generally split into two separate concepts: 1. Generating uniform samples in the unit hypercube. 2. Mapping that unit hypercube to the space you actually want the samples in. So, for example, generating samples inside a disc is broken into two steps: 1. generating uniform samples in the unit square, and 2. remapping those samples to the unit disc. The reason for this split is two-fold: - There are multiple ways you might want to generate samples. For example, a random number generator vs a low discrepancy sequence vs a blue noise distribution, etc. Being able to mix-and-match those sample generating techniques with any mapping is really useful. - There are often multiple competing mappings with different trade offs. For example, even for the seemingly simple case of mapping from a square to a disc there are radial, conformal, and many other mappings. And which one is appropriate depends to some extent on what you're doing. The paper you based your implementation on provides one possible way of mapping the unit cube to the surface of the 4D unit hypersphere. But there are certainly other mappings that may be more/less appropriate depending on circumstances. Having said all of that, I don't know if I necessarily agree that these should be entirely left up to node groups. (Although, admittedly, I'm not part of the geonodes module, so feel free to ignore.) But I do think that it should at least be split up into smaller components, like various sample generators and various mappings so that people can mix and match appropriately. And if those components were built-ins, then it would be at most two nodes to get the same result as the single node in this PR.

The problem for me is that I just don't know why a more general kind of random rotation operation is needed.
The case @nathanvegdahl ' describe is more like ray tracing node group. Or cases where you initially generate different euler angles (as a more geometric view) of rotations to randomize the model parts.
But if reduce all variations to simple noise, then I simply do not see the need for more control.
The generation of a vector can, in theory, be split as well, since its components can have different patterns or distributions in their ranges.
So I would take it simply as the most simple and fast function.

The problem for me is that I just don't know why a more general kind of random rotation operation is needed. The case @nathanvegdahl ' describe is more like ray tracing node group. Or cases where you initially generate different euler angles (as a more geometric view) of rotations to randomize the model parts. But if reduce all variations to simple noise, then I simply do not see the need for more control. The generation of a vector can, in theory, be split as well, since its components can have different patterns or distributions in their ranges. So I would take it simply as the most simple and fast function.
Member

The problem for me is that I just don't know why a more general kind of random rotation operation is needed.

For pure white-noise-style random numbers, you probably don't. In that case I suspect(?) the different mapping methods make no meaningful difference.

Where the different mapping methods definitely do make a difference is when you start using something like a blue noise distribution or other point generation methods. And for a lot of applications (including, I suspect, in many geometry node set ups) a more evenly distributed set of points like that is often exactly what you want.

The case @nathanvegdahl ' describe is more like ray tracing node group.

To clarify: I referenced path tracing since that's the field I learned these things from. But it has nothing to do with ray tracing. It's just about generating point samples (of which rotations are an example) in various useful ways.

> The problem for me is that I just don't know why a more general kind of random rotation operation is needed. For pure white-noise-style random numbers, you probably don't. In that case I suspect(?) the different mapping methods make no meaningful difference. Where the different mapping methods definitely *do* make a difference is when you start using something like a blue noise distribution or other point generation methods. And for a lot of applications (including, I suspect, in many geometry node set ups) a more evenly distributed set of points like that is often exactly what you want. > The case @nathanvegdahl ' describe is more like ray tracing node group. To clarify: I referenced path tracing since that's the field I learned these things from. But it has nothing to do with ray tracing. It's just about generating point samples (of which rotations are an example) in various useful ways.
Colin Basnett added 1 commit 2023-08-26 21:25:52 +02:00
b88012e6d8 Merge branch 'main' into geometry-nodes-random-value-quaternion
# Conflicts:
#	source/blender/nodes/function/nodes/node_fn_random_value.cc
Colin Basnett added 1 commit 2023-08-26 22:09:03 +02:00
Colin Basnett changed title from Geometry Nodes: Add Quaternion data type to Random Value node to Geometry Nodes: Add Random Rotation node 2023-08-26 22:09:05 +02:00
Author
Member

@HooglyBoogly I've updated this to make the Random Rotation a separate node. It is still done in native code, as I really think that Random Rotation is equally as foundational and in the same mental category as generating a Random Vector, which is also a native operation at this moment.

I also tried converting this algorithm to a node group, but I don't think it's even possible because the algorithm requires temporary variables to be stored, and the order of the noise samples is important. The only way to store temporary variables is within Geometry attributes, but that would be incredibly inefficient.

const float s = noise::hash_to_float(seed, id, 0);
const float sigma1 = math::sqrt(1.0f - s);
const float sigma2 = math::sqrt(s);
const float theta1 = M_PI * 2 * noise::hash_to_float(seed, id, 1);
const float theta2 = M_PI * 2 * noise::hash_to_float(seed, id, 2);
const float w = math::cos(theta2) * sigma2;
const float x = math::sin(theta1) * sigma1;
const float y = math::cos(theta1) * sigma1;
const float z = math::sin(theta2) * sigma2;
return math::Quaternion(w, x, y, z);
@HooglyBoogly I've updated this to make the Random Rotation a separate node. It is still done in native code, as I really think that Random Rotation is equally as foundational and in the same mental category as generating a Random Vector, which is also a native operation at this moment. I also tried converting this algorithm to a node group, but I don't think it's even possible because the algorithm requires temporary variables to be stored, and the order of the noise samples is important. The only way to store temporary variables is within Geometry attributes, but that would be incredibly inefficient. ```Cpp const float s = noise::hash_to_float(seed, id, 0); const float sigma1 = math::sqrt(1.0f - s); const float sigma2 = math::sqrt(s); const float theta1 = M_PI * 2 * noise::hash_to_float(seed, id, 1); const float theta2 = M_PI * 2 * noise::hash_to_float(seed, id, 2); const float w = math::cos(theta2) * sigma2; const float x = math::sin(theta1) * sigma1; const float y = math::cos(theta1) * sigma1; const float z = math::sin(theta2) * sigma2; return math::Quaternion(w, x, y, z); ```

I'm still left with the idea that this operation is simple enough to be part of a common random node. This also corresponds to the idea of separating nodes not by types, but by operations.

I'm still left with the idea that this operation is simple enough to be part of a common random node. This also corresponds to the idea of separating nodes not by types, but by operations.
First-time contributor

I also tried converting this algorithm to a node group

Here's an implementation of that algorithm:

random_quaternion_nodes.png

One issue is that exposing the ID input of the random value node causes it default to zero instead of the implicit ID field, making all values uniform:

default_ID_issue.png

> I also tried converting this algorithm to a node group Here's an implementation of that algorithm: ![random_quaternion_nodes.png](/attachments/9ebdf9d0-18bc-4144-a92f-f93100307e8c) One issue is that exposing the ID input of the random value node causes it default to zero instead of the implicit ID field, making all values uniform: ![default_ID_issue.png](/attachments/e8b8df9e-18f4-4892-8da3-e1bcd174feca)
Member

For reference, I previously created a similar node Add Random Spherical Distribution node which had controls to limit the radius, angles. (See https://archive.blender.org/developer/D12746). This was abandoned at the last minute in favour of a more generic approach and shipping it as a node group.

As a result a task was created Nodes to convert between cartesian/spherical coordinates by @SimonThommes (See https://archive.blender.org/developer/T94722). I picked this up (see https://archive.blender.org/developer/D13772) before it was picked up again by @guitargeek (see https://archive.blender.org/developer/D15653).

Hopefully it would be good to see a built in solution soon.

Euler based distribution example.

image

For reference, I previously created a similar node *Add Random Spherical Distribution node* which had controls to limit the radius, angles. (See https://archive.blender.org/developer/D12746). This was abandoned at the last minute in favour of a more generic approach and shipping it as a node group. As a result a task was created *Nodes to convert between cartesian/spherical coordinates* by @SimonThommes (See https://archive.blender.org/developer/T94722). I picked this up (see https://archive.blender.org/developer/D13772) before it was picked up again by @guitargeek (see https://archive.blender.org/developer/D15653). Hopefully it would be good to see a built in solution soon. Euler based distribution example. ![image](/attachments/6d6b9021-6c61-4aca-aec1-9a5cd68f8638)
Member

After 20d8f27ed2 we have all the tools we need to add this as a node group asset, so I'll close this PR.

After 20d8f27ed2d72ec4bb7277b278c974de8644125d we have all the tools we need to add this as a node group asset, so I'll close this PR.
Hans Goudey closed this pull request 2023-10-31 12:06:04 +01:00

Pull request closed

Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No Assignees
6 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#109846
No description provided.