[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [51454] trunk/blender/intern/cycles: Cycles OSL: light path, texture coordinate, bump and blended box mapping now up

Brecht Van Lommel brechtvanlommel at pandora.be
Sat Oct 20 17:09:36 CEST 2012


Revision: 51454
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=51454
Author:   blendix
Date:     2012-10-20 15:09:36 +0000 (Sat, 20 Oct 2012)
Log Message:
-----------
Cycles OSL: light path, texture coordinate, bump and blended box mapping now up
to date and working.

Modified Paths:
--------------
    trunk/blender/intern/cycles/kernel/kernel_types.h
    trunk/blender/intern/cycles/kernel/osl/nodes/CMakeLists.txt
    trunk/blender/intern/cycles/kernel/osl/nodes/node_bump.osl
    trunk/blender/intern/cycles/kernel/osl/nodes/node_image_texture.osl
    trunk/blender/intern/cycles/kernel/osl/nodes/node_texture_coordinate.osl
    trunk/blender/intern/cycles/kernel/osl/osl_services.cpp
    trunk/blender/intern/cycles/kernel/svm/svm_image.h
    trunk/blender/intern/cycles/render/nodes.cpp
    trunk/blender/intern/cycles/render/osl.cpp

Added Paths:
-----------
    trunk/blender/intern/cycles/kernel/osl/nodes/node_set_normal.osl

Modified: trunk/blender/intern/cycles/kernel/kernel_types.h
===================================================================
--- trunk/blender/intern/cycles/kernel/kernel_types.h	2012-10-20 15:09:27 UTC (rev 51453)
+++ trunk/blender/intern/cycles/kernel/kernel_types.h	2012-10-20 15:09:36 UTC (rev 51454)
@@ -152,9 +152,7 @@
 	PRNG_BOUNCE_NUM = 8
 };
 
-/* these flag values correspond exactly to OSL defaults, so be careful not to
- * change this, or if you do, set the "raytypes" shading system attribute with
- * your own new ray types and bitflag values.
+/* these flags values correspond to raytypes in osl.cpp, so keep them in sync!
  *
  * for ray visibility tests in BVH traversal, the upper 20 bits are used for
  * layer visibility tests. */

Modified: trunk/blender/intern/cycles/kernel/osl/nodes/CMakeLists.txt
===================================================================
--- trunk/blender/intern/cycles/kernel/osl/nodes/CMakeLists.txt	2012-10-20 15:09:27 UTC (rev 51453)
+++ trunk/blender/intern/cycles/kernel/osl/nodes/CMakeLists.txt	2012-10-20 15:09:36 UTC (rev 51454)
@@ -48,6 +48,7 @@
 	node_particle_info.osl
 	node_rgb_ramp.osl
 	node_separate_rgb.osl
+	node_set_normal.osl
 	node_sky_texture.osl
 	node_texture_coordinate.osl
 	node_translucent_bsdf.osl

Modified: trunk/blender/intern/cycles/kernel/osl/nodes/node_bump.osl
===================================================================
--- trunk/blender/intern/cycles/kernel/osl/nodes/node_bump.osl	2012-10-20 15:09:27 UTC (rev 51453)
+++ trunk/blender/intern/cycles/kernel/osl/nodes/node_bump.osl	2012-10-20 15:09:36 UTC (rev 51454)
@@ -22,25 +22,28 @@
  * Morten S. Mikkelsen, 2010 */
 
 surface node_bump(
+	normal NormalIn = N,
+	float Strength = 0.0,
 	float SampleCenter = 0.0,
 	float SampleX = 0.0,
 	float SampleY = 0.0,
 	output normal Normal = N)
 {
-	float dx = SampleX - SampleCenter;
-	float dy = SampleY - SampleCenter;
-
+	/* get surface tangents from normal */
 	vector dPdx = Dx(P);
 	vector dPdy = Dy(P);
 
-	vector Rx = cross(dPdy, N);
-	vector Ry = cross(N, dPdx);
+	vector Rx = cross(dPdy, NormalIn);
+	vector Ry = cross(NormalIn, dPdx);
 
+	/* compute surface gradient and determinant */
 	float det = dot(dPdx, Rx);
-	vector surfgrad = dx * Rx + dy * Ry;
+	vector surfgrad = (SampleX - SampleCenter) * Rx + (SampleY - SampleCenter) * Ry;
 
-	surfgrad *= 0.1; /* todo: remove this factor */
+	surfgrad *= Strength;
+	float absdet = fabs(det);
 	
-	Normal = normalize(abs(det) * N - sign(det) * surfgrad);
+	/* compute and output perturbed normal */
+	Normal = normalize(absdet * NormalIn - sign(det) * surfgrad);
 }
 

