php - Procedurally generating a texture -



php - Procedurally generating a texture -

i'm trying figure out script generate texture (which can multiplied grayscale image "apply" it). far method involves seeding rng, randomly generating 8x8 matrix of integers in range [0,3], scaling matrix 256x256 image using level of interpolation.

here's illustration output (seed value 24):

on left matrix scaled nearest-neighbor interpolation. on right effort @ bilinear interpolation. part seems okay, structures near middle-left there 2 diagonally-adjoining orange squares faced 2 diagonally-adjoining reddish squares, andthe result no interpolation area. additionally, it's beingness treated more heatmap (as shown abundance of orange in top-left corner) , that's causing more problems.

here's code have "bilinear interpolation":

<?php $matrix = array(); srand(24); $dim = 256; $scale = 32; for($y=0;$y<=$dim/$scale;$y++) for($x=0;$x<=$dim/$scale;$x++) $matrix[$y][$x] = rand(0,3); $img = imagecreate($dim,$dim); imagecolorallocate($img,255,255,255); $cols = array( imagecolorallocate($img,128,0,0), imagecolorallocate($img,128,64,32), imagecolorallocate($img,128,128,0), imagecolorallocate($img,64,64,64) ); for($y=0;$y<$dim;$y++) { for($x=0;$x<$dim;$x++) { $xx = floor($x/$scale); $yy = floor($y/$scale); $x2 = $x%$scale; $y2 = $y%$scale; $col = $cols[round(( $matrix[$yy][$xx]*($scale-$x2)*($scale-$y2) + $matrix[$yy][$xx+1]*$x2*($scale-$y2) + $matrix[$yy+1][$xx]*($scale-$x2)*$y2 + $matrix[$yy+1][$xx+1]*$x2*$y2 )/($scale*$scale))]; imagesetpixel($img,$x,$y,$col); } } header("content-type: image/png"); imagepng($img); exit;

in reality, may bit of xy problem. i'm trying generate "fur patterns" creatures in game i'm planning. in particular want able have breeding mixes elements 2 parents (be colour or elements of pattern), having random seed won't cutting it. ideally need kind of vector-based approach, i'm way out of depth there help much appreciated.

a couple things come mind:

you not interpolating color values. expand on zakinster's comment, interpolating color indices, , rounding nearest one. 1 effect of wind swath of yellowish (index 2) in between orange (index 1) , grayness (index 3) areas. if interpolated color values instead, wind with, perhaps, grayish orange?

you have more yellowish , orange, , less reddish , grayness in final image. because of using round() snap color index. calculation (before round()) may produce floats evenly distributed between 0 3, rounding doesn't preserve it.

so, here suggestions:

if not limited 4 colors, utilize more. interpolate color values (i.e. (128,0,0) mixed (64,64,64) produces (91,32,32)) rather indices.

if limited 4 colors, seek kind of dithering. simple approach, minimal changes code, add together randomness color index chosen. so, instead of round(...), this: calculation produces value 1.7. then, round 2 70% probability, , downwards 1 other 30%. blend colors, may produce noisy image. if prepared alter code more substantially, check out floyd-steinberg dithering.

php image-manipulation

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -