three实现三维地形可调节参数的生成器代码

代码语言:html

所属分类:三维

代码描述:three实现三维地形可调节参数的生成器代码

代码标签: three 三维 地形 调节 参数 生成器 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  


  
  
<style>
* {
  margin: 0;
  padding: 0;
}
canvas {
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  margin: 0;
  padding: 0;
}

#heatMap {
  position: absolute;
  top: 0;
  left: 0;
  width: 10vw;
  height: 100%;
  border: 1px solid black;
  display: none;
}
</style>

  
</head>

<body translate="no">

<canvas id="heatMap"></canvas>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/es-module-shims.1.6.3.js"></script>
<script type="importmap">
  {
    "imports": {      
      "three": "//repo.bfw.wiki/bfwrepo/js/module/three/build/160/three.module.js",
      "three/addons/": "//repo.bfw.wiki/bfwrepo/js/module/three/examples/160/jsm/"
    }
  }
</script>
  
      <script type="module">

"use strict";

import * as THREE from "three";

import { OrbitControls as e } from "three/addons/controls/OrbitControls.js";

import { ImprovedNoise as t } from "three/addons/math/ImprovedNoise.js";

import { OBJExporter as n } from "three/addons/exporters/OBJExporter.js";

import { GUI as o } from "three/addons/libs/lil-gui.module.min.js";

!function () {
  function a() {
    y = 1e3, T = 420, M = 450, b = 501, C = 3, R = 4, S = 5;
  }
  function i() {
    h.remove(x), x.material.dispose(), x.geometry.dispose(), r();
  }
  function r() {
    const e = function (e, n) {
      const o = e * n;
      let a = [...Array(o)].map(() => 0),i = new t(),r = 100 * Math.random(),d = 1;
      for (let t = 0; t < R; t++) {
        for (let t = 0; t < o; t++) {
          let n = t % e,o = ~~(t / e);
          a[t] += Math.abs(i.noise(n / d, o / d, r) * d);
        }
        d *= S;
      }
      let s = new THREE.Vector3(e / 2, n / 2, 0),l = Math.floor(Math.min(e / 2, n / 2)),c = (Math.floor(Math.max(e / 2, n / 2)),
      l * T * 2 / y),u = l * M * 2 / y;
      for (let t = 0; t < o; t++) {
        let n = t % e,o = ~~(t / e),i = new THREE.Vector3(n, o, 0);
        var m = Math.abs(i.distanceTo(s));
        m >= l - .5 ? a[t] = -11 : m >= c && m < u ? a[t] = 0 : m >= u ? (a[t] = -Math.abs(a[t]),
        a[t] *= Math.tan((m - u) / l * Math.PI) * C, a[t] < -11 && (a[t] = -11)) : a[t] *= Math.cos(m / c * (Math.PI / 2)) * C;
      }
      return a;
    }(b, b);
    (g = new THREE.PlaneGeometry(y, y, b - 1, b - 1)).rotateX(-Math.PI / 2);
    const n = g.attributes.position.array;
    let o,a = 0;
    for (let t = 0, i = 0, r = n.length; t < r; t++, i += 3) o = 1 * e[t], n[i + 1] = o,
    a < o && (a = o);
    g.translate(0, -1 * C, 0), g.attributes.position.needsUpdate = !0, g.computeVertexNormals(),
    g.computeBoundingBox();
    let i = {
      colorTexture: {
        value: _ },

      limits: {
        value: a } };


    E = new THREE.MeshLambertMaterial({
      side: THREE.DoubleSide,
      onBeforeCompile: e => {
        e.uniforms.colorTexture = i.colorTexture, e.uniforms.limits = i.limits, e.vertexShader = `\n      varying vec3 vPos;\n      ${e.vertexShader}\n    `.replace("#include <fog_vertex>", "#include <fog_vertex>\n      vPos = vec3(position);\n      "),
        e.fragmentShader = `\n      uniform float limits;\n      uniform sampler2D colorTexture;\n      \n      varying vec3 vPos;\n      ${e.fragmentShader}\n    `.replace("vec4 diffuseColor = vec4( diffuse, opacity );", "\n        float h = (vPos.y - (-limits))/(limits * 2.);\n        h = clamp(h, 0., 1.);\n        vec4 diffuseColor = texture2D(colorTexture, vec2(0, h));\n      ");
      } }),
    (x = new THREE.Mesh(g.........完整代码请登录后点击上方下载按钮下载查看

网友评论0