Modified: trunk/blender/intern/cycles/kernel/osl/nodes/node_image_texture.osl
===================================================================
--- trunk/blender/intern/cycles/kernel/osl/nodes/node_image_texture.osl	2012-10-20 15:09:27 UTC (rev 51453)
+++ trunk/blender/intern/cycles/kernel/osl/nodes/node_image_texture.osl	2012-10-20 15:09:36 UTC (rev 51454)
@@ -19,16 +19,102 @@
 #include "stdosl.h"
 #include "node_color.h"
 
+color image_texture_lookup(string filename, string color_space, float u, float v, output float Alpha)
+{
+	color rgb = (color)texture(filename, u, 1.0 - v, "wrap", "periodic", "alpha", Alpha);
+
+	if(color_space == "sRGB")
+		rgb = color_srgb_to_scene_linear(rgb);
+
+	return rgb;
+}
+
 shader node_image_texture(
 	point Vector = P,
 	string filename = "",
 	string color_space = "sRGB",
+	string projection = "Flat",
+	float projection_blend = 0.0,
 	output color Color = color(0.0, 0.0, 0.0),
 	output float Alpha = 1.0)
 {
-	Color = (color)texture(filename, Vector[0], 1.0 - Vector[1], "wrap", "periodic", "alpha", Alpha);
+	if(projection == "Flat") {
+		Color = image_texture_lookup(filename, color_space, Vector[0], Vector[1], Alpha);
+	}
+	else if(projection == "Box") {
+		/* object space normal */
+		vector Nob = transform("world", "object", N);
 
-	if (color_space == "sRGB")
-		Color = color_srgb_to_scene_linear(Color);
+		/* project from direction vector to barycentric coordinates in triangles */
+		Nob = vector(fabs(Nob[0]), fabs(Nob[1]), fabs(Nob[2]));
+		Nob /= (Nob[0] + Nob[1] + Nob[2]);
+
+		/* basic idea is to think of this as a triangle, each corner representing
+		 * one of the 3 faces of the cube. in the corners we have single textures,
+		 * in between we blend between two textures, and in the middle we a blend
+		 * between three textures.
+		 *
+		 * the Nxyz values are the barycentric coordinates in an equilateral
+		 * triangle, which in case of blending, in the middle has a smaller
+		 * equilateral triangle where 3 textures blend. this divides things into
+		 * 7 zones, with an if() test for each zone */
+
+		vector weight = vector(0.0, 0.0, 0.0);
+		float blend = projection_blend;
+		float limit = 0.5*(1.0 + blend);
+
+		/* first test for corners with single texture */
+		if(Nob[0] > limit*(Nob[0] + Nob[1]) && Nob[0] > limit*(Nob[0] + Nob[2])) {
+			weight[0] = 1.0;
+		}
+		else if(Nob[1] > limit*(Nob[0] + Nob[1]) && Nob[1] > limit*(Nob[1] + Nob[2])) {
+			weight[1] = 1.0;
+		}
+		else if(Nob[2] > limit*(Nob[0] + Nob[2]) && Nob[2] > limit*(Nob[1] + Nob[2])) {
+			weight[2] = 1.0;
+		}
+		else if(blend > 0.0) {
+			/* in case of blending, test for mixes between two textures */
+			if(Nob[2] < (1.0 - limit)*(Nob[1] + Nob[0])) {
+				weight[0] = Nob[0]/(Nob[0] + Nob[1]);
+				weight[0] = clamp((weight[0] - 0.5*(1.0 - blend))/blend, 0.0, 1.0);
+				weight[1] = 1.0 - weight[0];
+			}
+			else if(Nob[0] < (1.0 - limit)*(Nob[1] + Nob[2])) {
+				weight[1] = Nob[1]/(Nob[1] + Nob[2]);
+				weight[1] = clamp((weight[1] - 0.5*(1.0 - blend))/blend, 0.0, 1.0);
+				weight[2] = 1.0 - weight[1];
+			}
+			else if(Nob[1] < (1.0 - limit)*(Nob[0] + Nob[2])) {
+				weight[0] = Nob[0]/(Nob[0] + Nob[2]);
+				weight[0] = clamp((weight[0] - 0.5*(1.0 - blend))/blend, 0.0, 1.0);
+				weight[2] = 1.0 - weight[0];
+			}
+			else {
+				/* last case, we have a mix between three */
+				weight[0] = ((2.0 - limit)*Nob[0] + (limit - 1.0))/(2.0*limit - 1.0);
+				weight[1] = ((2.0 - limit)*Nob[1] + (limit - 1.0))/(2.0*limit - 1.0);
+				weight[2] = ((2.0 - limit)*Nob[2] + (limit - 1.0))/(2.0*limit - 1.0);
+			}
+		}
+
+		Color = color(0.0, 0.0, 0.0);
+		Alpha = 0.0;
+
+		float tmp_alpha;
+
+		if(weight[0] > 0.0) {
+			Color += weight[0]*image_texture_lookup(filename, color_space, Vector[1], Vector[2], tmp_alpha);
+			Alpha += weight[0]*tmp_alpha;
+		}
+		if(weight[1] > 0.0) {
+			Color += weight[1]*image_texture_lookup(filename, color_space, Vector[0], Vector[2], tmp_alpha);
+			Alpha += weight[1]*tmp_alpha;
+		}
+		if(weight[2] > 0.0) {
+			Color += weight[2]*image_texture_lookup(filename, color_space, Vector[1], Vector[0], tmp_alpha);
+			Alpha += weight[2]*tmp_alpha;
+		}
+	}
 }
 

