Giter Club home page Giter Club logo

threejs_autoscaler's Introduction

Three.js Autoscaler โš–๏ธ

Created: Early 2019

Models come in different shapes and sizes. In Threejs this means that you need to manually adjust the camera to frame the model/scene correctly. Autoscaler makes sure that every model gets properly framed within their canvas.

How to embed Autoscaler in your Three.js code

In this example the autoscaler is used with a STL loader

  1. Add the autoscale code as the last child of the loader.

   var negX = initBoundingBox.min[Object.keys(initBoundingBox.min)[0]];
   var posX = initBoundingBox.max[Object.keys(initBoundingBox.max)[0]];
   var modelWidth = posX - (negX);

   var negY = initBoundingBox.min[Object.keys(initBoundingBox.min)[1]];
   var posY = initBoundingBox.max[Object.keys(initBoundingBox.max)[1]];
   var modelHeight = posY - (negY);

   if (modelWidth >= modelHeight) {
    // Scale based on the width
    var scaleFactor = 2 / modelWidth;
   } else {
    // Scale based on the height
    var scaleFactor = 1.5 / modelHeight;
   }
    mesh.scale.set(scaleFactor, scaleFactor, scaleFactor);

   // Adjust model position on Y axis
   var boundingBox = new THREE.Box3().setFromObject(mesh);

   var newNegY = boundingBox.min[Object.keys(boundingBox.min)[1]];
   var newPosY = boundingBox.max[Object.keys(boundingBox.max)[1]];
   var newModelHeight = newPosY - (newNegY);

   var heightAdjust = (newModelHeight / 2) - newPosY;
   mesh.position.set(0, heightAdjust, 0);
   scene.add(mesh);

What happens under the hood

  1. Autoscaler will get the vectors from the model with the help of the BoundingBox.
  2. The X and Y values will be stored inside variables.
    1. The width of the model gets determined by subtracting the negative x vector from the positive x vector.
    2. The height of the model gets determined by substracting the negative y vector from the positive y vector.
  3. Then the model gets scaled based on which side is bigger. For example, a tall model will have a height bigger than the width. In this case the height will be scaled to fit the canvas.
  4. The y axis position of the model gets calculated and centered.

What does Three.js autoscaler look like applied on a broad and a tall model

Broad model on the left and tall model on the right. Perfectly scaled to match the canvas: mainpage

threejs_autoscaler's People

Contributors

tiesfa avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.