User:Wfpokorny/DensityFile/FillingVolumeWithShapes

From POV-Wiki
Jump to navigation Jump to search
Density_file as isosurace of many individually textured shapes filling another shape.

This example exploits the fact, with the Github branch patch updates, individual shapes and textures per voxel can be created with the density_file pattern. Using interpolation 3 or 4 at small values gives a base spherical density function in each voxel. Interpolation 0 at the same df3 size can be used to directly control the map value for pigment, texture, etc. So long as the same warps are applied to both the map value df3 and the df3 forming shapes, the map values follow - no matter how drastic the warps/turbulence.

The code below also creates the container df3 by converting a function defining a spheroid. Commented code is shown for an alternate method using a standard sphere to define the volume via a function based upon the object pattern.

The code also uses 'poly_wave 3.0' to reduce blobbing as a way to get more individualized or separated spheres. See additional commented code for various other options.

The complete scene file is:

#version 3.8;
global_settings {
    assumed_gamma 1
    ambient_light srgb <1,1,1>
    noise_generator 3
 // radiosity {
 //   pretrace_start 0.08
 //   pretrace_end   2/max(image_width,image_height)
 //   count 250
 //   nearest_count 10
 //   error_bound 0.45
 //   recursion_limit 1
 //   low_error_factor 0.5
 //   gray_threshold 0
 //   minimum_reuse 0.015
 //   brightness 1.0
 //   adc_bailout 0.01/2
 //   always_sample off
 //   normal on
 // }
}

#include "arrays.inc"           // For ARRAYS_WriteDF3()
#include "arraycoupleddf3s.inc" // Array coupled df3 macros & functions.

//------ First create the df3 for colormap.
#include "arrays.inc"
#declare maxZ=30;
#declare maxY=30;
#declare maxX=30;
#declare Ary=array[maxX][maxY][maxZ]
#declare midX=int(maxX/2);
#declare midY=int(maxY/2);
#declare midZ=int(maxZ/2);
#declare padMaxX=maxX-6; // Start padded at 5 end padMaxX
#declare padMaxY=maxY-6;
#declare padMaxZ=maxZ-6;
//-- First initialize all voxels to 0.0
#for (Z,0,maxZ-1)
  #for (Y,0,maxY-1)
    #for (X,0,maxX-1)
       #declare Ary[X][Y][Z]=0.0;
    #end
  #end
#end
//-- Create box at mid y
#local tmpSeed=seed(now*24*60*60);
#for (Z,5,padMaxZ)
  #for (Y,5,padMaxY)
    #for (X,5,padMaxX)
       #declare Ary[X][Y][Z]=rand(tmpSeed);
    #end
  #end
#end
#declare Df3FileName="FunctionToDF3ShapesMap.df3";
ARRAYS_WriteDF3(Ary,Df3FileName,8)
//---

#include "functions.inc"

   //--- Can turn objects into df3 via ConvertFunctionToDF3 as follows
// #declare Sphere00 = sphere { 0.5,0.2 }
// #declare Fnct00 = function { pattern { object { Sphere00 } } }
// ConvertFunctionToDF3(30,30,30,0.0,0.2,0.9,1.1,Fnct00,"FunctionToDF3Shapes.df3",8,0)

   #declare Fnct00 = function (x,y,z) { f_sphere((x-0.5)*1.5,y-0.5,(z-0.5)*2,0.33) }

ConvertFunctionToDF3(30,30,30,0.0,0.2,-9e10,0.0,Fnct00,"FunctionToDF3Shapes.df3",8,0)

// #error "Stop before render in just creating df3 for future work"

#declare Color_2 = srgbft <0.65,0.65,0.65,0,0>;
#declare Color_3 = srgbft <0.8,0.8,0.8,0,0>;
#declare ColorMap_CloudyGraySky2 = color_map {
    [ 0 Color_2 ]
    [ 1 Color_3 ]
}
#declare Pigment_CloudyGraySky2 = pigment {
    bozo sine_wave frequency 3.33 turbulence 1.0
    color_map { ColorMap_CloudyGraySky2 }
}
#declare Sky_Sphere00 = sky_sphere {
    pigment { Pigment_CloudyGraySky2 }
    emission rgb <1,1,1>
}
#declare Camera00 = camera {
    perspective
    location <2.7,2.7,-2.701>
    sky <0,1,0>
    angle 35
    right x*(image_width/image_height)
    look_at <0,0,0>
}
#declare White = srgbft <1,1,1,0,0>;
#declare Light00 = light_source {
    <50,150,-250>, White
}
#macro TheWarps00 ()
       scale 2.7
       warp { turbulence <0.027,0.025,0.026> octaves 9 omega 0.95 lambda 1.414 } // Sphere of stones?
       scale 1/2.7
    // warp { turbulence -0.02 octaves 5 omega 1.33 lambda 1.7 }  //--- This cool
    // warp { turbulence 0.01 octaves 3 omega 1.2 lambda 9.0 }    //--- Sphere-ish
    // warp { turbulence 0.03 octaves 4 omega 1.70 lambda 3.3 }   //
#end

#declare Fnct01 = function {
    pattern { density_file df3 "FunctionToDF3Shapes.df3" interpolate 3
       poly_wave 3.0
       TheWarps00()
    }
}
#declare Fnct02 = function (x,y,z) {
  //0.025-(Fnct01(x,y,z)-f_granite(x*20,y*20,z*20)*0.05) // Needs higher max gradient
    0.025-Fnct01(x,y,z)
}
#declare Iso00 = isosurface {
    function { Fnct02(x,y,z) }
    contained_by { box { <0.2,0.1,0.30>,<0.8,0.9,0.70> } }
    threshold 0
    accuracy 0.001
    max_gradient 6
    all_intersections
}
#declare Fnct03 = function {
    pattern { density_file df3 "FunctionToDF3ShapesMap.df3" interpolate 0
        TheWarps00()
    }
}
#declare Object00 = object {
    Iso00
    texture {
      pigment {
         function { Fnct03(x,y,z) }
         color_map {
         [ 0.00 srgb <1,0.4784,0.3804> ]
         [ 0.37 srgb <0.9333,0.8667,0.7647> ]
         [ 0.70 srgb <0.9882,0.9608,0.8078> ]
         [ 0.77 srgb <0.8098,0.69412,0.08235> ]
         [ 0.79 srgb <0.1098,0.09412,0.08235> ]
         [ 1.00 srgb <0.7216,0.3059,0.1569>*0.8 ]
         }
      }
      finish { phong 0.7 phong_size 77 }
    }
    translate -0.5
    scale 4.0
}

//---
sky_sphere { Sky_Sphere00 }
camera { Camera00 }
object { Object00 }
light_source { Light00 }