2017-12-22 2 views
2

phongmaterial을 사용하여 메쉬 뷰 큐브에 이미지를 적용하려하지만 색상이없는 검은 색 큐브 만 화면에 표시됩니다. 그것은 Box와 큐브로 작업했지만 지금은 그것도 사용하지 않습니다.Javafx : 이미지가 메쉬보기에 적용되지 않음

또한 특정 큐브의 얼굴을 얻는 방법을 알려 줄 수 있습니까? 여기

내 코드입니다 :

Rotate rxBox = new Rotate(30, 0, 0, 0, Rotate.X_AXIS); 
Rotate ryBox = new Rotate(50, 0, 0, 0, Rotate.Y_AXIS); 
Rotate rzBox = new Rotate(30, 0, 0, 0, Rotate.Z_AXIS); 

@Override 
public void start(Stage primaryStage) { 


    float hw = 100/2f; 
    float hh = 100/2f; 
    float hd = 100/2f; 

    float points[] = { 
      hw, hh, hd, 
      hw, hh, -hd, 
      hw, -hh, hd, 
      hw, -hh, -hd, 
      -hw, hh, hd, 
      -hw, hh, -hd, 
      -hw, -hh, hd, 
      -hw, -hh, -hd}; 

    float tex[] = { 
      100, 0, 
      200, 0, 
      0, 100, 
      100, 100, 
      200, 100, 
      300, 100, 
      400, 100, 
      0, 200, 
      100, 200, 
      200, 200, 
      300, 200, 
      400, 200, 
      100, 300, 
      200, 300}; 

    int faces[] = { 
      0, 10, 2, 5, 1, 9, 
      2, 5, 3, 4, 1, 9, 
      4, 7, 5, 8, 6, 2, 
      6, 2, 5, 8, 7, 3, 
      0, 13, 1, 9, 4, 12, 
      4, 12, 1, 9, 5, 8, 
      2, 1, 6, 0, 3, 4, 
      3, 4, 6, 0, 7, 3, 
      0, 10, 4, 11, 2, 5, 
      2, 5, 4, 11, 6, 6, 
      1, 9, 3, 4, 5, 8, 
      5, 8, 3, 4, 7, 3}; 

    int[] facesSmoothingGroups = {0,0,1,1,2,2,3,3,4,4,5,5}; 

    // URL resource = Main.class.getResource("DFv8z.png"); 
    Image diffuseMap = new Image(Main.class.getResource("PCmIW.png").toExternalForm()); 

    TriangleMesh mesh = new TriangleMesh(); 
    mesh.getPoints().addAll(points); 
    mesh.getTexCoords().addAll(tex); 
    mesh.getFaces().addAll(faces); 
    // mesh.getFaceSmoothingGroups().addAll(facesSmoothingGroups); 

    Material cubeMaterial = new PhongMaterial(Color.TRANSPARENT, diffuseMap, null, null, null); 

    MeshView m = new MeshView(mesh); 
    m.getTransforms().addAll(rxBox, ryBox, rzBox); 
    m.setMaterial(cubeMaterial); 

    final Group g = new Group(m); 

    g.setTranslateX(400/2); 
    g.setTranslateY(400/2); 
    //g.setTranslateZ(400/2); 
    g.setRotationAxis(Rotate.Y_AXIS); 
    g.setRotationAxis(Rotate.X_AXIS); 
    //g.setRotationAxis(Rotate.Z_AXIS); 




    Scene scene = new Scene(g, 400, 400, Color.SKYBLUE); 


    primaryStage.setTitle("Hello World"); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 

} 

public static void main(String[] args) { 
    launch(args); 
} 

@Override 
public void handle(MouseEvent event) { 
    // TODO Auto-generated method stub 

} 

}

Here is the image i want to use.

답변

1

당신이 올바른 궤도에 있지만 필요 : 0.0 사이

  • 를 사용하여 정규화 된 텍스처 좌표, f 및 1.0f
  • 확산 색상을 투명하게 설정하지 말고 확산 맵 이미지를 설정하십시오.

제의 경우, 바로 순수한 이미지의 전체 길이 및 높이를 정의

float L = 400f; 
float H = 300f; 

을 다음 텍스처 좌표 정상화 : 단지

float tex[] = { 
     100f/L, 0f/H, 
     200f/L, 0f/H, 
      0f/L, 100f/H, 
     100f/L, 100f/H, 
     200f/L, 100f/H, 
     300f/L, 100f/H, 
     400f/L, 100f/H, 
      0f/L, 200f/H, 
     100f/L, 200f/H, 
     200f/L, 200f/H, 
     300f/L, 200f/H, 
     400f/L, 200f/H, 
     100f/L, 300f/H, 
     200f/L, 300f/H};  

을 두번째 부분 들어 세터 사용 :

PhongMaterial cubeMaterial = new PhongMaterial(); 
cubeMaterial.setDiffuseMap(diffuseMap); 

당신의 질감 큐브 (스무딩 그룹을 가능)했습니다 : 당신이 정의하는 경우

Textured cube

편집 상단면을 찾기 위해

을, 가능한 회전 후이 쉽게 수행 할 수 있습니다 그물 이미지와 방법면의 순서에 따라 현재의 색상과 법선이 정의됩니다

final String[] colors = {"Blue", "Red", "White", "Yellow", "Cyan", "Green"}; 

float normals[] = { 
    1f, 0f, 0f, 
    -1f, 0f, 0f, 
    0f, 1f, 0f, 
    0f, -1f, 0f, 
    0f, 0f, 1f, 
    0f, 0f, -1f, 
}; 

그런 다음, 당신은 012,307,326,242 정상에 해당하는 윗면을 추적 할 장면 좌표의.

그래서 기본적으로는 메쉬 좌표 즉 정상적인 표현해야

Point3D normal = m.sceneToLocal(0, -1, 0); 

을 마지막으로 당신은 normals에 정의 된 모든이 정상과 일치해야합니다. 그 중 하나의 색인은 당신에게 색깔을 줄 것입니다 :

private String getColor(MeshView m, float[] normals) { 

    Point3D normal = m.sceneToLocal(0, -1, 0); 

    for (int i = 0; i < normals.length; i += 3) { 
     if (normals[i] * normal.getX() + normals[i+1] * normal.getY() + normals[i+2] * normal.getZ() > 0.99) { 
      return colors[i/3]; 
     } 
    } 
    return "no color found"; 
} 
+0

안녕 jose, 이제 이미지가 잘 작동합니다. 하지만 주사위를 무작위로 굴릴 때 나는 위에있는 색상을 인쇄하고 싶습니다. 나는 얼굴을 돌려 보려고했지만 총 얼굴 수를 반환합니다. –

+0

텍스처 좌표를 적절하게 스케일링하면 (0-1 사이) 올바르게 게시해야합니다. L (400) 또는 H (300)가 아닌 ** 100 ** **로 텍스쳐 좌표를 나눌 경우 _each_면 위에 그물을 얻습니다. –

+0

faceSmoothingGroups를 사용하여 상단면을 추적하는 방법은 무엇입니까? –