1

完全にカスタム化された SCNNode をプログラムで作成しました。このノードは、カスタム ジオメトリ、子ノードの階層としてのスケルトン リグ、およびスケルトンとジオメトリを結合するための SCNSkinner オブジェクトを備えています。問題は次のとおりです。カスタム ノードのクローンを作成するとすぐに、ジオメトリとスケルトンの間のバインドが失われます。

この問題の根本にたどり着くために、かなり複雑なキャラクター ノードを、考えられる最も単純なジオメトリとリグの組み合わせ (12 頂点のジオメトリ ブロックと 3 ジョイントの骨格リグ) に置き換えました。

この時点で画像を共有したかったのですが、まだ十分な評判がないのでできません...

-(SCNNode *)createCustomRigBlock {

    // baseGeometry
    SCNVector3 positions[] = {
        SCNVector3Make(0, 0, 0),
        SCNVector3Make(0, 0, 1),
        SCNVector3Make(1, 0, 1),
        SCNVector3Make(1, 0, 0),
        SCNVector3Make(0, 1, 0),
        SCNVector3Make(0, 1, 1),
        SCNVector3Make(1, 1, 1),
        SCNVector3Make(1, 1, 0),
        SCNVector3Make(0, 2, 0),
        SCNVector3Make(0, 2, 1),
        SCNVector3Make(1, 2, 1),
        SCNVector3Make(1, 2, 0)
    };
    SCNGeometrySource * baseGeometrySource = [SCNGeometrySource geometrySourceWithVertices:positions count:12];

    typedef struct {
        uint16_t a, b, c;
    } Triangles;

    Triangles tVectors[20] = {
        0,1,2,
        0,2,3,
        0,1,5,
        0,4,5,
        4,5,9,
        4,8,9,
        1,2,6,
        1,5,6,
        5,6,10,
        5,9,10,
        2,3,7,
        2,6,7,
        6,7,11,
        6,10,11,
        3,0,4,
        3,4,7,
        7,4,8,
        7,8,11,
        8,9,10,
        8,10,11
    };

    NSData *triangleData = [NSData dataWithBytes:tVectors length:sizeof(tVectors)];
    SCNGeometryElement * baseGeometryElement = [SCNGeometryElement geometryElementWithData:triangleData primitiveType:SCNGeometryPrimitiveTypeTriangles primitiveCount:20 bytesPerIndex:sizeof(uint16_t)];
    SCNGeometry * baseGeometry = [SCNGeometry geometryWithSources:[NSArray arrayWithObject:baseGeometrySource] elements:[NSArray arrayWithObject:baseGeometryElement]];

    baseGeometry.firstMaterial.emission.contents = [UIColor greenColor];
    baseGeometry.firstMaterial.doubleSided = YES;
    baseGeometry.firstMaterial.transparency = 0.5;
    SCNNode *customBlock = [SCNNode nodeWithGeometry:baseGeometry];

    int stageSize = 30;
    customBlock.position = SCNVector3Make(stageSize/2, 0, stageSize/2-6);

    int vectorCount = (int)[(SCNGeometrySource *)[customBlock.geometry geometrySourcesForSemantic:SCNGeometrySourceSemanticVertex].firstObject vectorCount];

    //bones ... the bones of the rig
    NSMutableArray * bonesArray = [NSMutableArray new];
    for (int i = 0; i < 3; i++) {
        SCNNode * boneNode = [SCNNode new];
        boneNode.name = [NSString stringWithFormat:@"bone_%i",i];
        if (bonesArray.count > 0) {
            [bonesArray.lastObject addChildNode:boneNode];
        }
        boneNode.position = SCNVector3Make((i>0 ? 0 : 0.5), (i>0 ? 0.75 : 0.25), (i>0 ? 0 : 0.5));

        //add a sphere to each bone, to visually check its position etc.
        SCNSphere *boneSphereGeom = [SCNSphere sphereWithRadius:0.1];
        boneSphereGeom.firstMaterial.emission.contents = [UIColor redColor];
        SCNNode * boneSphere = [SCNNode nodeWithGeometry:boneSphereGeom];
        [boneNode addChildNode:boneSphere];

        [bonesArray addObject:boneNode];
    }

    [customBlock addChildNode:bonesArray.firstObject];

    //boneInverseBindTransforms  ... this defines the geometries transformation in the default pose!
    NSMutableArray * bibtArray = [NSMutableArray new];
    for (int i = 0; i < 3; i++) {
        SCNMatrix4 initialPositionMatrix = SCNMatrix4MakeTranslation(0.5, (i*0.75)+0.25, 0.5);
        SCNMatrix4 inverseFinalMatrix = SCNMatrix4Invert(initialPositionMatrix);
        NSValue * bibtValue = [NSValue valueWithSCNMatrix4:inverseFinalMatrix];
        [bibtArray addObject:bibtValue];
    }

    //boneWeights ... the weights, at which each vertex is influenced by certain bones (which bones is defined by "boneIndices")
    typedef struct {
        float a, b, c;
    } WeightVectors;

    WeightVectors vectors[vectorCount];
    for (int i = 0; i < vectorCount; i++) {
        // set the same boneWeights for every vertex
        vectors[i].a = 1;
        vectors[i].b = 0;
        vectors[i].c = 0;
    }

    NSData *weightData = [NSData dataWithBytes:vectors length:sizeof(vectors)];
    SCNGeometrySource * boneWeightsGeometrySource = [SCNGeometrySource geometrySourceWithData:weightData
                                                                                     semantic:SCNGeometrySourceSemanticBoneWeights
                                                                                  vectorCount:vectorCount
                                                                              floatComponents:YES
                                                                          componentsPerVector:3
                                                                            bytesPerComponent:sizeof(float)
                                                                                   dataOffset:offsetof(WeightVectors, a)
                                                                                   dataStride:sizeof(WeightVectors)];

    //boneIndices
    typedef struct {
        short k, l, m;    // boneWeight
    } IndexVectors;

    IndexVectors iVectors[vectorCount];
    for (int i = 0; i < vectorCount; i++) {
        if (i > 7) {
            iVectors[i].k = 1;
            iVectors[i].l = 0;
            iVectors[i].m = 0;
        } else {
            iVectors[i].k = 0;
            iVectors[i].l = 0;
            iVectors[i].m = 0;
        }
    }

    NSData *indexData = [NSData dataWithBytes:iVectors length:sizeof(iVectors)];
    SCNGeometrySource * boneIndicesGeometrySource = [SCNGeometrySource geometrySourceWithData:indexData
                                                                                     semantic:SCNGeometrySourceSemanticBoneIndices
                                                                                  vectorCount:vectorCount
                                                                              floatComponents:NO
                                                                          componentsPerVector:3
                                                                            bytesPerComponent:sizeof(short)
                                                                                   dataOffset:offsetof(IndexVectors, k)
                                                                                   dataStride:sizeof(IndexVectors)];

    SCNSkinner * customBlockSkinner = [SCNSkinner skinnerWithBaseGeometry:baseGeometry
                                                                    bones:bonesArray
                                                boneInverseBindTransforms:bibtArray
                                                              boneWeights:boneWeightsGeometrySource
                                                              boneIndices:boneIndicesGeometrySource];

    customBlock.skinner = customBlockSkinner;
    [[bonesArray objectAtIndex:1] runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:0 y:0 z:2 duration:2]]];

    return customBlock;
}

