cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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>
  <script src="https://code.playcanvas.com/playcanvas-latest.js"></script>
  <script src="https://code.playcanvas.com/ammo.751ec5f.js"></script>
  <body>
    <canvas id="application-canvas"></canvas>
    <a id="logo" href="https://playcanvas.com/" target="_blank"><img src="http://static.playcanvas.com/images/logo/PLAY_FLAT_ORANGE3_SMALL.png" /></a>
  </body>
</html>
            
          
!
            
              canvas {
  position: absolute;
  top: 0px;
  left: 0px;
  width: 100%;
  height: 100%;
}

#logo {
  position: absolute;
  top: 10px;
  right: 10px;
}
            
          
!
            
              // https://github.com/playcanvas/engine

// Create a PlayCanvas application
var canvas = document.getElementById("application-canvas");
var app = new pc.Application(canvas, {});
app.start();

// Fill the available space at full resolution
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
app.setCanvasResolution(pc.RESOLUTION_AUTO);

app.scene.ambientLight = new pc.Color(0.2, 0.2, 0.2);

// Utility function to create a material
function createMaterial(r, g, b) {
  var material = new pc.StandardMaterial();
  material.ambient.set(r, g, b);
  material.diffuse.set(r, g, b);
  material.specular.set(0.5, 0.5, 0.5);
  material.shininess = 50;
  material.update();
  return material;
}

// Create camera entity
function Camera() {
  var cam = new pc.Entity();
  cam.addComponent('camera', {
    clearColor: new pc.Color(0.1, 0.1, 0.1),
    farClip: 20
  });
  app.root.addChild(cam);
  this.entity = cam;
  this.timer = 0;
}

Camera.prototype.update = function (dt) {
  this.timer += dt;
  // Spin the camera around a center point
  var x = Math.sin(this.timer * 0.25) * 6;
  var z = Math.cos(this.timer * 0.25) * 4;
  var e = this.entity;
  e.setPosition(x, 5, z);
  e.lookAt(0, 3, 0);
}

// Create spot light entity
function Light() {
  var light = new pc.Entity();
  light.setPosition(10, 10, 10);
  light.setEulerAngles(45, 45, 0);
  light.addComponent('light', {
    type: "spot",
    intensity: 3.5,
    castShadows: true,
    range: 40
  });
  light.light.shadowResolution = 2048;
  light.light.shadowDistance = 8;
  light.light.shadowBias = 0.005;
  light.light.normalOffsetBias = 0.01;
  app.root.addChild(light);
  this.entity = light;
}

// Create ground
function Ground() {
  var ground = new pc.Entity();
  ground.setPosition(0, -0.5, 0);
  ground.setLocalScale(10, 1, 10);
  ground.addComponent('model', {
    type: "box"
  });
  ground.addComponent('rigidbody', {
    type: "static"
  });
  ground.addComponent('collision', {
    type: "box",
    halfExtents: [5, 0.5, 5]
  });
  var blue = createMaterial(0.28, 0.46, 1);
  ground.model.model.meshInstances[0].material = blue;
  app.root.addChild(ground);
  this.entity = ground;
}

// Create wall
function Wall() {
  var black = createMaterial(0, 0, 0);
  var white = createMaterial(1, 1, 1);

  this.bricks = [];

  for (var i = 0; i < 25; i++) {
    var body = new pc.Entity();
    body.addComponent('model', {
      type: "box",
      castShadows: true
    });
    body.addComponent('rigidbody', {
      type: "dynamic"
    });
    body.addComponent('collision', {
      type: "box",
      halfExtents: [0.5, 0.5, 0.5]
    });
    app.root.addChild(body);
    body.model.model.meshInstances[0].material = i % 2 ? black : white;

    this.bricks.push(body);
  }
  this.reset();
}

Wall.prototype.reset = function () {
  for (var i = 0; i < this.bricks.length; i++) {
    var e = this.bricks[i];
    e.rigidbody.teleport(i % 5 - 2, Math.floor(i / 5) + 0.5, 0, 0, 0, 0);
    e.rigidbody.linearVelocity = pc.Vec3.ZERO;
    e.rigidbody.angularVelocity = pc.Vec3.ZERO;
  }
};

function Ball() {
  var e = new pc.Entity();
  e.setPosition(0, -10, 0);
  e.addComponent('model', {
    type: "sphere",
    castShadows: true
  });
  e.addComponent('rigidbody', {
    type: "dynamic"
  });
  e.addComponent('collision', {
    type: "sphere",
    radius: 0.5
  });
  var red = createMaterial(1, 0.28, 0.28);
  e.model.model.meshInstances[0].material = red;
  app.root.addChild(e);
  this.entity = e;
}

Ball.prototype.fire = function () {
  var e = this.entity;
  e.rigidbody.teleport(0, 2, 5);
  e.rigidbody.linearVelocity = new pc.Vec3((Math.random() - 0.5) * 10, 7, -30);
  e.rigidbody.angularVelocity = pc.Vec3.ZERO;
};

// Create the scene
var camera = new Camera();
var light = new Light();
var ground = new Ground();
var wall = new Wall();
var ball = new Ball();

// Reset the wall and fire the ball every 4 seconds
var n = 0;
setInterval(function () {
  n++;
  if (n % 4 === 0)
    wall.reset();
  if (n % 4 === 1)
    ball.fire();
}, 1000);

// Register an update event to rotate the camera
app.on("update", function (dt) {
  camera.update(dt);
});
            
          
!
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.
Loading ..................

Console