繁体   English   中英

将.jpg和.mtl文件应用于SceneKit中的.obj文件

[英]Apply .jpg and .mtl file to .obj file in SceneKit

设计师提供了三个文件:

  • 图片。 JPG
  • 一些东西。 MTL
  • 随你。 OBJ

我可以成功将.obj文件加载到我的场景中,如下所示:

  SCNView * sceneView = [SCNView new];
  sceneView.frame = view.bounds;
  [view addSubview:sceneView];

  SCNScene * scene = [SCNScene sceneNamed:@"models.scnassets/whatever.obj"];
  [sceneView setScene:scene];

我正在努力将.jpg和.mtl文件应用于.obj文件。 我尝试使用以下代码应用图像,但没有爱:

SCNMaterial * material = [SCNMaterial material];
material.diffuse.contents = [UIImage imageNamed:@"image.jpg"];

SCNNode * materialNode = [SCNNode node];
materialNode.geometry.firstMaterial = material;
[scene.rootNode addChildNode:materialNode];

您好@Johnny有多种方法可以在.obj文件上应用纹理文件。

  1. 如果你想加载一些具有很棒自定义功能的.obj文件,你可以在Xcode中打开.obj文件并应用纹理(漫反射贴图,法线贴图等,环境和灯光摄像头),然后.obj文件将转换.scn文件以在SCNView中使用。

  2. 如果要动态地在.obj或.scn文件的特定节点上应用纹理/更改纹理,可以使用模型I / O框架,该框架使用集成了MetalKit,GLKit和SceneKit的通用基础结构提供导入,导出和操作3D模型。 SceneKit为ModelIO资产提供了类.. +(instancetype)sceneWithMDLAsset:(MDLAsset *)mdlAsset;(如@Zeeshan回答)。

  3. 您的设计师可以在.dae formate中导出3D模型文件。 在.dae文件中,单个文件包含纹理,相机,光引用。 在.obj中使用时,不需要.mtl或其他文件。 .dae也在SCNKit中得到支持

  4. 如果使用渲染的金属,则可以直接从NSURL加载纹理。

虽然没有使用.mtl文件,但这让我有了一些方法:

//START 3D
    NSURL * url = [[NSBundle mainBundle] URLForResource:@"playerModel" withExtension:@"obj"];
    MDLAsset * asset = [[MDLAsset alloc] initWithURL:url];
    MDLMesh * object = (MDLMesh *)[asset objectAtIndex:0];

    MDLScatteringFunction * scatFunction = [MDLScatteringFunction new];
    MDLMaterial * material = [[MDLMaterial alloc] initWithName:@"playerMaterial" scatteringFunction:scatFunction];

    NSURL * materialURL = [[NSBundle mainBundle] URLForResource:skinFilename withExtension:@"jpg"];
    MDLMaterialProperty * baseColour = [MDLMaterialProperty new];
    [baseColour setType:MDLMaterialPropertyTypeTexture];
    [baseColour setSemantic:MDLMaterialSemanticBaseColor];
    [baseColour setURLValue:materialURL];
    [material setProperty:baseColour];

    for (MDLSubmesh * sub in object.submeshes){
        sub.material = material;
    }

    SCNScene * scene = [SCNScene new];
    SCNNode * node = [SCNNode nodeWithMDLObject:object];
    SCNVector3 v = SCNVector3Make(100, 200, 0);
    [node setPosition:v];
    [scene.rootNode addChildNode:node];

    SCNView * view = [SCNView new];
    view.frame = transferInView.bounds;
    view.autoenablesDefaultLighting = true;
    view.allowsCameraControl = false;
    view.scene = scene;
    view.backgroundColor = [UIColor clearColor];
    [transferInView addSubview:view];

    SCNAction * rotate = [SCNAction rotateByX:0 y:1 z:0 duration:1.0f];
    [node runAction:[SCNAction repeatActionForever:rotate]];

    SCNVector3 min = SCNVector3Zero;
    SCNVector3 max = SCNVector3Zero;
    [node getBoundingBoxMin:&min max:&max];
    node.pivot = SCNMatrix4MakeTranslation((max.x - min.x) / 2 + min.x, (max.y - min.y) / 2 + min.y, 0);

    //END 3D

我遇到了同样的问题,我找到的唯一解决方案是在这个链接点击这里 我可以加载.mtl信息是通过场景创建对象

let scene = SCNScene(named: "rose.obj")

确保包含纹理的.mtl和jpg。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM