Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                 <div class="world" id="world">


  
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>app_test</title>
    <script>
    </script>
</head>
  
              
            
!

CSS

              
                .world {
	position: fixed;
	width: 100%;
	/* height: 100%; */
	height: auto !important;

	overflow: hidden;
    background: #aeebfa;
}
 
              
            
!

JS

              
                import * as THREE from "https://cdn.jsdelivr.net/npm/three@0.117.1/build/three.module.js";
import * as CANNON from "https://cdn.jsdelivr.net/npm/cannon-es@0.19.0/dist/cannon-es.js";
import Stats from "https://unpkg.com/three@0.117.1/examples/jsm/libs/stats.module";
import { CSM } from "https://cdn.jsdelivr.net/npm/three@0.117.1/examples/jsm/csm/CSM.js";
import { OrbitControls } from "https://cdn.jsdelivr.net/npm/three@0.117.1/examples/jsm/controls/OrbitControls.js";

var container,
  renderer,
  scene,
  camera,
  ambient_light,
  controls,
  csm,
  stats,
  world,
  clock;
var is_touch = false;
var is_mobile;
const random = (min, max) => Math.floor(Math.random() * (max - min + 1) + min);
//material
var m = {};
//geometry
var g = {};
//objects
var o = {};

var data = {
  
  area: 20,
  n_boat: 10,
 
  orthographic: false,
  fade: false,
  far: 100,
  posy: 1.2,
  mode: "practical",
  lightX: -1,
  lightY: -1,
  lightZ: -1,
  margin: 100,
  lightFar: 500,
  lightNear: 1,

  degtorad: 0.01745327,
  game_over: false,
};

data.ecart = -data.area - data.area * 0.5;

function Minimal() {
  //css
  container = document.getElementById("world");
  scene = new THREE.Scene();
  // camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 5000);
  camera = new THREE.PerspectiveCamera(
    70,
    window.innerWidth / window.innerHeight,
    1,
    1e6
  );

  camera.position.x = 9.03217677433446;
  camera.position.y = 30.44400367439401;
  camera.position.z = -18.538510785343405;

  camera.rotation.x = -2.11;
  camera.rotation.y = 0.248;
  camera.rotation.z = 2.75;

  renderer = new THREE.WebGLRenderer({
    antialias: true,
    alpha: true
  });

  renderer.setPixelRatio(Math.max(1, window.devicePixelRatio / 2));
  renderer.setSize(window.innerWidth, window.innerHeight);
  //Shadows
  renderer.shadowMap.enabled = true;
  renderer.shadowMap.type = THREE.PCFSoftShadowMap;

  const axesHelper = new THREE.AxesHelper(50);
  scene.add(axesHelper);

  //  ORBIT
  controls = new OrbitControls(camera, renderer.domElement);
  controls.maxPolarAngle = Math.PI / 2;
  // camera.position.set(60, 10, 0);
  controls.target = new THREE.Vector3(0, 0, 0);
  controls.update();

  ambient_light = new THREE.AmbientLight(0xffffff, 0.5);
  // ambient_light = new THREE.AmbientLight(0xffffff, 0.05);
  scene.add(ambient_light);

  //PLUG IN SHADOW FOR HUGE SCENE
  csm = new CSM({
    maxFar: data.far,
    cascades: 4,
    mode: data.mode,
    parent: scene,
    // shadowMapSize: 2048,
    shadowMapSize: 2018,
    lightDirection: new THREE.Vector3(
      data.lightX,
      data.lightY,
      data.lightZ
    ).normalize(),
    camera: camera
  });

  csm.fade = true;
  //FPS
  stats = new Stats();
  stats.domElement.style.left = "12px";
  stats.showPanel(0); // 0: fps, 1: ms, 2: mb, 3+: custom

  // append to container
  //container.appendChild(stats.domElement);
  container.appendChild(renderer.domElement);

  window.addEventListener(
    "resize",
    function () {
      camera.aspect = window.innerWidth / window.innerHeight;
      // camera.updateProjectionMatrix();
      // updateOrthoCamera();
      // csm.updateFrustums();
      renderer.setSize(window.innerWidth, window.innerHeight);
    },
    false
  );
  renderer.domElement.addEventListener("touchstart", on_touch_start, false);
  renderer.domElement.addEventListener("touchend", on_touch_down, false);
  console.log(scene);

  function on_touch_start() {
    console.log("start");
    is_touch = true;
  }

  function on_touch_down() {
    console.log("stop");
    is_touch = false;
  }
}

