[Bf-blender-cvs] [9b40616] master: Cycles: Point density texture support

Sergey Sharybin noreply at git.blender.org
Sat Jul 18 23:00:48 CEST 2015


Commit: 9b406162490d9ee93e9786350432cdc1563b7bf6
Author: Sergey Sharybin
Date:   Sat Jul 18 22:36:09 2015 +0200
Branches: master
https://developer.blender.org/rB9b406162490d9ee93e9786350432cdc1563b7bf6

Cycles: Point density texture support

This commit implements point density texture for Cycles shading nodes.

It's done via creating voxel texture at shader compilation time, Not
totally memory efficient, but avoids adding sampling code to kernel
(which keeps render time as low as possible), In the future this will
be compensated by using OpenVDB for more efficient storage of sparse
volume data.

Sampling of the voxel texture is happening at blender side and the
same code is used as for Blender Internal's renderer.

This texture is controlled by only object, particle system and radius.
Linear falloff is used and there's no turbulence. This is because
falloff is expected to happen using Curve Mapping node. Turbulence
will be done as a distortion on the input coordinate. It's already
possible to fake it using nose textures and in the future we can add
more proper turbulence distortion node, which then could also be used
for 2D texture mapping.

Particle color support is done by Lukas, thanks!

===================================================================

M	intern/cycles/blender/CMakeLists.txt
M	intern/cycles/blender/addon/ui.py
M	intern/cycles/blender/blender_session.cpp
M	intern/cycles/blender/blender_shader.cpp
A	intern/cycles/blender/blender_texture.cpp
A	intern/cycles/blender/blender_texture.h
M	release/scripts/startup/nodeitems_builtins.py
M	source/blender/blenkernel/BKE_node.h
M	source/blender/blenkernel/BKE_texture.h
M	source/blender/blenkernel/intern/node.c
M	source/blender/blenkernel/intern/texture.c
M	source/blender/editors/space_node/drawnode.c
M	source/blender/makesdna/DNA_node_types.h
M	source/blender/makesrna/intern/rna_nodetree.c
M	source/blender/nodes/CMakeLists.txt
M	source/blender/nodes/NOD_shader.h
M	source/blender/nodes/NOD_static_types.h
A	source/blender/nodes/shader/nodes/node_shader_tex_pointdensity.c
M	source/blender/render/intern/source/pointdensity.c

===================================================================

diff --git a/intern/cycles/blender/CMakeLists.txt b/intern/cycles/blender/CMakeLists.txt
index fff9ed2..c6a2b91 100644
--- a/intern/cycles/blender/CMakeLists.txt
+++ b/intern/cycles/blender/CMakeLists.txt
@@ -31,10 +31,12 @@ set(SRC
 	blender_session.cpp
 	blender_shader.cpp
 	blender_sync.cpp
+	blender_texture.cpp
 
 	CCL_api.h
 	blender_sync.h
 	blender_session.h
+	blender_texture.h
 	blender_util.h
 )
 
diff --git a/intern/cycles/blender/addon/ui.py b/intern/cycles/blender/addon/ui.py
index 8bb086d..fdb2afd 100644
--- a/intern/cycles/blender/addon/ui.py
+++ b/intern/cycles/blender/addon/ui.py
@@ -1236,7 +1236,8 @@ class CyclesTexture_PT_mapping(CyclesButtonsPanel, Panel):
     @classmethod
     def poll(cls, context):
         node = context.texture_node
-        return node and CyclesButtonsPanel.poll(context)
+        # TODO(sergey): perform a faster/nicer check?
+        return node and hasattr(node, 'texture_mapping') and CyclesButtonsPanel.poll(context)
 
     def draw(self, context):
         layout = self.layout
diff --git a/intern/cycles/blender/blender_session.cpp b/intern/cycles/blender/blender_session.cpp
index 7342ed3..e1d5e13 100644
--- a/intern/cycles/blender/blender_session.cpp
+++ b/intern/cycles/blender/blender_session.cpp
@@ -1016,6 +1016,18 @@ void BlenderSession::builtin_image_info(const string &builtin_name, void *builti
 
 		is_float = true;
 	}
