Skip to content

Instantly share code, notes, and snippets.

@trevex
Created April 1, 2013 11:55
Show Gist options
  • Select an option

  • Save trevex/5284566 to your computer and use it in GitHub Desktop.

Select an option

Save trevex/5284566 to your computer and use it in GitHub Desktop.
Possible bug of texture rendering with three.js
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - test</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background:#030;
color:#fff;
padding:0;
margin:0;
overflow:hidden;
font-family:georgia;
text-align:center;
}
</style>
</head>
<body>
<script src="../build/three.min.js"></script>
<script src="js/Detector.js"></script>
<div id="viewport"></div>
<script>
if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
var domParent = document.getElementById("viewport"),
camera, scene, renderer,
root;
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000);
renderer = new THREE.WebGLRenderer({ antialias: true });
scene = new THREE.Scene();
root = new THREE.Object3D();
scene.fog = new THREE.FogExp2(0x6699bb, 0.025);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMapWidth = 1024;
renderer.shadowMapHeight = 1024;
renderer.shadowMapEnabled = true;
renderer.shadowMapSoft = true;
domParent.appendChild(renderer.domElement);
camera.position.set(-10, 12, -10);
camera.lookAt(new THREE.Vector3(0, 0, 0));
root.add(camera);
var light = new THREE.DirectionalLight(0xffffff),
shadowCameraSize = 20;
light.position.set(-8, 10, -2);
light.castShadow = true;
light.shadowCameraVisible = true;
light.shadowCameraRight = shadowCameraSize;
light.shadowCameraLeft = -shadowCameraSize;
light.shadowCameraTop = shadowCameraSize;
light.shadowCameraBottom = -shadowCameraSize;
light.shadowCameraNear = 1;
light.shadowCameraFar = 35;
root.add(light);
var ambientLight = new THREE.AmbientLight(0x001111);
scene.add(ambientLight);
var planeGeometry = new THREE.PlaneGeometry(1000, 1000, 10, 10),
planeTexture = THREE.ImageUtils.loadTexture("textures/terrain/grasslight-big.jpg"),
planeMaterial = new THREE.MeshLambertMaterial({ map: planeTexture });
planeGeometry.applyMatrix(new THREE.Matrix4().rotateX(-Math.PI/2));
planeTexture.wrapS = planeTexture.wrapT = THREE.RepeatWrapping;
planeTexture.repeat.set(100, 100);
var plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.receiveShadow = true;
scene.add(plane);
var cubeGeometry = new THREE.CubeGeometry(1, 1, 1),
cubeMaterial = new THREE.MeshLambertMaterial({ color: 0xFF0000 }),
cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cube.position.y = 0.5;
cube.position.x = 2.0;
cube.castShadow = true;
cube.receiveShadow = true;
scene.add(cube);
scene.add(root);
window.addEventListener('resize', onWindowResize, false);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestAnimationFrame( animate );
render();
}
function render() {
renderer.render(scene, camera);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment