美文网首页
创建立方体的小例子

创建立方体的小例子

作者: 无边小猪 | 来源:发表于2016-09-02 17:38 被阅读0次
        #import "ViewController.h"
        #import
        #import
        #define LIGHT_DIRECTION 0, 1, -0.5
        #define AMBIENT_LIGHT 0.5
        @interface ViewController ()
        @property (nonatomic, weak) IBOutlet UIView *containerView;
        @property (nonatomic, strong) IBOutletCollection(UIView) NSArray *faces;
        @end
        @implementation ViewController
        - (void)applyLightingToFace:(CALayer *)face
        {
            //add lighting layer
            CALayer *layer = [CALayer layer];
            layer.frame = face.bounds;
            [face addSublayer:layer];
            //convert the face transform to matrix
            //(GLKMatrix4 has the same structure as CATransform3D)
            CATransform3D transform = face.transform;
            GLKMatrix4 matrix4 = *(GLKMatrix4 *)&transform;
            GLKMatrix3 matrix3 = GLKMatrix4GetMatrix3(matrix4);
            //get face normal
            GLKVector3 normal = GLKVector3Make(0, 0, 1);
            normal = GLKMatrix3MultiplyVector3(matrix3, normal);
            normal = GLKVector3Normalize(normal);
            //get dot product with light direction
            GLKVector3 light = GLKVector3Normalize(GLKVector3Make(LIGHT_DIRECTION));
            float dotProduct = GLKVector3DotProduct(light, normal);
            //set lighting layer opacity
            CGFloat shadow = 1 + dotProduct - AMBIENT_LIGHT;
            UIColor *color = [UIColor colorWithWhite:0 alpha:shadow];
            layer.backgroundColor = color.CGColor;
        }
        - (void)addFace:(NSInteger)index withTransform:(CATransform3D)transform
        {
            //get the face view and add it to the container
            UIView *face = self.faces[index];
            [self.containerView addSubview:face];
            //center the face view within the container
            CGSize containerSize = self.containerView.bounds.size;
            face.center = CGPointMake(containerSize.width / 2.0, containerSize.height / 2.0);
            // apply the transform
            face.layer.transform = transform;
            //apply lighting
            [self applyLightingToFace:face.layer];
        }
        - (void)viewDidLoad
        {
            [super viewDidLoad];
            //set up the container sublayer transform
            CATransform3D perspective = CATransform3DIdentity;
            perspective.m34 = -1.0 / 500.0;
            perspective = CATransform3DRotate(perspective, -M_PI_4, 1, 0, 0);
            perspective = CATransform3DRotate(perspective, -M_PI_4, 0, 1, 0);
            self.containerView.layer.sublayerTransform = perspective;
            //add cube face 1
            CATransform3D transform = CATransform3DMakeTranslation(0, 0, 100);
            [self addFace:0 withTransform:transform];
            //add cube face 2
            transform = CATransform3DMakeTranslation(100, 0, 0);
            transform = CATransform3DRotate(transform, M_PI_2, 0, 1, 0);
            [self addFace:1 withTransform:transform];
            //add cube face 3
            transform = CATransform3DMakeTranslation(0, -100, 0);
            transform = CATransform3DRotate(transform, M_PI_2, 1, 0, 0);
            [self addFace:2 withTransform:transform];
            //add cube face 4
            transform = CATransform3DMakeTranslation(0, 100, 0);
            transform = CATransform3DRotate(transform, -M_PI_2, 1, 0, 0);
            [self addFace:3 withTransform:transform];
            //add cube face 5
            transform = CATransform3DMakeTranslation(-100, 0, 0);
            transform = CATransform3DRotate(transform, -M_PI_2, 0, 1, 0);
            [self addFace:4 withTransform:transform];
            //add cube face 6
            transform = CATransform3DMakeTranslation(0, 0, -100);
            transform = CATransform3DRotate(transform, M_PI, 0, 1, 0);
            [self addFace:5 withTransform:transform];
        }
        @end
    

    相关文章

      网友评论

          本文标题:创建立方体的小例子

          本文链接:https://www.haomeiwen.com/subject/ohqkettx.html