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, body, canvas {
  margin: 0;
  width: 100%;
  height: 100%;
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  cursor: none;
}
            
          
!
            
              var texture = THREE.ImageUtils.loadTexture(prairieImg, {}, function() {
var mouse = {
  x: window.innerWidth / 2,
  y: window.innerHeight / 2
}
//fefe = {
//access mouse pos with mouse.x and mouse.y
function getMousePos(event) {
  return {
    x: event.clientX,
    y: event.clientY
  }
}
window.addEventListener('mousemove', function(event) {
  mouse = getMousePos(event)
})
function walk(x) {
  return Math.abs(Math.sin(x))
}
//three.js:
var scene = new THREE.Scene();
scene.background = new THREE.Color( '#9999ff' );
scene.add( new THREE.AmbientLight( 0xffffff, 0.5 ) )
var light;
    light = new THREE.DirectionalLight(0xdfebff, 2.5); //2.5
    light.position.set(-350, 80, 10);
    light.position.multiplyScalar(1.3);//1.3

    light.castShadow = true;
    light.shadowCameraVisible = true;

    light.shadowMapWidth = 5000;
    light.shadowMapHeight = 5000;

    var d = 500;

    light.shadowCameraLeft = -d;
    light.shadowCameraRight = d;
    light.shadowCameraTop = d;
    light.shadowCameraBottom = -d;

    light.shadowCameraFar = 10000;
    light.shadowDarkness = 0.001;

    scene.add(light);
			var camera = new THREE.PerspectiveCamera( 45, window.innerWidth/window.innerHeight, 0.1, 1000 );
scene.fog = new THREE.Fog('#9999ff', 400, 500);
			var renderer = new THREE.WebGLRenderer({antialias:true});
    renderer.setClearColor(0x333F47, 1);
    renderer.shadowMapEnabled = true;
    renderer.shadowMapSoft = true;
			renderer.setSize( window.innerWidth, window.innerHeight );
			document.body.appendChild( renderer.domElement );
      //Create elements here:
function dec2hex(i) {
    var result = "0x"
   result = result + i.slice(1)
  return result
  }
//creating the gui:
var Controls = function() {
    this.Cast_Shadow = true;
    this.Tree_Min = 20;
    this.Tree_Max = 75;
    this.Delay_Min = 2;
    this.Delay_Max = 5;
    this.Sky_Color = 'rgb(0, 0, 255)' //'#0000ff';
    this.Ground_Color = '#008000';
    this.Wood_Color = '#800000';
    this.Leaf_Color = '#008000';
    this.Day_Cycle = true;
	};

	var data = new Controls(),
		gui = new dat.GUI();
    var f0 = gui.addFolder('Trees')
    f0.add(data, 'Cast_Shadow', false)
    var f1 = f0.addFolder('Size');
    f1.add(data, 'Tree_Min', 5, 200)
    f1.add(data, 'Tree_Max', 5, 200)
    var f2 = f0.addFolder('Spawn_Delay');
    f2.add(data, 'Delay_Min', 1, 25)
    f2.add(data, 'Delay_Max', 1, 25)
    var f3 = gui.addFolder('Colors');
    f3.addColor(data, 'Sky_Color');
    f3.addColor(data, 'Ground_Color');
    f3.addColor(data, 'Wood_Color');
    f3.addColor(data, 'Leaf_Color');
    gui.add(data, 'Day_Cycle', false)
var cgc = 0
var csc = 0
//more three.js:
function randint(min, max) {
  return Math.floor(Math.random() * max) + min
}
function rotateAround(point, center, angle) {
  angle = (angle) * (Math.PI/180); // Convert to radians
var rotatedX = Math.cos(angle) * (point.x - center.x) - Math.sin(angle) * (point.y-center.y) + center.x;
var rotatedY = Math.sin(angle) * (point.x - center.x) + Math.cos(angle) * (point.y - center.y) + center.y;
  return {x: rotatedX, y: rotatedY}
}
var geometry = new THREE.BoxGeometry(1000, 1, 600 );
var material = new THREE.MeshLambertMaterial( {color: '#008000',ambient: 0x000000,
      map:texture} );
var floor = new THREE.Mesh( geometry, material );
floor.receiveShadow = true;
scene.add( floor );
  var floor2 = new THREE.Mesh( geometry, material );
floor2.receiveShadow = true;
scene.add( floor2 );
var geometry = new THREE.SphereGeometry( 500, 32, 32 );
var material = new THREE.MeshLambertMaterial( {color: 'blue',fog: false} );
var sky = new THREE.Mesh( geometry, material );
//scene.add( sky );
sky.material.side = THREE.DoubleSide;
function newTree(type, distance) {
  gr = {}
  gr.x = distance + camera.position.x
  gr.y = 0
  
  gr.size = randint(data.Tree_Min, data.Tree_Max - data.Tree_Min)
  gr.geometry = new THREE.CylinderGeometry( gr.size / 10, gr.size / 10, gr.size * 2, 8 );
  gr.material = new THREE.MeshLambertMaterial( {color: data.Wood_Color,ambient:data.Wood_Color} );
  gr.trunk = new THREE.Mesh( gr.geometry, gr.material );
  cont = false
  while (cont == false) {
    gr.z = randint(-200 - camera.position.z, 400)
    if (gr.z < -1 - (gr.size / 5) ||
        gr.z >  1 + (gr.size / 5)) {
      cont = true
    }
  }
  gr.trunk.position.x = gr.x
  //gr.trunk.position.y = gr.size / 2
  gr.trunk.position.z = gr.z
  
  if (type == "sphere") {
    gr.geometry = new THREE.SphereGeometry( gr.size / 2, 5, 5 );
    gr.material = new THREE.MeshLambertMaterial( {color: data.Leaf_Color,ambient:data.Leaf_Color} );
    gr.leaves = new THREE.Mesh( gr.geometry, gr.material );
    scene.add( gr.leaves );
    gr.leaves.position.x = gr.x
    gr.leaves.position.y = gr.size
    gr.leaves.position.z = gr.z
  }
  if (type == "cone") {
    gr.geometry = new THREE.ConeGeometry( gr.size / 2, gr.size, 5 );
    gr.material = new THREE.MeshLambertMaterial( {color: data.Leaf_Color,ambient:data.Leaf_Color} );
    gr.leaves = new THREE.Mesh( gr.geometry, gr.material );
    
    gr.leaves.position.x = gr.x
    gr.leaves.position.y = gr.size + gr.size / 4
    gr.leaves.position.z = gr.z
  }
  gr.type = type
  if (data.Cast_Shadow) {
  gr.trunk.castShadow = true;
  gr.leaves.castShadow = true;
  }
  scene.add( gr.trunk );
  scene.add( gr.leaves );
  return gr
}
function newBush(distance) {
  gr = {}
  gr.x = distance + camera.position.x
  gr.y = 0
  
  gr.size = randint(5, 10)
  gr.geometry = new THREE.SphereGeometry( gr.size, randint(4, 5), randint(4, 5) );
  gr.material = new THREE.MeshLambertMaterial( {color: data.Leaf_Color} );
  gr.bush = new THREE.Mesh( gr.geometry, gr.material );
  scene.add( gr.bush );
  gr.bush.scale.x = randint(7, 6) / 5
  gr.bush.scale.z = randint(7, 6) / 5
  gr.bush.position.x = gr.x
  //gr.trunk.position.y = gr.size / 2
  cont = false
  while (cont == false) {
    gr.z = randint(-200 - camera.position.z, 400)
    if (gr.z < -1 - (gr.bush.scale.z * gr.size) || 
        gr.z >  1 + (gr.bush.scale.z * gr.size)) {
      cont = true
    }
  }
  gr.bush.position.z = gr.z
  gr.bush.castShadow = true;
  gr.bush.receiveShadow = true;
  return gr
}
/*
function newStartingTree(type) {
  gr = newTree(type)
  gr.x = randint(-50, 500)
  return gr
}
*/
var text2 = document.createElement('div');
text2.style.position = 'absolute';
//text2.style.zIndex = 1;    // if you still don't see the label, try uncommenting this
text2.style.width = 100;
text2.style.height = 100;
text2.style.color = "white";
text2.innerHTML = "";
text2.style.top = 2 + 'px';
text2.style.left = 2 + 'px';
document.body.appendChild(text2);
addNote("white")
var geometry = new THREE.SphereGeometry( 30, 32, 32 );
var material = new THREE.MeshBasicMaterial( {color: 'yellow',side: THREE.DoubleSide} );
var sun = new THREE.Mesh( geometry, material );
scene.add( sun );
var trees = []
var bushes = []
var treeCount = 200
var bushDelay = 0
var treeDelay = 0
var cz = 0
var treeTypes = ["sphere", "cone"]
var gametick = 0
var speed = 1
for (var i = 0; i < 100; i++) {
  trees.push(newTree(treeTypes[randint(0, treeTypes.length)], randint(0,600)))
}
for (var i = 0; i < 50; i++) {
  bushes.push(newBush(randint(0,600)))
}
      //end of elements
			camera.position.x = 0;
      camera.position.y = 10
      camera.position.z = 0;
      lookAt = new THREE.Vector3( 1, 10, 0 )
      camera.lookAt(lookAt)
     //camera.rotation.y = 270
			var render = function () {
				requestAnimationFrame( render );
        mainloop()
				renderer.render(scene, camera);
			};
function mainloop() {
  gametick += speed
  camera.position.y = 10 + (walk(gametick / 10) * 2)
  floor.position.x = (-(gametick*speed) % 1000)+500
  floor2.position.x = (-(gametick*speed) % 1000)+1500
  /*
  lookAt = new THREE.Vector3( 1, 0, 0 )
      camera.lookAt(lookAt)
  center = {x: 0, y: 0}
  point = {x: camera.position.x, y: camera.position.z}
  newPos = rotateAround(point, center, 0.5)
  camera.position.x = newPos.x
  camera.position.z = newPos.y
  */
treeDelay -= speed
  if (treeDelay < 1) {
    trees.push(newTree(treeTypes[randint(0, treeTypes.length)], 600))
    treeDelay = randint(data.Delay_Min, data.Delay_Max - data.Delay_Min)
  }
  bushDelay -= speed
  if (bushDelay < 1) {
    bushes.push(newBush(600))
    bushDelay = randint(1, 5)
  }
  for (var i = 0; i < trees.length; i++) {
    tree = trees[i]
    if (tree.x < camera.position.x - 250) {
      scene.remove(tree.trunk)
      scene.remove(tree.leaves)
      trees.splice(i, 1)
    }
    tree.x -= speed
    tree.trunk.position.x -= speed
    tree.leaves.position.x -= speed
  }
  for (var i = 0; i < bushes.length; i++) {
    bush = bushes[i]
    if (bush.x < camera.position.x - 100) {
      scene.remove(bush.bush)
      bushes.splice(i, 1)
    }
    bush.x -= speed
    bush.bush.position.x -= speed
  }
  //updating gui:
  if (cgc !== data.Ground_Color) {
    cgc = data.Ground_Color
    floor.material.color.setHex( dec2hex(data.Ground_Color) );
    }
  if (csc !== data.Sky_Color) {
    csc = data.Sky_Color
    scene.background = new THREE.Color( csc );
    scene.fog = new THREE.Fog(csc, 400, 500);
    }
  //text2.innerHTML = light.position.x;
  //updating camera:
  //camera.position.x += 1
  //floor.position.x += 1
  //light.position.x += 1
  lookX = mouse.x - window.innerWidth / 2
  lookX /= 50
  //lookX = -lookX
  lookY = mouse.y - window.innerHeight / 2
  lookY /= 50
  lookY = -lookY
  lookAt = new THREE.Vector3(10, lookY + camera.position.y, lookX + camera.position.x);
  camera.lookAt(lookAt);
  if (data.Day_Cycle) {
  point = {x: light.position.x, y: light.position.y}
  center = {x: 0, y: 0}
  if (light.position.y > 0) {
    if (light.intensity < 2.5) {
    light.intensity += 0.02
    }
    rM = -0.15
  } else {
    if (light.intensity > 0.1) {
    light.intensity -= 0.05
    }
    rM = -0.5
  }
  r = rotateAround(point, center, rM)
  light.position.x = r.x
  light.position.y = r.y
  }
  sun.position.x = light.position.x
  sun.position.y = light.position.y
  sun.position.z = light.position.z
}
			render();


window.addEventListener( 'resize', onWindowResize, false );

function onWindowResize(){

    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();

    renderer.setSize( window.innerWidth, window.innerHeight );

}
  
});
            
          
!
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