added some missing tests

This commit is contained in:
joshuastray 2015-05-18 14:43:31 +08:00
parent 59ff1cf7f3
commit 06846b5b1a
5 changed files with 961 additions and 2 deletions

View File

@ -38,6 +38,7 @@
"src/TransitionsTest/TransitionsTest.js",
"src/DrawPrimitivesTest/DrawPrimitivesTest.js",
"src/ParticleTest/ParticleTest.js",
"src/Particle3DTest/Particle3DTest.js",
"src/ProgressActionsTest/ProgressActionsTest.js",
"src/LayerTest/LayerTest.js",
"src/SceneTest/SceneTest.js",
@ -186,6 +187,7 @@
"src/ReflectionTest/ReflectionTest.js",
"src/SpritePolygonTest/SpritePolygonTest.js",
"src/MaterialSystemTest/MaterialSystemTest.js"
"src/MaterialSystemTest/MaterialSystemTest.js",
"src/TerrainTest/TerrainTest.js"
]
}

View File

@ -0,0 +1,436 @@
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2011-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
if(cc.sys.isNative)(function(){
var Particle3DTestIdx = -1;
const PARTICLE_SYSTEM_TAG = 0x0001;
jsb.fileUtils.addSearchPath("res/Sprite3DTest");
jsb.fileUtils.addSearchPath("res/Particle3D/materials");
jsb.fileUtils.addSearchPath("res/Particle3D/scripts");
var Particle3DTestDemo = cc.Layer.extend({
_title:"Particle3D Test",
_subtitle:"",
_camera:null,
_particleLabel:null,
_angle:0,
ctor:function () {
this._super();
},
//
// Menu
//
onEnter:function () {
this._super();
var label = new cc.LabelTTF(this._title, "Arial", 28);
this.addChild(label, 100, BASE_TEST_TITLE_TAG);
label.x = winSize.width / 2;
label.y = winSize.height - 50;
var label2 = new cc.LabelTTF(this._subtitle, "Thonburi", 16);
this.addChild(label2, 101, BASE_TEST_SUBTITLE_TAG);
label2.x = winSize.width / 2;
label2.y = winSize.height - 80;
var item1 = new cc.MenuItemImage(s_pathB1, s_pathB2, this.onBackCallback, this);
var item2 = new cc.MenuItemImage(s_pathR1, s_pathR2, this.onRestartCallback, this);
var item3 = new cc.MenuItemImage(s_pathF1, s_pathF2, this.onNextCallback, this);
item1.tag = BASE_TEST_MENUITEM_PREV_TAG;
item2.tag = BASE_TEST_MENUITEM_RESET_TAG;
item3.tag = BASE_TEST_MENUITEM_NEXT_TAG;
var menu = new cc.Menu(item1, item2, item3);
menu.x = 0;
menu.y = 0;
var width = item2.width, height = item2.height;
item1.x = winSize.width/2 - width*2;
item1.y = height/2 ;
item2.x = winSize.width/2;
item2.y = height/2 ;
item3.x = winSize.width/2 + width*2;
item3.y = height/2 ;
this.addChild(menu, 102, BASE_TEST_MENU_TAG);
var size = cc.winSize;
this._camera = cc.Camera.createPerspective(30.0, size.width / size.height, 1.0, 1000.0);
this._camera.setPosition3D(cc.math.vec3(0, 0, 100));
this._camera.lookAt(cc.math.vec3(0, 0, 0), cc.math.vec3(0, 1, 0));
this._camera.setCameraFlag(cc.CameraFlag.USER1);
this.addChild(this._camera);
var ttfconfig = {outlineSize:0, fontSize:15, glyphs:0, customGlyphs:"", fontFilePath:"fonts/tahoma.ttf"};
this._particleLabel = cc.Label.createWithTTF(ttfconfig, "Particle count: 0");
this._particleLabel.setPosition(0, size.height/6);
this._particleLabel.setAnchorPoint(cc.p(0, 0));
this.addChild(this._particleLabel);
cc.eventManager.addListener({
event:cc.EventListener.TOUCH_ALL_AT_ONCE,
onTouchesMoved:this.onTouchesMoved.bind(this)
}, this);
this.scheduleUpdate();
},
onTouchesMoved:function(touches, event){
var touch = touches[0];
var delta = touch.getDelta();
this._angle -= cc.degreesToRadians(delta.x);
this._camera.setPosition3D(cc.math.vec3(100*Math.sin(this._angle), 0, 100*Math.cos(this._angle)));
this._camera.lookAt(cc.math.vec3(0, 0, 0), cc.math.vec3(0, 1, 0));
},
update:function(dt){
var ps = this.getChildByTag(PARTICLE_SYSTEM_TAG);
if(ps){
var count = 0;
var children = ps.children;
for(var i = 0; i < children.length; ++i){
var child = children[i];
if(child && child.getAliveParticleCount)
count += child.getAliveParticleCount();
}
this._particleLabel.setString("Particle Count: "+count);
}
},
onRestartCallback:function (sender) {
var s = new Particle3DTestScene();
s.addChild(restartParticle3DTest());
director.runScene(s);
},
onNextCallback:function (sender) {
var s = new Particle3DTestScene();
s.addChild(nextParticle3DTest());
director.runScene(s);
},
onBackCallback:function (sender) {
var s = new Particle3DTestScene();
s.addChild(previousParticle3DTest());
director.runScene(s);
}
});
Particle3DTestScene = cc.Scene.extend({
ctor:function () {
this._super();
var label = new cc.LabelTTF("Main Menu", "Arial", 20);
var menuItem = new cc.MenuItemLabel(label, this.onMainMenuCallback, this);
var menu = new cc.Menu(menuItem);
menu.x = 0;
menu.y = 0;
menuItem.x = winSize.width - 50;
menuItem.y = 25;
this.addChild(menu);
},
onMainMenuCallback:function () {
var scene = new cc.Scene();
var layer = new TestController();
scene.addChild(layer);
director.runScene(scene);
},
runThisTest:function (num) {
Particle3DTestIdx = (num || num == 0) ? (num - 1) : -1;
var layer = nextParticle3DTest();
this.addChild(layer);
director.runScene(this);
}
});
var Particle3DExplosionSystemDemo = Particle3DTestDemo.extend({
_subtitle:"ExplosionSystem",
ctor:function(){
this._super();
var rootps = jsb.PUParticleSystem3D.create("explosionSystem.pu");
rootps.setCameraMask(cc.CameraFlag.USER1);
rootps.startParticleSystem();
this.addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
}
});
var Particle3DLineStreakDemo = Particle3DTestDemo.extend({
_subtitle:"LineStreak",
ctor:function(){
this._super();
var rootps = jsb.PUParticleSystem3D.create("lineStreak.pu", "pu_mediapack_01.material");
rootps.setCameraMask(2);
rootps.setScale(5);
rootps.startParticleSystem();
this.addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
}
});
var Particle3DBlackHoleDemo = Particle3DTestDemo.extend({
_subtitle:"BlackHole",
ctor:function(){
this._super();
var rootps = jsb.PUParticleSystem3D.create("blackHole.pu", "pu_mediapack_01.material");
rootps.setCameraMask(2);
rootps.runAction(cc.sequence(
cc.moveBy(2, cc.p(50, 0)),
cc.moveBy(2, cc.p(-50, 0))
).repeatForever());
rootps.startParticleSystem();
this.addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
}
});
var Particle3DHypnoDemo = Particle3DTestDemo.extend({
_subtitle:"Hypno",
ctor:function(){
this._super();
var rootps = jsb.PUParticleSystem3D.create("hypno.pu", "pu_mediapack_01.material");
rootps.setCameraMask(2);
rootps.startParticleSystem();
this.addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
}
});
var Particle3DAdvancedLodSystemDemo = Particle3DTestDemo.extend({
_subtitle:"AdvancedSystem",
ctor:function(){
this._super();
var rootps = jsb.PUParticleSystem3D.create("advancedLodSystem.pu");
rootps.setCameraMask(2);
rootps.runAction(cc.rotateBy(1, cc.math.vec3(0, 0, 100)).repeatForever());
var scale = cc.scaleBy(1, 2, 2, 2);
rootps.runAction(cc.sequence(scale, scale.reverse()).repeatForever());
rootps.startParticleSystem();
this.addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
}
});
var Particle3DTimeShiftDemo = Particle3DTestDemo.extend({
_subtitle:"TimeShift",
ctor:function(){
this._super();
var rootps = jsb.PUParticleSystem3D.create("timeShift.pu", "pu_mediapack_01.material");
rootps.setScale(2);
rootps.setCameraMask(2);
rootps.startParticleSystem();
this.addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
}
});
var Particle3DUVAnimDemo = Particle3DTestDemo.extend({
_subtitle:"UVAnim",
ctor:function(){
this._super();
var rootps = jsb.PUParticleSystem3D.create("UVAnimation.pu", "pu_mediapack_01.material");
rootps.setCameraMask(2);
rootps.startParticleSystem();
this.addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
}
});
var Particle3DFirePlaceDemo = Particle3DTestDemo.extend({
_subtitle:"Fire",
ctor:function(){
this._super();
var rootps = jsb.PUParticleSystem3D.create("mp_torch.pu", "pu_mediapack_01.material");
rootps.setScale(5);
rootps.setCameraMask(2);
rootps.startParticleSystem();
this.addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
}
});
var Particle3DElectricBeamSystemDemo = Particle3DTestDemo.extend({
_subtitle:"ElectricBeamSystem",
ctor:function(){
this._super();
var rootps = jsb.PUParticleSystem3D.create("electricBeamSystem.pu");
rootps.setCameraMask(2);
rootps.startParticleSystem();
this.addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
}
});
var Particle3DFlareShieldDemo = Particle3DTestDemo.extend({
_subtitle:"flareShield",
ctor:function(){
this._super();
var rootps = jsb.PUParticleSystem3D.create("flareShield.pu");
rootps.setCameraMask(2);
rootps.startParticleSystem();
this.addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
}
});
var Particle3DLightningBoltDemo = Particle3DTestDemo.extend({
_subtitle:"LightningBolt",
ctor:function(){
this._super();
var rootps = jsb.PUParticleSystem3D.create("lightningBolt.pu");
rootps.setCameraMask(2);
rootps.startParticleSystem();
this.addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
}
});
var Particle3DCanOfWormsDemo = Particle3DTestDemo.extend({
_subtitle:"CanOfWorms",
ctor:function(){
this._super();
var rootps = jsb.PUParticleSystem3D.create("canOfWorms.pu");
rootps.setCameraMask(2);
rootps.startParticleSystem();
this.addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
}
});
var Particle3DRibbonTrailDemo = Particle3DTestDemo.extend({
_subtitle:"RibbonTrailTest",
ctor:function(){
this._super();
var rootps = jsb.PUParticleSystem3D.create("ribbonTrailTest.pu");
rootps.setCameraMask(2);
rootps.startParticleSystem();
this.addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
}
});
var Particle3DWeaponTrailDemo = Particle3DTestDemo.extend({
_subtitle:"WeaponTrail",
ctor:function(){
this._super();
var rootps = jsb.PUParticleSystem3D.create("weaponTrail.pu");
rootps.setCameraMask(2);
rootps.startParticleSystem();
this.addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
}
});
var Particle3DWithSprite3DDemo = Particle3DTestDemo.extend({
_subtitle:"Particle with Sprite3D",
ctor:function(){
this._super();
var c3bfileName = "Sprite3DTest/orc.c3b";
var sprite = new jsb.Sprite3D(c3bfileName);
this.addChild(sprite);
sprite.setPosition3D(cc.math.vec3(-20, 0, 0));
sprite.setRotation3D(cc.math.vec3(0, 180, 0));
sprite.setCameraMask(2);
var animation = jsb.Animation3D.create(c3bfileName);
if(animation){
var animate = jsb.Animate3D.create(animation);
sprite.runAction(cc.repeatForever(animate));
}
var billboard = new jsb.BillBoard("Images/Icon.png");
billboard.setPosition3D(cc.math.vec3(20, 0, 0));
billboard.setScale(0.2);
billboard.setCameraMask(2);
this.addChild(billboard);
var rootps = jsb.PUParticleSystem3D.create("lineStreak.pu");
rootps.setCameraMask(2);
rootps.startParticleSystem();
this.addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
}
})
//
// Flow control
//
var arrayOfParticle3DTest = [
Particle3DExplosionSystemDemo,
Particle3DLineStreakDemo,
Particle3DBlackHoleDemo,
Particle3DHypnoDemo,
Particle3DAdvancedLodSystemDemo,
Particle3DTimeShiftDemo,
Particle3DUVAnimDemo,
Particle3DFirePlaceDemo,
Particle3DElectricBeamSystemDemo,
Particle3DFlareShieldDemo,
Particle3DLightningBoltDemo,
Particle3DCanOfWormsDemo,
Particle3DRibbonTrailDemo,
Particle3DWeaponTrailDemo,
Particle3DWithSprite3DDemo
];
var nextParticle3DTest = function () {
Particle3DTestIdx++;
Particle3DTestIdx = Particle3DTestIdx % arrayOfParticle3DTest.length;
return new arrayOfParticle3DTest[Particle3DTestIdx ]();
};
var previousParticle3DTest = function () {
Particle3DTestIdx--;
if (Particle3DTestIdx < 0)
Particle3DTestIdx += arrayOfParticle3DTest.length;
return new arrayOfParticle3DTest[Particle3DTestIdx ]();
};
var restartParticle3DTest = function () {
return new arrayOfParticle3DTest[Particle3DTestIdx ]();
};
})();

View File

@ -1531,6 +1531,85 @@ var Sprite3DBasicToonShaderTest = Sprite3DTestDemo.extend({
}
});
var Sprite3DCubeMapTest = Sprite3DTestDemo.extend({
_title:"CubeMap & Skybox Test",
_subtitle:"",
_camera:null,
_angle:0,
ctor:function(){
this._super();
var visibleSize = cc.director.getVisibleSize();
var camera = cc.Camera.createPerspective(60, visibleSize.width/visibleSize.height, 10, 1000);
camera.setCameraFlag(cc.CameraFlag.USER1);
camera.setPosition3D(cc.math.vec3(0, 0, 50));
cc.eventManager.addListener({
event:cc.EventListener.TOUCH_ALL_AT_ONCE,
onTouchesMoved:this.onTouchesMoved.bind(this)
}, this);
//create a teap
var teapot = new jsb.Sprite3D("Sprite3DTest/teapot.c3b");
//create and set our custom shader
var shader = new cc.GLProgram("Sprite3DTest/cube_map.vert","Sprite3DTest/cube_map.frag");
var state = cc.GLProgramState.create(shader);
//create the second texture for cylinder
var textureCube = jsb.TextureCube.create("Sprite3DTest/skybox/left.jpg","Sprite3DTest/skybox/right.jpg", "Sprite3DTest/skybox/top.jpg", "Sprite3DTest/skybox/bottom.jpg", "Sprite3DTest/skybox/front.jpg", "Sprite3DTest/skybox/back.jpg");
//set the texture parameters
textureCube.setTexParameters(gl.LINEAR, gl.LINEAR, gl.MIRRORED_REPEAT, gl.MIRRORED_REPEAT);
//pass the texture sampler to our custom shader
state.setUniformTexture("u_cubeTex", textureCube);
teapot.setGLProgramState(state);
teapot.setPosition3D(cc.math.vec3(0, -5, 0));
teapot.setRotation3D(cc.math.vec3(-90, 180, 0));
teapot.runAction(cc.rotateBy(1.5, cc.math.vec3(0, 30, 0)).repeatForever());
//pass mesh's attribute to shader
var offset = 0;
var attributeCount = teapot.getMesh().getMeshVertexAttribCount();
for(var i = 0; i < attributeCount; ++i){
var meshattribute = teapot.getMesh().getMeshVertexAttribute(i);
state.setVertexAttribPointer(cc.attributeNames[meshattribute.vertexAttrib],
meshattribute.size,
meshattribute.type,
gl.FALSE,
teapot.getMesh().getVertexSizeInBytes(),
offset);
offset += meshattribute.attribSizeBytes;
}
this.addChild(teapot);
//config skybox
var skybox = jsb.Skybox.create();
skybox.setTexture(textureCube);
this.addChild(skybox);
skybox.setScale(700);
this.addChild(camera);
this.setCameraMask(2);
this._camera = camera;
},
onTouchesMoved:function(touches, event){
if(touches.length > 0){
var touch = touches[0];
var delta = touch.getDelta();
this._angle -= cc.degreesToRadians(delta.x);
this._camera.setPosition3D(cc.math.vec3(50*Math.sin(this._angle), 0, 50*Math.cos(this._angle)));
this._camera.lookAt(cc.math.vec3(0, 0, 0), cc.math.vec3(0, 1, 0));
}
}
});
//
// Flow control
//
@ -1548,7 +1627,8 @@ var arrayOfSprite3DTest = [
Sprite3DEmptyTest,
Sprite3DForceDepthTest,
UseCaseSprite3D1,
UseCaseSprite3D2
UseCaseSprite3D2,
Sprite3DCubeMapTest
];
// 3DEffect use custom shader which is not supported on WP8/WinRT yet.

View File

@ -0,0 +1,426 @@
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2011-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
if(cc.sys.isNative)(function(){
var TerrainTestIdx = -1;
var TerrainTestDemo = cc.Layer.extend({
_title:"",
_subtitle:"",
ctor:function () {
this._super();
},
//
// Menu
//
onEnter:function () {
this._super();
var label = new cc.LabelTTF(this._title, "Arial", 28);
this.addChild(label, 100, BASE_TEST_TITLE_TAG);
label.x = winSize.width / 2;
label.y = winSize.height - 50;
var label2 = new cc.LabelTTF(this._subtitle, "Thonburi", 16);
this.addChild(label2, 101, BASE_TEST_SUBTITLE_TAG);
label2.x = winSize.width / 2;
label2.y = winSize.height - 80;
var item1 = new cc.MenuItemImage(s_pathB1, s_pathB2, this.onBackCallback, this);
var item2 = new cc.MenuItemImage(s_pathR1, s_pathR2, this.onRestartCallback, this);
var item3 = new cc.MenuItemImage(s_pathF1, s_pathF2, this.onNextCallback, this);
item1.tag = BASE_TEST_MENUITEM_PREV_TAG;
item2.tag = BASE_TEST_MENUITEM_RESET_TAG;
item3.tag = BASE_TEST_MENUITEM_NEXT_TAG;
var menu = new cc.Menu(item1, item2, item3);
menu.x = 0;
menu.y = 0;
var width = item2.width, height = item2.height;
item1.x = winSize.width/2 - width*2;
item1.y = height/2 ;
item2.x = winSize.width/2;
item2.y = height/2 ;
item3.x = winSize.width/2 + width*2;
item3.y = height/2 ;
this.addChild(menu, 102, BASE_TEST_MENU_TAG);
},
onRestartCallback:function (sender) {
var s = new TerrainTestScene();
s.addChild(restartTerrainTest());
director.runScene(s);
},
onNextCallback:function (sender) {
var s = new TerrainTestScene();
s.addChild(nextTerrainTest());
director.runScene(s);
},
onBackCallback:function (sender) {
var s = new TerrainTestScene();
s.addChild(previousTerrainTest());
director.runScene(s);
},
});
TerrainTestScene = cc.Scene.extend({
ctor:function () {
this._super();
var label = new cc.LabelTTF("Main Menu", "Arial", 20);
var menuItem = new cc.MenuItemLabel(label, this.onMainMenuCallback, this);
var menu = new cc.Menu(menuItem);
menu.x = 0;
menu.y = 0;
menuItem.x = winSize.width - 50;
menuItem.y = 25;
this.addChild(menu);
},
onMainMenuCallback:function () {
var scene = new cc.Scene();
var layer = new TestController();
scene.addChild(layer);
director.runScene(scene);
},
runThisTest:function (num) {
TerrainTestIdx = (num || num == 0) ? (num - 1) : -1;
var layer = nextTerrainTest();
this.addChild(layer);
director.runScene(this);
}
});
var TerrainSimple = TerrainTestDemo.extend({
_title:"Terrain with skirt",
_subtitle:"Drag to walkthru",
_camera:null,
ctor:function(){
this._super();
var visibleSize = cc.director.getVisibleSize();
//use custom camera
this._camera = cc.Camera.createPerspective(60,visibleSize.width/visibleSize.height,0.1,800);
this._camera.setCameraFlag(cc.CameraFlag.USER1);
this._camera.setPosition3D(cc.math.vec3(-1, 1.6, 4));
this.addChild(this._camera);
var r = jsb.Terrain.detailMap("TerrainTest/dirt.jpg", 35),
g = jsb.Terrain.detailMap("TerrainTest/Grass2.jpg", 35),
b = jsb.Terrain.detailMap("TerrainTest/road.jpg", 35),
a = jsb.Terrain.detailMap("TerrainTest/GreenSkin.jpg", 35);
var data = jsb.Terrain.terrainData(
"TerrainTest/heightmap16.jpg",
"TerrainTest/alphamap.png",
[r, g, b, a],
cc.size(32,32),
2,
0.1
);
var terrain = jsb.Terrain.create(data, jsb.Terrain.CrackFixedType.SKIRT);
terrain.setLODDistance(3.2,6.4,9.6);
terrain.setMaxDetailMapAmount(4);
this.addChild(terrain);
terrain.setCameraMask(2);
terrain.setDrawWire(false);
cc.eventManager.addListener({
event:cc.EventListener.TOUCH_ALL_AT_ONCE,
onTouchesMoved:this.onTouchesMoved.bind(this)
}, this);
},
onTouchesMoved:function(touches, event){
var delta = cc.director.getDeltaTime();
var touch = touches[0];
var location = touch.getLocation();
var previousLocation = touch.getPreviousLocation();
var newPos = cc.p(previousLocation.x - location.x, previousLocation.y - location.y);
var m = this._camera.getNodeToWorldTransform3D();
var cameraDir = cc.math.vec3(-m[8], -m[9], -m[10]);
cameraDir.normalize();
cameraDir.y = 0;
var cameraRightDir = cc.math.vec3(m[0], m[1], m[2]);
cameraRightDir.normalize();
cameraRightDir.y = 0;
var cameraPos = this._camera.getPosition3D();
cameraPos.x += cameraDir.x * newPos.y * 0.5 *delta + cameraRightDir.x * newPos.x * 0.5 *delta;
cameraPos.y += cameraDir.y * newPos.y * 0.5 *delta + cameraRightDir.y * newPos.x * 0.5 *delta;
cameraPos.z += cameraDir.z * newPos.y * 0.5 *delta + cameraRightDir.z * newPos.x * 0.5 *delta;
this._camera.setPosition3D(cameraPos);
}
});
var TerrainWalkThru = (function(){
const PlayerState = {
LEFT : 0,
RIGHT : 1,
IDLE : 2,
FORWARD : 3,
BACKWARD : 4
};
const PLAYER_HEIGHT = 0;
const camera_offset = cc.math.vec3(0, 45, 60);
var Player = jsb.Sprite3D.extend({
_targetPos:null,
_headingAxis:null,
_headingAngle:0,
_playerState:PlayerState.IDLE,
_camera:null,
_terrain:null,
ctor:function(file, camera, terrain){
this._super(file);
this._camera = camera;
this._terrain = terrain;
this.scheduleUpdate();
},
update:function(dt){
switch(this._playerState){
case PlayerState.IDLE:
break;
case PlayerState.FORWARD:
var curPos = this.getPosition3D();
var newFaceDir = cc.math.vec3Sub(this._targetPos, curPos);
newFaceDir.y = 0;
newFaceDir.normalize();
curPos.x += newFaceDir.x * 25 * dt;
curPos.y += newFaceDir.y * 25 * dt;
curPos.z += newFaceDir.z * 25 * dt;
this.setPosition3D(curPos);
break;
case PlayerState.BACKWARD:
break;
case PlayerState.LEFT:
this.setRotation3D(cc.math.vec3Add(this.getRotation3D(), cc.math.vec3(0, 25*dt, 0)));
break;
case PlayerState.RIGHT:
this.setRotation3D(cc.math.vec3Add(this.getRotation3D(), cc.math.vec3(0, -25*dt, 0)));
break;
default:
break;
}
var player_h = this._terrain.getHeight(this.getPositionX(), this.getVertexZ());
this.y = player_h + PLAYER_HEIGHT;
this._camera.setPosition3D(cc.math.vec3Add(this.getPosition3D(), camera_offset));
if(this._headingAxis){
var quat = cc.math.quaternion(cc.math.vec3(0, 1, 0), -Math.PI);
var headingQ = cc.math.quaternion(this._headingAxis, this._headingAngle);
this.setRotationQuat(cc.math.quatMultiply(quat, headingQ));
}
this.updateState();
},
turnLeft:function(){
this._playerState = PlayerState.LEFT;
},
turnRight:function(){
this._playerState = PlayerState.RIGHT;
},
forward:function(){
this._playerState = PlayerState.FORWARD;
},
backward:function(){
this._playerState = PlayerState.BACKWARD;
},
idle:function(){
this._playerState = PlayerState.IDLE;
},
updateState:function(){
if(this._playerState === PlayerState.FORWARD){
var playerPos = cc.p(this.getPositionX(), this.getVertexZ());
var targetPos = cc.p(this._targetPos.x, this._targetPos.z);
var dist = cc.pDistance(playerPos, targetPos);
if(dist < 1){
this._playerState = PlayerState.IDLE;
}
}
}
});
return TerrainTestDemo.extend({
_title:"Player walk around in terrain",
_subtitle:"touch to move",
_camera:null,
_player:null,
_terrain:null,
ctor:function(){
this._super();
cc.eventManager.addListener({
event:cc.EventListener.TOUCH_ALL_AT_ONCE,
onTouchesEnded:this.onTouchesEnded.bind(this)
}, this);
var visibleSize = cc.director.getVisibleSize();
this._camera = cc.Camera.createPerspective(60, visibleSize.width/visibleSize.height, 0.1, 200);
this._camera.setCameraFlag(cc.CameraFlag.USER1);
this.addChild(this._camera);
var r = jsb.Terrain.detailMap("TerrainTest/dirt.jpg"),
g = jsb.Terrain.detailMap("TerrainTest/Grass2.jpg", 10),
b = jsb.Terrain.detailMap("TerrainTest/road.jpg"),
a = jsb.Terrain.detailMap("TerrainTest/GreenSkin.jpg", 20);
var data = jsb.Terrain.terrainData(
"TerrainTest/heightmap16.jpg",
"TerrainTest/alphamap.png",
[r, g, b, a],
cc.size(32,32),
40.0,
2
);
this._terrain = jsb.Terrain.create(data, jsb.Terrain.CrackFixedType.SKIRT);
this._terrain.setLODDistance(64, 128, 192);
this._terrain.setMaxDetailMapAmount(4);
this._terrain.setCameraMask(2);
this._terrain.setDrawWire(false);
this._terrain.setSkirtHeightRatio(3);
this.addChild(this._terrain);
this._player = new Player("Sprite3DTest/girl.c3b", this._camera, this._terrain);
this._player.setCameraMask(2);
this._player.setScale(0.08);
this._player.y = this._terrain.getHeight(this._player.x, this._player.getVertexZ()) + PLAYER_HEIGHT;
var animation = jsb.Animation3D.create("Sprite3DTest/girl.c3b", "Take 001");
if(animation){
var animate = jsb.Animate3D.create(animation);
this._player.runAction(cc.repeatForever(animate));
}
this.addChild(this._player);
this._camera.setPosition3D(cc.math.vec3Add(this._player.getPosition3D(), camera_offset));
this._camera.setRotation3D(cc.math.vec3(-45, 0, 0));
},
onTouchesEnded:function(touches, event){
var touch = touches[0];
var location = touch.getLocationInView();
if(this._camera && this._player){
var nearP = cc.math.vec3(location.x, location.y, 0);
var farP = cc.math.vec3(location.x, location.y, 1);
nearP = this._camera.unproject(nearP);
farP = this._camera.unproject(farP);
var dir = cc.math.vec3Sub(farP, nearP);
dir.normalize();
var rayStep = cc.math.vec3(15*dir.x, 15*dir.y, 15*dir.z);
var rayPos = nearP;
var rayStartPosition = nearP;
var lastRayPosition = rayPos;
rayPos.x += rayStep.x;
rayPos.y += rayStep.y;
rayPos.z += rayStep.z;
// Linear search - Loop until find a point inside and outside the terrain Vector3
var height = this._terrain.getHeight(rayPos.x, rayPos.z);
while(rayPos.y > height){
lastRayPosition = rayPos;
rayPos.x += rayStep.x;
rayPos.y += rayStep.y;
rayPos.z += rayStep.z;
height = this._terrain.getHeight(rayPos.x, rayPos.z);
}
var startPosition = lastRayPosition;
var endPosition = rayPos;
for(var i = 0; i < 32; ++i){
// Binary search pass
var middlePoint = cc.math.vec3((startPosition.x+endPosition.x)*0.5, (startPosition.y+endPosition.y)*0.5, (startPosition.z+endPosition.z)*0.5);
if(middlePoint.y < height)
endPosition = middlePoint;
else
startPosition = middlePoint;
}
var collisionPoint = cc.math.vec3((startPosition.x+endPosition.x)*0.5, (startPosition.y+endPosition.y)*0.5, (startPosition.z+endPosition.z)*0.5);
dir = cc.math.vec3Sub(collisionPoint, this._player.getPosition3D());
dir.y = 0;
dir.normalize();
this._player._headingAngle = -1 * Math.acos(cc.math.vec3Dot(dir, cc.math.vec3(0, 0, -1)));
this._player._headingAxis = cc.math.vec3Cross(dir, cc.math.vec3(0, 0, -1));
this._player._targetPos = collisionPoint;
this._player.forward();
}
}
});
})();
//
// Flow control
//
var arrayOfTerrainTest = [
TerrainSimple,
TerrainWalkThru
];
var nextTerrainTest = function () {
TerrainTestIdx++;
TerrainTestIdx = TerrainTestIdx % arrayOfTerrainTest.length;
return new arrayOfTerrainTest[TerrainTestIdx ]();
};
var previousTerrainTest = function () {
TerrainTestIdx--;
if (TerrainTestIdx < 0)
TerrainTestIdx += arrayOfTerrainTest.length;
return new arrayOfTerrainTest[TerrainTestIdx ]();
};
var restartTerrainTest = function () {
return new arrayOfTerrainTest[TerrainTestIdx ]();
};
})();

View File

@ -497,6 +497,13 @@ var testNames = [
return new ParallaxTestScene();
}
},
{
title:"Particle3D Test",
platforms: PLATFORM_JSB,
testScene:function () {
return new Particle3DTestScene();
}
},
{
title:"Particle Test",
platforms: PLATFORM_ALL,
@ -614,6 +621,14 @@ var testNames = [
return new S9SpriteTestScene();
}
},
{
title:"Terrain Test",
platforms: PLATFORM_JSB,
linksrc:"src/TerrainTest/TerrainTest.js",
testScene:function () {
return new TerrainTestScene();
}
},
{
title:"TextInput Test",
platforms: PLATFORM_HTML5,