New in Wolfram
Mathematica
8: Compiler Performance Enhancements
◄
previous
|
next
►
Software Development
Use Compile to Procedurally Generate Textures
Compile
can be used to generate procedural textures that can be applied on objects.
In[1]:=
X
dot = With[{grad = {{1, 1, 0}, {-1, 1, 0}, {1, -1, 0}, {-1, -1, 0}, {1, 0, 1}, {-1, 0, 1}, {1, 0, -1}, {-1, 0, -1}, {0, 1, 1}, {0, -1, 1}, {0, 1, -1}, {0, -1, -1}}}, Compile[{{gradIdx, _Integer}, {x, _Real}, {y, _Real}, {z, _Real}}, grad[[gradIdx + 1]][[1]]*x + grad[[gradIdx + 1]][[2]]*y + grad[[gradIdx + 1]][[3]]*z ] ]; fade = Compile[{{t, _Real}}, t*t*t*(t*(t*6.0 - 15.0) + 10.0)]; lerp = Compile[{{x, _Real}, {y, _Real}, {t, _Real}}, (1.0 - t)*x + t*y]; signedNoise = With[{permutations = Join[(permutations = RandomSample[Range[0, 255]]), permutations]}, Compile[{{x0, _Real}, {y0, _Real}, {z0, _Real}}, Module[{x, y, z, ix, iy, iz, g000, g001, g010, g011, g100, g101, g110, g111, n000, n100, n010, n110, n001, n101, n011, n111, u, v, w, nx00, nx01, nx10, nx11, nxy0, nxy1, nxyz}, ix = IntegerPart[x0]; iy = IntegerPart[y0]; iz = IntegerPart[z0]; x = x0 - ix; y = y0 - iy; z = z0 - iz; ix = Mod[ix, 255] + 1; iy = Mod[iy, 255] + 1; iz = Mod[iz, 255] + 1; g000 = Mod[permutations[[ ix + permutations[[iy + permutations[[iz]]]]]], 12]; g001 = Mod[permutations[[ ix + permutations[[iy + permutations[[iz + 1]]]]]], 12]; g010 = Mod[permutations[[ ix + permutations[[iy + 1 + permutations[[iz]]]]]], 12]; g011 = Mod[permutations[[ ix + permutations[[iy + 1 + permutations[[iz + 1]]]]]], 12]; g100 = Mod[permutations[[ ix + 1 + permutations[[iy + permutations[[iz]]]]]], 12]; g101 = Mod[permutations[[ ix + 1 + permutations[[iy + permutations[[iz + 1]]]]]], 12]; g110 = Mod[permutations[[ ix + 1 + permutations[[iy + 1 + permutations[[iz]]]]]], 12]; g111 = Mod[permutations[[ ix + 1 + permutations[[iy + 1 + permutations[[iz + 1]]]]]], 12]; n000 = dot[g000, x, y, z]; n100 = dot[g100, x - 1, y, z]; n010 = dot[g010, x, y - 1, z]; n110 = dot[g110, x - 1, y - 1, z]; n001 = dot[g001, x, y, z - 1]; n101 = dot[g101, x - 1, y, z - 1]; n011 = dot[g011, x, y - 1, z - 1]; n111 = dot[g111, x - 1, y - 1, z - 1]; u = fade[x]; v = fade[y]; w = fade[z]; nx00 = lerp[n000, n100, u]; nx01 = lerp[n001, n110, u]; nx10 = lerp[n010, n110, u]; nx11 = lerp[n011, n111, u]; nxy0 = lerp[nx00, nx10, v]; nxy1 = lerp[nx01, nx11, v]; nxyz = lerp[nxy0, nxy1, w]; nxyz ], "CompilationTarget" -> "C" ] ]; classicPerlin = With[{octaves = 8}, Compile[{{xIndex, _Integer}, {yIndex, _Integer}, {amplitude, \ _Real}, {frequency, _Real}, {gain, _Real}, {lacunarity, _Real}, \ {scale, _Real}, {increment, _Real}, {width, _Integer}, {height, \ _Integer}}, Module[{noiseVal = 0.0, x, y, z, freq = frequency, amp = amplitude}, x = xIndex*frequency/scale; y = yIndex*frequency/scale; z = 1.0*frequency/scale; Do[ noiseVal += signedNoise[x*freq, y*freq, z*freq]*amp; freq *= lacunarity; amp *= gain, {octaves} ]; Min[Max[noiseVal, 0.0], 1.0] ] ] ]; ImageRecolor[img_, r_, g_, b_] := Image[{r*img, g*img, b*img}, Interleaving -> False] perlin[width_Integer, height_Integer] := Table[classicPerlin[ii, jj, amplitude, frequency, gain, lacunarity, scale, increment, width, height], {ii, 0, width}, {jj, 0, height}];
In[2]:=
X
amplitude = 0.5; frequency = 1.0; gain = 0.5; lacunarity = 2.0; increment = 5.0; scale = 10.0; ReliefImage[perlin[256, 256]]
Out[2]=
In[3]:=
X
amplitude = 0.5; frequency = 1.0; gain = 0.5; lacunarity = 0.5; scale = 50.0; increment = 5.0; data = 20.0*perlin[256, 256]; img = ImageRecolor[data - Map[IntegerPart, data, Infinity], 1.0, 0.4, 0.1]
Out[3]=
In[4]:=
X
ParametricPlot3D[{(2 + Cos[v]) Cos[u], (2 + Cos[v]) Sin[u], Sin[v]}, {u, 0, 2 Pi}, {v, 0, 2 Pi}, Mesh -> None, TextureCoordinateFunction -> ({#1, #2} &), PlotStyle -> Texture[img], Boxed -> False, Axes -> None]
Out[4]=