Skip to content

Instantly share code, notes, and snippets.

@valex
Created September 25, 2018 15:56
Show Gist options
  • Save valex/ee7ff4ac9ac5f79ceccc4304704c1011 to your computer and use it in GitHub Desktop.
Save valex/ee7ff4ac9ac5f79ceccc4304704c1011 to your computer and use it in GitHub Desktop.
Repeat wrapping
<!DOCTYPE html>
<html>
<head>
<title>Example 10.08 - Repeat mapping</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/three.js/96/three.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/stats.js/r16/Stats.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.2/dat.gui.min.js"></script>
<style>
body {
/* set margin to 0 and overflow to hidden, to go fullscreen */
margin: 0;
overflow: hidden;
}
</style>
</head>
<body>
<div id="Stats-output">
</div>
<!-- Div which will hold the Output -->
<div id="WebGL-output">
</div>
<!-- Javascript code that runs our Three.js examples -->
<script type="text/javascript">
// once everything is loaded, we run our Three.js stuff.
function init() {
var stats = initStats();
// create a scene, that will hold all our elements such as objects, cameras and lights.
var scene = new THREE.Scene();
// create a camera, which defines where we're looking at.
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
// create a render and set the size
var webGLRenderer = new THREE.WebGLRenderer();
webGLRenderer.setClearColor(new THREE.Color(0xEEEEEE));
webGLRenderer.setSize(window.innerWidth, window.innerHeight);
webGLRenderer.shadowMap.enabled = true;
webGLRenderer.shadowMap.type = THREE.PCFSoftShadowMap; // default THREE.PCFShadowMap
var sphere = createMesh(new THREE.SphereGeometry(5, 20, 20), "floor-wood.jpg");
scene.add(sphere);
sphere.position.x = 7;
var cube = createMesh(new THREE.BoxGeometry(6, 6, 6), "brick-wall.jpg");
cube.position.x = -7;
scene.add(cube);
console.log(cube.geometry.faceVertexUvs);
// position and point the camera to the center of the scene
camera.position.x = 00;
camera.position.y = 12;
camera.position.z = 20;
camera.lookAt(new THREE.Vector3(0, 0, 0));
var ambiLight = new THREE.AmbientLight(0x141414);
scene.add(ambiLight);
var light = new THREE.DirectionalLight();
light.position.set(0, 30, 20);
scene.add(light);
// add the output of the renderer to the html element
document.getElementById("WebGL-output").appendChild(webGLRenderer.domElement);
// call the render function
var step = 0;
// var polyhedron = createMesh(new THREE.PolyhedronGeometry(vertices, faces, controls.radius, controls.detail));
// setup the control gui
var controls = new function () {
this.repeatX = 1;
this.repeatY = 1;
this.repeatWrapping = true;
this.updateRepeat = function (e) {
cube.material.map.repeat.set(controls.repeatX, controls.repeatY);
sphere.material.map.repeat.set(controls.repeatX, controls.repeatY);
if (controls.repeatWrapping) {
cube.material.map.wrapS = THREE.RepeatWrapping;
cube.material.map.wrapT = THREE.RepeatWrapping;
sphere.material.map.wrapS = THREE.RepeatWrapping;
sphere.material.map.wrapT = THREE.RepeatWrapping;
} else {
cube.material.map.wrapS = THREE.ClampToEdgeWrapping;
cube.material.map.wrapT = THREE.ClampToEdgeWrapping;
sphere.material.map.wrapS = THREE.ClampToEdgeWrapping;
sphere.material.map.wrapT = THREE.ClampToEdgeWrapping;
}
cube.material.map.needsUpdate = true;
sphere.material.map.needsUpdate = true;
}
};
var gui = new dat.GUI();
gui.add(controls, "repeatX", -4, 4).onChange(controls.updateRepeat);
gui.add(controls, "repeatY", -4, 4).onChange(controls.updateRepeat);
gui.add(controls, "repeatWrapping").onChange(controls.updateRepeat);
render();
function createMesh(geom, texture) {
var texture = new THREE.TextureLoader().load("../assets96/textures/general/" + texture);
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
geom.computeVertexNormals();
var mat = new THREE.MeshPhongMaterial();
mat.map = texture;
// create a multimaterial
var mesh = new THREE.Mesh(geom, mat);
return mesh;
}
function render() {
stats.update();
step += 0.01;
cube.rotation.y = step;
cube.rotation.x = step;
sphere.rotation.y = step;
sphere.rotation.x = step;
// render using requestAnimationFrame
requestAnimationFrame(render);
webGLRenderer.render(scene, camera);
}
function initStats() {
var stats = new Stats();
stats.setMode(0); // 0: fps, 1: ms
// Align top-left
stats.domElement.style.position = 'absolute';
stats.domElement.style.left = '0px';
stats.domElement.style.top = '0px';
document.getElementById("Stats-output").appendChild(stats.domElement);
return stats;
}
}
window.onload = init;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment