forked from DerSchmale/threejs-thin-film-iridescence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
96 lines (79 loc) · 2.39 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var scene;
var camera;
var renderer;
var container;
var controls;
var iridescenceLookUp;
var iridescenceMaterial;
var torus;
window.onload = function() {
init();
initScene();
initGui();
onResize();
render();
window.addEventListener("resize", onResize, false);
};
function init() {
container = document.getElementById("webglcontainer");
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio(window.devicePixelRatio);
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera();
camera.position.set(0, 0, 50);
scene.add(camera);
controls = new THREE.OrbitControls(camera, container);
container.appendChild(renderer.domElement);
iridescenceLookUp = new ThinFilmFresnelMap();
controls.update();
}
function onResize() {
var width = window.innerWidth;
var height = window.innerHeight;
renderer.setSize(width, height);
renderer.domElement.style.width = width + "px";
renderer.domElement.style.height = height + "px";
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
function render() {
torus.rotation.y += .01;
torus.rotation.x += .009;
controls.update();
renderer.render(scene, camera);
requestAnimationFrame(render);
}
function initScene() {
var size = 1000;
var skyGeom = new THREE.CubeGeometry(size, size, size);
var radiance = loadCubeMap("assets/skybox/radiance");
var irradiance = loadCubeMap("assets/skybox/irradiance");
var skyboxMaterial = new SkyMaterial(radiance);
var skyBox = new THREE.Mesh(skyGeom, skyboxMaterial);
scene.add(skyBox);
var torusGeom = new THREE.TorusKnotGeometry(10, 3, 100, 16);
iridescenceMaterial = new IridescentMaterial(irradiance, radiance, iridescenceLookUp);
torus = new THREE.Mesh(torusGeom, iridescenceMaterial);
scene.add(torus);
}
function loadCubeMap(path) {
var files = [
path + "/posX.jpg",
path + "/negX.jpg",
path + "/posY.jpg",
path + "/negY.jpg",
path + "/posZ.jpg",
path + "/negZ.jpg"
];
var loader = new THREE.CubeTextureLoader();
return loader.load(files);
}
function initGui() {
var gui = new dat.GUI();
gui.remember(iridescenceLookUp);
gui.remember(iridescenceMaterial);
gui.add(iridescenceLookUp, "filmThickness").min(100).max(1000);
gui.add(iridescenceLookUp, "refractiveIndexFilm").min(1).max(5);
gui.add(iridescenceLookUp, "refractiveIndexBase").min(1).max(5);
gui.add(iridescenceMaterial, "boost").min(1).max(50);
}