def smoothNoise2D(p: Tensor) -> Tensor: w = p.shape[0] h = p.shape[1] local_uv = simpleSmoothstep(gl.fract(p)) local_id = torch.floor(p) # Find the noise value at the four corners of a local box bl = random(local_id) br = random(local_id + torch.tensor((1, 0)).repeat(w, h, 1)) tl = random(local_id + torch.tensor((0, 1)).repeat(w, h, 1)) tr = random(local_id + torch.tensor((1, 1)).repeat(w, h, 1)) # Interpolate between bottom, top, and bottom -> top b = gl.mix(bl, br, local_uv[:, :, 0]) t = gl.mix(tl, tr, local_uv[:, :, 0]) return gl.mix(b, t, local_uv[:, :, 1]).unsqueeze(-1)
def shade_iter(self, frag_pos: Tensor, h: Tensor, s: Tensor, v: Tensor) -> Tensor: K = torch.tensor((1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0)) p = torch.abs( gl.fract(torch.stack([h, h, h]) + K[0:3]) * torch.tensor(6.0) - torch.stack([K[3], K[3], K[3]])) color = v * gl.mix( torch.stack([K[0], K[0], K[0]]), torch.clamp(p - torch.stack([K[0], K[0], K[0]]), 0, 1.0), s) return color
def shade_iter(self, frag_pos: Tensor, color1: Tensor, color2: Tensor, scale: Tensor) -> Tensor: p = frag_pos * scale p_int = torch.abs(torch.floor(p)) check = torch.eq(torch.eq(gl.mod(p_int[0], 2.0), gl.mod(p_int[1], 2.0)), gl.mod(p_int[2], 2.0)) if check: return color1 else: return color2 return gl.mix(color2, color1, check)
def shade_mat(self, mortar_scale: Tensor, brick_scale: Tensor, brick_elongate: Tensor, brick_shift: Tensor, color_brick: Tensor, color_mortar: Tensor) -> Tensor: scale = torch.cat([ torch.div(brick_scale, brick_elongate + TINY_FLOAT), brick_scale, brick_scale ], dim=2) uv3 = pattern.tile( Shader.frag_pos(), scale, torch.cat((brick_shift, torch.zeros_like(brick_shift)), dim=2)) b = pattern.box(uv3[:, :, 0:2], torch.cat((mortar_scale, mortar_scale), dim=2)) frag_color = gl.mix(color_mortar, color_brick, b) return frag_color
def shade_mat(self, mortar_scale: Tensor, brick_scale: Tensor) -> Tensor: brick_elongate = self.tensor(2.0) brick_shift = self.tensor(0.5) color_brick = self.tensor((0.69, 0.25, 0.255)) color_mortar = self.tensor((0.9, 0.9, 0.9)) scale = torch.cat([ torch.div(brick_scale, brick_elongate + TINY_FLOAT), brick_scale, brick_scale ], dim=2) uv3 = pattern.tile( Shader.frag_pos(), scale, torch.cat((brick_shift, torch.zeros_like(brick_shift)), dim=2)) b = pattern.box(uv3[:, :, 0:2], torch.cat((mortar_scale, mortar_scale), dim=2)) frag_color = gl.mix(color_mortar, color_brick, b) return frag_color
def shade_mat(self, brick_color1, brick_color2, mortar_color, scale, mortar_size, mortar_smooth, bias, brick_width, row_height, offset_amount, offset_frequency, squash_amount, squash_frequency) -> typing.Tuple[Tensor, Tensor]: f2 = blender.calc_brick_texture(Shader.frag_pos() * scale, mortar_size, mortar_smooth, bias, brick_width, row_height, offset_amount, offset_frequency, squash_amount, squash_frequency) tint = vec.x(f2) f = vec.y(f2) brick_color1 = torch.where(f != 1.0, (1.0 - tint) * brick_color1 + tint * brick_color2, brick_color1) color = gl.mix(brick_color1, mortar_color, f) fac = f return (color, fac)
def shade_iter(self, frag_pos: Tensor, x: Tensor, y: Tensor, a: Tensor) -> Tensor: return gl.mix(x, y, a)
def shade_mat(self, x: Tensor, y: Tensor, a: Tensor) -> Tensor: return gl.mix(x, y, a)
def shade_mat(self, h: Tensor, s: Tensor, v: Tensor) -> Tensor: c = vec.vec3(h, s, v) K = vec.vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0) p = torch.abs(gl.fract(vec.xxx(c) + vec.xyz(K)) * 6.0 - vec.www(K)) return vec.z(c) * gl.mix( vec.xxx(K), torch.clamp(p - vec.xxx(K), 0.0, 1.0), vec.y(c))
def shade_mat(self, scale: Tensor) -> typing.Tuple[Tensor, Tensor]: frag_pos = Shader.frag_pos() * scale Pi0 = torch.floor(frag_pos) Pi1 = Pi0 + vec.vec3(1.0) Pi0 = pn.mod289(Pi0) Pi1 = pn.mod289(Pi1) Pf0 = gl.fract(frag_pos) Pf1 = Pf0 - vec.vec3(1.0) ix = vec.vec4(vec.x(Pi0), vec.x(Pi1), vec.x(Pi0), vec.x(Pi1)) iy = vec.vec4(vec.yy(Pi0), vec.yy(Pi1)) iz0 = vec.zzzz(Pi0) iz1 = vec.zzzz(Pi1) ixy = pn.permute(pn.permute(ix) + iy) ixy0 = pn.permute(ixy + iz0) ixy1 = pn.permute(ixy + iz1) gx0 = ixy0 * (1.0 / 7.0) gy0 = gl.fract(torch.floor(gx0) * (1.0 / 7.0)) - 0.5 gx0 = gl.fract(gx0) gz0 = 0.5 - torch.abs(gx0) - torch.abs(gy0) sz0 = gl.step(gz0, 0.0) gx0 = gx0 - sz0 * (gl.step(0.0, gx0) - 0.5) gy0 = gy0 - sz0 * (gl.step(0.0, gy0) - 0.5) gx1 = ixy1 * (1.0 / 7.0) gy1 = gl.fract(torch.floor(gx1) * (1.0 / 7.0)) - 0.5 gx1 = gl.fract(gx1) gz1 = 0.5 - torch.abs(gx1) - torch.abs(gy1) sz1 = gl.step(gz1, 0.0) gx1 = gx1 - sz1 * (gl.step(0.0, gx1) - 0.5) gy1 = gy1 - sz1 * (gl.step(0.0, gy1) - 0.5) g000 = vec.vec3(vec.x(gx0), vec.x(gy0), vec.x(gz0)) g100 = vec.vec3(vec.y(gx0), vec.y(gy0), vec.y(gz0)) g010 = vec.vec3(vec.z(gx0), vec.z(gy0), vec.z(gz0)) g110 = vec.vec3(vec.w(gx0), vec.w(gy0), vec.w(gz0)) g001 = vec.vec3(vec.x(gx1), vec.x(gy1), vec.x(gz1)) g101 = vec.vec3(vec.y(gx1), vec.y(gy1), vec.y(gz1)) g011 = vec.vec3(vec.z(gx1), vec.z(gy1), vec.z(gz1)) g111 = vec.vec3(vec.w(gx1), vec.w(gy1), vec.w(gz1)) norm0 = pn.taylorInvSqrt(vec.vec4(gl.dot(g000, g000), gl.dot(g010, g010), gl.dot(g100, g100), gl.dot(g110, g110))) g000 = g000 * vec.x(norm0) g010 = g010 * vec.y(norm0) g100 = g100 * vec.z(norm0) g110 = g110 * vec.w(norm0) norm1 = pn.taylorInvSqrt(vec.vec4(gl.dot(g001, g001), gl.dot(g011,g011), gl.dot(g101,g101), gl.dot(g111,g111))) g001 = g001 * vec.x(norm1) g011 = g011 * vec.y(norm1) g101 = g101 * vec.z(norm1) g111 = g111 * vec.w(norm1) n000 = gl.dot(g000, Pf0) n100 = gl.dot(g100, vec.vec3(vec.x(Pf1), vec.yz(Pf0))) n010 = gl.dot(g010, vec.vec3(vec.x(Pf0), vec.y(Pf1), vec.z(Pf0))) n110 = gl.dot(g110, vec.vec3(vec.xy(Pf1), vec.z(Pf0))) n001 = gl.dot(g001, vec.vec3(vec.xy(Pf0), vec.z(Pf1))) n101 = gl.dot(g101, vec.vec3(vec.x(Pf1), vec.y(Pf0), vec.z(Pf1))) n011 = gl.dot(g011, vec.vec3(vec.x(Pf0), vec.yz(Pf1))) n111 = gl.dot(g111, Pf1) fade_xyz = pn.fade(Pf0) n_z = gl.mix(vec.vec4(n000, n100, n010, n110), vec.vec4(n001, n101, n011, n111), vec.z(fade_xyz)) n_yz = gl.mix(vec.xy(n_z), vec.zw(n_z), vec.y(fade_xyz)) n_xyz = gl.mix(vec.x(n_yz), vec.y(n_yz), vec.x(fade_xyz)) fac = 2.2 * n_xyz color = vec.vec3(fac, fac, fac) return (fac, color)