Qt.include("three.js")
var camera, scene, renderer, cubeCamera;
var caseMesh, frontMesh, iconMesh;
var caseColor = new THREE.Color("#000000");
var meshesReady = false;
var canvasTextureProvider = null;
var zeroVector = new THREE.Vector3(0, 0, 0);
var cameraLight;
var sphereImage, iconImage;
var caseMeshFile, frontMeshFile, iconMeshFile;
function initializeGL(canvas, textureSource) {
scene = new THREE.Scene();
cubeCamera = new THREE.CubeCamera(5, 100, 512);
cubeCamera.renderTarget.texture.minFilter = THREE.LinearMipMapLinearFilter;
scene.add(cubeCamera);
var sphere = new THREE.Mesh(
new THREE.SphereGeometry(40, 16, 16),
new THREE.MeshBasicMaterial());
sphere.side = THREE.BackSide;
sphere.scale.x = -1;
scene.add(sphere);
var textureLoader = new THREE.TextureLoader();
if (sphereImage) {
var sphereTexture = textureLoader.load(sphereImage, updateEnvMap);
sphereTexture.mapping = THREE.UVMapping;
sphereTexture.minFilter = THREE.LinearFilter;
sphere.material.map = sphereTexture;
} else {
sphere.material.color = new THREE.Color("black");
}
camera = new THREE.PerspectiveCamera( 75, canvas.width / canvas.height, 0.001, 1000 );
var light = new THREE.AmbientLight( 0x666666 );
scene.add( light );
cameraLight = new THREE.DirectionalLight( 0xffffff, 1 );
cameraLight.position.y = 1.5;
scene.add( cameraLight );
var caseMaterial = new THREE.MeshPhongMaterial({ envMap: cubeCamera.renderTarget,
combine: THREE.MultiplyOperation,
reflectivity: 0.35,
specular: "#555555" });
var frontTexture = new THREE.QtQuickItemTexture( textureSource );
var frontMaterial = new THREE.MeshPhongMaterial( { map: frontTexture } );
var iconMaterial = new THREE.MeshPhongMaterial( { transparent:true } );
if (iconImage) {
var iconTexture = textureLoader.load(iconImage);
iconTexture.minFilter = THREE.LinearFilter;
iconMaterial.map = iconTexture;
} else {
iconMaterial.opacity = 0;
}
renderer = new THREE.Canvas3DRenderer(
{ canvas: canvas, antialias: true, devicePixelRatio: canvas.devicePixelRatio });
renderer.setSize( canvas.width, canvas.height );
var loader = new THREE.JSONLoader();
loader.load( caseMeshFile, function ( geometry, materials ) {
geometry.computeVertexNormals();
var bufferGeometry = new THREE.BufferGeometry();
bufferGeometry.fromGeometry(geometry);
caseMesh = new THREE.Mesh( bufferGeometry, caseMaterial );
if (iconMesh && frontMesh)
meshesReady = true;
caseMesh.material.color = caseColor;
updateEnvMap();
scene.add( caseMesh );
} );
loader.load( frontMeshFile, function ( geometry, materials ) {
var bufferGeometry = new THREE.BufferGeometry();
bufferGeometry.fromGeometry(geometry);
frontMesh = new THREE.Mesh( bufferGeometry, frontMaterial );
if (iconMesh && caseMesh)
meshesReady = true;
scene.add( frontMesh );
} );
loader.load( iconMeshFile, function ( geometry, materials ) {
var bufferGeometry = new THREE.BufferGeometry();
bufferGeometry.fromGeometry(geometry);
iconMesh = new THREE.Mesh( bufferGeometry, iconMaterial );
if (caseMesh && frontMesh)
meshesReady = true;
scene.add( iconMesh );
} );
}
function updateEnvMap() {
if (caseMesh) {
caseMesh.material.envMap = cubeCamera.renderTarget;
cubeCamera.updateCubeMap(renderer, scene);
caseMesh.material.needsUpdate = true;
}
}
function setSphereTexture(image) {
sphereImage = image;
}
function setIconTexture(image) {
iconImage = image;
}
function setMeshFiles(caseFile, frontFile, iconFile) {
caseMeshFile = caseFile;
frontMeshFile = frontFile;
iconMeshFile = iconFile;
}
function setCaseColor(color) {
caseColor.set(color);
if (caseMesh)
caseMesh.material.color = caseColor;
}
function resizeGL(canvas) {
camera.aspect = canvas.width / canvas.height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(canvas.devicePixelRatio);
renderer.setSize( canvas.width, canvas.height );
}
function degToRad(degrees) {
return degrees * Math.PI / 180;
}
function paintGL(canvas) {
if (meshesReady) {
var cameraRad = degToRad(canvas.cameraAngle);
var lightRad = cameraRad - 0.8;
caseMesh.rotation.x = degToRad(canvas.xRotAnim);
caseMesh.rotation.y = degToRad(canvas.yRotAnim);
caseMesh.rotation.z = cameraRad + degToRad(canvas.zRotAnim);
frontMesh.rotation.set(caseMesh.rotation.x, caseMesh.rotation.y, caseMesh.rotation.z);
iconMesh.rotation.set(caseMesh.rotation.x, caseMesh.rotation.y, caseMesh.rotation.z);
camera.position.x = canvas.distance * Math.sin(cameraRad);
camera.position.z = canvas.distance * Math.cos(cameraRad);
cameraLight.position.x = (canvas.distance + 2) * Math.sin(lightRad);
cameraLight.position.z = (canvas.distance + 2) * Math.cos(lightRad);
camera.lookAt(zeroVector);
}
renderer.render( scene, camera );
}