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.

            
              #app
  
template#stage
  .stage-container
    .controls
      button(@click="addBall") Add Tree
      p(style="color:white; ") Add trees, click 'em. That's it.
    .info(v-if="hoveredMesh") {{hoveredMesh}}
      
    canvas#stage.stage(ref="canvas", @click="canvasClick", @mousemove="canvasHover")
            
          
!
            
              body{
  display:block;
  width:100%;
  height:100vh;
  overflow:hidden;
}
.stage-container{
  width:100%;
  height:100%;
  position:relative;
}
.stage{
  display:block;
  width:100%;
  height:100%;
}
.controls{
  position:absolute;
  top:10px;
  left:10px;
  z-index:9999;
}
.info{
  display:block;
  position:absolute;
  right:0;
  top:0;
  z-index:9999;
}
            
          
!
            
              class Piece {
  constructor(scene, key, x, y, z, size){
    this.scene = scene
    this.key = key;
    this.x = x;
    this.y = y;
    this.z=z;
    
    this.position = new BABYLON.Vector3(this.x, this.y, this.z);
    this.meshes = [];
    
    let stumpMaterial = new BABYLON.StandardMaterial('stump-mat-' + key, this.scene);
    stumpMaterial.diffuseColor = new BABYLON.Color3(.3, .15, .13);
    
    this.top = this.makeTop();
    this.bottom = this.makeBottom();
    this.stump = this.makeStump();
    this.stump.material = stumpMaterial;
    this.meshes= [...this.meshes, ...[this.top, this.bottom, this.stump]];
    
    // console.log(this.meshes)
  }
  makeTop(){
    let _this = this;
    var top = BABYLON.MeshBuilder.CreateCylinder("cone", {diameterTop: 0, height:1, size: .5, tessellation: 6}, _this.scene);
    var newY = this.position.y + .7;
    top.parentMesh = _this;
    top.position = new BABYLON.Vector3(this.position.x, newY, this.position.z)
    return top;
  }
  makeBottom(){
    let _this = this;
    var bottom = BABYLON.MeshBuilder.CreateCylinder("cone", {diameterTop: 0, diameterBottom: 1.5, height: 1.2, tessellation: 6}, _this.scene);
    bottom.position = this.position
    bottom.parentMesh = _this;
    return bottom;
  }
  makeStump(){
    let _this = this;
    var stump = BABYLON.MeshBuilder.CreateCylinder("cone", {diameterTop: 0, diameterBottom: .5, diameterTop:.5, height: 1, tessellation: 6}, _this.scene);
    
    var newY = this.position.y - .8;
    stump.parentMesh = _this;
    stump.position = new BABYLON.Vector3(this.position.x, newY, this.position.z);
    stump.ignoreMaterials = true;
    return stump;
  }
  assignMaterial(mat){
    // console.log(mat)
    this.meshes.forEach(mesh=>{
      if(mesh.ignoreMaterials) return;
      mesh.material = mat;
    })
  }
  remove(){
    this.meshes.forEach( mesh=>{
      mesh.dispose();
    });
  }
}

const Stage = {
  data(){
    return {
      app: null,
      balls: 0,
      engine: null,
      canvas: null,
      scene: null,
      hoveredMesh: null,
      assets: {},
      selectedBall: null,
      previousMaterial: null
    }
  },
  mounted(){
    // console.log('loading stage');
    this.init()
    console.log(this.scene)
  },
  methods:{
     init(){
       const _this = this;
       this.canvas = this.$refs.canvas;
       this.engine = new BABYLON.Engine(this.canvas, true);
        this.scene = new BABYLON.Scene(this.engine);
       
       //camera
       this.mainCamera =  new BABYLON.ArcRotateCamera("ArcRotateCamera", 1.5, 1.3, 20, BABYLON.Vector3.Zero(), this.scene);

       //lights
       this.mainLight = new BABYLON.HemisphericLight('light1', new BABYLON.Vector3(0,1,0), this.scene);
       
       //Render Stuff
       this.scene.beforeRender = function(){
         
       }
       
       
       this.engine.runRenderLoop( function(){
         _this.scene.render();
       });
       window.addEventListener('resize', function(){
         _this.engine.resize();
       });
       
       setTimeout( ()=>{
         this.addBall();
       }, 500);
       setTimeout( ()=>{
         this.addBall();
       }, 1000);
       setTimeout( ()=>{
         this.addBall();
       }, 1500);
       
     },
    
     addBall(){
      if(this.balls >= 25){
        alert('Play with the trees you\'ve got!');
        return;
      }
      //
      let x = Math.random() * 10;
      x = Math.random() > .5 ? x : -x;
      let y = 1//Math.random() * 5;
      let z = Math.random() * 5;
      let size = Math.random() * 5;
      this.balls ++;
      let key = this.balls;
       
      // let orb = new BABYLON.Mesh.CreateSphere('sphere-' + key , 16, size, this.scene);
      // orb.position = new BABYLON.Vector3(x, y, z);
       let orb = new Piece(this.scene, key, x, y, z, size)

        orb.top.actionManager = new BABYLON.ActionManager(this.scene);	
       
       let orbMat = new BABYLON.StandardMaterial('orbMat-' + key, this.scene);
       orbMat.diffuseColor = new BABYLON.Color3(Math.random(),Math.random(),Math.random());
   
     orb.assignMaterial(orbMat);
    },
    canvasHover(e){
      console.log(this.hoveredItem)
    },
    canvasClick(evt){
      var _this = this;
      var pickResult = this.scene.pick(evt.clientX, evt.clientY);
      var mesh = pickResult.pickedMesh;
      if(!mesh) return false;
      if(this.selectedMesh && this.previousMaterial){
        this.selectedMesh.material = this.previousMaterial;
      }
      this.selectedMesh = mesh;
      
      console.log(evt)
      
      var mat = mesh.material;
      this.previousMaterial = mat;
      var newMat = new BABYLON.StandardMaterial('testMat', this.scene);
      newMat.diffuseColor = new BABYLON.Color3(1,1,1);
      mesh.parentMesh.assignMaterial(newMat);
      setTimeout( ()=>{
        this.balls --;
        mesh.parentMesh.remove();
      }, 30)
    },
    canvasHover(){
      this.hoveredMesh = this.scene.meshUnderPointer || false
      console.log(this.hoveredMesh)
    },
    mouseOverMesh (unit_mesh) {
      console.log ("mouse over");
      console.log (unit_mesh);
      // if (unit_mesh.meshUnderPointer !== null) {
      //     unit_mesh.meshUnderPointer.renderOutline = false;	
      //     unit_mesh.meshUnderPointer.outlineWidth = 1;
      // }
    },
    mouseOutUnit (unit_mesh) {
      console.log ("mouse out");
      console.log (unit_mesh);
      // if (unit_mesh.meshUnderPointer !== null) {
      //     unit_mesh.meshUnderPointer.renderOutline = true;	
      //     unit_mesh.meshUnderPointer.outlineWidth = 1;
      // }
    }
  
  },
  computed:{
    hoveredItem(){
      return this.scene && this.scene.meshUnderPointer || false;
      // return false
      // return this.scene
    }
  },
  template: "#stage"
};


new Vue({
  el: "#app",
  render: h => h(Stage)
})
            
          
!
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