Pen Settings

HTML

CSS

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

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

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.

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

              
                
              
            
!

CSS

              
                body { margin: 0; }
			canvas { width: 100%; height: 100% }

button {
	background: white;
	color: black;
	border: 1px solid black;
	padding: .5em 1em;
	position: absolute;
	top: 1em;
	left: 1em;
	cursor: pointer;
}
              
            
!

JS

              
                import * as THREE from "https://unpkg.com/three@0.125.1/build/three.module.js";
import { GLTFLoader } from "https://unpkg.com/three@0.125.1/examples/jsm/loaders/GLTFLoader.js";
import { Flow } from "https://unpkg.com/three@0.125.1/examples/jsm/modifiers/CurveModifier.js";
import { OrbitControls } from "https://unpkg.com/three@0.125.1/examples/jsm/controls/OrbitControls.js";

class Scene {
  constructor(sketch, settings) {
    this.sketch = sketch;
    this.settings = { ...settings };

    this.scene = new THREE.Scene();
    this.scene.background = new THREE.Color(0xffffff);
    // this.scene.fog = new THREE.FogExp2(0x222277, 0.2);
    return this.scene;
  }
}
class Renderer {
  constructor(sketch, settings) {
    this.sketch = sketch;
    this.settings = { ...settings };
    this.renderer = new THREE.WebGLRenderer({ antialias: true });
    this.renderer.setSize(this.sketch.sizes.width, this.sketch.sizes.height);
    this.renderer.setPixelRatio(Math.min(2, window.devicePixelRatio));
    this.renderer.shadowMap.enabled = true;
		this.renderer.outputEncoding = THREE.sRGBEncoding;

    this.renderer.update = this.update.bind(this.sketch);

    return this.renderer;
  }
  update() {
    this.renderer.render(this.scene, this.camera);
  }
}
class Camera {
  constructor(sketch, settings) {
    this.sketch = sketch;
    this.settings = { ...settings };

    this.camera = new THREE.PerspectiveCamera(
      75,
      this.sketch.sizes.width / this.sketch.sizes.height,
      0.01,
      500
    );
   this.camera.position.x = -50;
    this.camera.position.y = 50;
    this.camera.position.z = 50;
    this.sketch.scene.add(this.camera);

    return this.camera;
  }
}
class Animator {
  constructor(sketch, settings) {
    this.sketch = sketch;
    this.settings = { ...settings };

    this.tasks = [];
  }
  /**
   *
   * @param {function} fn
   */
  add(fn) {
    this.tasks.push(fn);
  }
  animate() {
    requestAnimationFrame(this.animate.bind(this));

    this.tasks.forEach((task) => task());

    this.sketch.renderer.update();
  }
}
class Controls {
  constructor(sketch, settings) {
    this.sketch = sketch;
    this.settings = { ...settings };

    this.controls = new OrbitControls(
      this.sketch.camera,
      this.sketch.renderer.domElement
    );

    return this.controls;
  }
}
class Events {
  constructor(sketch, settings) {
    this.sketch = sketch;
    this.settings = { ...settings };

    this.addEvents();
  }
  addEvents() {
    window.addEventListener("resize", this.onWindowResize.bind(this), false);
  }
  onWindowResize() {
    this.sketch.sizes = {
      width: window.innerWidth,
      height: window.innerHeight,
    };

    this.sketch.camera.aspect =
      this.sketch.sizes.width / this.sketch.sizes.height;
    this.sketch.camera.updateProjectionMatrix();
    this.sketch.renderer.setSize(
      this.sketch.sizes.width,
      this.sketch.sizes.height
    );
    this.sketch.renderer.setPixelRatio(Math.min(2, window.devicePixelRatio));
  }
}
class Lights {
  constructor(sketch, settings) {
    this.sketch = sketch;
    this.settings = { ...settings };

    this.ambient();
    this.directional(3, 5, 3);
    this.directional(3, 5, -3);
  }
  ambient() {
    let ambLight = new THREE.AmbientLight(0xffffff, 0.2, 100);
    this.sketch.scene.add(ambLight);
  }
  directional(x, y, z) {
    let dirLight = new THREE.DirectionalLight(0xffffff, 0.6, 100);
    dirLight.position.set(x, y, z);
    this.sketch.scene.add(dirLight);
  }
}
class Loader {
  constructor(sketch, settings) {
    this.settings = {
      load: () => {
        console.log("loaded");
      },
      progress: (itemURL, itemsLoaded, itemsTotal) => {
        console.log("%loaded:", itemsLoaded / itemsTotal);
      },
      ...settings,
    };
    this.manager = new THREE.LoadingManager(
      () => this.settings.load(),
      (itemURL, itemsLoaded, itemsTotal) =>
        this.settings.progress(itemURL, itemsLoaded, itemsTotal)
    );
  }
}

let flow;
class Sketch {
  constructor() {
    this.sizes = {
      width: window.innerWidth,
      height: window.innerHeight,
    };
    this.scene = new Scene(this);
    this.renderer = new Renderer(this);
    this.camera = new Camera(this);
    this.lights = new Lights(this);
    this.controls = new Controls(this);
    this.events = new Events(this);
    this.animator = new Animator(this);
    this.loader = new Loader(this, {
      load: () => {
        this.setUpMaterials();
        this.resetMaterials();
      },
    });
  }
  init() {
    this.addObjects();
    document.body.appendChild(this.renderer.domElement);
    this.animator.animate();
  }
  addObjects() {
    const that = this;

    const gltfloader = new GLTFLoader(this.loader.manager);

    gltfloader.load("https://assets.codepen.io/5946/mp4_1.glb", function (gltf) {
      const model = gltf.scene.children[0];

      model.position.set(0, 0, 0);
      model.rotation.set(Math.PI * -0.5, 0, 0);
      model.scale.set(1, 1, 1);
      model.updateMatrix();
      that.scene.add(model);
    });

    const cubeloader = new THREE.CubeTextureLoader(this.loader.manager);
    cubeloader.setPath("https://assets.codepen.io/5946/");

    this.textureCube = cubeloader.load([
      "posx.jpeg",
      "negx.jpeg",
      "posy.jpeg",
      "negy.jpeg",
      "posz.jpeg",
      "negz.jpeg",
    ]);
    this.textureCube.encoding = THREE.sRGBEncoding;
    this.scene.background = this.textureCube;
  }
  resetMaterials() {
    let teams = ["ferrari", "mclaren"];
    teams.forEach((team) => {
      for (let mat in this.materials[team]) {
        let material = this.materials[team][mat];
        material.roughness = 0.0;
        material.metalness = 0.5;
        material.envMap = this.textureCube;
        material.needsUpdate = true;
      }
    });
		this.livery = "mclaren"
    // this.changeLivery();
  }
  setUpMaterials() {
    this.materials = {
      mclaren: {},
      ferrari: {},
    };
    this.materialSeed = this.scene.children[4].children[1].children[3].material.clone();

    // McLaren
    this.materials.mclaren.orange = this.materialSeed.clone();
    this.materials.mclaren.white = this.materialSeed.clone();
    this.materials.mclaren.white.color = new THREE.Color(0xffffff);
    this.materials.mclaren.stripe = this.scene.children[4].children[4].children[1].material.clone();
    this.materials.mclaren.black = this.materialSeed.clone();
    this.materials.mclaren.black.color = new THREE.Color(0x0b0d10);

    // Ferrari
    this.materials.ferrari.red = this.materialSeed.clone();
    this.materials.ferrari.red.color = new THREE.Color(0xbb1e29);
    this.materials.ferrari.black = this.materialSeed.clone();
    this.materials.ferrari.black.color = new THREE.Color(0x0b0d10);
  }
  changeLivery() {
		if ( this.livery == "mclaren") {
			this.livery="ferrari"
		}else{
			this.livery="mclaren"
		};
		const team = this.livery;
		console.log({team})
    sketch.scene.traverse((obj) => {
      const teams = {
        ferrari: {
          Monocock: "red",
          air_intake: "red",
          top_foil: "black",
          bottom_foil: "black",
          right_front_endplate: "black",
          left_front_endplate: "black",
          stand: "black",
          right_endplate: "black",
          left_endplate: "black",
          top_foil_1: "black",
          bottom_foil_1: "black",
        },
        mclaren: {
          Monocock: "stripe",
          air_intake: "stripe",
          top_foil: "white",
          bottom_foil: "white",
          right_front_endplate: "white",
          left_front_endplate: "white",
          stand: "black",
          right_endplate: "orange",
          left_endplate: "orange",
          top_foil_1: "white",
          bottom_foil_1: "orange",
        },
      };
      if (teams[team].hasOwnProperty(obj.name)) {
        let color = teams[team][obj.name];
        obj.material = this.materials[team][color];
      }
    });
  }
}

window.sketch = new Sketch();
// window.addEventListener('click',sketch.changeLivery.bind(sketch))
sketch.init();
              
            
!
999px

Console