+	else {
+		/* TODO(sergey): Check we're indeed in shader node tree. */
+		PointerRNA ptr;
+		RNA_pointer_create(NULL, &RNA_Node, builtin_data, &ptr);
+		BL::Node b_node(ptr);
+		if(b_node.is_a(&RNA_ShaderNodeTexPointDensity)) {
+			BL::ShaderNodeTexPointDensity b_point_density_node(b_node);
+			channels = 4;
+			width = height = depth = b_point_density_node.resolution();
+			is_float = true;
+		}
+	}
 }
 
 bool BlenderSession::builtin_image_pixels(const string &builtin_name, void *builtin_data, unsigned char *pixels)
@@ -1158,6 +1170,17 @@ bool BlenderSession::builtin_image_float_pixels(const string &builtin_name, void
 
 		fprintf(stderr, "Cycles error: unexpected smoke volume resolution, skipping\n");
 	}
+	else {
+		/* TODO(sergey): Check we're indeed in shader node tree. */
+		PointerRNA ptr;
+		RNA_pointer_create(NULL, &RNA_Node, builtin_data, &ptr);
+		BL::Node b_node(ptr);
+		if(b_node.is_a(&RNA_ShaderNodeTexPointDensity)) {
+			BL::ShaderNodeTexPointDensity b_point_density_node(b_node);
+			int length;
+			b_point_density_node.calc_point_density(b_scene, &length, &pixels);
+		}
+	}
 
 	return false;
 }
diff --git a/intern/cycles/blender/blender_shader.cpp b/intern/cycles/blender/blender_shader.cpp
index 2b0e8ac..6c54149 100644
--- a/intern/cycles/blender/blender_shader.cpp
+++ b/intern/cycles/blender/blender_shader.cpp
@@ -22,6 +22,7 @@
 #include "scene.h"
 #include "shader.h"
 
+#include "blender_texture.h"
 #include "blender_sync.h"
 #include "blender_util.h"
 
@@ -736,6 +737,34 @@ static ShaderNode *add_node(Scene *scene,
 		uvm->from_dupli = b_uvmap_node.from_dupli();
 		node = uvm;
 	}
+	else if(b_node.is_a(&RNA_ShaderNodeTexPointDensity)) {
+		BL::ShaderNodeTexPointDensity b_point_density_node(b_node);
+		PointDensityTextureNode *point_density = new PointDensityTextureNode();
+		point_density->filename = b_point_density_node.name();
+		point_density->space =
+		        PointDensityTextureNode::space_enum[(int)b_point_density_node.space()];
+		point_density->interpolation =
+		        (InterpolationType)b_point_density_node.interpolation();
+		point_density->builtin_data = b_point_density_node.ptr.data;
+
+		/* Transformation form world space to texture space. */
+		BL::Object b_ob(b_point_density_node.object());
+		if(b_ob) {
+			float3 loc, size;
+			point_density_texture_space(b_point_density_node, loc, size);
+			point_density->tfm =
+			        transform_translate(-loc) * transform_scale(size) *
+			        transform_inverse(get_transform(b_ob.matrix_world()));
+		}
+
+		/* TODO(sergey): Use more proper update flag. */
+		if(true) {
+			scene->image_manager->tag_reload_image(point_density->filename,
+			                                       point_density->builtin_data,
+			                                       point_density->interpolation);
+		}
+		node = point_density;
+	}
 
 	if(node)
 		graph->add(node);
diff --git a/intern/cycles/blender/blender_texture.cpp b/intern/cycles/blender/blender_texture.cpp
new file mode 100644
index 0000000..cb4dd17
--- /dev/null
+++ b/intern/cycles/blender/blender_texture.cpp
@@ -0,0 +1,116 @@
+/*
+ * Copyright 2011-2015 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 "blender_texture.h"
+
+CCL_NAMESPACE_BEGIN
+
+namespace {
+
+/* Point density helpers. */
+
+static void density_texture_space_invert(float3& loc,
+                                         float3& size)
+{
+	if(size.x != 0.0f) size.x = 0.5f/size.x;
+	if(size.y != 0.0f) size.y = 0.5f/size.y;
+	if(size.z != 0.0f) size.z = 0.5f/size.z;
+
+	loc = loc*size - make_float3(0.5f, 0.5f, 0.5f);
+}
+
+static void density_object_texture_space(BL::Object b_ob,
+                                         float radius,
+                                         float3& loc,
+                                         float3& size)
+{
+	if(b_ob.type() == BL::Object::type_MESH) {
+		BL::Mesh b_mesh(b_ob.data());
+		loc = get_float3(b_mesh.texspace_location());
+		size = get_float3(b_mesh.texspace_size());
+	}
+	else {
+		/* TODO(sergey): Not supported currently. */
+	}
+	/* Adjust texture space to include density points on the boundaries. */
+	size = size + make_float3(radius, radius, radius);
+	density_texture_space_invert(loc, size);
+}
+
+static void density_particle_system_texture_space(
+        BL::Object b_ob,
+        BL::ParticleSystem b_particle_system,
+        float radius,
+        float3& loc,
+        float3& size)
+{
+	if(b_particle_system.settings().type() == BL::ParticleSettings::type_HAIR) {
+		/* TODO(sergey): Not supported currently. */
+		return;
+	}
+	Transform tfm = get_transform(b_ob.matrix_world());
+	Transform itfm = transform_inverse(tfm);
+	float3 min = make_float3(FLT_MAX, FLT_MAX, FLT_MAX),
+	       max = make_float3(-FLT_MAX, -FLT_MAX, -FLT_MAX);
+	float3 particle_size = make_float3(radius, radius, radius);
+	for(int i = 0; i < b_particle_system.particles.length(); ++i) {
+		BL::Particle particle = b_particle_system.particles[i];
+		float3 location = get_float3(particle.location());
+		location = transform_point(&itfm, location);
+		min = ccl::min(min, location - particle_size);
+		max = ccl::max(max, location + particle_size);
+	}
+	/* Calculate texture space from the particle bounds.  */
+	loc = (min + max) * 0.5f;
+	size = (max - min) * 0.5f;
+	density_texture_space_invert(loc, size);
+}
+
+}  /* namespace */
+
+void point_density_texture_space(BL::ShaderNodeTexPointDensity b_point_density_node,
+                                 float3& loc,
+                                 float3& size)
+{
+	/* Fallback values. */
+	loc = make_float3(0.0f, 0.0f, 0.0f);
+	size = make_float3(0.0f, 0.0f, 0.0f);
+	BL::Object b_ob(b_point_density_node.object());
+	if(!b_ob) {
+		return;
+	}
+	if(b_point_density_node.point_source() ==
+	   BL::ShaderNodeTexPointDensity::point_source_PARTICLE_SYSTEM)
+	{
+		BL::ParticleSystem b_particle_system(
+		        b_point_density_node.particle_system());
+		if(b_particle_system) {
+			density_particle_system_texture_space(b_ob,
+			                                      b_particle_system,
+			                                      b_point_density_node.radius(),
+			                                      loc,
+			                                      size);
+		}
+	}
+	else {
+		density_object_texture_space(b_ob,
+		                             b_point_density_node.radius(),
+		                             loc,
+		                             size);
+	}
+}
+
+CCL_NAMESPACE_END
diff --git a/intern/cycles/blender/blender_texture.h b/intern/cycles/blender/blender_texture.h
new file mode 100644
index 0000000..74fbca0
--- /dev/null
+++ b/intern/cycles/blender/blender_texture.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2011-2015 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.
+ */
+
+#ifndef __BLENDER_TEXTURE_H__
+#define __BLENDER_TEXTURE_H__
+
+#include <stdlib.h>
+#include "blender_sync.h"
+
+CCL_NAMESPACE_BEGIN
+
+void point_density_texture_space(BL::ShaderNodeTexPointDensity b_point_density_node,
+                                 float3& loc,
+                                 float3& size);
+
+CCL_NAMESPACE_END
+
+#endif  /* __BLENDER_TEXTURE_H__ */
diff --git a/release/scripts/startup/nodeitems_builtins.py b/release/scripts/startup/nodeitems_builtins.py
index cada125..c3def14 100644
--- a/release/scripts/startup/nodeitems_builtins.py
+++ b/release/scripts/startup/nodeitems_builtins.py
@@ -237,6 +237,7 @@ shader_node_categories = [
         NodeItem("ShaderNodeTexMagic"),
         NodeItem("ShaderNodeTexChecker"),
         NodeItem("ShaderNodeTexBrick"),
+        NodeItem("ShaderNodeTexPointDensity"),
         ]),
     ShaderNewNodeCategory("SH_NEW_OP_COLOR", "Color", items=[
         NodeItem("ShaderNodeMixRGB"),
diff --git a/source/blender/blenkernel/BKE_node.h b/source/b

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list