main.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //import * as THREE from 'three';
  2. import * as THREE from '../js/three.module.js';
  3. import initTinyUSDZ from '../js/tinyusdz.js';
  4. const urlAll =window.location.href.split('?m=')[1]
  5. console.log(123456,urlAll);
  6. const USDZ_FILEPATH = `https://4dkankan.oss-cn-shenzhen.aliyuncs.com/sxz/${urlAll}.usdz`;
  7. const usd_res = await fetch(USDZ_FILEPATH);
  8. const usd_data = await usd_res.arrayBuffer();
  9. const usd_binary = new Uint8Array(usd_data);
  10. initTinyUSDZ().then(function(TinyUSDZLoader) {
  11. const usd = new TinyUSDZLoader.TinyUSDZLoader(usd_binary);
  12. console.log(usd.numMeshes());
  13. const scene = new THREE.Scene();
  14. const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
  15. const renderer = new THREE.WebGLRenderer();
  16. renderer.setSize( window.innerWidth, window.innerHeight );
  17. renderer.setAnimationLoop( animate );
  18. document.body.appendChild( renderer.domElement );
  19. // First mesh only
  20. const mesh = usd.getMesh(0);
  21. //console.log("usd", usd)
  22. //console.log("mesh", mesh);
  23. //const geometry = new THREE.BoxGeometry( 1, 1, 1 );
  24. const geometry = new THREE.BufferGeometry();
  25. geometry.setAttribute( 'position', new THREE.BufferAttribute( mesh.points, 3 ) );
  26. // TODO: set normal from mesh
  27. if (mesh.hasOwnProperty('texcoords')) {
  28. console.log(mesh.texcoords);
  29. geometry.setAttribute( 'uv', new THREE.BufferAttribute( mesh.texcoords, 2 ) );
  30. }
  31. const usdMaterial = usd.getMaterial(mesh.materialId);
  32. //console.log("usdMat", usdMaterial);
  33. //if (usdMaterial.aaa) {
  34. // console.log("aaa");
  35. //}
  36. var material;
  37. if (usdMaterial.hasOwnProperty('diffuseColorTextureId')) {
  38. const diffTex = usd.getTexture(usdMaterial.diffuseColorTextureId);
  39. const img = usd.getImage(diffTex.textureImageId);
  40. console.log(img);
  41. // assume RGBA for now.
  42. let image8Array = new Uint8ClampedArray(img.data);
  43. let imgData = new ImageData(image8Array, img.width, img.height);
  44. const texture = new THREE.DataTexture( imgData, img.width, img.height );
  45. texture.flipY = true;
  46. texture.needsUpdate = true;
  47. material = new THREE.MeshBasicMaterial({
  48. map: texture
  49. });
  50. } else {
  51. material = new THREE.MeshNormalMaterial();
  52. }
  53. // Assume triangulated indices.
  54. geometry.setIndex( new THREE.Uint32BufferAttribute(mesh.faceVertexIndices, 1) );
  55. geometry.computeVertexNormals();
  56. //const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
  57. const cube = new THREE.Mesh( geometry, material );
  58. scene.add( cube );
  59. //camera.position.z = 25;
  60. camera.position.z = 1.0;
  61. function animate() {
  62. //cube.rotation.x += 0.01;
  63. cube.rotation.y += 0.02;
  64. renderer.render( scene, camera );
  65. }
  66. });