๐ŸŽฎArcadeLab

Dream Home

by QuantumPenguin63
348 lines11.9 KB๐Ÿ› ๏ธ Three.js (3D graphics)
โ–ถ Play

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<title>Dream Home</title>
<style>
*{box-sizing:border-box}
body{margin:0;overflow:hidden;background:#ddd;font-family:Arial,sans-serif;color:#30251f}
#game{position:fixed;inset:0}
canvas{display:block;width:100%;height:100%;touch-action:none}
#top{position:absolute;top:12px;left:12px;right:12px;display:flex;justify-content:space-between;align-items:center;pointer-events:none}
.logo{background:#fffdf0eF;padding:12px 16px;border-radius:14px;font-weight:bold;box-shadow:0 3px 12px #0002}
#roomName{font-size:12px;color:#80634e;margin-top:4px}
#rooms{position:absolute;top:82px;left:10px;right:10px;display:flex;gap:7px;overflow-x:auto}
button{border:0;border-radius:12px;padding:12px 14px;background:#fff;color:#49372b;font-weight:bold;box-shadow:0 3px 9px #0002;white-space:nowrap}
button.active{background:#77533d;color:white}
#panel{position:absolute;bottom:0;left:0;right:0;background:#fffdf4f2;border-radius:22px 22px 0 0;padding:12px 12px 22px;box-shadow:0 -5px 20px #0002;max-height:36vh;overflow-y:auto}
#items{display:flex;gap:8px;overflow-x:auto;padding:8px 0}
.item{min-width:78px;text-align:center;padding:10px 7px;background:#f1e8dc;border-radius:12px;font-size:12px}
.item span{font-size:25px;display:block;margin-bottom:5px}
.row{display:flex;align-items:center;justify-content:space-between;gap:8px}
h3{font-size:14px;margin:5px 0}
small{color:#806c5b}
input[type=color]{width:44px;height:38px;border:0;background:none}
#hint{position:absolute;top:135px;left:50%;transform:translateX(-50%);background:#fffdf0dd;padding:8px 12px;border-radius:20px;font-size:11px;white-space:nowrap}
#rotate{background:#75543f;color:white}
</style>
</head>
<body>
<div id="game"></div>

<div id="top">
 <div class="logo">๐Ÿก DREAM HOME<div id="roomName">Modern Villa ยท Living Room</div></div>
 <button id="rotate">โ†ป Rotate</button>
</div>

<div id="rooms">
 <button class="active" data-room="living">๐Ÿ›‹ Living</button>
 <button data-room="kitchen">๐Ÿณ Kitchen</button>
 <button data-room="bedroom">๐Ÿ› Bedroom</button>
 <button data-room="kids">๐Ÿงธ Kids Room</button>
</div>

<div id="hint">Drag to look around ยท Tap furniture to move it</div>

<div id="panel">
 <div class="row">
  <h3>โœจ Customize your room</h3>
  <div class="row"><small>Wall color</small><input type="color" id="wallColor" value="#eee5d9"></div>
 </div>
 <div class="row">
  <h3>Furniture store</h3>
  <small>Tap to add</small>
 </div>
 <div id="items">
  <button class="item" data-item="sofa"><span>๐Ÿ›‹๏ธ</span>Sofa</button>
  <button class="item" data-item="chair"><span>๐Ÿช‘</span>Chair</button>
  <button class="item" data-item="table"><span>๐ŸŸซ</span>Table</button>
  <button class="item" data-item="bed"><span>๐Ÿ›๏ธ</span>Bed</button>
  <button class="item" data-item="plant"><span>๐Ÿชด</span>Plant</button>
  <button class="item" data-item="lamp"><span>๐Ÿ’ก</span>Lamp</button>
  <button class="item" data-item="cabinet"><span>๐Ÿ—„๏ธ</span>Cabinet</button>
 </div>
 <div class="row">
  <small id="selected">Select a furniture item to move it</small>
  <button id="delete">Delete selected</button>
 </div>
</div>

<script>
(() => {
const container=document.getElementById('game');
const scene=new THREE.Scene();
scene.background=new THREE.Color('#c9d0d1');
scene.fog=new THREE.Fog('#c9d0d1',22,45);

const camera=new THREE.PerspectiveCamera(42,innerWidth/innerHeight,.1,100);
camera.position.set(12,11,14);

const renderer=new THREE.WebGLRenderer({antialias:true});
renderer.setSize(innerWidth,innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio,1.6));
renderer.shadowMap.enabled=true;
renderer.shadowMap.type=THREE.PCFSoftShadowMap;
renderer.outputEncoding=THREE.sRGBEncoding;
renderer.toneMapping=THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure=1.1;
container.appendChild(renderer.domElement);

scene.add(new THREE.HemisphereLight(0xffffff,0x8c8172,1.25));
const sun=new THREE.DirectionalLight(0xffefd9,2.1);
sun.position.set(-7,14,9);
sun.castShadow=true;
sun.shadow.mapSize.set(1024,1024);
scene.add(sun);

const mat=(color,rough=.8,metal=0)=>new THREE.MeshStandardMaterial({color,roughness:rough,metalness:metal});
const wood=mat('#9b7554');
const cream=mat('#e9dfd1');
const dark=mat('#423a34');
const white=mat('#f7f3ed');
const glass=new THREE.MeshStandardMaterial({color:'#b8d5dd',roughness:.15,metalness:.1});
const floorMat=mat('#c6a98a');

let roomGroup=new THREE.Group();
let furnitureGroup=new THREE.Group();
scene.add(roomGroup,furnitureGroup);
let selected=null,wallMaterial;
let room='living';
let wallColor='#eee5d9';

const raycaster=new THREE.Raycaster();
const pointer=new THREE.Vector2();
const ground=new THREE.Plane(new THREE.Vector3(0,1,0),0);
let dragging=false,rotating=false;
let previousX=0,previousY=0;
let angle=Math.PI/4,tilt=.72,distance=19;

function box(parent,x,y,z,w,h,d,material,cast=true){
 const m=new THREE.Mesh(new THREE.BoxGeometry(w,h,d),material);
 m.position.set(x,y,z);
 m.castShadow=cast;
 m.receiveShadow=true;
 parent.add(m);
 return m;
}
function cylinder(parent,x,y,z,r1,r2,h,material){
 const m=new THREE.Mesh(new THREE.CylinderGeometry(r1,r2,h,20),material);
 m.position.set(x,y,z);
 m.castShadow=true;
 parent.add(m);
 return m;
}
function group(){return new THREE.Group()}

function makeRoom(){
 roomGroup.clear();
 furnitureGroup.clear();
 selected=null;
 document.getElementById('selected').textContent='Select furniture to move it';
 
 box(roomGroup,0,-.18,0,12,.35,10,floorMat,false);
 // wooden floor strips
 for(let z=-4.7;z<5;z+=.55){
  box(roomGroup,0,.005,z,11.95,.025,.018,mat('#ad8967'),false);
 }
 wallMaterial=mat(wallColor);
 box(roomGroup,0,2.2,-5,12,4.4,.18,wallMaterial,false);
 box(roomGroup,-6,2.2,0,.18,4.4,10,wallMaterial,false);
 // skirting
 box(roomGroup,0,.13,-4.86,11.8,.22,.12,white,false);
 box(roomGroup,-5.85,.13,0,.12,.22,9.7,white,false);
 // right open side and windows
 box(roomGroup,3,3.2,-4.87,4.6,2.4,.04,glass,false);
 box(roomGroup,3,3.2,-4.82,.08,2.5,.1,white,false);
 box(roomGroup,3,3.2,-4.82,4.7,.08,.1,white,false);
 box(roomGroup,5.35,2.2,0,.15,4.4,10,wallMaterial,false);
 // window frames
 box(roomGroup,3,3.2,-4.72,.07,2.5,.1,white,false);
 box(roomGroup,3,3.2,-4.72,4.7,.07,.1,white,false);
 box(roomGroup,3,3.2,-4.72,.07,2.5,.1,white,false);
 // ceiling border beams
 box(roomGroup,0,4.4,-2.5,12,.13,.13,white,false);
 box(roomGroup,-5.8,4.4,0,.13,.13,10,white,false);
 // rug
 box(roomGroup,0,.035,0,5,.07,3.5,mat('#d8c5ad'),false);
 // decorative ceiling light
 cylinder(roomGroup,0,4.15,0,.22,.22,.1,mat('#d2b47c',.3,.4));
 
 if(room==='living'){
  addFurniture('sofa',-1,.15,1);
  addFurniture('table',0,.1,-.2);
  addFurniture('chair',3,.15,1);
  addFurniture('plant',4,.1,-3);
  addFurniture('lamp',-4,.1,2);
 }else if(room==='kitchen'){
  addFurniture('cabinet',-2,.1,-3);
  addFurniture('cabinet',1,.1,-3);
  addFurniture('table',0,.1,1);
  addFurniture('chair',2,.1,1);
  addFurniture('plant',4,.1,-2);
 }else if(room==='bedroom'){
  addFurniture('bed',0,.1,-1);
  addFurniture('cabinet',-3,.1,2);
  addFurniture('lamp',3,.1,1);
  addFurniture('plant',4,.1,-3);
 }else{
  addFurniture('bed',0,.1,-1);
  addFurniture('table',-2,.1,2);
  addFurniture('chair',2,.1,2);
  addFurniture('plant',4,.1,-3);
 }
}

function addFurniture(type,x,y,z){
 const g=group();
 g.position.set(x,y,z);
 g.userData.type=type;
 const beige=mat('#c7a88a'),fabric=mat('#d7c9b9'),legs=mat('#574333');
 if(type==='sofa'){
  box(g,0,.48,0,2.8,.65,1.05,fabric);
  box(g,0,1,-.43,2.8,.95,.24,beige);
  box(g,-1.3,.75,0,.25,.75,1.05,beige);
  box(g,1.3,.75,0,.25,.75,1.05,beige);
  for(let x of [-1,1])for(let z of [-.35,.35])cylinder(g,x,.12,z,.06,.06,.24,legs);
 }else if(type==='chair'){
  box(g,0,.65,0,.9,.2,.85,fabric);
  box(g,0,1.25,-.35,.9,1,.16,beige);
  for(let x of [-.35,.35])for(let z of [-.3,.3])box(g,x,.3,z,.09,.6,.09,legs);
 }else if(type==='table'){
  box(g,0,.75,0,1.65,.16,1.1,wood);
  for(let x of [-.65,.65])for(let z of [-.38,.38])box(g,x,.36,z,.1,.7,.1,legs);
 }else if(type==='bed'){
  box(g,0,.45,0,2.2,.35,3,wood);
  box(g,0,.68,0,2.1,.2,2.9,white);
  box(g,0,.95,-1.35,2.2,.95,.18,beige);
  box(g,-.6,.84,1, .65,.18,.55,white);
  box(g,.6,.84,1,.65,.18,.55,white);
 }else if(type==='plant'){
  cylinder(g,0,.3,0,.28,.22,.55,mat('#a36e4b'));
  for(let i=0;i<7;i++){
   let a=i*Math.PI*2/7;
   const leaf=box(g,Math.cos(a)*.25,.95+Math.sin(i)*.1,Math.sin(a)*.25,.16,.75,.16,mat('#4d7952'));
   leaf.rotation.z=Math.cos(a)*.45;
  }
 }else if(type==='lamp'){
  cylinder(g,0,.1,0,.35,.35,.18,wood);
  cylinder(g,0,1,0,.045,.045,1.8,legs);
  cylinder(g,0,1.95,0,.45,.15,.5,mat('#e5d4ac'));
 }else if(type==='cabinet'){
  box(g,0,.9,0,2,.0+1.7,.8,wood);
  box(g,0,.9,.43,1.85,1.5,.06,beige);
  for(let x of [-.45,.45])cylinder(g,x,.9,.51,.035,.035,.1,legs);
 }
 furnitureGroup.add(g);
 return g;
}

function updateCamera(){
 const target=new THREE.Vector3(0,1,0);
 camera.position.set(
 target.x+distance*Math.sin(angle)*Math.cos(tilt),
 target.y+distance*Math.sin(tilt),
 target.z+distance*Math.cos(angle)*Math.cos(tilt)
 );
 camera.lookAt(target);
}
updateCamera();

function setPointer(e){
 const r=renderer.domElement.getBoundingClientRect();
 pointer.x=(e.clientX-r.left)/r.width*2-1;
 pointer.y=-(e.clientY-r.top)/r.height*2+1;
 raycaster.setFromCamera(pointer,camera);
}
function pick(e){
 setPointer(e);
 const hits=raycaster.intersectObjects(furnitureGroup.children,true);
 if(!hits.length)return null;
 let obj=hits[0].object;
 while(obj.parent&&obj.parent!==furnitureGroup)obj=obj.parent;
 return obj.parent===furnitureGroup?obj:null;
}
function moveToPointer(e){
 setPointer(e);
 const p=new THREE.Vector3();
 if(raycaster.ray.intersectPlane(ground,p)){
  selected.position.x=Math.max(-4.8,Math.min(4.8,p.x));
  selected.position.z=Math.max(-3.8,Math.min(3.8,p.z));
 }
}
renderer.domElement.addEventListener('pointerdown',e=>{
 previousX=e.clientX;previousY=e.clientY;
 const found=pick(e);
 if(found){
  selected=found;
  dragging=true;
  document.getElementById('selected').textContent='Selected: '+found.userData.type;
  moveToPointer(e);
 }else rotating=true;
 renderer.domElement.setPointerCapture(e.pointerId);
});
renderer.domElement.addEventListener('pointermove',e=>{
 if(dragging&&selected){moveToPointer(e);}
 else if(rotating){
  angle-=(e.clientX-previousX)*.008;
  tilt=Math.max(.2,Math.min(1.15,tilt+(e.clientY-previousY)*.005));
  updateCamera();
 }
 previousX=e.clientX;previousY=e.clientY;
});
function stop(){dragging=false;rotating=false}
renderer.domElement.addEventListener('pointerup',stop);
renderer.domElement.addEventListener('pointercancel',stop);

document.querySelectorAll('[data-room]').forEach(btn=>{
 btn.addEventListener('click',()=>{
  room=btn.dataset.room;
  document.querySelectorAll('[data-room]').forEach(b=>b.classList.toggle('active',b===btn));
  const names={living:'Living Room',kitchen:'Kitchen',bedroom:'Bedroom',kids:'Kids Playroom'};
  document.getElementById('roomName').textContent='Modern Villa ยท '+names[room];
  makeRoom();
 });
});

document.querySelectorAll('[data-item]').forEach(btn=>{
 btn.addEventListener('click',()=>{
  const obj=addFurniture(btn.dataset.item,0,.1,0);
  selected=obj;
  document.getElementById('selected').textContent='Selected: '+obj.userData.type+' ยท Drag to move';
 });
});

document.getElementById('wallColor').addEventListener('input',e=>{
 wallColor=e.target.value;
 wallMaterial.color.set(wallColor);
});
document.getElementById('delete').addEventListener('click',()=>{
 if(selected){
  furnitureGroup.remove(selected);
  selected=null;
  document.getElementById('selected').textContent='Furniture removed';
 }
});
document.getElementById('rotate').addEventListener('click',()=>{
 angle+=Math.PI/3;
 updateCamera();
});

addEventListener('resize',()=>{
 camera.aspect=innerWidth/innerHeight;
 camera.updateProjectionMatrix();
 renderer.setSize(innerWidth,innerHeight);
});
makeRoom();

function animate(){
 requestAnimationFrame(animate);
 renderer.render(scene,camera);
}
animate();
})();
</script>
</body>
</html>

Game Source: Dream Home

Creator: QuantumPenguin63

Libraries: three

Complexity: complex (348 lines, 11.9 KB)

The full source code is displayed above on this page.

Remix Instructions

To remix this game, copy the source code above and modify it. Add a ARCADELAB header at the top with "remix_of: dream-home-quantumpenguin63" to link back to the original. Then publish at arcadelab.ai/publish.