POV-Ray : Newsgroups : povray.binaries.images : Crackle : Re: Crackle Server Time
23 Apr 2024 08:36:43 EDT (-0400)
  Re: Crackle  
From: Bald Eagle
Date: 14 Nov 2022 19:40:00
Message: <web.6372dfa5302862a41f9dae3025979125@news.povray.org>
"Bald Eagle" <cre### [at] netscapenet> wrote:

> I've had a lot of success (not 100%) with adapting ShaderToy code to SDL, so
> maybe at some point I can figure out how to make a voronoi pattern in SDL based
> on some of the scripts.


Y'know - like this:
I'm not sure when I make all the squares or spheres fill up everything that it
turns black, with a few colored spots.  My POV-Ray suckage, likely.
But it's proof of concept, so here we go.

Next stage would be to code it using only functions, and then it could be used
as a pigment pattern.



#version 3.8;
global_settings {assumed_gamma 1.0}

#include "colors.inc"
//#include "BezierInclude.inc"
#include "math.inc"
#declare E = 0.0000001;

#declare Camera_Orthographic = true;
#declare Camera_Position = <0.5, 0.5, -100> ;  // front view
#declare Camera_Look_At  = <0.5, 0.5,  0> ;
#declare Fraction = 450;     // functions as a zoom for the orthographic view: 4
zooms in 4x, 8 zooms in 8x, etc.

// ###########################################
camera {
 #if (Camera_Orthographic = true)
  orthographic
  right     x*image_width/(Fraction)
  up   y*image_height/(Fraction)
 #else
  right     x*image_width/image_height
  up y
 #end
 location  Camera_Position
 look_at   Camera_Look_At}
// ###########################################

sky_sphere {pigment {rgb <1, 1, 1>}}
#declare LS = <0, 0, -20>;
light_source {LS rgb 1}
#declare Line = 0.5/image_width; // line thickness


// DOT product
#declare SFn_vdot = function (ax, ay, az, bx, by, bz) {ax*bx + ay*by + az*bz}

// Length or Norm
#declare SFn_vlength = function (ax, ay, az) {sqrt(ax*ax + ay*ay + az*az)}

#declare Clip = function (_Val, _Min, _Max) {min(_Max, max(_Val, _Min))}
#declare Window = function (_Val, _Eq, _Min, _Max, _E) {select (_Val-_Min, _Val,
select(_Max-_Val, _Val, _Eq))}
#declare LimitReps = function (Element, Period, Min, Max) {Element - Period *
Clip (Element/Period, Min, Max)}

#declare Smoothstep0 = function (_Val, _From, _To) {Clip ((_Val - _From) / (_To
- _From), 0.0, 1.0)}
#declare Smoothstep = function (_Val, _From, _To) {Smoothstep0 (_Val, _From,
_To) * Smoothstep0 (_Val, _From, _To) * (3.0 - 2.0 * Smoothstep0 (_Val, _From,
_To))}




#macro Clamp (N, minVal, maxVal)
 #local Val = min (max (N, minVal), maxVal);

 Val
#end

#macro hash (p)
 // input: 2D vector
 // output: 2D vector

    //p = mod(p, 4.0); // tile
    #local PX = vdot (p, <127.1, 311.7>);
    #local PY = vdot (p, <269.5, 183.3>);

    #local PXr = mod (sin (PX)*18.5453, 1);
    #local PXy = mod (sin (PY)*18.5453, 1);

    <PXr, PXy>

#end


