Shading: Add color output to White Noise node

Hash input values to a color.

Differential Revision: https://developer.blender.org/D6672
This commit is contained in:
Charlie Jolly 2020-01-27 15:04:49 +00:00
parent 84c537e685
commit 20e803ac6b
5 changed files with 71 additions and 27 deletions

View File

@ -15,23 +15,33 @@
*/
#include "stdosl.h"
#include "vector2.h"
#include "vector4.h"
#include "node_hash.h"
#define vector3 point
shader node_white_noise_texture(string dimensions = "3D",
point Vector = point(0.0, 0.0, 0.0),
float W = 0.0,
output float Value = 0.0)
output float Value = 0.0,
output color Color = 0.0)
{
if (dimensions == "1D") {
Value = noise("hash", W);
Color = hash_float_to_color(W);
}
else if (dimensions == "2D") {
Value = noise("hash", Vector[0], Vector[1]);
Color = hash_vector2_to_color(vector2(Vector[0], Vector[1]));
}
else if (dimensions == "3D") {
Value = noise("hash", Vector);
Color = hash_vector3_to_color(vector3(Vector[0], Vector[1], Vector[2]));
}
else if (dimensions == "4D") {
Value = noise("hash", Vector, W);
Color = hash_vector4_to_color(vector4(Vector[0], Vector[1], Vector[2], W));
}
else {
warning("%s", "Unknown dimension!");

View File

@ -21,35 +21,61 @@ ccl_device void svm_node_tex_white_noise(KernelGlobals *kg,
float *stack,
uint dimensions,
uint inputs_stack_offsets,
uint value_stack_offset,
uint ouptuts_stack_offsets,
int *offset)
{
uint vector_stack_offset, w_stack_offset;
uint vector_stack_offset, w_stack_offset, value_stack_offset, color_stack_offset;
svm_unpack_node_uchar2(inputs_stack_offsets, &vector_stack_offset, &w_stack_offset);
svm_unpack_node_uchar2(ouptuts_stack_offsets, &value_stack_offset, &color_stack_offset);
float3 vector = stack_load_float3(stack, vector_stack_offset);
float w = stack_load_float(stack, w_stack_offset);
float value;
switch (dimensions) {
case 1:
value = hash_float_to_float(w);
break;
case 2:
value = hash_float2_to_float(make_float2(vector.x, vector.y));
break;
case 3:
value = hash_float3_to_float(vector);
break;
case 4:
value = hash_float4_to_float(make_float4(vector.x, vector.y, vector.z, w));
break;
default:
value = 0.0f;
kernel_assert(0);
break;
if (stack_valid(color_stack_offset)) {
float3 color;
switch (dimensions) {
case 1:
color = hash_float_to_float3(w);
break;
case 2:
color = hash_float2_to_float3(make_float2(vector.x, vector.y));
break;
case 3:
color = hash_float3_to_float3(vector);
break;
case 4:
color = hash_float4_to_float3(make_float4(vector.x, vector.y, vector.z, w));
break;
default:
color = make_float3(1.0f, 0.0f, 1.0f);
kernel_assert(0);
break;
}
stack_store_float3(stack, color_stack_offset, color);
}
if (stack_valid(value_stack_offset)) {
float value;
switch (dimensions) {
case 1:
value = hash_float_to_float(w);
break;
case 2:
value = hash_float2_to_float(make_float2(vector.x, vector.y));
break;
case 3:
value = hash_float3_to_float(vector);
break;
case 4:
value = hash_float4_to_float(make_float4(vector.x, vector.y, vector.z, w));
break;
default:
value = 0.0f;
kernel_assert(0);
break;
}
stack_store_float(stack, value_stack_offset, value);
}
stack_store_float(stack, value_stack_offset, value);
}
CCL_NAMESPACE_END

View File

@ -1288,6 +1288,7 @@ NODE_DEFINE(WhiteNoiseTextureNode)
SOCKET_IN_FLOAT(w, "W", 0.0f);
SOCKET_OUT_FLOAT(value, "Value");
SOCKET_OUT_COLOR(color, "Color");
return type;
}
@ -1301,15 +1302,17 @@ void WhiteNoiseTextureNode::compile(SVMCompiler &compiler)
ShaderInput *vector_in = input("Vector");
ShaderInput *w_in = input("W");
ShaderOutput *value_out = output("Value");
ShaderOutput *color_out = output("Color");
int vector_stack_offset = compiler.stack_assign(vector_in);
int w_stack_offset = compiler.stack_assign(w_in);
int value_stack_offset = compiler.stack_assign(value_out);
int color_stack_offset = compiler.stack_assign(color_out);
compiler.add_node(NODE_TEX_WHITE_NOISE,
dimensions,
compiler.encode_uchar4(vector_stack_offset, w_stack_offset),
value_stack_offset);
compiler.encode_uchar4(value_stack_offset, color_stack_offset));
}
void WhiteNoiseTextureNode::compile(OSLCompiler &compiler)

View File

@ -1,21 +1,25 @@
/* White Noise */
void node_white_noise_1d(vec3 vector, float w, out float value)
void node_white_noise_1d(vec3 vector, float w, out float value, out vec4 color)
{
value = hash_float_to_float(w);
color.xyz = hash_float_to_vec3(w);
}
void node_white_noise_2d(vec3 vector, float w, out float value)
void node_white_noise_2d(vec3 vector, float w, out float value, out vec4 color)
{
value = hash_vec2_to_float(vector.xy);
color.xyz = hash_vec2_to_vec3(vector.xy);
}
void node_white_noise_3d(vec3 vector, float w, out float value)
void node_white_noise_3d(vec3 vector, float w, out float value, out vec4 color)
{
value = hash_vec3_to_float(vector);
color.xyz = hash_vec3_to_vec3(vector);
}
void node_white_noise_4d(vec3 vector, float w, out float value)
void node_white_noise_4d(vec3 vector, float w, out float value, out vec4 color)
{
value = hash_vec4_to_float(vec4(vector, w));
color.xyz = hash_vec4_to_vec3(vec4(vector, w));
}

View File

@ -28,6 +28,7 @@ static bNodeSocketTemplate sh_node_tex_white_noise_in[] = {
static bNodeSocketTemplate sh_node_tex_white_noise_out[] = {
{SOCK_FLOAT, 0, N_("Value")},
{SOCK_RGBA, 0, N_("Color")},
{-1, 0, ""},
};