123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <head>
  <title>Test génération terrain</title>
	<script type="text/javascript" src="http://threejs.org/build/three.js"></script>
	<script type="text/javascript" src="http://threejs.org/examples/js/libs/stats.min.js"></script>
	<script type="text/javascript" src="http://threejs.org/examples/js/controls/OrbitControls.js"></script>
  <script src="http://threejs.org/examples/js/modifiers/SubdivisionModifier.js"></script>
  <script src="http://threejs.org/examples/js/modifiers/SimplifyModifier.js"></script>
</head>
<body>
		<div id="Stats-output"></div>
		 <div id='title'>Test génération terrain</div>
		<div id="output"></div>
</body>
            
          
!
            
              body {
  margin: 0;
  overflow: hidden;
}

#title {
  position: absolute;
	font-size: 75px;
	width: 100%;
	text-align: center;
}

            
          
!
            
              let glob_render;
// once everything is loaded, we run our Three.js stuff.
var geom;
let inter = undefined;
let camera;
let vertices;
let new_vertices = false;

function init() {
  var stats = initStats();

  // create a scene, that will hold all our elements such as objects, cameras and lights.
  window.scene = new THREE.Scene();
  var light = new THREE.AmbientLight( 0xffffff );
  scene.add( light );

  var spotLight = new THREE.SpotLight( {color: 0xffffff, angle: 1, intensity: 50, distance: 2000000} );
  spotLight.position.set( -50000, 5000, -30000 );
  //scene.add( spotLight );

  var spotLightHelper = new THREE.SpotLightHelper( spotLight );
  scene.add( spotLightHelper );

  var axesHelper = new THREE.AxesHelper( 200 );
  scene.add( axesHelper );

  // create a camera, which defines where we're looking at.
  camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000000);

  // create a render and set the size
  var renderer = new THREE.WebGLRenderer({antialias: false});

  renderer.setClearColor(new THREE.Color(0xAFAFAF));
  renderer.setSize(window.innerWidth, window.innerHeight);
  renderer.shadowMap.enabled = true;
  renderer.shadowMap.type = THREE.PCFSoftShadowMap;

  // position and point the camera to the center of the scene
  camera.position.x = -54688;
  camera.position.y = 65180;
  camera.position.z = 52541;
  camera.lookAt(new THREE.Vector3(0, 0, 0));

  var controls = new THREE.OrbitControls( camera, renderer.domElement );
  controls.maxPolarAngle = Math.PI * 0.5;
  controls.minDistance = 0;
  controls.maxDistance = 100000;

  // add the output of the renderer to the html element
  document.getElementById("output").appendChild(renderer.domElement);

  // call the render function
  var step = 0;

  let AMOUNTX = 100;
  let count = 0;

  geom = new THREE.BufferGeometry();
  vertices = new Array(448200);
  geom.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array(vertices), 3 ));
  geom.attributes.position.dynamic = true;
  geom.attributes.position.count = 0;

  let prev_vert = [];
  let curr_vert = [];

  var material = new THREE.MeshPhongMaterial( {
    color: 0xFF1060,
    wireframe: true,
  } );

  var mesh = new THREE.Mesh(geom, material);

  mesh.castShadow = true;
  mesh.receiveShadow = true;
  mesh.name = 'bathy';


  scene.add(mesh);
  render();

  let counter = 0;
  let offset_x = -50000;

  let y_ref = [];
  let del = 500;

  var color = new THREE.Color();

  for (let idx = 0; idx < AMOUNTX; idx++) {
    x = counter * 500 + offset_x + (Math.random() * ( del + del ) - del);
    curr_vert.push(x);
    z = (idx - (AMOUNTX/2)) * 1000 + (Math.random() * ( del + del ) - del);
    y = (((Math.sin((z + count) * 0.4) + 1) * 200 * (Math.random() * (5 - (-5)) - 5).toFixed(4) ) + ((Math.sin((x + count) * 0.6) + 1) * 150 * (Math.random() * (5 - (-5)) - 5).toFixed(4)));
    curr_vert.push(y);
    y_ref.push(y);
    curr_vert.push(z);
  }

  let max = 1;
  let min = 0;
  let way = 1;
  inter = setInterval(() => {
    if(counter >= 250) {
      clearInterval(inter);
      return;
    }
    prev_vert = curr_vert;
    curr_vert = [];
    let vert_idx = 0
    if (counter > 150 && counter < 175) {
      way += 125;
    }
    else if(counter >75 && counter < 100) {
      way += 75;
    }
    else if((counter < 75 || counter > 200) && way > 1) {
      way -= 100;
    }
    for (let idx = 0; idx < AMOUNTX; idx++) {
      x = (counter * 500 + offset_x) + (Math.random() * ( del + del ) - del);
      curr_vert.push(x);

      vert_idx++;

      let off = 1;
      if ((counter > 50 && counter < 100) || (counter > 175 && counter < 200)) {
        way = counter * 25;
        if (idx > AMOUNTX / 4 && idx < AMOUNTX / 2) {
          way += 1000;
        }
        if (idx > AMOUNTX / 3 && idx < AMOUNTX / 2) {
          way += 1000;
        }
      }
      for(let jdx = 0; jdx < Math.random() * 100; jdx++) {
        off = Math.random() * (max - min) + min;
      }
      y = y_ref[vert_idx] * off + way;
      curr_vert.push(y);

      vert_idx++;

      z = (idx - (AMOUNTX/2)) * 1000 + (Math.random() * ( del + del ) - del);
      curr_vert.push(z);
    }
    count += 0.01;
    counter++;

    if (counter > 1) {
      for (idx = 0; idx < curr_vert.length; idx+=3) {
        let curIdx = geom.attributes.position.count;
        vertices[curIdx++] = curr_vert[idx];
        vertices[curIdx++] = curr_vert[idx + 1];
        vertices[curIdx++] = curr_vert[idx + 2];

        vertices[curIdx++] = prev_vert[idx + 3];
        vertices[curIdx++] = prev_vert[idx + 4];
        vertices[curIdx++] = prev_vert[idx + 5];

        vertices[curIdx++] = prev_vert[idx];
        vertices[curIdx++] = prev_vert[idx + 1];
        vertices[curIdx++] = prev_vert[idx + 2];

        vertices[curIdx++] = prev_vert[idx + 3];
        vertices[curIdx++] = prev_vert[idx + 4];
        vertices[curIdx++] = prev_vert[idx + 5];

        vertices[curIdx++] = curr_vert[idx + 3];
        vertices[curIdx++] = curr_vert[idx + 4];
        vertices[curIdx++] = curr_vert[idx + 5];

        vertices[curIdx++] = curr_vert[idx];
        vertices[curIdx++] = curr_vert[idx + 1];
        vertices[curIdx++] = curr_vert[idx + 2];

        vertices[curIdx++] = prev_vert[idx + 3];
        vertices[curIdx++] = prev_vert[idx + 4];
        vertices[curIdx++] = prev_vert[idx + 5];

        geom.attributes.position.count = curIdx;
      }
      new_vertices = true;
    }
  }, 100);

  function render() {
    requestAnimationFrame(render);
    stats.update();

    if (new_vertices) {
      let foo = new Float32Array(vertices);
      geom.attributes.position.array = foo;

      geom.attributes.position.needsUpdate = true;
      geom.computeVertexNormals();
      geom.normalizeNormals();

    }
    // render using requestAnimationFrame
    renderer.render(scene, camera);
    new_vertices = false;
  }

  glob_render = render;

  function initStats() {

    var stats = new Stats();

    stats.setMode(0); // 0: fps, 1: ms

    // Align top-left
    stats.domElement.style.position = 'absolute';
    stats.domElement.style.left = '0px';
    stats.domElement.style.top = '0px';

    document.getElementById("Stats-output").appendChild(stats.domElement);

    return stats;
  }
}
window.onload = init


            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console