File: /var/www/html/www.winghung.com/demo_v7/insertCard.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once('../wp-load.php');
session_start();
date_default_timezone_set("Asia/Hong_Kong");
global $wpdb, $sitepress;
if(isset($_GET['hash']) && $_GET['hash']!=""){
$result = $wpdb->get_results("SELECT * FROM `custom_upload_files` WHERE hash = '".$_GET['hash']."' LIMIT 1");
$file = $result[0]->file;
} else {
$file = "pattern.jpg";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert Card</title>
<style>
body { margin: 0; }
</style>
</head>
<body>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three@0.130.0/build/three.module.js"
}
}
</script>
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="module">
import * as THREE from 'https://cdn.skypack.dev/three@0.130.0';
import { OrbitControls } from 'https://unpkg.com/three@0.130.0/examples/jsm/controls/OrbitControls.js';
var setting = {
'type':'<?php echo $_GET['type'];?>',
'dimension': {
'length':<?php echo $_GET['length'];?>,
'width':<?php echo $_GET['width'];?>,
'height':<?php echo $_GET['height'];?>
},
'iphone': {
'length':7.2,
'width':0.7,
'height':14.7
},
'position': {
'posX':<?php echo 0-($_GET['length']/2)-(5/2);?>,
'posY':0,
'posZ':0
},
'background':{
'color':0x000000,
'opacity':0.1
},
'camera':{
'posX':0,
'posY':0,
'posZ':<?php echo max($_GET['length'], $_GET['width'])*2;?>
},
'light':{
'color':0xffffff,
'posX':0,
'posY':10,
'posZ':-20
},
'material':[
{'color':0xe3fefe, 'opacity':'1.0', 'transparent':false},
{'color':0x000000, 'opacity':'1.0', 'transparent':false},
],
'outline':{
'color':0xcccccc,
'opacity':1.0
}
}
const scene = new THREE.Scene();
const loader = new THREE.CubeTextureLoader();
const texture = loader.load([
'https://r105.threejsfundamentals.org/threejs/resources/images/cubemaps/computer-history-museum/pos-x.jpg',
'https://r105.threejsfundamentals.org/threejs/resources/images/cubemaps/computer-history-museum/neg-x.jpg',
'https://r105.threejsfundamentals.org/threejs/resources/images/cubemaps/computer-history-museum/pos-y.jpg',
'https://r105.threejsfundamentals.org/threejs/resources/images/cubemaps/computer-history-museum/neg-y.jpg',
'https://r105.threejsfundamentals.org/threejs/resources/images/cubemaps/computer-history-museum/pos-z.jpg',
'https://r105.threejsfundamentals.org/threejs/resources/images/cubemaps/computer-history-museum/neg-z.jpg',
]);
scene.background = texture;
// 建立相機
const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
camera.position.set(setting.camera.posX, setting.camera.posY, setting.camera.posZ); // 相機位置
camera.lookAt(scene.position); // 相機焦點
// 建立光源
const pointLight = new THREE.PointLight(0xffffff);
pointLight.position.set(setting.position.posX,30,0);
//scene.add(pointLight);
const spotLight = new THREE.SpotLight( 0xffffff );
spotLight.position.set( setting.position.posX,40,0 );
//scene.add( spotLight );
// 環境光 (non directional)
const ambientLight = new THREE.AmbientLight('#ffffff');
scene.add(ambientLight);
// 建立光源
/*
var pointLight = new THREE.PointLight(0xffffff);
pointLight.position.set(setting.light.posX, setting.light.posY, setting.light.posZ);
scene.add(pointLight);
*/
const renderer = new THREE.WebGLRenderer({ alpha: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(setting.background.color, setting.background.opacity); //預設背景顏色
renderer.shadowMap.enable = true; //陰影效果
document.body.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
const textureLoader = new THREE.TextureLoader();
const image = 'upload/<?php echo $file;?>';
const group = new THREE.Group();
const phoneGroup = new THREE.Group();
const cardGroup = new THREE.Group();
// 面卡
const frontCardGeometry = new THREE.PlaneGeometry( setting.dimension.length, setting.dimension.width );
var frontCardPlaneMat = new THREE.MeshPhongMaterial({
<?php if ($file!="") { ?>
map: new THREE.TextureLoader().load(image, tex => {
<?php if ($_GET['hash']=="") { ?>
const ratio = frontCardGeometry.parameters.height/frontCardGeometry.parameters.width;
tex.wrapS = tex.wrapT = THREE.RepeatWrapping;
tex.offset.set( 0, 0 );
tex.repeat.set( 1, ratio);
<?php } ?>
/*
var repeatX, repeatY;
repeatX = frontCardGeometry.parameters.height * tex.image.width/ (frontCardGeometry.parameters.width * tex.image.height);
//
if (repeatX > 1) { // checked correct
//fill the width and adjust the height accordingly
repeatX = 1;
repeatY = frontCardGeometry.parameters.height * tex.image.width / (frontCardGeometry.parameters.width * tex.image.height );
tex.repeat.set(repeatX, repeatY);
tex.offset.y= (repeatY - 1) / 2 * -1;
} else { // checked correct
//fill the height and adjust the width accordingly
repeatX = frontCardGeometry.parameters.width * tex.image.height/ (frontCardGeometry.parameters.height * tex.image.width);
repeatY = 1;
tex.repeat.set(repeatX, repeatY);
tex.offset.x = (repeatX - 1) / 2 * -1;
}
*/
}),
<?php } ?>
color: setting.material[0].color,
opacity: setting.material[0].opacity,
transparent: setting.material[0].transparent,
side: THREE.FrontSide
});
// 面卡正面
const frontCard = new THREE.Mesh(frontCardGeometry, frontCardPlaneMat);
frontCard.position.set(setting.position.posX, setting.position.posY, setting.position.posZ+setting.dimension.height/2);
cardGroup.add(frontCard);
// 面卡邊線
const frontCardEdges = new THREE.EdgesGeometry( frontCardGeometry );
const frontCardLine = new THREE.LineSegments( frontCardEdges, new THREE.LineBasicMaterial( { color: setting.outline.color } ) );
frontCardLine.position.set(setting.position.posX, setting.position.posY, setting.position.posZ+setting.dimension.height/2);
cardGroup.add( frontCardLine );
// 面卡背面
const frontCardBackGeometry = new THREE.PlaneGeometry(setting.dimension.length, setting.dimension.width);
const frontCardBackMaterial = new THREE.MeshBasicMaterial( {color: setting.material[0].color, side: THREE.BackSide} );
const frontCardBackPlane = new THREE.Mesh( frontCardBackGeometry, frontCardBackMaterial );
frontCardBackPlane.position.set(setting.position.posX, setting.position.posY, setting.position.posZ+setting.dimension.height/2);
cardGroup.add(frontCardBackPlane);
if(setting.type=='three'){
// 背卡
const backCardGeometry = new THREE.PlaneGeometry( setting.dimension.length, setting.dimension.width );
const backCard = new THREE.Mesh(backCardGeometry, new THREE.MeshBasicMaterial( { color: setting.material[0].color, side: THREE.DoubleSide} ));
backCard.position.set(setting.position.posX, setting.position.posY, setting.position.posZ-setting.dimension.height/2);
cardGroup.add(backCard);
// 背卡邊線
const backCardEdges = new THREE.EdgesGeometry( backCardGeometry );
const backCardLine = new THREE.LineSegments( backCardEdges, new THREE.LineBasicMaterial( { color: setting.outline.color } ) );
backCardLine.position.set(setting.position.posX, setting.position.posY, setting.position.posZ-setting.dimension.height/2);
cardGroup.add(backCardLine);
}
if(setting.type=='two' || setting.type=='three'){
// 底卡正面
const bottomCardFrontGeometry = new THREE.PlaneGeometry( setting.dimension.length, setting.dimension.height );
const bottomCardFront = new THREE.Mesh(bottomCardFrontGeometry, new THREE.MeshBasicMaterial( { color: setting.material[0].color, side: THREE.DoubleSide} ));
bottomCardFront.position.set(setting.position.posX, setting.position.posY-setting.dimension.width/2, setting.position.posZ);
bottomCardFront.rotation.x = Math.PI / 2;
cardGroup.add(bottomCardFront);
// 底卡正面邊線
const bottomCardFrontEdges = new THREE.EdgesGeometry( bottomCardFrontGeometry );
const bottomCardFrontLine = new THREE.LineSegments( bottomCardFrontEdges, new THREE.LineBasicMaterial( { color: setting.outline.color } ) );
bottomCardFrontLine.scale.multiplyScalar(0.99);
bottomCardFrontLine.position.set(setting.position.posX, setting.position.posY-setting.dimension.width/2, setting.position.posZ);
bottomCardFrontLine.rotation.x = Math.PI / 2;
cardGroup.add(bottomCardFrontLine);
// 底卡背面
const bottomCardBackGeometry = new THREE.PlaneGeometry( setting.dimension.length, setting.dimension.height );
const bottomCardBack = new THREE.Mesh(bottomCardBackGeometry, new THREE.MeshBasicMaterial( { color: setting.material[0].color, side: THREE.FrontSide} ));
bottomCardBack.position.set(setting.position.posX, setting.position.posY-setting.dimension.width/2, setting.position.posZ);
bottomCardBack.rotation.x = Math.PI / 2;
// group.add(bottomCardBack);
// 底卡背面邊線
const bottomCardBackEdges = new THREE.EdgesGeometry( bottomCardBackGeometry );
const bottomCardBackLine = new THREE.LineSegments( bottomCardBackEdges, new THREE.LineBasicMaterial( { color: setting.outline.color } ) );
bottomCardBackLine.position.set(setting.position.posX, setting.position.posY-setting.dimension.width/2, setting.position.posZ);
bottomCardBackLine.rotation.x = Math.PI / 2;
// group.add(bottomCardBackLine);
}
group.add(cardGroup);
// phone object
const phoneGeometry = new THREE.BoxGeometry(setting.iphone.length, setting.iphone.height, setting.iphone.width);
const phoneTexture = textureLoader.load("asset/iphone.png");
var phoneMaterial = [
new THREE.MeshBasicMaterial({
color: setting.material[1].color,
opacity: setting.material[1].opacity,
transparent: setting.material[1].transparent,
}),
new THREE.MeshBasicMaterial({
color: setting.material[1].color,
opacity: setting.material[1].opacity,
transparent: setting.material[1].transparent,
}),
new THREE.MeshBasicMaterial({
color: setting.material[1].color,
opacity: setting.material[1].opacity,
transparent: setting.material[1].transparent,
}),
new THREE.MeshBasicMaterial({
color: setting.material[1].color,
opacity: setting.material[1].opacity,
transparent: setting.material[1].transparent,
}),
new THREE.MeshBasicMaterial({
color: setting.material[0].color,
opacity: setting.material[0].opacity,
transparent: setting.material[0].transparent,
map: phoneTexture,
}),
new THREE.MeshBasicMaterial({
color: setting.material[1].color,
opacity: setting.material[1].opacity,
transparent: setting.material[1].transparent,
}),
];
//const phoneMaterial = new THREE.MeshBasicMaterial({map:phoneTexture});
const phone = new THREE.Mesh(phoneGeometry, phoneMaterial);
phone.position.set(setting.iphone.length/2+5/2, setting.position.posY, setting.position.posZ);
//scene.add(phone);
phoneGroup.add(phone);
group.add(phoneGroup);
scene.add(group);
/* box helper */
//const boxHelper = new THREE.BoxHelper( box, 0x000000 );
//scene.add( boxHelper );
/* grid helper
const size = 100;
const divisions = 100;
const gridHelper = new THREE.GridHelper( size, divisions );
scene.add( gridHelper );
*/
/* spotlight helper
const spotLightHelper = new THREE.SpotLightHelper( spotLight );
scene.add( spotLightHelper );
*/
/* pointlight helper
const sphereSize = 30;
const pointLightHelper = new THREE.PointLightHelper( pointLight, sphereSize );
scene.add( pointLightHelper );
*/
/* axes helper
const axesHelper = new THREE.AxesHelper( 100 );
scene.add( axesHelper );
*/
/* camera helper
const camerhelper = new THREE.CameraHelper( camera );
scene.add( camerhelper );
*/
//camera.position.z = 5;
function animate(){
//sphere.rotation.x += 0.01;
//sphere.rotation.y += 0.01;
//box.rotation.x += 0.01;
//box.rotation.y += 0.01;
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
// 監聽螢幕寬高來做簡單 RWD 設定
window.addEventListener('resize', function() {
console.log("run");
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
})
animate();
</script>
</body>
</html>