class Boat {
  constructor(config) {
    var g = {};
    var m = {};
    this.config = config;
    g.body = new THREE.SphereGeometry(0.5, 32, 16);
    m.body = new THREE.MeshToonMaterial({
      color: this.config.color_body
    });
    csm.setupMaterial(m.body);

    this.mesh = new THREE.Mesh(g.body, m.body);
    this.mesh.castShadow = true;
    // this.mesh.receiveShadow = true
    //////////////////////////////////////////////////////////////////////

    this.config.scene.add(this.mesh);
  }
}

function deg_to_rad(degrees) {
  var pi = Math.PI;
  return degrees * (pi / 180);
}

let oldElapsedTime = 0;
/////////////////////////////////////////////////////////////////////////////////
init_app();

function init_app() {
  console.log("init_app");
  init();
  animate();
}

function init() {
  clock = new THREE.Clock();
  Minimal();
  //create_physic_world();
  create_virtual_world();
  // create_joystick();
}

function create_virtual_world() {
  create_boat_player();
  create_sea();
  create_ground_sea();
}

function create_ground_sea() {
  g.ground_sea = new THREE.PlaneGeometry(data.area, data.area, 1, 1);
  let rad = deg_to_rad(90);
  g.ground_sea.rotateX(-rad);
  m.ground_sea = new THREE.MeshPhongMaterial({
    color: 0x4f9789,
    shininess: 0
    // wireframe: true,
  });
  csm.setupMaterial(m.ground_sea);
  o.ground_sea = new THREE.Mesh(g.ground_sea, m.ground_sea);
  o.ground_sea.castShadow = true;
  o.ground_sea.receiveShadow = true;
  o.ground_sea.position.y = -3;
  scene.add(o.ground_sea);
  o.ground_sea.renderOrder = 1;
  console.log(o.ground_sea.geometry);
}

function create_boat_player() {
  o.boat = [];
  for (let i = 0; i < data.n_boat; i++) {
    let c = {
      color_body: 0xffff00,
      scene: scene,
      world: world
    };

    console.log(c.position);
    o.boat[i] = new Boat(c);
    o.boat[i].mesh.position.set(random(-8,8),0,random(-8,8))
  }
}

function create_sea() {
  g.sea = new THREE.PlaneGeometry(data.area, data.area, 20, 20);
  let rad = deg_to_rad(90);
  g.sea.rotateX(-rad);
  m.sea = new THREE.MeshToonMaterial({
    color: 0x4f9789,
    shininess: 0,
    transparent: true,
    opacity: 0.8
  });
  csm.setupMaterial(m.sea);
  o.sea = new THREE.Mesh(g.sea, m.sea);
  o.sea.castShadow = true;
  o.sea.receiveShadow = true;
  scene.add(o.sea);
  o.sea.renderOrder = 1;
}

function init_physics() {
  world = new CANNON.World();
  world.gravity.set(0, -9.82, 0);
}

function animate() {
  for (let i = 0; i < data.n_boat; i++) {
    //o.boat[i].animate(o.ball[i].mesh)
  }
  const t = clock.getElapsedTime();
  const deltaTime = t - oldElapsedTime;
  oldElapsedTime = t;

  o.sea.geometry.vertices.map((v) => {
    let longueur_wave = 0.2; // plus le chiffre est grand, plus les vagues sont courtes
    let hauteur_wave = 1; // plus le chiffre est grand, plus les vagues sont hautes
    v.y = hauteur_wave * Math.sin(v.x * longueur_wave + t);
    for (let i = 0; i < data.n_boat; i++) {
      o.boat[i].mesh.position.y = hauteur_wave * Math.sin(o.boat[i].mesh.position.x * longueur_wave + t);
     // o.boat[i].mesh.position.y = v.y;
    }
  });

  o.sea.geometry.verticesNeedUpdate = true;
  renderer.render(scene, camera);
  window.requestAnimationFrame(animate);
}

              
            
!
999px

Console