用glsl编写早上描述一块大陆的海拔值,要求大陆上有山脊有平原 还有3个小湖泊
- User
用glsl编写早上描述一块大陆的海拔值,要求大陆上有山脊有平原 还有3个小湖泊
- Assistant
在GLSL中,我们可以通过噪声函数(如 Perlin 噪声 或 Simplex 噪声)来生成具有连续性质的土地形状。我们可以利用这些生成的海拔高度来生成山脊、平原和湖泊。
以下是一个简单的 GLSL 代码,演示了如何在一个给定的大陆上生成山脊、平原及三个小湖泊的海拔值:
// The following noise functions are for illustration purposes. // In an actual shader, you should replace these functions with a proper implementation of Perlin or Simplex noise. float noise(float x) { return fract(sin(x * 12.9898) * 43758.5453); } float noise2D(vec2 v) { float t = v.x + v.y * 57.0; return noise(t); } // Combines different scales of noise to create fractal noise float fractalNoise(vec2 v, float roughness, float frequency) { float scale = 1.0; float height = 0.0; for (int i = 0; i < 4; ++i) { height += noise2D(v * frequency) * scale; scale *= 0.5; frequency *= 2.0; } return height * roughness; } float elevation(vec2 pos) { vec2 terrainSize = vec2(512.0, 512.0); vec2 uv = pos / terrainSize; // Elevation for ridges float ridge = 1.0 - abs(fractalNoise(uv, 0.5, 6.0) - 0.5) * 2.0; // Elevation for plains float plain = fractalNoise(uv, 0.2, 2.0); // Elevation for lakes float lake1 = 1.0 - smoothstep(0.2, 0.5, distance(uv, vec2(0.25, 0.35))) * 1.5; float lake2 = 1.0 - smoothstep(0.2, 0.5, distance(uv, vec2(0.6, 0.4))) * 1.5; float lake3 = 1.0 - smoothstep(0.2, 0.5, distance(uv, vec2(0.45, 0.65))) * 1.5; float lakes = min(min(lake1, lake2), lake3); // Combine elevations float elevation = mix(plain, ridge, lakes); elevation = max(elevation, 0.0); return elevation; } - User
我复制粘贴你的代码到glsl编辑器里面只显示了一张白色的图片
- Assistant
抱歉这个给的单独的 elevation 函数不能直接应用到 GLSL 片段着色器中显示。以下是我重新改写的 GLSL 代码,可以直接运行在 GLSL 沙盒中显示出地形效果。
#ifdef GL_ES precision mediump float; #endif uniform float u_time; uniform vec2 u_resolution; uniform vec2 u_mouse; // 节点函数 float noise(float x) { return fract(sin(x * 12.9898) * 43758.5453); } float noise2D(vec2 v) { float t = v.x + v.y * 57.0; return noise(t); } // 多重尺度的噪声 float fractalNoise(vec2 v, float roughness, float frequency) { float scale = 1.0; float height = 0.0; for (int i = 0; i < 4; ++i) { height += noise2D(v * frequency) * scale; scale *= 0.5; frequency *= 2.0; } return height * roughness; } float elevation(vec2 pos) { vec2 terrainSize = vec2(512.0, 512.0); vec2 uv = pos / terrainSize; // 山脊海拔 float ridge = 1.0 - abs(fractalNoise(uv, 0.5, 6.0) - 0.5) * 2.0; // 平原海拔 float plain = fractalNoise(uv, 0.2, 2.0); // 湖泊海拔 float lake1 = 1.0 - smoothstep(0.2, 0.5, distance(uv, vec2(0.25, 0.35))) * 1.5; float lake2 = 1.0 - smoothstep(0.2, 0.5, distance(uv, vec2(0.6, 0.4))) * 1.5; float lake3 = 1.0 - smoothstep(0.2, 0.5, distance(uv, vec2(0.45, 0.65))) * 1.5; float lakes = min(min(lake1, lake2), lake3); // 混合海拔 float elevation = mix(plain, ridge, lakes); elevation = max(elevation, 0.0); return elevation; } void main() { vec2 uv = gl_FragCoord.xy; // 获取当前片段位置 // 获取该片段的海拔高度 float height = elevation(gl_FragCoord.xy); float heightColor = smoothstep(0.0, 1.0, height); // 根据海拔高度绘制颜色 vec3 color = vec3(0.0); if (heightColor < 0.2) { color = vec3(0.0, 0.0, 1.0); // 湖泊颜色 - 蓝色 } else if(heightColor < 0.4){ color = vec3(0.0, 1.0, 0.0); // 平原颜色 - 绿色 } else { color = vec3(1.0, 1.0, 1.0); // 山脊颜色 - 白色 } gl_FragColor = vec4(color, 1.0); }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.