// return distance, and cell id
#macro voronoi (P)
 // input: 2D vector
 // output: 2D vector

 #local nx = floor (P.x);
 #local ny = floor (P.y);
 #local n  = <nx, ny>;

 #local fx = mod (P.x, 1);
 #local fy = mod (P.y, 1);

 #local m = <8, 8, 8>;

 #for (j, -1, 1)
  #for (i, -1, 1)
   #local g = <i, j>;
   #local o = hash (n + g);
        // #local r = g - f + o;
   #local rx = g.x - fx + (0.5 + 0.5*sin(clock + 6.2831*o.x));
   #local ry = g.y - fy + (0.5 + 0.5*sin(clock + 6.2831*o.y));
   #local r = <rx, ry>;

   #local d = vdot (r, r);
   #if(d < m.x)
    #local m = <d, o.x, o.y>; // m = vec3 (d, o);
   #end
  #end
 #end

 <sqrt (m.x), m.y + m.z>;

#end

#local M = max(image_width,image_height);
#local D = 1.75;

#macro mainImage (X, Y)


   #local PX = X / M;
   #local PY = Y / M;

   // computer voronoi pattern
   #local C = voronoi ( <(14.0 + 6.0 * sin (0.2*clock)) * PX,  (14.0 + 6.0 * sin
(0.2*clock)) * PY>);

   // colorize

   #local ColorR = 0.5 + 0.5 * cos (C.y*6.2831 + 0);
   #local ColorG = 0.5 + 0.5 * cos (C.y*6.2831 + 1);
   #local ColorB = 0.5 + 0.5 * cos (C.y*6.2831 + 2);

   #local C1 = Clamp (1.0 - 0.4*C.x*C.x, 0.0, 1.0);

   #local ColorR = ColorR * C1;
   #local ColorG = ColorG * C1;
   #local ColorB = ColorB * C1;

   #local C2 = (1.0 - Smoothstep (0.08, 0.09, C.x));

   #local ColorR = ColorR - C2;
   #local ColorG = ColorG - C2;
   #local ColorB = ColorB - C2;

   #local fragColor = <ColorR, ColorG, ColorB>;

   //sphere {<PX, PY>, Line*2 texture {pigment {rgb fragColor} finish {diffuse
1}} }
   //sphere {<PX, PY>, Line*2 pigment {rgb fragColor} }
   box {<PX-Line*D, PY-Line*D, 0>, <PX+Line*D, PY+Line*D, Line> pigment {rgb
fragColor} }

#end


#declare Step = 2;
 #for (Y, 0, image_width, Step)
  #debug concat("Y = ", str(Y, 0, 0), "\n")
  #for (X, 0, image_width, Step)
   mainImage (X, Y)
  #end
 #end

/*
vec2 hash( vec2 p )
{
    //p = mod(p, 4.0); // tile
    p = vec2(dot(p,vec2(127.1,311.7)),
             dot(p,vec2(269.5,183.3)));
    return fract(sin(p)*18.5453);
}

// return distance, and cell id
vec2 voronoi( in vec2 x )
{
    vec2 n = floor( x );
    vec2 f = fract( x );

 vec3 m = vec3( 8.0 );
    for( int j=-1; j<=1; j++ )
    for( int i=-1; i<=1; i++ )
    {
        vec2  g = vec2( float(i), float(j) );
        vec2  o = hash( n + g );
      //vec2  r = g - f + o;
     vec2  r = g - f + (0.5+0.5*sin(iTime+6.2831*o));
  float d = dot( r, r );
        if( d<m.x )
            m = vec3( d, o );
    }

    return vec2( sqrt(m.x), m.y+m.z );
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    vec2 p = fragCoord.xy/max(iResolution.x,iResolution.y);

    // computer voronoi patterm
    vec2 c = voronoi( (14.0+6.0*sin(0.2*iTime))*p );

    // colorize
    vec3 col = 0.5 + 0.5*cos( c.y*6.2831 + vec3(0.0,1.0,2.0) );
    col *= clamp(1.0 - 0.4*c.x*c.x,0.0,1.0);
    col -= (1.0-smoothstep( 0.08, 0.09, c.x));

    fragColor = vec4( col, 1.0 );
}
*/


Post a reply to this message


Attachments:
Download 'new_iq_voronoipattern.png' (416 KB)

Preview of image 'new_iq_voronoipattern.png'
new_iq_voronoipattern.png


 

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.