This also fixes glsl version of fmod when both inputs are negative. Differential Revision: https://developer.blender.org/D6704
113 lines
2.9 KiB
Plaintext
113 lines
2.9 KiB
Plaintext
/*
|
|
* Copyright 2011-2013 Blender Foundation
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
#include "stdosl.h"
|
|
|
|
float safe_divide(float a, float b)
|
|
{
|
|
return (b != 0.0) ? a / b : 0.0;
|
|
}
|
|
|
|
vector safe_divide(vector a, vector b)
|
|
{
|
|
return vector((b[0] != 0.0) ? a[0] / b[0] : 0.0,
|
|
(b[1] != 0.0) ? a[1] / b[1] : 0.0,
|
|
(b[2] != 0.0) ? a[2] / b[2] : 0.0);
|
|
}
|
|
|
|
vector project(vector v, vector v_proj)
|
|
{
|
|
float lenSquared = dot(v_proj, v_proj);
|
|
return (lenSquared != 0.0) ? (dot(v, v_proj) / lenSquared) * v_proj : vector(0.0);
|
|
}
|
|
|
|
vector snap(vector a, vector b)
|
|
{
|
|
return floor(safe_divide(a, b)) * b;
|
|
}
|
|
|
|
shader node_vector_math(string type = "add",
|
|
vector Vector1 = vector(0.0, 0.0, 0.0),
|
|
vector Vector2 = vector(0.0, 0.0, 0.0),
|
|
float Scale = 1.0,
|
|
output float Value = 0.0,
|
|
output vector Vector = vector(0.0, 0.0, 0.0))
|
|
{
|
|
if (type == "add") {
|
|
Vector = Vector1 + Vector2;
|
|
}
|
|
else if (type == "subtract") {
|
|
Vector = Vector1 - Vector2;
|
|
}
|
|
else if (type == "multiply") {
|
|
Vector = Vector1 * Vector2;
|
|
}
|
|
else if (type == "divide") {
|
|
Vector = safe_divide(Vector1, Vector2);
|
|
}
|
|
else if (type == "cross_product") {
|
|
Vector = cross(Vector1, Vector2);
|
|
}
|
|
else if (type == "project") {
|
|
Vector = project(Vector1, Vector2);
|
|
}
|
|
else if (type == "reflect") {
|
|
Vector = reflect(Vector1, normalize(Vector2));
|
|
}
|
|
else if (type == "dot_product") {
|
|
Value = dot(Vector1, Vector2);
|
|
}
|
|
else if (type == "distance") {
|
|
Value = distance(Vector1, Vector2);
|
|
}
|
|
else if (type == "length") {
|
|
Value = length(Vector1);
|
|
}
|
|
else if (type == "scale") {
|
|
Vector = Vector1 * Scale;
|
|
}
|
|
else if (type == "normalize") {
|
|
Vector = normalize(Vector1);
|
|
}
|
|
else if (type == "snap") {
|
|
Vector = snap(Vector1, Vector2);
|
|
}
|
|
else if (type == "floor") {
|
|
Vector = floor(Vector1);
|
|
}
|
|
else if (type == "ceil") {
|
|
Vector = ceil(Vector1);
|
|
}
|
|
else if (type == "modulo") {
|
|
Vector = fmod(Vector1, Vector2);
|
|
}
|
|
else if (type == "fraction") {
|
|
Vector = Vector1 - floor(Vector1);
|
|
}
|
|
else if (type == "absolute") {
|
|
Vector = abs(Vector1);
|
|
}
|
|
else if (type == "minimum") {
|
|
Vector = min(Vector1, Vector2);
|
|
}
|
|
else if (type == "maximum") {
|
|
Vector = max(Vector1, Vector2);
|
|
}
|
|
else {
|
|
warning("%s", "Unknown vector math operator!");
|
|
}
|
|
}
|