Skip to content
The Chat Archive
gpt-4-0314· OpenAI WildChat-4.8M (AI2)

用glsl编写早上描述一块大陆的海拔值,要求大陆上有山脊有平原 还有3个小湖泊

  1. User

    用glsl编写早上描述一块大陆的海拔值,要求大陆上有山脊有平原 还有3个小湖泊

  2. Assistant

    要用GLSL描述一个早晨的大陆,我们可以创建一个随机噪声来生成地形,并通过改变噪声频率和幅度,可以创建山脊、平原和小湖泊。以下是一个包含山脊、平原和三个小湖泊的GLSL代码示例:

    #ifdef GL_ES
    precision mediump float;
    #endif
    
    uniform float u_time;
    uniform vec2 u_resolution;
    varying vec2 v_texCoord;
    
    const float PI = 6.28318;
    const float INV_TAU = 0.15915;
    const float persistence = 0.5;
    const int octaves = 6;
    
    float hash(vec2 p) {
        return fract(sin(dot(p ,vec2(672.0, 161.0))) * 67469.123);
    }
    
    // 基本次数噪声值
    float noise(vec2 p) {
        vec2 i = floor(p);
        vec2 f = fract(p);
        vec2 u = f * f * (3.0 - 2.0 * f);
        return mix(mix(hash(i + vec2(0.0, 0.0)),
                      hash(i + vec2(1.0, 0.0)), u.x),
                   mix(hash(i + vec2(0.0, 1.0)),
                      hash(i + vec2(1.0, 1.0)), u.x), u.y);
    }
    
    // 分形次数噪声值
    float fbm(vec2 p) {
        float amplitude = 1.0;
        float frequency = 1.0;
        float sum = 0.0;
    
        for (int i = 0; i < octaves; ++i) {
        sum += amplitude * noise(p * frequency);
        amplitude *= persistence;
        frequency *= 2.0;
        }
    
    return sum;
    }
    
    // 计算大陆海拔高度
    float terrain(vec2 p) {
        return fbm(0.25 * p);
    }
    
    void main(void) {
        vec2 uv = v_texCoord * u_resolution;
        uv /= 1024.0; // 归一化UV坐标
    
        // 计算地形高度
        float height = terrain(uv);
    
        // 创建山脊
        float ridge = smoothstep(0.15, 0.18, height);
    
        // 创建平原
        float plain = smoothstep(0.1, 0.15, height);
    
        // 创建湖泊
        float lake1 = smoothstep(0.08, 0.1, length(uv - vec2(0.5, 0.35)));
        float lake2 = smoothstep(0.08, 0.1, length(uv - vec2(0.2, 0.8)));
        float lake3 = smoothstep(0.08, 0.1, length(uv - vec2(0.9, 0.7)));
    
        // 最终输出
        vec3 finalColor = vec3(height);
        finalColor *= (1.0 - ridge) * plain + (1.0 - lake1 - lake2 - lake3);
        finalColor.r *= 0.7 + 0.5 * ridge;
        finalColor.g *= 0.7 + 0.5 * ridge;
        finalColor.b *= 0.75 + 0.5 * ridge;
        gl_FragColor = vec4(finalColor, 1.0);
    }
    
  3. User

    我复制你的代码到glsl到编辑器只看到一张纯白的图片

  4. Assistant

    对不起,我的失误。这里是一个包含地形特征的改进版GLSL代码:

    precision highp float;
    
    uniform float u_time;
    uniform vec2 u_resolution;
    
    vec2 hash22(vec2 p) {
      p = fract(p * vec2(5.3983, 5.3101));
      p *= dot(p, p + 33.333);
      return fract((vec2(161.196, 27.039) + 13.109218 * sin(u_time)) * p);
    }
    
    float noise(vec2 p) {
      vec3 i = floor(vec3(p, p + 1.)).xyy;
      vec2 f = fract(p);
      vec2 w = f * f * (3. - 2. * f);
      return mix(
        mix(
          dot(i.xyp, p), dot(i.zzp, p - vec2(1., 0.)), w.x
        ),
        mix(
          dot(i.zyx, p - vec2(0., 1.)), dot(i.wzw, p - vec2(1., 1.)), w.x
        ),
        w.y
      );
    }
    
    float terrain(vec2 p) {
      float a = 0.;
      float b = 0.8;
      for ( int i = 0; i < 6; ++i ) {
        a += b * noise(p);
        b *= 0.5;
        p *= 2.;
      }
      return a;
    }
    
    void main(void) {
      vec2 uv = gl_FragCoord.xy / u_resolution;
    
      float boundary = 0.1;
    
      // 计算地形高度
      float height = terrain(uv * 4.0);
    
      // 创建山脊
      float ridge = smoothstep(boundary + 0.1, boundary + 0.15, height);
    
      // 创建平原
      float plain = smoothstep(boundary, boundary + 0.1, height);
    
      // 创建湖泊
      float d1 = length(uv - vec2(0.4, 0.4));
      float lake1 = smoothstep(boundary, boundary + 0.2, d1);
    
      float d2 = length(uv - vec2(0.7, 0.6));
      float lake2 = smoothstep(boundary, boundary + 0.1, d2);
    
      float d3 = length(uv - vec2(0.6, 0.34));
      float lake3 = smoothstep(boundary, boundary + 0.15, d3);
    
      float combinedLakes = max(max(lake1, lake2), lake3);
    
      float terrainWeight = (plain + ridge) * (1. - combinedLakes);
    
      // 最终输出
      vec3 color = terrainWeight * vec3(0.5, 0.85, 0.75);
    
      color = mix(color, vec3(0.0, 0.5, 1.0), combinedLakes);
    
      gl_FragColor = vec4(color, 1.0);
    }
    

    这个新版本的代码应该可以显示地形特征,含有山脊、平原和三个湖泊。请尝试在GLSL编辑器中运行这个代码。

  5. User

    dot(i.xyp, p), dot(i.zzp, p - vec2(1., 0.)), w.x这行报错x'xyp' : illegal - vector component fields not from the same set

  6. Assistant

    非常抱歉,让我们对这段代码进行一些修改,以确保对每个组件的访问都正确。以下是修复了该问题的代码:

    precision highp float;
    
    uniform float u_time;
    uniform vec2 u_resolution;
    
    vec2 hash22(vec2 p) {
      p = fract(p * vec2(5.3983, 5.3101));
      p *= dot(p, p + 33.333);
      return fract((vec2(117.166, 79.029) + 13.109218 * sin(u_time)) * p);
    }
    
    float noise(vec2 p) {
      vec3 i = floor(vec3(p, p + 1.)).xyy;
      vec2 f = fract(p);
      vec2 w = f * f * (3. - 2. * f);
    
      return mix(
        mix(
          dot(vec3(i.x, i.y, p.x), p), dot(vec3(i.z, i.y, p.x - 1.), p - vec2(1., 0.)), w.x
        ),
        mix(
          dot(vec3(i.x, i.z, p.x), p - vec2(0., 1.)), dot(vec3(i.z, i.z, p.x - 1.), p - vec2(1., 1.)), w.x
        ),
        w.y
      );
    }
    
    float terrain(vec2 p) {
      float a = 0.;
      float b = 0.8;
      for ( int i = 0; i < 6; ++i ) {
        a += b * noise(p);
        b *= 0.5;
        p *= 2.;
      }
      return a;
    }
    
    void main(void) {
      vec2 uv = gl_FragCoord.xy / u_resolution;
    
      float boundary = 0.1;
    
      // 计算地形高度
      float height = terrain(uv * 4.0);
    
      // 创建山脊
      float ridge = smoothstep(boundary + 0.1, boundary + 0.15, height);
    
      // 创建平原
      float plain = smoothstep(boundary, boundary + 0.1, height);
    
      // 创建湖泊
      float d1 = length(uv - vec2(0.4, 0.4));
      float lake1 = smoothstep(boundary, boundary + 0.2, d1);
    
      float d2 = length(uv - vec2(0.7, 0.6));
      float lake2 = smoothstep(boundary, boundary + 0.1, d2);
    
      float d3 = length(uv - vec2(0.6, 0.34));
      float lake3 = smoothstep(boundary, boundary + 0.15, d3);
    
      float combinedLakes = max(max(lake1, lake2), lake3);
    
      float terrainWeight = (plain + ridge) * (1. - combinedLakes);
    
      // 最终输出
      vec3 color = terrainWeight * vec3(0.5, 0.85, 0.75);
    
      color = mix(color, vec3(0.0, 0.5, 1.0), combinedLakes);
    
      gl_FragColor = vec4(color, 1.0);
    }
    

    现在代码应该可以在GLSL编辑器中正常工作,显示地形特征。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.