2013-04-01 2 views
2

shapeGeometry에 텍스처를 렌더링 할 수 있습니까? 나는 여러 가지 접근법을 시도했지만, 이것은 내가 얻을 수 있었던 것만 큼 가까운 것이다. 텍스처가로드되고있는 것 같습니다. 그것은 구형에 적용하면 작동하지만 회색 및 흰색 줄무늬로만 화면에 표시됩니다. 감사합니다, 데이비드Three.JS shapeGeometry에 텍스처를 렌더링 할 수 있습니까?

<script> 

     var container, stats; 

     var camera, scene, renderer; 

     var text, plane; 

     var targetRotation = 0; 
     var targetRotationOnMouseDown = 0; 

     var mouseX = 0; 
     var mouseXOnMouseDown = 0; 

     var windowHalfX = window.innerWidth/2; 
     var windowHalfY = window.innerHeight/2; 

     init(); 
     animate(); 

     function init() { 

      container = document.createElement('div'); 
      document.body.appendChild(container); 

      camera = new THREE.PerspectiveCamera(50, window.innerWidth/window.innerHeight, 1, 1000); 
      camera.position.set(0, 150, 500); 

      scene = new THREE.Scene(); 

      parent = new THREE.Object3D(); 
      parent.position.y = 50; 
      scene.add(parent); 

          var earthTexture = new THREE.Texture(); 
      var loader = new THREE.ImageLoader(); 
      loader.addEventListener('load', function (event) { 

       earthTexture.image = event.content; 
       earthTexture.needsUpdate = true; 

      }); 

      loader.load('textures/land_ocean_ice_cloud_2048.jpg'); 

          var triangleShape = new THREE.Shape(); 
      triangleShape.moveTo( 80, 20); 
      triangleShape.lineTo( 40, 80); 
      triangleShape.lineTo(120, 80); 
      triangleShape.lineTo( 80, 20); // close path 
          var geometry = new THREE.ShapeGeometry(triangleShape); 
          var material = new THREE.MeshBasicMaterial({ map: earthTexture, overdraw: true }); 
          var mesh = new THREE.Mesh(geometry, material); 
          parent.add(mesh); 

      function addShape(shape, color, lncolor, x, y, z, rx, ry, rz, s) { 

       // flat shape 

       var geometry = new THREE.ShapeGeometry(shape); 
            var material = new THREE.MeshBasicMaterial({ map: color, overdraw: true }); 
            //var material = new THREE.MeshBasicMaterial({ color: color, overdraw: true}); 


       var mesh = new THREE.Mesh(geometry, material); 
       mesh.position.set(x, y, z); 
       mesh.rotation.set(rx, ry, rz); 
       mesh.scale.set(s, s, s); 
       parent.add(mesh); 

       // line 

       var geometry = shape.createPointsGeometry(); 
       var material = new THREE.LineBasicMaterial({ linewidth: 2, color: lncolor, transparent: true }); 

       var line = new THREE.Line(geometry, material); 
       line.position.set(x, y, z); 
       line.rotation.set(rx, ry, rz); 
       line.scale.set(s, s, s); 
       parent.add(line); 

      } 

      // California 

      var californiaPts = []; 

      californiaPts.push(new THREE.Vector2 (610, 320)); 
      californiaPts.push(new THREE.Vector2 (450, 300)); 
      californiaPts.push(new THREE.Vector2 (392, 392)); 
      californiaPts.push(new THREE.Vector2 (266, 438)); 
      californiaPts.push(new THREE.Vector2 (190, 570)); 
      californiaPts.push(new THREE.Vector2 (190, 600)); 
      californiaPts.push(new THREE.Vector2 (160, 620)); 
      californiaPts.push(new THREE.Vector2 (160, 650)); 
      californiaPts.push(new THREE.Vector2 (180, 640)); 
      californiaPts.push(new THREE.Vector2 (165, 680)); 
      californiaPts.push(new THREE.Vector2 (150, 670)); 
      californiaPts.push(new THREE.Vector2 ( 90, 737)); 
      californiaPts.push(new THREE.Vector2 ( 80, 795)); 
      californiaPts.push(new THREE.Vector2 ( 50, 835)); 
      californiaPts.push(new THREE.Vector2 ( 64, 870)); 
      californiaPts.push(new THREE.Vector2 ( 60, 945)); 
      californiaPts.push(new THREE.Vector2 (300, 945)); 
      californiaPts.push(new THREE.Vector2 (300, 743)); 
      californiaPts.push(new THREE.Vector2 (600, 473)); 
      californiaPts.push(new THREE.Vector2 (626, 425)); 
      californiaPts.push(new THREE.Vector2 (600, 370)); 
      californiaPts.push(new THREE.Vector2 (610, 320)); 

      var californiaShape = new THREE.Shape(californiaPts); 



      // addShape(shape, color, x, y, z, rx, ry,rz, s); 

      addShape(californiaShape,earthTexture, 0x000000, -300, -100, 0, 0, 0, 0, 0.25); 


      // 


      renderer = new THREE.CanvasRenderer({ antialias: true }); 
      renderer.setSize(window.innerWidth, window.innerHeight); 
      //renderer.sortObjects = false; 
      //renderer.sortElements = false; 
      container.appendChild(renderer.domElement); 

      stats = new Stats(); 
      stats.domElement.style.position = 'absolute'; 
      stats.domElement.style.top = '0px'; 
      container.appendChild(stats.domElement); 

      document.addEventListener('mousedown', onDocumentMouseDown, false); 
      document.addEventListener('touchstart', onDocumentTouchStart, false); 
      document.addEventListener('touchmove', onDocumentTouchMove, false); 

      // 

      window.addEventListener('resize', onWindowResize, false); 

     } 

     function onWindowResize() { 

      windowHalfX = window.innerWidth/2; 
      windowHalfY = window.innerHeight/2; 

      camera.aspect = window.innerWidth/window.innerHeight; 
      camera.updateProjectionMatrix(); 

      renderer.setSize(window.innerWidth, window.innerHeight); 

     } 
     function loadImage(path) { 

      var image = document.createElement('img'); 
      var texture = new THREE.Texture(image, THREE.UVMapping) 

      image.onload = function() { texture.needsUpdate = true; }; 
      image.src = path; 

      return texture; 

     } 

     // 

     function onDocumentMouseDown(event) { 

      event.preventDefault(); 

      document.addEventListener('mousemove', onDocumentMouseMove, false); 
      document.addEventListener('mouseup', onDocumentMouseUp, false); 
      document.addEventListener('mouseout', onDocumentMouseOut, false); 

      mouseXOnMouseDown = event.clientX - windowHalfX; 
      targetRotationOnMouseDown = targetRotation; 

     } 

     function onDocumentMouseMove(event) { 

      mouseX = event.clientX - windowHalfX; 

      targetRotation = targetRotationOnMouseDown + (mouseX - mouseXOnMouseDown) * 0.02; 

     } 

     function onDocumentMouseUp(event) { 

      document.removeEventListener('mousemove', onDocumentMouseMove, false); 
      document.removeEventListener('mouseup', onDocumentMouseUp, false); 
      document.removeEventListener('mouseout', onDocumentMouseOut, false); 

     } 

     function onDocumentMouseOut(event) { 

      document.removeEventListener('mousemove', onDocumentMouseMove, false); 
      document.removeEventListener('mouseup', onDocumentMouseUp, false); 
      document.removeEventListener('mouseout', onDocumentMouseOut, false); 

     } 

     function onDocumentTouchStart(event) { 

      if (event.touches.length == 1) { 

       event.preventDefault(); 

       mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX; 
       targetRotationOnMouseDown = targetRotation; 

      } 

     } 

     function onDocumentTouchMove(event) { 

      if (event.touches.length == 1) { 

       event.preventDefault(); 

       mouseX = event.touches[ 0 ].pageX - windowHalfX; 
       targetRotation = targetRotationOnMouseDown + (mouseX - mouseXOnMouseDown) * 0.05; 

      } 

     } 

     // 

     function animate() { 

      requestAnimationFrame(animate); 

      render(); 
      stats.update(); 

     } 

     function render() { 

      parent.rotation.y += (targetRotation - parent.rotation.y) * 0.05; 
      renderer.render(scene, camera); 

     } 

    </script> 

답변

1

는 구체적으로이 질문에 Texture mapping on extrude geometry ThreeJS

봐이 지역

mesh = THREE.SceneUtils.createMultiMaterialObject(geometry, [material, new  THREE.MeshBasicMaterial({ 
    color: 0x000000, 
    wireframe: true, 
    transparent: true 
})]); 
+0

감사를! 그것은 많은 도움이되었습니다! –

+0

도움이 된 것을 기쁘게 생각합니다. –

+0

이 답변은 기본적으로 설명이없는 작은 코드 샘플을 가진 링크입니다. 좋은 답변은 구체적으로 질문 된 질문에 집중되어 있어야하며 여기서 바로 답변해야합니다. 링크는 더 많은 참조 용입니다. – fivedogit

관련 문제