Copied: trunk/blender/intern/cycles/kernel/osl/nodes/node_set_normal.osl (from rev 51453, trunk/blender/intern/cycles/kernel/osl/nodes/node_image_texture.osl)
===================================================================
--- trunk/blender/intern/cycles/kernel/osl/nodes/node_set_normal.osl	                        (rev 0)
+++ trunk/blender/intern/cycles/kernel/osl/nodes/node_set_normal.osl	2012-10-20 15:09:36 UTC (rev 51454)
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2012, Blender Foundation.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "stdosl.h"
+
+surface node_set_normal(
+	normal Direction = N,
+	output normal Normal = N)
+{
+	N = Direction;
+	Normal = Direction;
+}
+

Modified: trunk/blender/intern/cycles/kernel/osl/nodes/node_texture_coordinate.osl
===================================================================
--- trunk/blender/intern/cycles/kernel/osl/nodes/node_texture_coordinate.osl	2012-10-20 15:09:27 UTC (rev 51453)
+++ trunk/blender/intern/cycles/kernel/osl/nodes/node_texture_coordinate.osl	2012-10-20 15:09:36 UTC (rev 51454)
@@ -19,8 +19,9 @@
 #include "stdosl.h"
 
 shader node_texture_coordinate(
-	normal Normal = N,
+	normal NormalIn = N,
 	int is_background = 0,
+	int from_dupli = 0,
 	string bump_offset = "center",
 
 	output point Generated = point(0.0, 0.0, 0.0),
@@ -28,6 +29,7 @@
 	output point Object = point(0.0, 0.0, 0.0),
 	output point Camera = point(0.0, 0.0, 0.0),
 	output point Window = point(0.0, 0.0, 0.0),
+	output normal Normal = normal(0.0, 0.0, 0.0),
 	output point Reflection = point(0.0, 0.0, 0.0))
 {
 	if (is_background) {
@@ -37,27 +39,40 @@
 		point Pcam = transform("camera", "world", point(0, 0, 0));
 		Camera = transform("camera", P + Pcam);
 		Window = transform("NDC", P + Pcam);
+		Normal = NormalIn;
 		Reflection = I;
 	}
 	else {
-		getattribute("std::generated", Generated); 
-		getattribute("std::uv", UV);
+		if (from_dupli) {
+			getattribute("std::dupli_generated", Generated); 
+			getattribute("std::dupli_uv", UV);
+		}
+		else {
+			getattribute("std::generated", Generated); 
+			getattribute("std::uv", UV);
+		}
+
 		Object = transform("object", P);
 		Camera = transform("camera", P);
 		Window = transform("NDC", P);
-		Reflection = reflect(I, Normal);
+		Normal = transform("world", "object", NormalIn);
+		Reflection = reflect(I, NormalIn);
 	}
 
 	if (bump_offset == "dx") {
-		Generated += Dx(Generated);
-		UV += Dx(UV);
+		if (!from_dupli) {
+			Generated += Dx(Generated);
+			UV += Dx(UV);
+		}
 		Object += Dx(Object);
 		Camera += Dx(Camera);
 		Window += Dx(Window);
 	}
 	else if (bump_offset == "dy") {
-		Generated += Dy(Generated);
-		UV += Dy(UV);
+		if (!from_dupli) {
+			Generated += Dy(Generated);
+			UV += Dy(UV);
+		}
 		Object += Dy(Object);
 		Camera += Dy(Camera);
 		Window += Dy(Window);

Modified: trunk/blender/intern/cycles/kernel/osl/osl_services.cpp
===================================================================

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list