// GLSL 2D simplex noise function // Author : Ian McEwan, Ashima Arts //…
- User
// GLSL 2D simplex noise function // Author : Ian McEwan, Ashima Arts // https://github.com/ashima/webgl-noise // vec3 permute(vec3 x) { return mod(((x*34.0)+1.0)*x, 289.0); }
float snoise(vec2 v){ const vec4 C = vec4(0.211324865405187, 0.366025403784439, -0.577350269189626, 0.024390243902439); vec2 i = floor(v + dot(v, C.yy) ); vec2 x0 = v - i + dot(i, C.xx); vec2 i1; i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0); vec4 x12 = x0.xyxy + C.xxzz; x12.xy -= i1; i = mod(i, 289.0); vec3 p = permute( permute( i.y + vec3(0.0, i1.y, 1.0 ))
- i.x + vec3(0.0, i1.x, 1.0 )); vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy), dot(x12.zw,x12.zw)), 0.0); m = mm ; m = mm ; vec3 x = 2.0 * fract(p * C.www) - 1.0; vec3 h = abs(x) - 0.5; vec3 ox = floor(x + 0.5); vec3 a0 = x - ox; m = 1.79284291400159 - 0.85373472095314 * ( a0a0 + h*h ); vec3 g; g.x = a0.x * x0.x + h.x * x0.y; g.yz = a0.yz * x12.xz + h.yz * x12.yw; return 130.0 * dot(m, g); } 帮我翻译为python并补充缺少的函数
- Assistant
这里是将上述 GLSL 函数翻译成 Python(需安装 numpy 和 numba 包):
import numpy as np from numba import jit @jit(nopython=True) def permute(x): return np.mod(((x * 34.0) + 1.0) * x, 289.0) @jit(nopython=True) def snoise(v): C = np.array([0.211324865405187, 0.366025403784439, -0.577350269189626, 0.024390243902439]) i = np.floor(v + np.dot(v, C[1:3])) x0 = v - i + np.dot(i, C[0:2]) i1 = np.array([1.0, 0.0]) if x0[0] > x0[1] else np.array([0.0, 1.0]) x12 = np.outer(x0, np.array([1, 1, 1, 1])) + np.outer(np.array([1, 1]), C[[0, 2]]) x12[:, :2] -= i1 i = np.mod(i, 289.0) p = permute(permute(i[1] + np.array([0.0, i1[1], 1.0])) + i[0] + np.array([0.0, i1[0], 1.0])) m = np.maximum(0.5 - np.array([np.dot(x0, x0), np.dot(x12[:, :2], x12[:, :2].T), np.dot(x12[:, 2:], x12[:, 2:].T)]), 0.0) m = m * m m = m * m x = 2.0 * np.mod(p * C[3], 1) - 1.0 h = np.abs(x) - 0.5 ox = np.floor(x + 0.5) a0 = x - ox m *= 1.79284291400159 - 0.85373472095314 * (a0 * a0 + h * h) g = np.array([a0[0] * x0[0] + h[0] * x0[1], a0[1] * x12[0, 2] + h[1] * x12[0, 3], a0[2] * x12[1, 2] + h[2] * x12[1, 3]]) return 130.0 * np.dot(m, g)您可以使用
numpy中的array和mod函数,以及其他像dot()和floor()的数学函数。 请注意,返回值可能会有轻微的不同,因为 GLSL 和 Python 处理浮点数的方式可能有些不同。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.