このカスタム SCNNode は、シーンに直接追加する限り、完全に機能します。さて、現在のアプリケーションでは、このノードをシーンに数回追加する必要があります。そのため、ノードのクローンを作成し、代わりに結果のクローンをシーンに追加する必要があります。しかし、カスタム ノードのクローンを作成した結果、何かが大きく間違っています。上で述べたように、ジオメトリとスケルトンの間の結合が失われているように見えます。クローン ノードのスケルトンは引き続きシーンに配置できますが、ジオメトリはスケルトンの位置または変換に従わなくなります。

私がすでに試したこと:

1.) 新しいノードを作成し、元のジオメトリのコピーをそれに追加しました。次に、スケルトン ルート ノードのクローンを作成し、これを新しいノードにも追加しました。最後に、オリジナルのスキナーを新しいノードのスキナーとして設定します。残念ながら役に立ちません。結果は、直接複製されたノードとまったく同じように動作します。

SCNNode * newNode = [SCNNode node];
newNode.geometry = [node.geometry copy];
for (SCNNode * childNode in node.childNodes) {
    [newNode addChildNode:[childNode clone]];
}
newNode.skinner = node.skinner;

2.) ジオメトリとスケルトンの間のバインディングを再作成しようとして、SCNSkinner のディープ コピーを作成し、それを新しいノードのスキナーにしました。しかし、これは C3DSourceAccessorGetMutableValuePtrAtIndex でクラッシュを引き起こしただけで、修正できませんでした。

NSArray * newBones = [newNode childNodesPassingTest:^(SCNNode *child, BOOL *stop)
                      {
                          if (![child.name isEqualToString:@"manipulator"] ) {
                              return YES;
                          }
                          return NO;
                      }];
NSArray * newBoneInverseBindTransforms = [node.skinner.boneInverseBindTransforms copy];
SCNGeometrySource * newBoneWeights = [SCNGeometrySource geometrySourceWithData:[node.skinner.boneWeights.data copy]
                                                                      semantic:SCNGeometrySourceSemanticBoneWeights
                                                                   vectorCount:node.skinner.boneWeights.vectorCount
                                                               floatComponents:node.skinner.boneWeights.floatComponents
                                                           componentsPerVector:node.skinner.boneWeights.componentsPerVector
                                                             bytesPerComponent:node.skinner.boneWeights.bytesPerComponent
                                                                    dataOffset:node.skinner.boneWeights.dataOffset
                                                                    dataStride:node.skinner.boneWeights.dataStride];

SCNGeometrySource * newBoneIndices = [SCNGeometrySource geometrySourceWithData:[node.skinner.boneIndices.data copy]
                                                                      semantic:SCNGeometrySourceSemanticBoneIndices
                                                                   vectorCount:node.skinner.boneIndices.vectorCount
                                                               floatComponents:node.skinner.boneIndices.floatComponents
                                                           componentsPerVector:node.skinner.boneIndices.componentsPerVector
                                                             bytesPerComponent:node.skinner.boneIndices.bytesPerComponent
                                                                    dataOffset:node.skinner.boneIndices.dataOffset
                                                                    dataStride:node.skinner.boneIndices.dataStride];

SCNSkinner * newSkinner = [SCNSkinner skinnerWithBaseGeometry:newNode.geometry
                                                bones:newBones
                            boneInverseBindTransforms:newBoneInverseBindTransforms
                                          boneWeights:newBoneWeights
                                          boneIndices:newBoneIndices];
newNode.skinner = newSkinner;
4

1 に答える 1