我想在我的一个视图中集成cocos3d。所以,我有一个普通的视图控制器(MapEditorViewController
)和一个视图,在我的视图控制器中,(我创建了一个IBOutlet CCGLView *openGLView
)我希望cocos3d在其中。在我的视图控制器中,我有方法setupCocos3D:
MapEditorViewController
- (void)setupCocos3D {
[[CCDirector sharedDirector] setOpenGLView:openGLView];
// Create the customized CC3Layer that supports 3D rendering.
CC3Layer* cc3Layer = [HelloWorldLayer node];
// Create the customized 3D scene and attach it to the layer.
// Could also just create this inside the customer layer.
cc3Layer.cc3Scene = [testScene scene];
// Assign to a generic variable so we can uncomment options below to play with the capabilities
CC3ControllableLayer* mainLayer = cc3Layer;
// The 3D layer can run either directly in the scene, or it can run as a smaller "sub-window"
// within any standard CCLayer. So you can have a mostly 2D window, with a smaller 3D window
// embedded in it. To experiment with this smaller embedded 3D window, uncomment the following lines:
// CGSize winSize = CCDirector.sharedDirector.winSize;
// cc3Layer.position = ccp(30.0, 30.0);
// cc3Layer.contentSize = CGSizeMake(winSize.width - 100.0, winSize.width - 40.0);
// cc3Layer.alignContentSizeWithDeviceOrientation = YES;
// mainLayer = [CC3ControllableLayer node];
// [mainLayer addChild: cc3Layer];
// A smaller 3D layer can even be moved around on the screen dyanmically. To see this in action,
// uncomment the lines above as described, and also uncomment the following two lines.
// cc3Layer.position = ccp(0.0, 0.0);
// [cc3Layer runAction: [CCMoveTo actionWithDuration: 15.0 position: ccp(500.0, 250.0)]];
// Attach the layer to the controller and run a scene with it.
[CCDirector sharedDirector].animationInterval = (1.0f / kAnimationFrameRate);
[CCDirector sharedDirector].displayStats = YES;
[[CCDirector sharedDirector] enableRetinaDisplay: YES];
[[CCDirector sharedDirector] runWithScene:mainLayer];
}
setupCocos3D
在类viewDidLoad
的{{1}}中调用。
我有两个文件MapEditorViewController
和testScene
,它们是从hello world默认的cocos3d项目中复制的。
testScene.m
testLayer (called HelloWorldLayer here)
HelloWorldLayer.m
/**
* Constructs the 3D scene.
*
* Adds 3D objects to the scene, loading a 3D 'hello, world' message
* from a POD file, and creating the camera and light programatically.
*
* When adapting this template to your application, remove all of the content
* of this method, and add your own to construct your 3D model scene.
*
* NOTES:
*
* 1) To help you find your scene content once it is loaded, the onOpen method below contains
* code to automatically move the camera so that it frames the scene. You can remove that
* code once you know where you want to place your camera.
*
* 2) The POD file used for the 'hello, world' message model is fairly large, because converting a
* font to a mesh results in a LOT of triangles. When adapting this template project for your own
* application, REMOVE the POD file 'hello-world.pod' from the Resources folder of your project.
*/
-(void) initializeScene {
// Create the camera, place it back a bit, and add it to the scene
CC3Camera* cam = [CC3Camera nodeWithName: @"Camera"];
cam.location = cc3v( 0.0, 0.0, 6.0 );
[self addChild: cam];
// Create a light, place it back and to the left at a specific
// position (not just directional lighting), and add it to the scene
CC3Light* lamp = [CC3Light nodeWithName: @"Lamp"];
lamp.location = cc3v( -2.0, 0.0, 0.0 );
lamp.isDirectionalOnly = NO;
[cam addChild: lamp];
// This is the simplest way to load a POD resource file and add the
// nodes to the CC3Scene, if no customized resource subclass is needed.
[self addContentFromPODFile: @"hello-world.pod"];
// Create OpenGL ES buffers for the vertex arrays to keep things fast and efficient,
// and to save memory, release the vertex content in main memory because it is now redundant.
[self createGLBuffers];
[self releaseRedundantContent];
// That's it! The scene is now constructed and is good to go.
// To help you find your scene content once it is loaded, the onOpen method below contains
// code to automatically move the camera so that it frames the scene. You can remove that
// code once you know where you want to place your camera.
// If you encounter problems displaying your models, you can uncomment one or more of the
// following lines to help you troubleshoot. You can also use these features on a single node,
// or a structure of nodes. See the CC3Node notes for more explanation of these properties.
// Also, the onOpen method below contains additional troubleshooting code you can comment
// out to move the camera so that it will display the entire scene automatically.
// Displays short descriptive text for each node (including class, node name & tag).
// The text is displayed centered on the pivot point (origin) of the node.
// self.shouldDrawAllDescriptors = YES;
// Displays bounding boxes around those nodes with local content (eg- meshes).
// self.shouldDrawAllLocalContentWireframeBoxes = YES;
// Displays bounding boxes around all nodes. The bounding box for each node
// will encompass its child nodes.
// self.shouldDrawAllWireframeBoxes = YES;
// If you encounter issues creating and adding nodes, or loading models from
// files, the following line is used to log the full structure of the scene.
LogInfo(@"The structure of this scene is: %@", [self structureDescription]);
// ------------------------------------------
// And to add some dynamism, we'll animate the 'hello, world' message
// using a couple of actions...
// Fetch the 'hello, world' object that was loaded from the POD file and start it rotating
CC3MeshNode* helloTxt = (CC3MeshNode*)[self getNodeNamed: @"Hello"];
CCActionInterval* partialRot = [CC3RotateBy actionWithDuration: 1.0
rotateBy: cc3v(0.0, 30.0, 0.0)];
[helloTxt runAction: [CCRepeatForever actionWithAction: partialRot]];
// To make things a bit more appealing, set up a repeating up/down cycle to
// change the color of the text from the original red to blue, and back again.
GLfloat tintTime = 8.0f;
ccColor3B startColor = helloTxt.color;
ccColor3B endColor = { 50, 0, 200 };
CCActionInterval* tintDown = [CCTintTo actionWithDuration: tintTime
red: endColor.r
green: endColor.g
blue: endColor.b];
CCActionInterval* tintUp = [CCTintTo actionWithDuration: tintTime
red: startColor.r
green: startColor.g
blue: startColor.b];
CCActionInterval* tintCycle = [CCSequence actionOne: tintDown two: tintUp];
[helloTxt runAction: [CCRepeatForever actionWithAction: tintCycle]];
}
#pragma mark Updating custom activity
/**
* This template method is invoked periodically whenever the 3D nodes are to be updated.
*
* This method provides your app with an opportunity to perform update activities before
* any changes are applied to the transformMatrix of the 3D nodes in the scene.
*
* For more info, read the notes of this method on CC3Node.
*/
-(void) updateBeforeTransform: (CC3NodeUpdatingVisitor*) visitor {}
/**
* This template method is invoked periodically whenever the 3D nodes are to be updated.
*
* This method provides your app with an opportunity to perform update activities after
* the transformMatrix of the 3D nodes in the scen have been recalculated.
*
* For more info, read the notes of this method on CC3Node.
*/
-(void) updateAfterTransform: (CC3NodeUpdatingVisitor*) visitor {
// If you have uncommented the moveWithDuration: invocation in the onOpen: method, you
// can uncomment the following to track how the camera moves, where it ends up, and what
// the camera's clipping distances are, in order to determine how to position and configure
// the camera to view the entire scene.
// LogDebug(@"Camera: %@", activeCamera.fullDescription);
}
#pragma mark Scene opening and closing
/**
* Callback template method that is invoked automatically when the CC3Layer that
* holds this scene is first displayed.
*
* This method is a good place to invoke one of CC3Camera moveToShowAllOf:... family
* of methods, used to cause the camera to automatically focus on and frame a particular
* node, or the entire scene.
*
* For more info, read the notes of this method on CC3Scene.
*/
-(void) onOpen {
// Move the camera to frame the scene. You can uncomment the LogDebug line in the
// updateAfterTransform: method to track how the camera moves, where it ends up, and
// what the camera's clipping distances are, in order to determine how to position
// and configure the camera to view your entire scene. Then you can remove this code.
//[self.activeCamera moveWithDuration: 3.0 toShowAllOf: self withPadding: 0.5f];
// Uncomment this line to draw the bounding box of the scene.
// self.shouldDrawWireframeBox = YES;
}
/**
* Callback template method that is invoked automatically when the CC3Layer that
* holds this scene has been removed from display.
*
* For more info, read the notes of this method on CC3Scene.
*/
-(void) onClose {}
#pragma mark Handling touch events
/**
* This method is invoked from the CC3Layer whenever a touch event occurs, if that layer
* has indicated that it is interested in receiving touch events, and is handling them.
*
* Override this method to handle touch events, or remove this method to make use of
* the superclass behaviour of selecting 3D nodes on each touch-down event.
*
* This method is not invoked when gestures are used for user interaction. Your custom
* CC3Layer processes gestures and invokes higher-level application-defined behaviour
* on this customized CC3Scene subclass.
*
* For more info, read the notes of this method on CC3Scene.
*/
-(void) touchEvent: (uint) touchType at: (CGPoint) touchPoint {}
/**
* This callback template method is invoked automatically when a node has been picked
* by the invocation of the pickNodeFromTapAt: or pickNodeFromTouchEvent:at: methods,
* as a result of a touch event or tap gesture.
*
* Override this method to perform activities on 3D nodes that have been picked by the user.
*
* For more info, read the notes of this method on CC3Scene.
*/
-(void) nodeSelected: (CC3Node*) aNode byTouchEvent: (uint) touchType at: (CGPoint) touchPoint {
}
一切都在编译,我只看到黑屏。我已经包含了hello-world.pod文件,并且在控制台中一切似乎都很好(日志记录显示文件已正确加载)。
为什么我没有看到你好的世界,即使我看到了统计数据(fps等)?
答案 0 :(得分:0)
对我来说,在 MapEditorViewController :
中添加这一行mainLayer.contentSize = CGSizeMake(2048, 1320);
创建mainLayer
后解决了我的问题......