{"id":638,"date":"2022-08-10T18:43:53","date_gmt":"2022-08-10T18:43:53","guid":{"rendered":"http:\/\/fenstar.co\/wp\/?p=638"},"modified":"2023-01-21T17:56:45","modified_gmt":"2023-01-21T17:56:45","slug":"more-texture-generation-wood-grain","status":"publish","type":"post","link":"https:\/\/fenstar.co\/wp\/?p=638","title":{"rendered":"More texture generation (Wood grain)"},"content":{"rendered":"<p>I started thinking about the trunk of a tree. Essentially, you have a bunch of concentric growth rings. I visualized this as something looking like the image below.<\/p>\n<p>&nbsp;<\/p>\n<div id=\"attachment_639\" style=\"width: 603px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/fenstar.co\/wp\/?attachment_id=639\" rel=\"attachment wp-att-639\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-639\" class=\"size-full wp-image-639\" src=\"https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/TrunkA.png\" alt=\"The center of the tree trunk would be in the middle of the left side.\" width=\"593\" height=\"442\" srcset=\"https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/TrunkA.png 593w, https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/TrunkA-150x112.png 150w, https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/TrunkA-300x224.png 300w, https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/TrunkA-402x300.png 402w\" sizes=\"(max-width: 593px) 100vw, 593px\" \/><\/a><p id=\"caption-attachment-639\" class=\"wp-caption-text\">The center of the tree trunk would be in the middle of the left side.<\/p><\/div>\n<p><!--more-->Obviously, as you get closer to the top the growth rings shrink and disappear, but I figured it was best to start simple.<\/p>\n<p>This was represented by the\u00a0following code\u00a0(GUI Octave)<\/p>\n<pre>for i = 1:y\r\nfor j = 1:x\r\nT(i,j) = sqrt((i-(y\/2))^2+j^2);\r\nendfor\r\nendfor<\/pre>\n<pre>T=abs(sin(T\/s));<\/pre>\n<p>Where x, y, and s were 128, 128, and 8 respectively.<\/p>\n<p>So, if we view this as the trunk, we can &#8220;cut&#8221; a board out of it by taking a slice.<\/p>\n<div id=\"attachment_640\" style=\"width: 603px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/fenstar.co\/wp\/?attachment_id=640\" rel=\"attachment wp-att-640\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-640\" class=\"size-full wp-image-640\" src=\"https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/boardB.png\" alt=\"Visualization of &quot;cutting&quot; a board from the trunk of a tree. \" width=\"593\" height=\"442\" srcset=\"https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/boardB.png 593w, https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/boardB-150x112.png 150w, https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/boardB-300x224.png 300w, https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/boardB-402x300.png 402w\" sizes=\"(max-width: 593px) 100vw, 593px\" \/><\/a><p id=\"caption-attachment-640\" class=\"wp-caption-text\">Visualization of &#8220;cutting&#8221; a board from the trunk of a tree.<\/p><\/div>\n<p>This can be achieved with the code<\/p>\n<pre>for i =1:y\r\nfor j =1:x\r\nIM(i,j) = T(i,(x\/2));\r\nendfor\r\nendfor<\/pre>\n<p>which returns this super boring image<\/p>\n<div id=\"attachment_641\" style=\"width: 603px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/fenstar.co\/wp\/?attachment_id=641\" rel=\"attachment wp-att-641\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-641\" class=\"size-full wp-image-641\" src=\"https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/BoardA.png\" alt=\"ZzZzZz... oh, sorry, I fell asleep there for a moment.\" width=\"593\" height=\"442\" srcset=\"https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/BoardA.png 593w, https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/BoardA-150x112.png 150w, https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/BoardA-300x224.png 300w, https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/BoardA-402x300.png 402w\" sizes=\"(max-width: 593px) 100vw, 593px\" \/><\/a><p id=\"caption-attachment-641\" class=\"wp-caption-text\">ZzZzZz&#8230; oh, sorry, I fell asleep there for a moment.<\/p><\/div>\n<p>We can also simulate, to some extent, the tree shrinking as we go up the trunk by sloping the &#8220;cut&#8221;<\/p>\n<pre>for i =1:y\r\n for j =1:x\r\n IM(i,j) = T(i,int8((x\/2)+j\/(x\/4)));\r\n endfor\r\n endfor<\/pre>\n<div id=\"attachment_643\" style=\"width: 603px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/fenstar.co\/wp\/?attachment_id=643\" rel=\"attachment wp-att-643\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-643\" class=\"size-full wp-image-643\" src=\"https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/BoardAng.png\" alt=\"Sort of looks a bit like wood grain.\" width=\"593\" height=\"442\" srcset=\"https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/BoardAng.png 593w, https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/BoardAng-150x112.png 150w, https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/BoardAng-300x224.png 300w, https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/BoardAng-402x300.png 402w\" sizes=\"(max-width: 593px) 100vw, 593px\" \/><\/a><p id=\"caption-attachment-643\" class=\"wp-caption-text\">Sort of looks a bit like wood grain.<\/p><\/div>\n<p>in reality, this is obviously not <em>exactly<\/em> equivalent to what a perfectly straight tree would look like as the growth rings shrunk, but it is pretty close so we are going to use this for now.<\/p>\n<p>Of course, growth rings are not perfect circles in the real world, but in the same vain as the slope before, we can simulate irregularities\u00a0by applying an offset.<\/p>\n<p>I randomly generated and smoothed a matrix which I combined with the slice to create the image shown after the code below.<\/p>\n<p>My code, which I admit is not great, is shown below. (The lack of\u00a0matrix convolution is 100% not because I failed linear algebra and\/or I am too lazy to remember)<\/p>\n<pre>x = 128;\r\ny = x;\r\ns = 3;\r\nq=2;\r\nv=8;\r\n\r\nR = rand(y,x);\r\nS = zeros(y,x);\r\n\r\nfor i =q+1:(y-q)\r\nfor j =q+1:(x-q)\r\nfor k = -q:q\r\nfor l = -q:q\r\nS(i,j) = S(i,j)+R(i+k,j+l);\r\nendfor\r\nendfor\r\nendfor\r\nendfor\r\n\r\nS=S-((2*q+1)^2)\/2;\r\nS=S*s\/v;\r\n\r\nfor i = 1:y\r\nfor j = 1:x\r\nT(i,j) = sqrt((i-(y\/2))^2+j^2);\r\nendfor\r\nendfor\r\n\r\nT=abs(sin(T\/s));\r\n\r\nfor i =2:(y-2)\r\nfor j =2:(x-2)\r\nIM(i,j) = T(i,int8(S(i,j)+j\/(x\/4))+(x\/2));\r\nendfor\r\nendfor\r\n\r\nimshow(IM)\r\n<\/pre>\n<p>T is the cross cut of the tree, R is a random value matrix, S is a smoothed version of R, and IM is everything combined into the final image shown below.<\/p>\n<p>Or, equivalently, for those that don&#8217;t want to waste time, you can use the following code.<\/p>\n<pre>x = 128;\r\ny = x;\r\ns = 3;\r\nq=2;\r\nv=8;\r\n\r\nR = rand(y,x);\r\nS = ones(2*q+1,2*q+1);\r\n\r\nZ= conv2(R,S,\"same\");\r\nS=Z-((2*q+1)^2)\/2;\r\nS=S*s\/v;\r\n\r\nfor i = 1:y\r\nfor j = 1:x\r\nT(i,j) = sqrt((i-(y\/2))^2+j^2);\r\nendfor\r\nendfor\r\n\r\nT=abs(sin(T\/s));\r\n\r\nfor i =2:(y-2)\r\nfor j =2:(x-2)\r\nIM(i,j) = T(i,int8(S(i,j)+j\/(x\/4))+(x\/2));\r\nendfor\r\nendfor\r\n\r\nimshow(IM)\r\n<\/pre>\n<div id=\"attachment_644\" style=\"width: 603px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/fenstar.co\/wp\/?attachment_id=644\" rel=\"attachment wp-att-644\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-644\" class=\"size-full wp-image-644\" src=\"https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/BoardC.png\" alt=\"Getting closer\" width=\"593\" height=\"442\" srcset=\"https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/BoardC.png 593w, https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/BoardC-150x112.png 150w, https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/BoardC-300x224.png 300w, https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/BoardC-402x300.png 402w\" sizes=\"(max-width: 593px) 100vw, 593px\" \/><\/a><p id=\"caption-attachment-644\" class=\"wp-caption-text\">Getting closer<\/p><\/div>\n<p>Granted, this isn&#8217;t exactly a high resolution image and we are missing a lot of things\u00a0including, but not limited to<\/p>\n<ul>\n<li>knots<\/li>\n<li>xylem<\/li>\n<li>branches<\/li>\n<\/ul>\n<p>but we are moving in the right direction, even if we aren&#8217;t doing it in the most efficient manner.<\/p>\n<p>Now,\u00a0I am going to take a moment to go ahead and sort of bookmark our current code. I made a few modifications to clean it up a bit. I changed from int8 to int16 and bumped up the resolution, as well as changing the aspect ratio.<\/p>\n<pre>T=0;\r\nIM=0;\r\nx = 512;\r\ny = x;\r\ns = 8;\r\nv=.2;\r\nS = fspecial('gaussian',24,9);\r\n\r\nR = rand(y,(2*x));\r\n\r\nZ= conv2(R,S,\"same\");\r\nQ=Z-.5;\r\nQ=Q*s\/v;\r\n\r\nfor i = 1:y\r\nfor j = 1:x\r\nT(i,j) = sqrt((i-(y\/2))^2+j^2);\r\nendfor\r\nendfor\r\n\r\nT=abs(sin(T\/s));\r\n\r\nfor i = 1:y\r\nfor j = 1:(2*x)\r\nIM(i,j) = T(i,int16(Q(i,j)+j\/(x\/32))+(x\/2));\r\nendfor\r\nendfor\r\n\r\nimshow(IM)\r\n<\/pre>\n<p><a href=\"https:\/\/fenstar.co\/wp\/?attachment_id=647\" rel=\"attachment wp-att-647\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-647\" src=\"https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/BoardE.png\" alt=\"BoardE\" width=\"593\" height=\"442\" srcset=\"https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/BoardE.png 593w, https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/BoardE-150x112.png 150w, https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/BoardE-300x224.png 300w, https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/BoardE-402x300.png 402w\" sizes=\"(max-width: 593px) 100vw, 593px\" \/><\/a><\/p>\n<p>Now, you might notice an interesting effect here. As a byproduct of the gaussian blur we have actually created a bit\u00a0of a depth map, where the edges of the image look fairly close to what the edges of a board might look like. It isn&#8217;t too hard to see how you can generate a UV map for a 3D board that actually matches up on the ends.<\/p>\n<p>There is still a lot more to do, but I am going to stop here for now. Here is a quick preview of\u00a0the next part.<\/p>\n<div id=\"attachment_652\" style=\"width: 950px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/fenstar.co\/wp\/?attachment_id=652\" rel=\"attachment wp-att-652\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-652\" class=\"size-large wp-image-652\" src=\"https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/durr2-1024x512.png\" alt=\"Next time, we add some more variation and a &quot;layer&quot; for the xylem.\" width=\"940\" height=\"470\" srcset=\"https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/durr2-1024x512.png 1024w, https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/durr2-150x75.png 150w, https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/durr2-300x150.png 300w, https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/durr2-500x250.png 500w, https:\/\/fenstar.co\/wp\/wp-content\/uploads\/2022\/08\/durr2.png 2048w\" sizes=\"(max-width: 940px) 100vw, 940px\" \/><\/a><p id=\"caption-attachment-652\" class=\"wp-caption-text\">Next time, we add some more variation and a &#8220;layer&#8221; for the xylem.<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>I started thinking about the trunk of a tree. Essentially, you have a bunch of concentric growth rings. I visualized this as something looking like the image below. &nbsp; &hellip; <a class=\"more-link\" href=\"https:\/\/fenstar.co\/wp\/?p=638\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[5,3,35],"_links":{"self":[{"href":"https:\/\/fenstar.co\/wp\/index.php?rest_route=\/wp\/v2\/posts\/638"}],"collection":[{"href":"https:\/\/fenstar.co\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fenstar.co\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fenstar.co\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fenstar.co\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=638"}],"version-history":[{"count":8,"href":"https:\/\/fenstar.co\/wp\/index.php?rest_route=\/wp\/v2\/posts\/638\/revisions"}],"predecessor-version":[{"id":653,"href":"https:\/\/fenstar.co\/wp\/index.php?rest_route=\/wp\/v2\/posts\/638\/revisions\/653"}],"wp:attachment":[{"href":"https:\/\/fenstar.co\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fenstar.co\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fenstar.co\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}