Skip to content

Commit 0584773

Browse files
feat: distortion WIP
1 parent 9b5ef01 commit 0584773

File tree

7 files changed

+420
-0
lines changed

7 files changed

+420
-0
lines changed

build/cocos2d_libs.xcodeproj/project.pbxproj

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,18 @@
14571457
5034CA4A191D591100CE6051 /* ccShader_Label_df.frag in Headers */ = {isa = PBXBuildFile; fileRef = 5034CA0F191D591000CE6051 /* ccShader_Label_df.frag */; };
14581458
5034CA4B191D591100CE6051 /* ccShader_Label_df_glow.frag in Headers */ = {isa = PBXBuildFile; fileRef = 5034CA10191D591000CE6051 /* ccShader_Label_df_glow.frag */; };
14591459
5034CA4C191D591100CE6051 /* ccShader_Label_df_glow.frag in Headers */ = {isa = PBXBuildFile; fileRef = 5034CA10191D591000CE6051 /* ccShader_Label_df_glow.frag */; };
1460+
503D4F631CE29D4E0054A2D1 /* CCVRDistortionMesh.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 503D4F611CE29D4E0054A2D1 /* CCVRDistortionMesh.cpp */; };
1461+
503D4F641CE29D4E0054A2D1 /* CCVRDistortionMesh.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 503D4F611CE29D4E0054A2D1 /* CCVRDistortionMesh.cpp */; };
1462+
503D4F651CE29D4E0054A2D1 /* CCVRDistortionMesh.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 503D4F611CE29D4E0054A2D1 /* CCVRDistortionMesh.cpp */; };
1463+
503D4F661CE29D4E0054A2D1 /* CCVRDistortionMesh.h in Headers */ = {isa = PBXBuildFile; fileRef = 503D4F621CE29D4E0054A2D1 /* CCVRDistortionMesh.h */; };
1464+
503D4F671CE29D4E0054A2D1 /* CCVRDistortionMesh.h in Headers */ = {isa = PBXBuildFile; fileRef = 503D4F621CE29D4E0054A2D1 /* CCVRDistortionMesh.h */; };
1465+
503D4F681CE29D4E0054A2D1 /* CCVRDistortionMesh.h in Headers */ = {isa = PBXBuildFile; fileRef = 503D4F621CE29D4E0054A2D1 /* CCVRDistortionMesh.h */; };
1466+
503D4F6B1CE2BDBE0054A2D1 /* CCVRDistortion.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 503D4F691CE2BDBE0054A2D1 /* CCVRDistortion.cpp */; };
1467+
503D4F6C1CE2BDBE0054A2D1 /* CCVRDistortion.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 503D4F691CE2BDBE0054A2D1 /* CCVRDistortion.cpp */; };
1468+
503D4F6D1CE2BDBE0054A2D1 /* CCVRDistortion.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 503D4F691CE2BDBE0054A2D1 /* CCVRDistortion.cpp */; };
1469+
503D4F6E1CE2BDBE0054A2D1 /* CCVRDistortion.h in Headers */ = {isa = PBXBuildFile; fileRef = 503D4F6A1CE2BDBE0054A2D1 /* CCVRDistortion.h */; };
1470+
503D4F6F1CE2BDBE0054A2D1 /* CCVRDistortion.h in Headers */ = {isa = PBXBuildFile; fileRef = 503D4F6A1CE2BDBE0054A2D1 /* CCVRDistortion.h */; };
1471+
503D4F701CE2BDBE0054A2D1 /* CCVRDistortion.h in Headers */ = {isa = PBXBuildFile; fileRef = 503D4F6A1CE2BDBE0054A2D1 /* CCVRDistortion.h */; };
14601472
503DD8E01926736A00CD74DD /* CCApplication-ios.h in Headers */ = {isa = PBXBuildFile; fileRef = 503DD8CE1926736A00CD74DD /* CCApplication-ios.h */; };
14611473
503DD8E11926736A00CD74DD /* CCApplication-ios.mm in Sources */ = {isa = PBXBuildFile; fileRef = 503DD8CF1926736A00CD74DD /* CCApplication-ios.mm */; };
14621474
503DD8E21926736A00CD74DD /* CCCommon-ios.mm in Sources */ = {isa = PBXBuildFile; fileRef = 503DD8D01926736A00CD74DD /* CCCommon-ios.mm */; };
@@ -6427,6 +6439,10 @@
64276439
5034CA61191D91CF00CE6051 /* ccShader_PositionTextureColor.frag */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.glsl; path = ccShader_PositionTextureColor.frag; sourceTree = "<group>"; };
64286440
5034CA62191D91CF00CE6051 /* ccShader_PositionTextureColor_noMVP.vert */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.glsl; path = ccShader_PositionTextureColor_noMVP.vert; sourceTree = "<group>"; };
64296441
5034CA63191D91CF00CE6051 /* ccShader_PositionTextureColor_noMVP.frag */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.glsl; path = ccShader_PositionTextureColor_noMVP.frag; sourceTree = "<group>"; };
6442+
503D4F611CE29D4E0054A2D1 /* CCVRDistortionMesh.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCVRDistortionMesh.cpp; sourceTree = "<group>"; };
6443+
503D4F621CE29D4E0054A2D1 /* CCVRDistortionMesh.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCVRDistortionMesh.h; sourceTree = "<group>"; };
6444+
503D4F691CE2BDBE0054A2D1 /* CCVRDistortion.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCVRDistortion.cpp; sourceTree = "<group>"; };
6445+
503D4F6A1CE2BDBE0054A2D1 /* CCVRDistortion.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCVRDistortion.h; sourceTree = "<group>"; };
64306446
503DD8CE1926736A00CD74DD /* CCApplication-ios.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "CCApplication-ios.h"; sourceTree = "<group>"; };
64316447
503DD8CF1926736A00CD74DD /* CCApplication-ios.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = "CCApplication-ios.mm"; sourceTree = "<group>"; };
64326448
503DD8D01926736A00CD74DD /* CCCommon-ios.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = "CCCommon-ios.mm"; sourceTree = "<group>"; };
@@ -9909,6 +9925,10 @@
99099925
50F9654E1CD0360000ADE813 /* CCVRGeneric.cpp */,
99109926
50F9654F1CD0360000ADE813 /* CCVRGeneric.h */,
99119927
50F965501CD0360000ADE813 /* CCVRProtocol.h */,
9928+
503D4F611CE29D4E0054A2D1 /* CCVRDistortionMesh.cpp */,
9929+
503D4F621CE29D4E0054A2D1 /* CCVRDistortionMesh.h */,
9930+
503D4F691CE2BDBE0054A2D1 /* CCVRDistortion.cpp */,
9931+
503D4F6A1CE2BDBE0054A2D1 /* CCVRDistortion.h */,
99129932
);
99139933
name = vr;
99149934
path = ../cocos/vr;
@@ -11391,6 +11411,7 @@
1139111411
298C75D71C0465D1006BAE63 /* CCStencilStateManager.hpp in Headers */,
1139211412
B6DD2FDB1B04825B00E47F5F /* DetourLocalBoundary.h in Headers */,
1139311413
B6CAB3491AF9AA1A00B9B856 /* gim_clip_polygon.h in Headers */,
11414+
503D4F661CE29D4E0054A2D1 /* CCVRDistortionMesh.h in Headers */,
1139411415
B677B0DB1B18492D006762CB /* CCNavMeshUtils.h in Headers */,
1139511416
B6CAAFE41AF9A9E100B9B856 /* CCPhysics3D.h in Headers */,
1139611417
15AE1A7F19AAD40300C27E9E /* b2DistanceJoint.h in Headers */,
@@ -11866,6 +11887,7 @@
1186611887
B6CAB3A51AF9AA1A00B9B856 /* btConeTwistConstraint.h in Headers */,
1186711888
15AE181E19AAD2F700C27E9E /* CCBundle3DData.h in Headers */,
1186811889
1A5702F8180BCE750088DEC7 /* CCTMXTiledMap.h in Headers */,
11890+
503D4F6E1CE2BDBE0054A2D1 /* CCVRDistortion.h in Headers */,
1186911891
B665E2801AA80A6500DDB1C5 /* CCPUDoScaleEventHandlerTranslator.h in Headers */,
1187011892
5034CA21191D591100CE6051 /* ccShader_PositionTextureColorAlphaTest.frag in Headers */,
1187111893
D0FD03491A3B51AA00825BB5 /* CCAllocatorBase.h in Headers */,
@@ -13068,6 +13090,7 @@
1306813090
507B401C1C31BDD30067B53E /* CCNinePatchImageParser.h in Headers */,
1306913091
507B401D1C31BDD30067B53E /* CCController.h in Headers */,
1307013092
507B401E1C31BDD30067B53E /* btGpuDefines.h in Headers */,
13093+
503D4F701CE2BDBE0054A2D1 /* CCVRDistortion.h in Headers */,
1307113094
507B401F1C31BDD30067B53E /* ComAudioReader.h in Headers */,
1307213095
507B40201C31BDD30067B53E /* CCGameController.h in Headers */,
1307313096
507B40211C31BDD30067B53E /* boxBoxDistance.h in Headers */,
@@ -13132,6 +13155,7 @@
1313213155
507B405A1C31BDD30067B53E /* CCPUDoFreezeEventHandler.h in Headers */,
1313313156
507B405B1C31BDD30067B53E /* CCGL.h in Headers */,
1313413157
507B405C1C31BDD30067B53E /* CCArmatureAnimation.h in Headers */,
13158+
503D4F681CE29D4E0054A2D1 /* CCVRDistortionMesh.h in Headers */,
1313513159
507B405D1C31BDD30067B53E /* b2ContactManager.h in Headers */,
1313613160
507B405E1C31BDD30067B53E /* CCVertexIndexData.h in Headers */,
1313713161
507B405F1C31BDD30067B53E /* Vec3.h in Headers */,
@@ -14111,6 +14135,7 @@
1411114135
291901441B05895600F8B4BA /* CCNinePatchImageParser.h in Headers */,
1411214136
3E6176691960F89B00DE83F5 /* CCController.h in Headers */,
1411314137
B6CAB43E1AF9AA1A00B9B856 /* btGpuDefines.h in Headers */,
14138+
503D4F6F1CE2BDBE0054A2D1 /* CCVRDistortion.h in Headers */,
1411414139
3823841D1A2590D2002C4610 /* ComAudioReader.h in Headers */,
1411514140
3E6176781960F89B00DE83F5 /* CCGameController.h in Headers */,
1411614141
B6CAB4C81AF9AA1A00B9B856 /* boxBoxDistance.h in Headers */,
@@ -14175,6 +14200,7 @@
1417514200
B665E26D1AA80A6500DDB1C5 /* CCPUDoFreezeEventHandler.h in Headers */,
1417614201
50643BD519BFAECF00EF68ED /* CCGL.h in Headers */,
1417714202
15AE193919AAD35100C27E9E /* CCArmatureAnimation.h in Headers */,
14203+
503D4F671CE29D4E0054A2D1 /* CCVRDistortionMesh.h in Headers */,
1417814204
15AE1AA419AAD40300C27E9E /* b2ContactManager.h in Headers */,
1417914205
B276EF601988D1D500CD400F /* CCVertexIndexData.h in Headers */,
1418014206
50ABBD5F1925AB0000A911A9 /* Vec3.h in Headers */,
@@ -14580,6 +14606,7 @@
1458014606
382383F81A258FA7002C4610 /* idl_gen_general.cpp in Sources */,
1458114607
15AE1C1319AAE2C600C27E9E /* CCPhysicsSprite.cpp in Sources */,
1458214608
382384131A259092002C4610 /* NodeReaderProtocol.cpp in Sources */,
14609+
503D4F6B1CE2BDBE0054A2D1 /* CCVRDistortion.cpp in Sources */,
1458314610
B665E2C21AA80A6500DDB1C5 /* CCPUGeometryRotatorTranslator.cpp in Sources */,
1458414611
15AE1A5F19AAD40300C27E9E /* b2ContactManager.cpp in Sources */,
1458514612
B6CAB2451AF9AA1A00B9B856 /* btConvexConvexAlgorithm.cpp in Sources */,
@@ -15125,6 +15152,7 @@
1512515152
B68778F81A8CA82E00643ABF /* CCParticle3DAffector.cpp in Sources */,
1512615153
15AE19A019AAD39600C27E9E /* TextAtlasReader.cpp in Sources */,
1512715154
15AE1A2A19AAD3D500C27E9E /* b2Distance.cpp in Sources */,
15155+
503D4F631CE29D4E0054A2D1 /* CCVRDistortionMesh.cpp in Sources */,
1512815156
50ABBE6D1925AB6F00A911A9 /* CCEventListenerKeyboard.cpp in Sources */,
1512915157
15FB20871AE7C57D00C31518 /* shapes.cc in Sources */,
1513015158
B665E27A1AA80A6500DDB1C5 /* CCPUDoScaleEventHandler.cpp in Sources */,
@@ -15427,6 +15455,7 @@
1542715455
507B3A551C31BDD30067B53E /* CCArmatureDefine.cpp in Sources */,
1542815456
507B3A561C31BDD30067B53E /* CCPUOnRandomObserverTranslator.cpp in Sources */,
1542915457
507B3A571C31BDD30067B53E /* CCMeshCommand.cpp in Sources */,
15458+
503D4F6D1CE2BDBE0054A2D1 /* CCVRDistortion.cpp in Sources */,
1543015459
507B3A581C31BDD30067B53E /* CCStencilStateManager.cpp in Sources */,
1543115460
507B3A591C31BDD30067B53E /* CCComRender.cpp in Sources */,
1543215461
507B3A5A1C31BDD30067B53E /* SpriteReader.cpp in Sources */,
@@ -15972,6 +16001,7 @@
1597216001
507B3C741C31BDD30067B53E /* CCPUPlaneColliderTranslator.cpp in Sources */,
1597316002
507B3C751C31BDD30067B53E /* idl_gen_fbs.cpp in Sources */,
1597416003
507B3C761C31BDD30067B53E /* UIPageViewIndicator.cpp in Sources */,
16004+
503D4F651CE29D4E0054A2D1 /* CCVRDistortionMesh.cpp in Sources */,
1597516005
507B3C771C31BDD30067B53E /* CCPUColorAffector.cpp in Sources */,
1597616006
507B3C781C31BDD30067B53E /* CCPUBaseForceAffector.cpp in Sources */,
1597716007
507B3C791C31BDD30067B53E /* UILoadingBar.cpp in Sources */,
@@ -16274,6 +16304,7 @@
1627416304
15AE193C19AAD35100C27E9E /* CCArmatureDefine.cpp in Sources */,
1627516305
B665E35F1AA80A6500DDB1C5 /* CCPUOnRandomObserverTranslator.cpp in Sources */,
1627616306
B29594B51926D5EC003EEF37 /* CCMeshCommand.cpp in Sources */,
16307+
503D4F6C1CE2BDBE0054A2D1 /* CCVRDistortion.cpp in Sources */,
1627716308
298C75D61C0465D1006BAE63 /* CCStencilStateManager.cpp in Sources */,
1627816309
15AE194B19AAD35100C27E9E /* CCComRender.cpp in Sources */,
1627916310
382384451A25915C002C4610 /* SpriteReader.cpp in Sources */,
@@ -16819,6 +16850,7 @@
1681916850
B665E3931AA80A6500DDB1C5 /* CCPUPlaneColliderTranslator.cpp in Sources */,
1682016851
382383F71A258FA7002C4610 /* idl_gen_fbs.cpp in Sources */,
1682116852
B5A738971BB0051F00BAAEF8 /* UIPageViewIndicator.cpp in Sources */,
16853+
503D4F641CE29D4E0054A2D1 /* CCVRDistortionMesh.cpp in Sources */,
1682216854
B665E24B1AA80A6500DDB1C5 /* CCPUColorAffector.cpp in Sources */,
1682316855
B665E20F1AA80A6500DDB1C5 /* CCPUBaseForceAffector.cpp in Sources */,
1682416856
15AE1B7419AADA9A00C27E9E /* UILoadingBar.cpp in Sources */,

cocos/vr/CCVRDistortion.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/****************************************************************************
2+
Copyright (c) 2016 Google Inc.
3+
Copyright (c) 2016 Chukong Technologies Inc.
4+
5+
http://www.cocos2d-x.org
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
****************************************************************************/
25+
26+
#include "CCVRDistortion.h"
27+
28+
NS_CC_BEGIN
29+
30+
Distortion::Distortion()
31+
{
32+
_coefficients[0] = 0.441f;
33+
_coefficients[1] = 0.156f;
34+
}
35+
36+
void Distortion::setCoefficients(float *coefficients)
37+
{
38+
for (int i = 0; i < s_numberOfCoefficients; i++)
39+
{
40+
_coefficients[i] = coefficients[i];
41+
}
42+
}
43+
44+
float *Distortion::coefficients()
45+
{
46+
return _coefficients;
47+
}
48+
49+
float Distortion::distortionFactor(float radius)
50+
{
51+
float result = 1.0f;
52+
float rFactor = 1.0f;
53+
float squaredRadius = radius * radius;
54+
for (int i = 0; i < s_numberOfCoefficients; i++)
55+
{
56+
rFactor *= squaredRadius;
57+
result += _coefficients[i] * rFactor;
58+
}
59+
return result;
60+
}
61+
62+
float Distortion::distort(float radius)
63+
{
64+
return radius * distortionFactor(radius);
65+
}
66+
67+
float Distortion::distortInverse(float radius)
68+
{
69+
float r0 = radius / 0.9f;
70+
float r = radius * 0.9f;
71+
float dr0 = radius - distort(r0);
72+
while (fabsf(r - r0) > 0.0001f)
73+
{
74+
float dr = radius - distort(r);
75+
float r2 = r - dr * ((r - r0) / (dr - dr0));
76+
r0 = r;
77+
r = r2;
78+
dr0 = dr;
79+
}
80+
return r;
81+
}
82+
83+
NS_CC_END

cocos/vr/CCVRDistortion.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/****************************************************************************
2+
Copyright (c) 2016 Google Inc.
3+
Copyright (c) 2016 Chukong Technologies Inc.
4+
5+
http://www.cocos2d-x.org
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
****************************************************************************/
25+
26+
#ifndef CCVRDistortion_h
27+
#define CCVRDistortion_h
28+
29+
#include "platform/CCPlatformMacros.h"
30+
31+
NS_CC_BEGIN
32+
33+
// Barrel Distortion
34+
class Distortion
35+
{
36+
public:
37+
Distortion();
38+
39+
void setCoefficients(float *coefficients);
40+
float *coefficients();
41+
42+
float distortionFactor(float radius);
43+
float distort(float radius);
44+
float distortInverse(float radius);
45+
46+
private:
47+
constexpr static int s_numberOfCoefficients = 2;
48+
float _coefficients[s_numberOfCoefficients];
49+
};
50+
51+
NS_CC_END
52+
53+
#endif /* CCVRDistortion_h */

0 commit comments

Comments
 (0)