Threejs - Deck Prototype 2

by Thomas Carter

HTML

<script src="https://rawgit.com/mrdoob/three.js/dev/build/three.js"></script>
<script src="https://rawgit.com/mrdoob/three.js/dev/examples/js/controls/OrbitControls.js"></script>
<div id="con">
  A single mesh, however, cannot have two different materials in threejs. And a material cannot show more than one texture at once. So if we really want end grains...we'll have to create 2 meshes for every one board, which can eat performance depending on how many framming boards there are.
</div>
<!--
<img id='testImg' src='https://www.webkit.org/blog-files/acid3-100.png'>-->

CSS

div {
  border: thin solid grey;
  padding: 0;
  padding: 5px;
  font-family:'arial';
  font-size: 14px;
}
img {
  display:none;
}

JavaScript

var mod = {};
var objects = []; 
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 35, window.innerWidth/window.innerHeight, 0.1, 3000 );
var renderer = new THREE.WebGLRenderer();
renderer.shadowMap.enabled = true;   //req for shadow enabling
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
      
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setClearColor( 0x555555, 1 ); 
document.body.appendChild( renderer.domElement );
scene.add(camera);

var light1 = createSun();
light1.control.rotation.y += THREE.Math.degToRad(90);
light1.sun.intensity = 2.5;

var light2 = createSun();
light2.control.rotation.z += THREE.Math.degToRad(180);
light2.control.rotation.y += THREE.Math.degToRad(90);
light2.sun.intensity = 2.5;


controls = new THREE.OrbitControls(camera);
setToFullOrbit(controls)
camera.position.copy(new THREE.Vector3(125,125,125));
// fix first frame render issue going invisible
camera.lookAt(new THREE.Vector3(0,0,0));

var axisHelper = new THREE.AxisHelper(12);
scene.add(axisHelper);

// ---------------------- playground starts here ----------------------

// ground

/*
var planeGeo = new THREE.PlaneBufferGeometry(12,12);
var groundMat = new THREE.MeshBasicMaterial({ color:0xf4b942});
var planeMesh = new THREE.Mesh(planeGeo, groundMat);
//planeMesh.position.x += 32
//planeMesh.position.z += 32
//planeMesh.rotateX(THREE.Math.degToRad(-90));
scene.add(planeMesh);
rotateUVonPlanar(0, planeMesh);	// works if called right after
//planeMesh.receiveShadow = true;

//apply loaded texture to ground
var groundMatCallBack = function(base64Str){
  var groundImg = new Image();
  groundImg.addEventListener('load', function(){
    var groundTexture =  createTextureFromImage(fti(1), this);
    groundMat.map = groundTexture;
    groundTexture.needsUpdate = true;
    groundMat.needsUpdate = true;
  });
  groundImg.src = base64Str;
}
var grassUrl =...