diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2017-08-27 21:07:15 +0200 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2017-08-27 22:13:45 +0200 |
commit | 7ad14e7a3e6f87ddc450f7e34621eb5200808451 (patch) | |
tree | 8804d0dd24cc126087462edfbbbf73ed61b56b0e /servers | |
parent | 37da8155a4500a9386027b4d791a86186bc7ab4a (diff) |
Dead code tells no tales
Diffstat (limited to 'servers')
-rw-r--r-- | servers/audio/audio_rb_resampler.cpp | 56 | ||||
-rw-r--r-- | servers/physics/body_pair_sw.cpp | 24 | ||||
-rw-r--r-- | servers/physics/physics_server_sw.cpp | 112 | ||||
-rw-r--r-- | servers/physics/physics_server_sw.h | 12 | ||||
-rw-r--r-- | servers/physics/shape_sw.cpp | 117 | ||||
-rw-r--r-- | servers/physics/space_sw.cpp | 25 | ||||
-rw-r--r-- | servers/physics_2d/collision_solver_2d_sat.cpp | 185 | ||||
-rw-r--r-- | servers/physics_2d/joints_2d_sw.cpp | 118 | ||||
-rw-r--r-- | servers/physics_2d/joints_2d_sw.h | 41 | ||||
-rw-r--r-- | servers/physics_2d/shape_2d_sw.cpp | 1 | ||||
-rw-r--r-- | servers/physics_2d/space_2d_sw.cpp | 16 | ||||
-rw-r--r-- | servers/physics_server.h | 35 | ||||
-rw-r--r-- | servers/visual/rasterizer.cpp | 612 | ||||
-rw-r--r-- | servers/visual/rasterizer.h | 1029 | ||||
-rw-r--r-- | servers/visual/visual_server_raster.cpp | 7741 | ||||
-rw-r--r-- | servers/visual/visual_server_raster.h | 1 | ||||
-rw-r--r-- | servers/visual/visual_server_scene.cpp | 22 | ||||
-rw-r--r-- | servers/visual/visual_server_scene.h | 2 | ||||
-rw-r--r-- | servers/visual/visual_server_viewport.cpp | 31 | ||||
-rw-r--r-- | servers/visual_server.cpp | 39 | ||||
-rw-r--r-- | servers/visual_server.h | 1 |
21 files changed, 22 insertions, 10198 deletions
diff --git a/servers/audio/audio_rb_resampler.cpp b/servers/audio/audio_rb_resampler.cpp index e253347cf1..2f160fd7cd 100644 --- a/servers/audio/audio_rb_resampler.cpp +++ b/servers/audio/audio_rb_resampler.cpp @@ -173,44 +173,7 @@ bool AudioRBResampler::mix(int32_t *p_dest, int p_frames) { } int todo = MIN(((int64_t(rb_todo) << MIX_FRAC_BITS) / increment) + 1, p_frames); -#if 0 - if (int(src_mix_rate)==target_mix_rate) { - - if (channels==6) { - - for(int i=0;i<p_frames;i++) { - - int from = ((rb_read_pos+i)&rb_mask)*6; - int to = i*6; - - p_dest[from+0]=int32_t(rb[to+0])<<16; - p_dest[from+1]=int32_t(rb[to+1])<<16; - p_dest[from+2]=int32_t(rb[to+2])<<16; - p_dest[from+3]=int32_t(rb[to+3])<<16; - p_dest[from+4]=int32_t(rb[to+4])<<16; - p_dest[from+5]=int32_t(rb[to+5])<<16; - } - - } else { - int len=p_frames*channels; - int from=rb_read_pos*channels; - int mask=0; - switch(channels) { - case 1: mask=rb_len-1; break; - case 2: mask=(rb_len*2)-1; break; - case 4: mask=(rb_len*4)-1; break; - } - - for(int i=0;i<len;i++) { - - p_dest[i]=int32_t(rb[(from+i)&mask])<<16; - } - } - - rb_read_pos = (rb_read_pos+p_frames)&rb_mask; - } else -#endif { uint32_t read = 0; @@ -220,7 +183,7 @@ bool AudioRBResampler::mix(int32_t *p_dest, int p_frames) { case 4: read = _resample<4>(p_dest, todo, increment); break; case 6: read = _resample<6>(p_dest, todo, increment); break; } -#if 1 + //end of stream, fadeout int remaining = p_frames - todo; if (remaining && todo > 0) { @@ -238,23 +201,6 @@ bool AudioRBResampler::mix(int32_t *p_dest, int p_frames) { } } -#else - int remaining = p_frames - todo; - if (remaining && todo > 0) { - - for (int c = 0; c < channels; c++) { - - int32_t from = p_dest[(todo - 1) * channels + c] >> 8; - - for (int i = 0; i < remaining; i++) { - - uint32_t mul = (remaining - i) * 256 / remaining; - p_dest[(todo + i) * channels + c] = from * mul; - } - } - } -#endif - //zero out what remains there to avoid glitches for (int i = todo * channels; i < int(p_frames) * channels; i++) { diff --git a/servers/physics/body_pair_sw.cpp b/servers/physics/body_pair_sw.cpp index 25451a99b7..a289b4b0ca 100644 --- a/servers/physics/body_pair_sw.cpp +++ b/servers/physics/body_pair_sw.cpp @@ -28,6 +28,7 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "body_pair_sw.h" + #include "collision_solver_sw.h" #include "os/os.h" #include "space_sw.h" @@ -296,17 +297,7 @@ bool BodyPairSW::setup(real_t p_step) { c.rA = global_A - A->get_center_of_mass(); c.rB = global_B - B->get_center_of_mass() - offset_B; -// contact query reporting... -#if 0 - if (A->get_body_type() == PhysicsServer::BODY_CHARACTER) - static_cast<CharacterBodySW*>(A)->report_character_contact( global_A, global_B, B ); - if (B->get_body_type() == PhysicsServer::BODY_CHARACTER) - static_cast<CharacterBodySW*>(B)->report_character_contact( global_B, global_A, A ); - if (A->has_contact_query()) - A->report_contact( global_A, global_B, B ); - if (B->has_contact_query()) - B->report_contact( global_B, global_A, A ); -#endif + // contact query reporting... if (A->can_report_contacts()) { Vector3 crA = A->get_angular_velocity().cross(c.rA) + A->get_linear_velocity(); @@ -327,18 +318,7 @@ bool BodyPairSW::setup(real_t p_step) { kNormal += c.normal.dot(inertia_A.cross(c.rA)) + c.normal.dot(inertia_B.cross(c.rB)); c.mass_normal = 1.0f / kNormal; -#if 1 c.bias = -bias * inv_dt * MIN(0.0f, -depth + max_penetration); - -#else - if (depth > max_penetration) { - c.bias = (depth - max_penetration) * (1.0 / (p_step * (1.0 / RELAXATION_TIMESTEPS))); - } else { - real_t approach = -0.1 * (depth - max_penetration) / (CMP_EPSILON + max_penetration); - approach = CLAMP(approach, CMP_EPSILON, 1.0); - c.bias = approach * (depth - max_penetration) * (1.0 / p_step); - } -#endif c.depth = depth; Vector3 j_vec = c.normal * c.acc_normal_impulse + c.acc_tangent_impulse; diff --git a/servers/physics/physics_server_sw.cpp b/servers/physics/physics_server_sw.cpp index 824ad0428b..8d6f7b3fd8 100644 --- a/servers/physics/physics_server_sw.cpp +++ b/servers/physics/physics_server_sw.cpp @@ -28,6 +28,7 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "physics_server_sw.h" + #include "broad_phase_basic.h" #include "broad_phase_octree.h" #include "joints/cone_twist_joint_sw.h" @@ -1206,117 +1207,6 @@ bool PhysicsServerSW::generic_6dof_joint_get_flag(RID p_joint, Vector3::Axis p_a return generic_6dof_joint->get_flag(p_axis, p_flag); } -#if 0 -void PhysicsServerSW::joint_set_param(RID p_joint, JointParam p_param, real_t p_value) { - - JointSW *joint = joint_owner.get(p_joint); - ERR_FAIL_COND(!joint); - - switch(p_param) { - case JOINT_PARAM_BIAS: joint->set_bias(p_value); break; - case JOINT_PARAM_MAX_BIAS: joint->set_max_bias(p_value); break; - case JOINT_PARAM_MAX_FORCE: joint->set_max_force(p_value); break; - } - - -} - -real_t PhysicsServerSW::joint_get_param(RID p_joint,JointParam p_param) const { - - const JointSW *joint = joint_owner.get(p_joint); - ERR_FAIL_COND_V(!joint,-1); - - switch(p_param) { - case JOINT_PARAM_BIAS: return joint->get_bias(); break; - case JOINT_PARAM_MAX_BIAS: return joint->get_max_bias(); break; - case JOINT_PARAM_MAX_FORCE: return joint->get_max_force(); break; - } - - return 0; -} - - -RID PhysicsServerSW::pin_joint_create(const Vector3& p_pos,RID p_body_a,RID p_body_b) { - - BodySW *A=body_owner.get(p_body_a); - ERR_FAIL_COND_V(!A,RID()); - BodySW *B=NULL; - if (body_owner.owns(p_body_b)) { - B=body_owner.get(p_body_b); - ERR_FAIL_COND_V(!B,RID()); - } - - JointSW *joint = memnew( PinJointSW(p_pos,A,B) ); - RID self = joint_owner.make_rid(joint); - joint->set_self(self); - - return self; -} - -RID PhysicsServerSW::groove_joint_create(const Vector3& p_a_groove1,const Vector3& p_a_groove2, const Vector3& p_b_anchor, RID p_body_a,RID p_body_b) { - - - BodySW *A=body_owner.get(p_body_a); - ERR_FAIL_COND_V(!A,RID()); - - BodySW *B=body_owner.get(p_body_b); - ERR_FAIL_COND_V(!B,RID()); - - JointSW *joint = memnew( GrooveJointSW(p_a_groove1,p_a_groove2,p_b_anchor,A,B) ); - RID self = joint_owner.make_rid(joint); - joint->set_self(self); - return self; - - -} - -RID PhysicsServerSW::damped_spring_joint_create(const Vector3& p_anchor_a,const Vector3& p_anchor_b,RID p_body_a,RID p_body_b) { - - BodySW *A=body_owner.get(p_body_a); - ERR_FAIL_COND_V(!A,RID()); - - BodySW *B=body_owner.get(p_body_b); - ERR_FAIL_COND_V(!B,RID()); - - JointSW *joint = memnew( DampedSpringJointSW(p_anchor_a,p_anchor_b,A,B) ); - RID self = joint_owner.make_rid(joint); - joint->set_self(self); - return self; - -} - -void PhysicsServerSW::damped_string_joint_set_param(RID p_joint, DampedStringParam p_param, real_t p_value) { - - - JointSW *j = joint_owner.get(p_joint); - ERR_FAIL_COND(!j); - ERR_FAIL_COND(j->get_type()!=JOINT_DAMPED_SPRING); - - DampedSpringJointSW *dsj = static_cast<DampedSpringJointSW*>(j); - dsj->set_param(p_param,p_value); -} - -real_t PhysicsServerSW::damped_string_joint_get_param(RID p_joint, DampedStringParam p_param) const { - - JointSW *j = joint_owner.get(p_joint); - ERR_FAIL_COND_V(!j,0); - ERR_FAIL_COND_V(j->get_type()!=JOINT_DAMPED_SPRING,0); - - DampedSpringJointSW *dsj = static_cast<DampedSpringJointSW*>(j); - return dsj->get_param(p_param); -} - -PhysicsServer::JointType PhysicsServerSW::joint_get_type(RID p_joint) const { - - - JointSW *joint = joint_owner.get(p_joint); - ERR_FAIL_COND_V(!joint,JOINT_PIN); - - return joint->get_type(); -} - -#endif - void PhysicsServerSW::free(RID p_rid) { if (shape_owner.owns(p_rid)) { diff --git a/servers/physics/physics_server_sw.h b/servers/physics/physics_server_sw.h index 568fed1177..2e1fa7065a 100644 --- a/servers/physics/physics_server_sw.h +++ b/servers/physics/physics_server_sw.h @@ -264,18 +264,6 @@ public: virtual void joint_set_solver_priority(RID p_joint, int p_priority); virtual int joint_get_solver_priority(RID p_joint) const; -#if 0 - virtual void joint_set_param(RID p_joint, JointParam p_param, real_t p_value); - virtual real_t joint_get_param(RID p_joint,JointParam p_param) const; - - virtual RID pin_joint_create(const Vector3& p_pos,RID p_body_a,RID p_body_b=RID()); - virtual RID groove_joint_create(const Vector3& p_a_groove1,const Vector3& p_a_groove2, const Vector3& p_b_anchor, RID p_body_a,RID p_body_b); - virtual RID damped_spring_joint_create(const Vector3& p_anchor_a,const Vector3& p_anchor_b,RID p_body_a,RID p_body_b=RID()); - virtual void damped_string_joint_set_param(RID p_joint, DampedStringParam p_param, real_t p_value); - virtual real_t damped_string_joint_get_param(RID p_joint, DampedStringParam p_param) const; - - virtual JointType joint_get_type(RID p_joint) const; -#endif /* MISC */ virtual void free(RID p_rid); diff --git a/servers/physics/shape_sw.cpp b/servers/physics/shape_sw.cpp index a96e26e81d..f02ff03fcf 100644 --- a/servers/physics/shape_sw.cpp +++ b/servers/physics/shape_sw.cpp @@ -28,9 +28,11 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "shape_sw.h" + #include "geometry.h" #include "quick_hull.h" #include "sort.h" + #define _POINT_SNAP 0.001953125 #define _EDGE_IS_VALID_SUPPORT_THRESHOLD 0.0002 #define _FACE_IS_VALID_SUPPORT_THRESHOLD 0.9998 @@ -1473,119 +1475,6 @@ void ConcavePolygonShapeSW::_setup(PoolVector<Vector3> p_faces) { PoolVector<Vector3>::Read r = p_faces.read(); const Vector3 *facesr = r.ptr(); -#if 0 - Map<Vector3,int> point_map; - List<Face> face_list; - - - for(int i=0;i<src_face_count;i++) { - - Face3 faceaux; - - for(int j=0;j<3;j++) { - - faceaux.vertex[j]=facesr[i*3+j].snapped(_POINT_SNAP); - //faceaux.vertex[j]=facesr[i*3+j];//facesr[i*3+j].snapped(_POINT_SNAP); - } - - ERR_CONTINUE( faceaux.is_degenerate() ); - - Face face; - - for(int j=0;j<3;j++) { - - - Map<Vector3,int>::Element *E=point_map.find(faceaux.vertex[j]); - if (E) { - - face.indices[j]=E->value(); - } else { - - face.indices[j]=point_map.size(); - point_map.insert(faceaux.vertex[j],point_map.size()); - - } - } - - face_list.push_back(face); - } - - vertices.resize( point_map.size() ); - - PoolVector<Vector3>::Write vw = vertices.write(); - Vector3 *verticesw=vw.ptr(); - - AABB _aabb; - - for( Map<Vector3,int>::Element *E=point_map.front();E;E=E->next()) { - - if (E==point_map.front()) { - _aabb.pos=E->key(); - } else { - - _aabb.expand_to(E->key()); - } - verticesw[E->value()]=E->key(); - } - - point_map.clear(); // not needed anymore - - faces.resize(face_list.size()); - PoolVector<Face>::Write w = faces.write(); - Face *facesw=w.ptr(); - - int fc=0; - - for( List<Face>::Element *E=face_list.front();E;E=E->next()) { - - facesw[fc++]=E->get(); - } - - face_list.clear(); - - - PoolVector<_VolumeSW_BVH_Element> bvh_array; - bvh_array.resize( fc ); - - PoolVector<_VolumeSW_BVH_Element>::Write bvhw = bvh_array.write(); - _VolumeSW_BVH_Element *bvh_arrayw=bvhw.ptr(); - - - for(int i=0;i<fc;i++) { - - AABB face_aabb; - face_aabb.pos=verticesw[facesw[i].indices[0]]; - face_aabb.expand_to( verticesw[facesw[i].indices[1]] ); - face_aabb.expand_to( verticesw[facesw[i].indices[2]] ); - - bvh_arrayw[i].face_index=i; - bvh_arrayw[i].aabb=face_aabb; - bvh_arrayw[i].center=face_aabb.pos+face_aabb.size*0.5; - - } - - w=PoolVector<Face>::Write(); - vw=PoolVector<Vector3>::Write(); - - - int count=0; - _VolumeSW_BVH *bvh_tree=_volume_sw_build_bvh(bvh_arrayw,fc,count); - - ERR_FAIL_COND(count==0); - - bvhw=PoolVector<_VolumeSW_BVH_Element>::Write(); - - bvh.resize( count+1 ); - - PoolVector<BVH>::Write bvhw2 = bvh.write(); - BVH*bvh_arrayw2=bvhw2.ptr(); - - int idx=0; - _fill_bvh(bvh_tree,bvh_arrayw2,idx); - - set_aabb(_aabb); - -#else PoolVector<_VolumeSW_BVH_Element> bvh_array; bvh_array.resize(src_face_count); @@ -1638,8 +1527,6 @@ void ConcavePolygonShapeSW::_setup(PoolVector<Vector3> p_faces) { _fill_bvh(bvh_tree, bvh_arrayw2, idx); configure(_aabb); // this type of shape has no margin - -#endif } void ConcavePolygonShapeSW::set_data(const Variant &p_data) { diff --git a/servers/physics/space_sw.cpp b/servers/physics/space_sw.cpp index 759e354c99..17e2df6c9e 100644 --- a/servers/physics/space_sw.cpp +++ b/servers/physics/space_sw.cpp @@ -28,6 +28,7 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "space_sw.h" + #include "collision_solver_sw.h" #include "physics_server_sw.h" #include "project_settings.h" @@ -267,20 +268,13 @@ bool PhysicsDirectSpaceStateSW::cast_motion(const RID &p_shape, const Transform continue; } -//test initial overlap -#if 0 - if (CollisionSolverSW::solve_static(shape,p_xform,col_obj->get_shape(shape_idx),col_obj_xform,NULL,NULL,&sep_axis)) { - print_line("failed initial cast (collision at beginning)"); - return false; - } -#else + //test initial overlap sep_axis = p_motion.normalized(); if (!CollisionSolverSW::solve_distance(shape, p_xform, col_obj->get_shape(shape_idx), col_obj_xform, point_A, point_B, aabb, &sep_axis)) { //print_line("failed motion cast (no collision)"); return false; } -#endif //just do kinematic solving real_t low = 0; @@ -631,21 +625,6 @@ bool SpaceSW::test_body_motion(BodySW *p_body, const Transform &p_from, const Ve Vector3 a = sr[i * 2 + 0]; Vector3 b = sr[i * 2 + 1]; - -#if 0 - Vector3 rel = b-a; - real_t d = rel.length(); - if (d==0) - continue; - - Vector3 n = rel/d; - real_t traveled = n.dot(recover_motion); - a+=n*traveled; - - real_t d = a.distance_to(b); - if (d<margin) - continue; -#endif recover_motion += (b - a) * 0.4; } diff --git a/servers/physics_2d/collision_solver_2d_sat.cpp b/servers/physics_2d/collision_solver_2d_sat.cpp index b2363e30eb..bf8478d0b1 100644 --- a/servers/physics_2d/collision_solver_2d_sat.cpp +++ b/servers/physics_2d/collision_solver_2d_sat.cpp @@ -28,6 +28,7 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "collision_solver_2d_sat.h" + #include "geometry.h" struct _CollectorCallback2D { @@ -89,124 +90,6 @@ _FORCE_INLINE_ static void _generate_contacts_edge_edge(const Vector2 *p_points_ ERR_FAIL_COND(p_point_count_B != 2); // circle is actually a 4x3 matrix #endif -#if 0 - Vector2 rel_A=p_points_A[1]-p_points_A[0]; - Vector2 rel_B=p_points_B[1]-p_points_B[0]; - - Vector2 t = p_collector->normal.tangent(); - - print_line("tangent: "+t); - - real_t dA[2]={t.dot(p_points_A[0]),t.dot(p_points_A[1])}; - Vector2 pA[2]={p_points_A[0],p_points_A[1]}; - - if (dA[0]>dA[1]) { - SWAP(dA[0],dA[1]); - SWAP(pA[0],pA[1]); - } - - real_t dB[2]={t.dot(p_points_B[0]),t.dot(p_points_B[1])}; - Vector2 pB[2]={p_points_B[0],p_points_B[1]}; - if (dB[0]>dB[1]) { - SWAP(dB[0],dB[1]); - SWAP(pB[0],pB[1]); - } - - - if (dA[0]<dB[0]) { - - Vector2 n = (p_points_A[1]-p_points_A[0]).normalized().tangent(); - real_t d = n.dot(p_points_A[1]); - - if (dA[1]>dB[1]) { - //A contains B - for(int i=0;i<2;i++) { - - Vector2 b = p_points_B[i]; - Vector2 a = n.plane_project(d,b); - if (p_collector->normal.dot(a) > p_collector->normal.dot(b)-CMP_EPSILON) - continue; - p_collector->call(a,b); - - } - } else { - - // B0,A1 containment - - Vector2 n_B = (p_points_B[1]-p_points_B[0]).normalized().tangent(); - real_t d_B = n_B.dot(p_points_B[1]); - - // first, B on A - - { - Vector2 b = p_points_B[0]; - Vector2 a = n.plane_project(d,b); - if (p_collector->normal.dot(a) < p_collector->normal.dot(b)-CMP_EPSILON) - p_collector->call(a,b); - } - - // second, A on B - - { - Vector2 a = p_points_A[1]; - Vector2 b = n_B.plane_project(d_B,a); - if (p_collector->normal.dot(a) < p_collector->normal.dot(b)-CMP_EPSILON) - p_collector->call(a,b); - } - - - - } - - - } else { - - Vector2 n = (p_points_B[1]-p_points_B[0]).normalized().tangent(); - real_t d = n.dot(p_points_B[1]); - - if (dB[1]>dA[1]) { - //B contains A - for(int i=0;i<2;i++) { - - Vector2 a = p_points_A[i]; - Vector2 b = n.plane_project(d,a); - if (p_collector->normal.dot(a) > p_collector->normal.dot(b)-CMP_EPSILON) - continue; - p_collector->call(a,b); - } - } else { - - // A0,B1 containment - Vector2 n_A = (p_points_A[1]-p_points_A[0]).normalized().tangent(); - real_t d_A = n_A.dot(p_points_A[1]); - - // first A on B - - { - Vector2 a = p_points_A[0]; - Vector2 b = n.plane_project(d,a); - if (p_collector->normal.dot(a) < p_collector->normal.dot(b)-CMP_EPSILON) - p_collector->call(a,b); - - } - - //second, B on A - - { - - Vector2 b = p_points_B[1]; - Vector2 a = n_A.plane_project(d_A,b); - if (p_collector->normal.dot(a) < p_collector->normal.dot(b)-CMP_EPSILON) - p_collector->call(a,b); - } - - } - } - -#endif - -#if 1 - Vector2 n = p_collector->normal; Vector2 t = n.tangent(); real_t dA = n.dot(p_points_A[0]); @@ -246,38 +129,6 @@ _FORCE_INLINE_ static void _generate_contacts_edge_edge(const Vector2 *p_points_ p_collector->call(a, b); } } - -#elif 0 - Vector2 axis = rel_A.normalized(); //make an axis - Vector2 axis_B = rel_B.normalized(); - if (axis.dot(axis_B) < 0) - axis_B = -axis_B; - axis = (axis + axis_B) * 0.5; - Vector2 base_A = p_points_A[0] - axis * axis.dot(p_points_A[0]); - Vector2 base_B = p_points_B[0] - axis * axis.dot(p_points_B[0]); - - //sort all 4 points in axis - real_t dvec[4] = { axis.dot(p_points_A[0]), axis.dot(p_points_A[1]), axis.dot(p_points_B[0]), axis.dot(p_points_B[1]) }; - - //todo , find max/min and then use 2 central points - SortArray<real_t> sa; - sa.sort(dvec, 4); - - //use the middle ones as contacts - for (int i = 1; i <= 2; i++) { - - Vector2 a = base_A + axis * dvec[i]; - Vector2 b = base_B + axis * dvec[i]; - if (p_collector->normal.dot(a) > p_collector->normal.dot(b) - 0.01) { - print_line("fail a: " + a); - print_line("fail b: " + b); - continue; - } - print_line("res a: " + a); - print_line("res b: " + b); - p_collector->call(a, b); - } -#endif } static void _generate_contacts_from_supports(const Vector2 *p_points_A, int p_point_count_A, const Vector2 *p_points_B, int p_point_count_B, _CollectorCallback2D *p_collector) { @@ -504,37 +355,11 @@ public: } } - /* - - - print_line("**************************"); - printf("CBK: %p\n",callback->userdata); - print_line("type A: "+itos(shape_A->get_type())); - print_line("type B: "+itos(shape_B->get_type())); - print_line("xform A: "+*transform_A); - print_line("xform B: "+*transform_B); - print_line("normal: "+best_axis); - print_line("depth: "+rtos(best_depth)); - print_line("index: "+itos(best_axis_index)); - - for(int i=0;i<support_count_A;i++) { - - print_line("A-"+itos(i)+": "+supports_A[i]); - } - - for(int i=0;i<support_count_B;i++) { - - print_line("B-"+itos(i)+": "+supports_B[i]); - } -//*/ - callback->normal = best_axis; _generate_contacts_from_supports(supports_A, support_count_A, supports_B, support_count_B, callback); if (callback && callback->sep_axis && *callback->sep_axis != Vector2()) *callback->sep_axis = Vector2(); //invalidate previous axis (no test) - //CollisionSolver2DSW::CallbackResult cbk=NULL; - //cbk(Vector2(),Vector2(),NULL); } _FORCE_INLINE_ SeparatorAxisTest2D(const ShapeA *p_shape_A, const Transform2D &p_transform_a, const ShapeB *p_shape_B, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_A = Vector2(), const Vector2 &p_motion_B = Vector2(), real_t p_margin_A = 0, real_t p_margin_B = 0) { @@ -558,9 +383,6 @@ public: }; /****** SAT TESTS *******/ -/****** SAT TESTS *******/ -/****** SAT TESTS *******/ -/****** SAT TESTS *******/ #define TEST_POINT(m_a, m_b) \ ((!separator.test_axis(((m_a) - (m_b)).normalized())) || \ @@ -582,11 +404,6 @@ static void _collision_segment_segment(const Shape2DSW *p_a, const Transform2D & return; //this collision is kind of pointless - /* - if (!separator.test_previous_axis()) - return; - */ - if (!separator.test_cast()) return; diff --git a/servers/physics_2d/joints_2d_sw.cpp b/servers/physics_2d/joints_2d_sw.cpp index f4c8f9178d..897e145a3e 100644 --- a/servers/physics_2d/joints_2d_sw.cpp +++ b/servers/physics_2d/joints_2d_sw.cpp @@ -28,6 +28,7 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "joints_2d_sw.h" + #include "space_2d_sw.h" //based on chipmunk joint constraints @@ -87,127 +88,12 @@ normal_relative_velocity(Body2DSW *a, Body2DSW *b, Vector2 rA, Vector2 rB, Vecto return relative_velocity(a, b, rA, rB).dot(n); } -#if 0 - -bool PinJoint2DSW::setup(real_t p_step) { - - Space2DSW *space = A->get_space(); - ERR_FAIL_COND_V(!space,false;) - rA = A->get_transform().basis_xform(anchor_A); - rB = B?B->get_transform().basis_xform(anchor_B):anchor_B; - - Vector2 gA = A->get_transform().get_origin(); - Vector2 gB = B?B->get_transform().get_origin():Vector2(); - - Vector2 delta = gB - gA; - delta = (delta+rB) -rA; - - real_t jdist = delta.length(); - correct=false; - if (jdist==0) - return false; // do not correct - - correct=true; - - n = delta / jdist; - - // calculate mass normal - mass_normal = 1.0f/k_scalar(A, B, rA, rB, n); - - // calculate bias velocity - //real_t maxBias = joint->constraint.maxBias; - bias = -(get_bias()==0?space->get_constraint_bias():get_bias())*(1.0/p_step)*(jdist-dist); - bias = CLAMP(bias, -get_max_bias(), +get_max_bias()); - - // compute max impulse - jn_max = get_max_force() * p_step; - - // apply accumulated impulse - Vector2 j = n * jn_acc; - A->apply_impulse(rA,-j); - if (B) - B->apply_impulse(rB,j); - - print_line("setup"); - return true; -} - - - -void PinJoint2DSW::solve(real_t p_step){ - - if (!correct) - return; - - Vector2 ln = n; - - // compute relative velocity - real_t vrn = normal_relative_velocity(A,B, rA, rB, ln); - - // compute normal impulse - real_t jn = (bias - vrn)*mass_normal; - real_t jnOld = jn_acc; - jn_acc = CLAMP(jnOld + jn,-jn_max,jn_max); //cpfclamp(jnOld + jn, -joint->jnMax, joint->jnMax); - jn = jn_acc - jnOld; - print_line("jn_acc: "+rtos(jn_acc)); - Vector2 j = jn*ln; - - A->apply_impulse(rA,-j); - if (B) - B->apply_impulse(rB,j); - -} - - -PinJoint2DSW::PinJoint2DSW(const Vector2& p_pos,Body2DSW* p_body_a,Body2DSW* p_body_b) : Joint2DSW(_arr,p_body_b?2:1) { - - A=p_body_a; - B=p_body_b; - anchor_A = p_body_a->get_inv_transform().xform(p_pos); - anchor_B = p_body_b?p_body_b->get_inv_transform().xform(p_pos):p_pos; - - jn_acc=0; - dist=0; - - p_body_a->add_constraint(this,0); - if (p_body_b) - p_body_b->add_constraint(this,1); - -} - -PinJoint2DSW::~PinJoint2DSW() { - - if (A) - A->remove_constraint(this); - if (B) - B->remove_constraint(this); - -} - -#else - bool PinJoint2DSW::setup(real_t p_step) { Space2DSW *space = A->get_space(); ERR_FAIL_COND_V(!space, false;) rA = A->get_transform().basis_xform(anchor_A); rB = B ? B->get_transform().basis_xform(anchor_B) : anchor_B; -#if 0 - Vector2 gA = rA+A->get_transform().get_origin(); - Vector2 gB = B?rB+B->get_transform().get_origin():rB; - - VectorB delta = gB - gA; - - real_t jdist = delta.length(); - correct=false; - if (jdist==0) - return false; // do not correct -#endif - - // deltaV = deltaV0 + K * impulse - // invM = [(1/m1 + 1/m2) * eye(2) - skew(rA) * invI1 * skew(rA) - skew(rB) * invI2 * skew(rB)] - // = [1/m1+1/m2 0 ] + invI1 * [rA.y*rA.y -rA.x*rA.y] + invI2 * [rA.y*rA.y -rA.x*rA.y] - // [ 0 1/m1+1/m2] [-rA.x*rA.y rA.x*rA.x] [-rA.x*rA.y rA.x*rA.x] real_t B_inv_mass = B ? B->get_inv_mass() : 0.0; @@ -315,8 +201,6 @@ PinJoint2DSW::~PinJoint2DSW() { B->remove_constraint(this); } -#endif - ////////////////////////////////////////////// ////////////////////////////////////////////// ////////////////////////////////////////////// diff --git a/servers/physics_2d/joints_2d_sw.h b/servers/physics_2d/joints_2d_sw.h index 9b8bb16327..11cb41d9d1 100644 --- a/servers/physics_2d/joints_2d_sw.h +++ b/servers/physics_2d/joints_2d_sw.h @@ -56,46 +56,6 @@ public: max_force = max_bias = 3.40282e+38; }; }; -#if 0 - -class PinJoint2DSW : public Joint2DSW { - - union { - struct { - Body2DSW *A; - Body2DSW *B; - }; - - Body2DSW *_arr[2]; - }; - - Vector2 anchor_A; - Vector2 anchor_B; - real_t dist; - real_t jn_acc; - real_t jn_max; - real_t max_distance; - real_t mass_normal; - real_t bias; - - Vector2 rA,rB; - Vector2 n; //normal - bool correct; - - -public: - - virtual Physics2DServer::JointType get_type() const { return Physics2DServer::JOINT_PIN; } - - virtual bool setup(real_t p_step); - virtual void solve(real_t p_step); - - - PinJoint2DSW(const Vector2& p_pos,Body2DSW* p_body_a,Body2DSW* p_body_b=NULL); - ~PinJoint2DSW(); -}; - -#else class PinJoint2DSW : public Joint2DSW { @@ -129,7 +89,6 @@ public: ~PinJoint2DSW(); }; -#endif class GrooveJoint2DSW : public Joint2DSW { union { diff --git a/servers/physics_2d/shape_2d_sw.cpp b/servers/physics_2d/shape_2d_sw.cpp index 1fb817002a..20d0d38b17 100644 --- a/servers/physics_2d/shape_2d_sw.cpp +++ b/servers/physics_2d/shape_2d_sw.cpp @@ -28,6 +28,7 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "shape_2d_sw.h" + #include "geometry.h" #include "sort.h" diff --git a/servers/physics_2d/space_2d_sw.cpp b/servers/physics_2d/space_2d_sw.cpp index 8a6d100671..9b2e586993 100644 --- a/servers/physics_2d/space_2d_sw.cpp +++ b/servers/physics_2d/space_2d_sw.cpp @@ -28,6 +28,7 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "space_2d_sw.h" + #include "collision_solver_2d_sw.h" #include "physics_2d_server_sw.h" @@ -575,21 +576,6 @@ bool Space2DSW::test_body_motion(Body2DSW *p_body, const Transform2D &p_from, co Vector2 a = sr[i * 2 + 0]; Vector2 b = sr[i * 2 + 1]; - -#if 0 - Vector2 rel = b-a; - real_t d = rel.length(); - if (d==0) - continue; - - Vector2 n = rel/d; - real_t traveled = n.dot(recover_motion); - a+=n*traveled; - - real_t d = a.distance_to(b); - if (d<margin) - continue; -#endif recover_motion += (b - a) * 0.4; } diff --git a/servers/physics_server.h b/servers/physics_server.h index 6336c54714..c6d312e0fe 100644 --- a/servers/physics_server.h +++ b/servers/physics_server.h @@ -96,6 +96,7 @@ class PhysicsShapeQueryParameters : public Reference { GDCLASS(PhysicsShapeQueryParameters, Reference); friend class PhysicsDirectSpaceState; + RID shape; Transform transform; float margin; @@ -133,8 +134,6 @@ class PhysicsDirectSpaceState : public Object { GDCLASS(PhysicsDirectSpaceState, Object); - //Variant _intersect_ray(const Vector3& p_from, const Vector3& p_to,const Vector<RID>& p_exclude=Vector<RID>(),uint32_t p_collision_mask=0); - //Variant _intersect_shape(const RID& p_shape, const Transform& p_xform,int p_result_max=64,const Vector<RID>& p_exclude=Vector<RID>(),uint32_t p_collision_mask=0); public: enum ObjectTypeMask { TYPE_MASK_STATIC_BODY = 1 << 0, @@ -625,37 +624,6 @@ public: virtual void generic_6dof_joint_set_flag(RID p_joint, Vector3::Axis, G6DOFJointAxisFlag p_flag, bool p_enable) = 0; virtual bool generic_6dof_joint_get_flag(RID p_joint, Vector3::Axis, G6DOFJointAxisFlag p_flag) = 0; -#if 0 - enum JointType { - - JOINT_PIN, - JOINT_GROOVE, - JOINT_DAMPED_SPRING - }; - - enum JointParam { - JOINT_PARAM_BIAS, - JOINT_PARAM_MAX_BIAS, - JOINT_PARAM_MAX_FORCE, - }; - - virtual void joint_set_param(RID p_joint, JointParam p_param, real_t p_value)=0; - virtual real_t joint_get_param(RID p_joint,JointParam p_param) const=0; - - virtual RID pin_joint_create(const Vector3& p_anchor,RID p_body_a,RID p_body_b=RID())=0; - virtual RID groove_joint_create(const Vector3& p_a_groove1,const Vector3& p_a_groove2, const Vector3& p_b_anchor, RID p_body_a,RID p_body_b)=0; - virtual RID damped_spring_joint_create(const Vector3& p_anchor_a,const Vector3& p_anchor_b,RID p_body_a,RID p_body_b=RID())=0; - - enum DampedStringParam { - DAMPED_STRING_REST_LENGTH, - DAMPED_STRING_STIFFNESS, - DAMPED_STRING_DAMPING - }; - virtual void damped_string_joint_set_param(RID p_joint, DampedStringParam p_param, real_t p_value)=0; - virtual real_t damped_string_joint_get_param(RID p_joint, DampedStringParam p_param) const=0; - - virtual JointType joint_get_type(RID p_joint) const=0; -#endif /* QUERY API */ enum AreaBodyStatus { @@ -703,7 +671,6 @@ VARIANT_ENUM_CAST(PhysicsServer::SliderJointParam); VARIANT_ENUM_CAST(PhysicsServer::ConeTwistJointParam); VARIANT_ENUM_CAST(PhysicsServer::G6DOFJointAxisParam); VARIANT_ENUM_CAST(PhysicsServer::G6DOFJointAxisFlag); -//VARIANT_ENUM_CAST( PhysicsServer::ObjectType ); VARIANT_ENUM_CAST(PhysicsServer::AreaBodyStatus); VARIANT_ENUM_CAST(PhysicsServer::ProcessInfo); diff --git a/servers/visual/rasterizer.cpp b/servers/visual/rasterizer.cpp index 77ec95ad8c..fd594fa3ee 100644 --- a/servers/visual/rasterizer.cpp +++ b/servers/visual/rasterizer.cpp @@ -28,6 +28,7 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "rasterizer.h" + #include "os/os.h" #include "print_string.h" @@ -44,614 +45,3 @@ RasterizerStorage::RasterizerStorage() { base_singleton = this; } - -#if 0 - -RID Rasterizer::create_default_material() { - - return material_create(); -} - - -/* Fixed MAterial SHADER API */ - -RID Rasterizer::_create_shader(const SpatialMaterialShaderKey& p_key) { - - ERR_FAIL_COND_V(!p_key.valid,RID()); - Map<SpatialMaterialShaderKey,SpatialMaterialShader>::Element *E=fixed_material_shaders.find(p_key); - - if (E) { - E->get().refcount++; - return E->get().shader; - } - - uint64_t t = OS::get_singleton()->get_ticks_usec(); - - SpatialMaterialShader fms; - fms.refcount=1; - fms.shader=shader_create(); - - //create shader code - - - int texcoords_used=0; - String code; - - static const char* _uv_str[4]={"UV","uv_xform","UV2","uv_sphere"}; -#define _TEXUVSTR(m_idx) String(_uv_str[(p_key.texcoord_mask >> (m_idx * 2)) & 0x3]) - - - if (p_key.use_pointsize) { - - code+="UV=POINT_COORD;\n"; - } - - - for(int i=0;i<VS::FIXED_MATERIAL_PARAM_MAX;i++) { - - if (p_key.texture_mask&(1<<i)) - texcoords_used|=(1<<((p_key.texcoord_mask>>(i*2))&0x3)); - } - - if (texcoords_used&(1<<VS::FIXED_MATERIAL_TEXCOORD_UV_TRANSFORM)) { - - code+="uniform mat4 fmp_uv_xform;\n"; - code+="vec2 uv_xform = (fmp_uv_xform * vec4(UV,0,1)).xy;\n"; - } - - /* HANDLE NORMAL MAPPING */ - - - if (p_key.texture_mask&(1<<VS::FIXED_MATERIAL_PARAM_NORMAL)) { - - String scode; - scode+="uniform float fmp_normal;\n"; - scode+="uniform texture fmp_normal_tex;\n"; - String uv_str; - if (((p_key.texcoord_mask>>(VS::FIXED_MATERIAL_PARAM_NORMAL*2))&0x3)==VS::FIXED_MATERIAL_TEXCOORD_SPHERE) { - uv_str="uv"; //sorry not supported - } else { - uv_str=_TEXUVSTR(VS::FIXED_MATERIAL_PARAM_NORMAL); - } - if (p_key.use_xy_normalmap) { - scode+="vec2 ywnormal=tex( fmp_normal_tex,"+uv_str+").wy * vec2(2.0,2.0) - vec2(1.0,1.0);\n"; - scode+="NORMALMAP=vec3(ywnormal,sqrt(1 - (ywnormal.x * ywnormal.x) - (ywnormal.y * ywnormal.y) ));\n"; - } else { - scode+="NORMALMAP=tex( fmp_normal_tex,"+uv_str+").xyz * vec3(2.0,2.0,1.0) - vec3(1.0,1.0,0.0);\n"; - } - scode+="NORMALMAP_DEPTH=fmp_normal;\n"; - - code+=scode; - } - - //handle sphere uv if used, do it here because it needs the normal, which may be transformed by a normal map - - if (texcoords_used&(1<<VS::FIXED_MATERIAL_TEXCOORD_SPHERE)) { - - String tcode; - tcode="vec3 eye_normal = normalize(VERTEX);\n"; - tcode+="vec3 ref = (eye_normal - 2.0*dot(NORMAL, eye_normal)*NORMAL);\n"; - tcode+="ref.z+=1.0;\n"; - tcode+="vec2 uv_sphere = ref.xy*vec2(0.5,0.0-0.5)+vec2(0.5,0.0-0.5);\n"; - code+=tcode; - } - - /* HANDLE DIFFUSE LIGHTING */ - - code+="uniform color fmp_diffuse;\n"; - code+="color diffuse=fmp_diffuse;\n"; - - if (p_key.use_color_array) - code+="diffuse*=COLOR;\n"; - - if (p_key.texture_mask&(1<<VS::FIXED_MATERIAL_PARAM_DIFFUSE)) { - - - code+="uniform texture fmp_diffuse_tex;\n"; - code+="diffuse*=tex( fmp_diffuse_tex,"+_TEXUVSTR(VS::FIXED_MATERIAL_PARAM_DIFFUSE)+");\n"; - } - - if (p_key.texture_mask&(1<<VS::FIXED_MATERIAL_PARAM_DETAIL)) { - - String dcode; - dcode+="uniform texture fmp_detail_tex;\n"; - dcode+="uniform float fmp_detail;\n"; - dcode+="color detail=tex( fmp_detail_tex,"+_TEXUVSTR(VS::FIXED_MATERIAL_PARAM_DETAIL)+");\n"; - //aways mix - dcode+="diffuse=vec4(mix(diffuse.rgb,detail.rgb,detail.a*fmp_detail),diffuse.a);\n"; - - code+=dcode; - } - - if (p_key.use_alpha) { - code+="DIFFUSE_ALPHA=diffuse;\n"; - if (p_key.discard_alpha) { - code+="DISCARD=diffuse.a<0.5;\n"; - } - } else { - code+="DIFFUSE=diffuse.rgb;\n"; - } - - /* HANDLE SPECULAR LIGHTING */ - - code+="uniform color fmp_specular;\n"; - code+="color specular=fmp_specular;\n"; - - if (p_key.texture_mask&(1<<VS::FIXED_MATERIAL_PARAM_SPECULAR)) { - - String scode; - scode+="uniform texture fmp_specular_tex;\n"; - scode+="specular*=tex( fmp_specular_tex,"+_TEXUVSTR(VS::FIXED_MATERIAL_PARAM_SPECULAR)+");\n"; - code+=scode; - } - - code+="SPECULAR=specular.rgb;\n"; - - code+="uniform float fmp_specular_exp;\n"; - code+="float specular_exp=fmp_specular_exp;\n"; - - if (p_key.texture_mask&(1<<VS::FIXED_MATERIAL_PARAM_SPECULAR_EXP)) { - - String scode; - scode+="uniform texture fmp_specular_exp_tex;\n"; - scode+="specular_exp*=tex( fmp_specular_exp_tex,"+_TEXUVSTR(VS::FIXED_MATERIAL_PARAM_SPECULAR_EXP)+").r;\n"; - code+=scode; - } - - code+="SPEC_EXP=specular_exp;\n"; - - /* HANDLE EMISSION LIGHTING */ - - code+="uniform color fmp_emission;\n"; - code+="color emission=fmp_emission;\n"; - - if (p_key.texture_mask&(1<<VS::FIXED_MATERIAL_PARAM_EMISSION)) { - - String scode; - scode+="uniform texture fmp_emission_tex;\n"; - scode+="emission*=tex( fmp_emission_tex,"+_TEXUVSTR(VS::FIXED_MATERIAL_PARAM_EMISSION)+");\n"; - code+=scode; - } - - code+="EMISSION=emission.rgb;\n"; - - - /* HANDLE GLOW */ - - code+="uniform float fmp_glow;\n"; - code+="float glow=fmp_glow;\n"; - - if (p_key.texture_mask&(1<<VS::FIXED_MATERIAL_PARAM_GLOW)) { - - String scode; - scode+="uniform texture fmp_glow_tex;\n"; - scode+="glow*=tex( fmp_glow_tex,"+_TEXUVSTR(VS::FIXED_MATERIAL_PARAM_GLOW)+").r;\n"; - code+=scode; - } - - code+="GLOW=glow;\n"; - - if (p_key.texture_mask&(1<<VS::FIXED_MATERIAL_PARAM_SHADE_PARAM)) { - - String scode; - scode+="uniform texture fmp_shade_param_tex;\n"; - scode+="SHADE_PARAM=tex( fmp_shade_param_tex,"+_TEXUVSTR(VS::FIXED_MATERIAL_PARAM_SHADE_PARAM)+").r;\n"; - code+=scode; - } else { - - String scode; - scode+="uniform float fmp_shade_param;\n"; - scode+="SHADE_PARAM=fmp_shade_param;\n"; - code+=scode; - - } - - - //print_line("**FRAGMENT SHADER GENERATED code: \n"+code); - - String vcode; - vcode="uniform float "+_fixed_material_param_names[VS::FIXED_MATERIAL_PARAM_SPECULAR_EXP]+";\n"; - vcode+="SPEC_EXP="+_fixed_material_param_names[VS::FIXED_MATERIAL_PARAM_SPECULAR_EXP]+";\n"; - if (p_key.use_pointsize) { - - vcode+="uniform float "+_fixed_material_point_size_name+";\n"; - vcode+="POINT_SIZE="+_fixed_material_point_size_name+";\n"; - //vcode+="POINT_SIZE=10.0;\n"; - } - - String lcode; - - switch(p_key.light_shader) { - - case VS::FIXED_MATERIAL_LIGHT_SHADER_LAMBERT: { - //do nothing - - } break; - case VS::FIXED_MATERIAL_LIGHT_SHADER_WRAP: { - - lcode+="float NdotL = max(0.0,((dot( NORMAL, LIGHT_DIR )+SHADE_PARAM)/(1.0+SHADE_PARAM)));"; - lcode+="vec3 half_vec = normalize(LIGHT_DIR + EYE_VEC);"; - lcode+="float eye_light = max(dot(NORMAL, half_vec),0.0);"; - lcode+="LIGHT = LIGHT_DIFFUSE * DIFFUSE * NdotL;"; - lcode+="if (NdotL > 0.0) {"; - lcode+="\tLIGHT+=LIGHT_SPECULAR * SPECULAR * pow( eye_light, SPECULAR_EXP );"; - lcode+="};"; - - } break; - case VS::FIXED_MATERIAL_LIGHT_SHADER_VELVET: { - lcode+="float NdotL = max(0.0,dot( NORMAL, LIGHT_DIR ));"; - lcode+="vec3 half_vec = normalize(LIGHT_DIR + EYE_VEC);"; - lcode+="float eye_light = max(dot(NORMAL, half_vec),0.0);"; - lcode+="LIGHT = LIGHT_DIFFUSE * DIFFUSE * NdotL;"; - lcode+="float rim = (1.0-abs(dot(NORMAL,vec3(0,0,1))))*SHADE_PARAM;"; - lcode+="LIGHT += LIGHT_DIFFUSE * DIFFUSE * rim;"; - lcode+="if (NdotL > 0.0) {"; - lcode+="\tLIGHT+=LIGHT_SPECULAR * SPECULAR * pow( eye_light, SPECULAR_EXP );"; - lcode+="};"; - - - } break; - case VS::FIXED_MATERIAL_LIGHT_SHADER_TOON: { - - lcode+="float NdotL = dot( NORMAL, LIGHT_DIR );"; - lcode+="vec3 light_ref = reflect( LIGHT_DIR, NORMAL );"; - lcode+="float eye_light = clamp( dot( light_ref, vec3(0,0,0)-EYE_VEC), 0.0, 1.0 );"; - lcode+="float NdotL_diffuse = smoothstep( max( SHADE_PARAM-0.05, 0.0-1.0), min( SHADE_PARAM+0.05, 1.0), NdotL );"; - lcode+="float spec_radius=clamp((1.0-(SPECULAR_EXP/64.0)),0.0,1.0);"; - lcode+="float NdotL_specular = smoothstep( max( spec_radius-0.05, 0.0), min( spec_radius+0.05, 1.0), eye_light )*max(NdotL,0);"; - lcode+="LIGHT = NdotL_diffuse * LIGHT_DIFFUSE*DIFFUSE + NdotL_specular * LIGHT_SPECULAR*SPECULAR;"; - - } break; - - } - - //print_line("**VERTEX SHADER GENERATED code: \n"+vcode); - - shader_set_code(fms.shader,vcode,code,lcode,0,0); - - fixed_material_shaders[p_key]=fms; - return fms.shader; -} - -void Rasterizer::_free_shader(const SpatialMaterialShaderKey& p_key) { - - if (p_key.valid==0) - return; //not a valid key - - Map<SpatialMaterialShaderKey,SpatialMaterialShader>::Element *E=fixed_material_shaders.find(p_key); - - ERR_FAIL_COND(!E); - E->get().refcount--; - if (E->get().refcount==0) { - free(E->get().shader); - fixed_material_shaders.erase(E); - } - -} - - -void Rasterizer::fixed_material_set_flag(RID p_material, VS::SpatialMaterialFlags p_flag, bool p_enabled) { - - - Map<RID,SpatialMaterial*>::Element *E = fixed_materials.find(p_material); - ERR_FAIL_COND(!E); - SpatialMaterial &fm=*E->get(); - - switch(p_flag) { - - case VS::FIXED_MATERIAL_FLAG_USE_ALPHA: fm.use_alpha=p_enabled; break; - case VS::FIXED_MATERIAL_FLAG_USE_COLOR_ARRAY: fm.use_color_array=p_enabled; break; - case VS::FIXED_MATERIAL_FLAG_USE_POINT_SIZE: fm.use_pointsize=p_enabled; break; - case VS::FIXED_MATERIAL_FLAG_DISCARD_ALPHA: fm.discard_alpha=p_enabled; break; - case VS::FIXED_MATERIAL_FLAG_USE_XY_NORMALMAP: fm.use_xy_normalmap=p_enabled; break; - } - - if (!fm.dirty_list.in_list()) - fixed_material_dirty_list.add( &fm.dirty_list ); - -} - -bool Rasterizer::fixed_material_get_flag(RID p_material, VS::SpatialMaterialFlags p_flag) const{ - - const Map<RID,SpatialMaterial*>::Element *E = fixed_materials.find(p_material); - ERR_FAIL_COND_V(!E,false); - const SpatialMaterial &fm=*E->get(); - switch(p_flag) { - - case VS::FIXED_MATERIAL_FLAG_USE_ALPHA: return fm.use_alpha;; break; - case VS::FIXED_MATERIAL_FLAG_USE_COLOR_ARRAY: return fm.use_color_array;; break; - case VS::FIXED_MATERIAL_FLAG_USE_POINT_SIZE: return fm.use_pointsize;; break; - case VS::FIXED_MATERIAL_FLAG_DISCARD_ALPHA: return fm.discard_alpha;; break; - case VS::FIXED_MATERIAL_FLAG_USE_XY_NORMALMAP: return fm.use_xy_normalmap;; break; - - } - - - return false; -} - - -RID Rasterizer::fixed_material_create() { - - RID mat = material_create(); - fixed_materials[mat]=memnew( SpatialMaterial() ); - SpatialMaterial &fm=*fixed_materials[mat]; - fm.self=mat; - fm.get_key(); - material_set_flag(mat,VS::MATERIAL_FLAG_COLOR_ARRAY_SRGB,true); - for(int i=0;i<VS::FIXED_MATERIAL_PARAM_MAX;i++) { - - material_set_param(mat,_fixed_material_param_names[i],fm.param[i]); //must be there - } - fixed_material_dirty_list.add(&fm.dirty_list); - //print_line("FMC: "+itos(mat.get_id())); - return mat; -} - - - - -void Rasterizer::fixed_material_set_parameter(RID p_material, VS::SpatialMaterialParam p_parameter, const Variant& p_value){ - - Map<RID,SpatialMaterial*>::Element *E = fixed_materials.find(p_material); - ERR_FAIL_COND(!E); - SpatialMaterial &fm=*E->get(); - RID material=E->key(); - ERR_FAIL_INDEX(p_parameter,VS::FIXED_MATERIAL_PARAM_MAX); - - if ((p_parameter==VS::FIXED_MATERIAL_PARAM_DIFFUSE || p_parameter==VS::FIXED_MATERIAL_PARAM_SPECULAR || p_parameter==VS::FIXED_MATERIAL_PARAM_EMISSION)) { - - if (p_value.get_type()!=Variant::COLOR) { - ERR_EXPLAIN(String(_fixed_material_param_names[p_parameter])+" expects Color"); - ERR_FAIL(); - } - } else { - - if (!p_value.is_num()) { - ERR_EXPLAIN(String(_fixed_material_param_names[p_parameter])+" expects scalar"); - ERR_FAIL(); - } - } - - fm.param[p_parameter]=p_value; - VS::get_singleton()->material_set_param(material,_fixed_material_param_names[p_parameter],p_value); - - -} -Variant Rasterizer::fixed_material_get_parameter(RID p_material,VS::SpatialMaterialParam p_parameter) const{ - - const Map<RID,SpatialMaterial*>::Element *E = fixed_materials.find(p_material); - ERR_FAIL_COND_V(!E,Variant()); - const SpatialMaterial &fm=*E->get(); - ERR_FAIL_INDEX_V(p_parameter,VS::FIXED_MATERIAL_PARAM_MAX,Variant()); - return fm.param[p_parameter]; -} - -void Rasterizer::fixed_material_set_texture(RID p_material,VS::SpatialMaterialParam p_parameter, RID p_texture){ - - Map<RID,SpatialMaterial*>::Element *E = fixed_materials.find(p_material); - if (!E) { - - print_line("Not found: "+itos(p_material.get_id())); - } - ERR_FAIL_COND(!E); - SpatialMaterial &fm=*E->get(); - - - ERR_FAIL_INDEX(p_parameter,VS::FIXED_MATERIAL_PARAM_MAX); - RID material=E->key(); - fm.texture[p_parameter]=p_texture; - VS::get_singleton()->material_set_param(material,_fixed_material_tex_names[p_parameter],p_texture); - - if (!fm.dirty_list.in_list()) - fixed_material_dirty_list.add( &fm.dirty_list ); - - - - -} -RID Rasterizer::fixed_material_get_texture(RID p_material,VS::SpatialMaterialParam p_parameter) const{ - - const Map<RID,SpatialMaterial*>::Element *E = fixed_materials.find(p_material); - ERR_FAIL_COND_V(!E,RID()); - const SpatialMaterial &fm=*E->get(); - ERR_FAIL_INDEX_V(p_parameter,VS::FIXED_MATERIAL_PARAM_MAX,RID()); - - return fm.texture[p_parameter]; -} - - -void Rasterizer::fixed_material_set_texcoord_mode(RID p_material,VS::SpatialMaterialParam p_parameter, VS::SpatialMaterialTexCoordMode p_mode) { - - Map<RID,SpatialMaterial*>::Element *E = fixed_materials.find(p_material); - ERR_FAIL_COND(!E); - SpatialMaterial &fm=*E->get(); - ERR_FAIL_INDEX(p_parameter,VS::FIXED_MATERIAL_PARAM_MAX); - - fm.get_key(); - - fm.texture_tc[p_parameter]=p_mode; - - if (!fm.dirty_list.in_list()) - fixed_material_dirty_list.add( &fm.dirty_list ); - -} - -VS::SpatialMaterialTexCoordMode Rasterizer::fixed_material_get_texcoord_mode(RID p_material,VS::SpatialMaterialParam p_parameter) const { - - const Map<RID,SpatialMaterial*>::Element *E = fixed_materials.find(p_material); - ERR_FAIL_COND_V(!E,VS::FIXED_MATERIAL_TEXCOORD_UV); - const SpatialMaterial &fm=*E->get(); - ERR_FAIL_INDEX_V(p_parameter,VS::FIXED_MATERIAL_PARAM_MAX,VS::FIXED_MATERIAL_TEXCOORD_UV); - - return fm.texture_tc[p_parameter]; -} - -void Rasterizer::fixed_material_set_uv_transform(RID p_material,const Transform& p_transform) { - - Map<RID,SpatialMaterial*>::Element *E = fixed_materials.find(p_material); - ERR_FAIL_COND(!E); - SpatialMaterial &fm=*E->get(); - RID material=E->key(); - - VS::get_singleton()->material_set_param(material,_fixed_material_uv_xform_name,p_transform); - - fm.uv_xform=p_transform; - -} - - - -Transform Rasterizer::fixed_material_get_uv_transform(RID p_material) const { - - const Map<RID,SpatialMaterial*>::Element *E = fixed_materials.find(p_material); - ERR_FAIL_COND_V(!E,Transform()); - const SpatialMaterial &fm=*E->get(); - - return fm.uv_xform; -} - -void Rasterizer::fixed_material_set_light_shader(RID p_material,VS::SpatialMaterialLightShader p_shader) { - - Map<RID,SpatialMaterial*>::Element *E = fixed_materials.find(p_material); - ERR_FAIL_COND(!E); - SpatialMaterial &fm=*E->get(); - - fm.light_shader=p_shader; - - if (!fm.dirty_list.in_list()) - fixed_material_dirty_list.add( &fm.dirty_list ); - -} - -VS::SpatialMaterialLightShader Rasterizer::fixed_material_get_light_shader(RID p_material) const { - - const Map<RID,SpatialMaterial*>::Element *E = fixed_materials.find(p_material); - ERR_FAIL_COND_V(!E,VS::FIXED_MATERIAL_LIGHT_SHADER_LAMBERT); - const SpatialMaterial &fm=*E->get(); - - return fm.light_shader; -} - -void Rasterizer::fixed_material_set_point_size(RID p_material,float p_size) { - - Map<RID,SpatialMaterial*>::Element *E = fixed_materials.find(p_material); - ERR_FAIL_COND(!E); - SpatialMaterial &fm=*E->get(); - RID material=E->key(); - - VS::get_singleton()->material_set_param(material,_fixed_material_point_size_name,p_size); - - fm.point_size=p_size; - - -} - -float Rasterizer::fixed_material_get_point_size(RID p_material) const{ - - const Map<RID,SpatialMaterial*>::Element *E = fixed_materials.find(p_material); - ERR_FAIL_COND_V(!E,1.0); - const SpatialMaterial &fm=*E->get(); - - return fm.point_size; - -} - -void Rasterizer::_update_fixed_materials() { - - - while(fixed_material_dirty_list.first()) { - - SpatialMaterial &fm=*fixed_material_dirty_list.first()->self(); - - SpatialMaterialShaderKey new_key = fm.get_key(); - if (new_key.key!=fm.current_key.key) { - - _free_shader(fm.current_key); - RID new_rid = _create_shader(new_key); - fm.current_key=new_key; - material_set_shader(fm.self,new_rid); - - if (fm.texture[VS::FIXED_MATERIAL_PARAM_DETAIL].is_valid()) { - //send these again just in case. - material_set_param(fm.self,_fixed_material_param_names[VS::FIXED_MATERIAL_PARAM_DETAIL],fm.param[VS::FIXED_MATERIAL_PARAM_DETAIL]); - } - if (fm.texture[VS::FIXED_MATERIAL_PARAM_NORMAL].is_valid()) { - //send these again just in case. - material_set_param(fm.self,_fixed_material_param_names[VS::FIXED_MATERIAL_PARAM_NORMAL],fm.param[VS::FIXED_MATERIAL_PARAM_NORMAL]); - } - - material_set_param(fm.self,_fixed_material_uv_xform_name,fm.uv_xform); - if (fm.use_pointsize) { - material_set_param(fm.self,_fixed_material_point_size_name,fm.point_size); - } - } - - fixed_material_dirty_list.remove(fixed_material_dirty_list.first()); - } -} - - -void Rasterizer::_free_fixed_material(const RID& p_material) { - - Map<RID,SpatialMaterial*>::Element *E = fixed_materials.find(p_material); - - if (E) { - - _free_shader(E->get()->current_key); //free shader - if (E->get()->dirty_list.in_list()) - fixed_material_dirty_list.remove( &E->get()->dirty_list); - memdelete(E->get()); - fixed_materials.erase(E); //free material - } - - -} - - -void Rasterizer::flush_frame() { - - //not really necessary to implement -} - -Rasterizer::Rasterizer() { - - static const char* fm_names[VS::FIXED_MATERIAL_PARAM_MAX]={ - "diffuse", - "detail", - "specular", - "emission", - "specular_exp", - "glow", - "normal", - "shade_param"}; - - for(int i=0;i<VS::FIXED_MATERIAL_PARAM_MAX;i++) { - - _fixed_material_param_names[i]=String("fmp_")+fm_names[i]; - _fixed_material_tex_names[i]=String("fmp_")+fm_names[i]+"_tex"; - } - - _fixed_material_uv_xform_name="fmp_uv_xform"; - _fixed_material_point_size_name="fmp_point_size"; - - draw_viewport_func=NULL; - - ERR_FAIL_COND( sizeof(SpatialMaterialShaderKey)!=4); - -} - -RID Rasterizer::create_overdraw_debug_material() { - RID mat = fixed_material_create(); - fixed_material_set_parameter( mat,VisualServer::FIXED_MATERIAL_PARAM_SPECULAR,Color(0,0,0) ); - fixed_material_set_parameter( mat,VisualServer::FIXED_MATERIAL_PARAM_DIFFUSE,Color(0.1,0.1,0.2) ); - fixed_material_set_parameter( mat,VisualServer::FIXED_MATERIAL_PARAM_EMISSION,Color(0,0,0) ); - fixed_material_set_flag( mat, VS::FIXED_MATERIAL_FLAG_USE_ALPHA, true); - material_set_flag( mat, VisualServer::MATERIAL_FLAG_UNSHADED, true ); - material_set_blend_mode( mat,VisualServer::MATERIAL_BLEND_MODE_ADD ); - - - return mat; -} - -#endif diff --git a/servers/visual/rasterizer.h b/servers/visual/rasterizer.h index d4d8b6cdaa..9e4acac25d 100644 --- a/servers/visual/rasterizer.h +++ b/servers/visual/rasterizer.h @@ -101,10 +101,7 @@ public: Vector<float> blend_values; - //BakedLightData *baked_light; VS::ShadowCastingSetting cast_shadows; - //Transform *baked_light_octree_xform; - //int baked_lightmap_id; bool mirror : 8; bool receive_shadows : 8; @@ -114,8 +111,6 @@ public: float depth; //used for sorting SelfList<InstanceBase> dependency_item; - //InstanceBase *baked_light; //baked light to use - //SelfList<InstanceBase> baked_light_item; virtual void base_removed() = 0; virtual void base_changed() = 0; @@ -1029,1026 +1024,4 @@ public: virtual ~Rasterizer() {} }; -#if 0 -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ -#include "camera_matrix.h" -#include "map.h" -#include "self_list.h" -#include "servers/visual_server.h" - -class Rasterizer { -protected: - - - typedef void (*ItemDrawViewportFunc)(VisualServer*owner,void*ud,const Rect2& p_rect); - - RID create_default_material(); - RID create_overdraw_debug_material(); - - - /* Fixed Material Shader API */ - - union SpatialMaterialShaderKey { - - struct { - uint16_t texcoord_mask; - uint8_t texture_mask; - uint8_t light_shader:2; - bool use_alpha:1; - bool use_color_array:1; - bool use_pointsize:1; - bool discard_alpha:1; - bool use_xy_normalmap:1; - bool valid:1; - }; - - uint32_t key; - - _FORCE_INLINE_ bool operator<(const SpatialMaterialShaderKey& p_key) const { return key<p_key.key; } - }; - - struct SpatialMaterialShader { - - int refcount; - RID shader; - }; - - Map<SpatialMaterialShaderKey,SpatialMaterialShader> fixed_material_shaders; - - RID _create_shader(const SpatialMaterialShaderKey& p_key); - void _free_shader(const SpatialMaterialShaderKey& p_key); - - struct SpatialMaterial { - - - RID self; - bool use_alpha; - bool use_color_array; - bool discard_alpha; - bool use_pointsize; - bool use_xy_normalmap; - float point_size; - Transform uv_xform; - VS::SpatialMaterialLightShader light_shader; - RID texture[VS::FIXED_MATERIAL_PARAM_MAX]; - Variant param[VS::FIXED_MATERIAL_PARAM_MAX]; - VS::SpatialMaterialTexCoordMode texture_tc[VS::FIXED_MATERIAL_PARAM_MAX]; - - SelfList<SpatialMaterial> dirty_list; - - SpatialMaterialShaderKey current_key; - - _FORCE_INLINE_ SpatialMaterialShaderKey get_key() const { - - - SpatialMaterialShaderKey k; - k.key=0; - k.use_alpha=use_alpha; - k.use_color_array=use_color_array; - k.use_pointsize=use_pointsize; - k.use_xy_normalmap=use_xy_normalmap; - k.discard_alpha=discard_alpha; - k.light_shader=light_shader; - k.valid=true; - for(int i=0;i<VS::FIXED_MATERIAL_PARAM_MAX;i++) { - if (texture[i].is_valid()) { - //print_line("valid: "+itos(i)); - k.texture_mask|=(1<<i); - k.texcoord_mask|=(texture_tc[i])<<(i*2); - } - } - - return k; - } - - - SpatialMaterial() : dirty_list(this) { - - use_alpha=false; - use_color_array=false; - use_pointsize=false; - discard_alpha=false; - use_xy_normalmap=false; - point_size=1.0; - light_shader=VS::FIXED_MATERIAL_LIGHT_SHADER_LAMBERT; - for(int i=0;i<VS::FIXED_MATERIAL_PARAM_MAX;i++) { - texture_tc[i]=VS::FIXED_MATERIAL_TEXCOORD_UV; - } - param[VS::FIXED_MATERIAL_PARAM_DIFFUSE]=Color(1,1,1); - param[VS::FIXED_MATERIAL_PARAM_DETAIL]=1.0; - param[VS::FIXED_MATERIAL_PARAM_EMISSION]=Color(0,0,0); - param[VS::FIXED_MATERIAL_PARAM_GLOW]=0; - param[VS::FIXED_MATERIAL_PARAM_SHADE_PARAM]=0; - param[VS::FIXED_MATERIAL_PARAM_SPECULAR]=Color(0.0,0.0,0.0); - param[VS::FIXED_MATERIAL_PARAM_SPECULAR_EXP]=40; - param[VS::FIXED_MATERIAL_PARAM_NORMAL]=1; - - current_key.key=0; - - - } - }; - - StringName _fixed_material_param_names[VS::FIXED_MATERIAL_PARAM_MAX]; - StringName _fixed_material_tex_names[VS::FIXED_MATERIAL_PARAM_MAX]; - StringName _fixed_material_uv_xform_name; - StringName _fixed_material_point_size_name; - - Map<RID,SpatialMaterial*> fixed_materials; - - SelfList<SpatialMaterial>::List fixed_material_dirty_list; - -protected: - void _update_fixed_materials(); - void _free_fixed_material(const RID& p_material); - -public: - - enum ShadowFilterTechnique { - SHADOW_FILTER_NONE, - SHADOW_FILTER_PCF5, - SHADOW_FILTER_PCF13, - SHADOW_FILTER_ESM, - SHADOW_FILTER_VSM, - }; - - - - /* TEXTURE API */ - - virtual RID texture_create()=0; - RID texture_create_from_image(const Image& p_image,uint32_t p_flags=VS::TEXTURE_FLAGS_DEFAULT); // helper - virtual void texture_allocate(RID p_texture,int p_width, int p_height,Image::Format p_format,uint32_t p_flags=VS::TEXTURE_FLAGS_DEFAULT)=0; - virtual void texture_set_data(RID p_texture,const Image& p_image,VS::CubeMapSide p_cube_side=VS::CUBEMAP_LEFT)=0; - virtual Image texture_get_data(RID p_texture,VS::CubeMapSide p_cube_side=VS::CUBEMAP_LEFT) const=0; - virtual void texture_set_flags(RID p_texture,uint32_t p_flags)=0; - virtual uint32_t texture_get_flags(RID p_texture) const=0; - virtual Image::Format texture_get_format(RID p_texture) const=0; - virtual uint32_t texture_get_width(RID p_texture) const=0; - virtual uint32_t texture_get_height(RID p_texture) const=0; - virtual bool texture_has_alpha(RID p_texture) const=0; - virtual void texture_set_size_override(RID p_texture,int p_width, int p_height)=0; - - virtual void texture_set_reload_hook(RID p_texture,ObjectID p_owner,const StringName& p_function) const=0; - - virtual void texture_set_path(RID p_texture,const String& p_path)=0; - virtual String texture_get_path(RID p_texture) const=0; - virtual void texture_debug_usage(List<VS::TextureInfo> *r_info)=0; - - virtual void texture_set_shrink_all_x2_on_set_data(bool p_enable)=0; - - /* SHADER API */ - - virtual RID shader_create(VS::ShaderMode p_mode=VS::SHADER_MATERIAL)=0; - - virtual void shader_set_mode(RID p_shader,VS::ShaderMode p_mode)=0; - virtual VS::ShaderMode shader_get_mode(RID p_shader) const=0; - - virtual void shader_set_code(RID p_shader, const String& p_vertex, const String& p_fragment,const String& p_light,int p_vertex_ofs=0,int p_fragment_ofs=0,int p_light_ofs=0)=0; - virtual String shader_get_fragment_code(RID p_shader) const=0; - virtual String shader_get_vertex_code(RID p_shader) const=0; - virtual String shader_get_light_code(RID p_shader) const=0; - - virtual void shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const=0; - - virtual void shader_set_default_texture_param(RID p_shader, const StringName& p_name, RID p_texture)=0; - virtual RID shader_get_default_texture_param(RID p_shader, const StringName& p_name) const=0; - - virtual Variant shader_get_default_param(RID p_shader, const StringName& p_name)=0; - - /* COMMON MATERIAL API */ - - virtual RID material_create()=0; - - virtual void material_set_shader(RID p_shader_material, RID p_shader)=0; - virtual RID material_get_shader(RID p_shader_material) const=0; - - virtual void material_set_param(RID p_material, const StringName& p_param, const Variant& p_value)=0; - virtual Variant material_get_param(RID p_material, const StringName& p_param) const=0; - - virtual void material_set_flag(RID p_material, VS::MaterialFlag p_flag,bool p_enabled)=0; - virtual bool material_get_flag(RID p_material,VS::MaterialFlag p_flag) const=0; - - virtual void material_set_depth_draw_mode(RID p_material, VS::MaterialDepthDrawMode p_mode)=0; - virtual VS::MaterialDepthDrawMode material_get_depth_draw_mode(RID p_material) const=0; - - virtual void material_set_blend_mode(RID p_material,VS::MaterialBlendMode p_mode)=0; - virtual VS::MaterialBlendMode material_get_blend_mode(RID p_material) const=0; - - virtual void material_set_line_width(RID p_material,float p_line_width)=0; - virtual float material_get_line_width(RID p_material) const=0; - - - /* FIXED MATERIAL */ - - virtual RID fixed_material_create(); - - virtual void fixed_material_set_flag(RID p_material, VS::SpatialMaterialFlags p_flag, bool p_enabled); - virtual bool fixed_material_get_flag(RID p_material, VS::SpatialMaterialFlags p_flag) const; - - virtual void fixed_material_set_parameter(RID p_material, VS::SpatialMaterialParam p_parameter, const Variant& p_value); - virtual Variant fixed_material_get_parameter(RID p_material,VS::SpatialMaterialParam p_parameter) const; - - virtual void fixed_material_set_texture(RID p_material,VS::SpatialMaterialParam p_parameter, RID p_texture); - virtual RID fixed_material_get_texture(RID p_material,VS::SpatialMaterialParam p_parameter) const; - - virtual void fixed_material_set_texcoord_mode(RID p_material,VS::SpatialMaterialParam p_parameter, VS::SpatialMaterialTexCoordMode p_mode); - virtual VS::SpatialMaterialTexCoordMode fixed_material_get_texcoord_mode(RID p_material,VS::SpatialMaterialParam p_parameter) const; - - virtual void fixed_material_set_uv_transform(RID p_material,const Transform& p_transform); - virtual Transform fixed_material_get_uv_transform(RID p_material) const; - - virtual void fixed_material_set_light_shader(RID p_material,VS::SpatialMaterialLightShader p_shader); - virtual VS::SpatialMaterialLightShader fixed_material_get_light_shader(RID p_material) const; - - virtual void fixed_material_set_point_size(RID p_material,float p_size); - virtual float fixed_material_get_point_size(RID p_material) const; - - /* MESH API */ - - virtual RID mesh_create()=0; - - - virtual void mesh_add_surface(RID p_mesh,VS::PrimitiveType p_primitive,const Array& p_arrays,const Array& p_blend_shapes=Array(),bool p_alpha_sort=false)=0; - virtual Array mesh_get_surface_arrays(RID p_mesh,int p_surface) const=0; - virtual Array mesh_get_surface_morph_arrays(RID p_mesh,int p_surface) const=0; - - virtual void mesh_add_custom_surface(RID p_mesh,const Variant& p_dat)=0; - - virtual void mesh_set_morph_target_count(RID p_mesh,int p_amount)=0; - virtual int mesh_get_morph_target_count(RID p_mesh) const=0; - - virtual void mesh_set_morph_target_mode(RID p_mesh,VS::MorphTargetMode p_mode)=0; - virtual VS::MorphTargetMode mesh_get_morph_target_mode(RID p_mesh) const=0; - - virtual void mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material,bool p_owned=false)=0; - virtual RID mesh_surface_get_material(RID p_mesh, int p_surface) const=0; - - virtual int mesh_surface_get_array_len(RID p_mesh, int p_surface) const=0; - virtual int mesh_surface_get_array_index_len(RID p_mesh, int p_surface) const=0; - virtual uint32_t mesh_surface_get_format(RID p_mesh, int p_surface) const=0; - virtual VS::PrimitiveType mesh_surface_get_primitive_type(RID p_mesh, int p_surface) const=0; - - virtual void mesh_remove_surface(RID p_mesh,int p_index)=0; - virtual int mesh_get_surface_count(RID p_mesh) const=0; - - virtual AABB mesh_get_aabb(RID p_mesh,RID p_skeleton=RID()) const=0; - - virtual void mesh_set_custom_aabb(RID p_mesh,const AABB& p_aabb)=0; - virtual AABB mesh_get_custom_aabb(RID p_mesh) const=0; - - /* MULTIMESH API */ - - virtual RID multimesh_create()=0; - - virtual void multimesh_set_instance_count(RID p_multimesh,int p_count)=0; - virtual int multimesh_get_instance_count(RID p_multimesh) const=0; - - virtual void multimesh_set_mesh(RID p_multimesh,RID p_mesh)=0; - virtual void multimesh_set_aabb(RID p_multimesh,const AABB& p_aabb)=0; - virtual void multimesh_instance_set_transform(RID p_multimesh,int p_index,const Transform& p_transform)=0; - virtual void multimesh_instance_set_color(RID p_multimesh,int p_index,const Color& p_color)=0; - - virtual RID multimesh_get_mesh(RID p_multimesh) const=0; - virtual AABB multimesh_get_aabb(RID p_multimesh) const=0; - - virtual Transform multimesh_instance_get_transform(RID p_multimesh,int p_index) const=0; - virtual Color multimesh_instance_get_color(RID p_multimesh,int p_index) const=0; - - virtual void multimesh_set_visible_instances(RID p_multimesh,int p_visible)=0; - virtual int multimesh_get_visible_instances(RID p_multimesh) const=0; - - /* BAKED LIGHT */ - - - - - /* IMMEDIATE API */ - - virtual RID immediate_create()=0; - virtual void immediate_begin(RID p_immediate,VS::PrimitiveType p_rimitive,RID p_texture=RID())=0; - virtual void immediate_vertex(RID p_immediate,const Vector3& p_vertex)=0; - virtual void immediate_normal(RID p_immediate,const Vector3& p_normal)=0; - virtual void immediate_tangent(RID p_immediate,const Plane& p_tangent)=0; - virtual void immediate_color(RID p_immediate,const Color& p_color)=0; - virtual void immediate_uv(RID p_immediate,const Vector2& tex_uv)=0; - virtual void immediate_uv2(RID p_immediate,const Vector2& tex_uv)=0; - virtual void immediate_end(RID p_immediate)=0; - virtual void immediate_clear(RID p_immediate)=0; - virtual AABB immediate_get_aabb(RID p_immediate) const=0; - virtual void immediate_set_material(RID p_immediate,RID p_material)=0; - virtual RID immediate_get_material(RID p_immediate) const=0; - - - /* PARTICLES API */ - - virtual RID particles_create()=0; - - virtual void particles_set_amount(RID p_particles, int p_amount)=0; - virtual int particles_get_amount(RID p_particles) const=0; - - virtual void particles_set_emitting(RID p_particles, bool p_emitting)=0; - virtual bool particles_is_emitting(RID p_particles) const=0; - - virtual void particles_set_visibility_aabb(RID p_particles, const AABB& p_visibility)=0; - virtual AABB particles_get_visibility_aabb(RID p_particles) const=0; - - virtual void particles_set_emission_half_extents(RID p_particles, const Vector3& p_half_extents)=0; - virtual Vector3 particles_get_emission_half_extents(RID p_particles) const=0; - - virtual void particles_set_emission_base_velocity(RID p_particles, const Vector3& p_base_velocity)=0; - virtual Vector3 particles_get_emission_base_velocity(RID p_particles) const=0; - - virtual void particles_set_emission_points(RID p_particles, const PoolVector<Vector3>& p_points)=0; - virtual PoolVector<Vector3> particles_get_emission_points(RID p_particles) const=0; - - virtual void particles_set_gravity_normal(RID p_particles, const Vector3& p_normal)=0; - virtual Vector3 particles_get_gravity_normal(RID p_particles) const=0; - - virtual void particles_set_variable(RID p_particles, VS::ParticleVariable p_variable,float p_value)=0; - virtual float particles_get_variable(RID p_particles, VS::ParticleVariable p_variable) const=0; - - virtual void particles_set_randomness(RID p_particles, VS::ParticleVariable p_variable,float p_randomness)=0; - virtual float particles_get_randomness(RID p_particles, VS::ParticleVariable p_variable) const=0; - - virtual void particles_set_color_phase_pos(RID p_particles, int p_phase, float p_pos)=0; - virtual float particles_get_color_phase_pos(RID p_particles, int p_phase) const=0; - - virtual void particles_set_color_phases(RID p_particles, int p_phases)=0; - virtual int particles_get_color_phases(RID p_particles) const=0; - - virtual void particles_set_color_phase_color(RID p_particles, int p_phase, const Color& p_color)=0; - virtual Color particles_get_color_phase_color(RID p_particles, int p_phase) const=0; - - virtual void particles_set_attractors(RID p_particles, int p_attractors)=0; - virtual int particles_get_attractors(RID p_particles) const=0; - - virtual void particles_set_attractor_pos(RID p_particles, int p_attractor, const Vector3& p_pos)=0; - virtual Vector3 particles_get_attractor_pos(RID p_particles,int p_attractor) const=0; - - virtual void particles_set_attractor_strength(RID p_particles, int p_attractor, float p_force)=0; - virtual float particles_get_attractor_strength(RID p_particles,int p_attractor) const=0; - - virtual void particles_set_material(RID p_particles, RID p_material,bool p_owned=false)=0; - virtual RID particles_get_material(RID p_particles) const=0; - - virtual AABB particles_get_aabb(RID p_particles) const=0; - - virtual void particles_set_height_from_velocity(RID p_particles, bool p_enable)=0; - virtual bool particles_has_height_from_velocity(RID p_particles) const=0; - - virtual void particles_set_use_local_coordinates(RID p_particles, bool p_enable)=0; - virtual bool particles_is_using_local_coordinates(RID p_particles) const=0; - - /* SKELETON API */ - - virtual RID skeleton_create()=0; - virtual void skeleton_resize(RID p_skeleton,int p_bones)=0; - virtual int skeleton_get_bone_count(RID p_skeleton) const=0; - virtual void skeleton_bone_set_transform(RID p_skeleton,int p_bone, const Transform& p_transform)=0; - virtual Transform skeleton_bone_get_transform(RID p_skeleton,int p_bone)=0; - - - /* LIGHT API */ - - virtual RID light_create(VS::LightType p_type)=0; - virtual VS::LightType light_get_type(RID p_light) const=0; - - virtual void light_set_color(RID p_light,VS::LightColor p_type, const Color& p_color)=0; - virtual Color light_get_color(RID p_light,VS::LightColor p_type) const=0; - - virtual void light_set_shadow(RID p_light,bool p_enabled)=0; - virtual bool light_has_shadow(RID p_light) const=0; - - virtual void light_set_volumetric(RID p_light,bool p_enabled)=0; - virtual bool light_is_volumetric(RID p_light) const=0; - - virtual void light_set_projector(RID p_light,RID p_texture)=0; - virtual RID light_get_projector(RID p_light) const=0; - - virtual void light_set_var(RID p_light, VS::LightParam p_var, float p_value)=0; - virtual float light_get_var(RID p_light, VS::LightParam p_var) const=0; - - virtual void light_set_operator(RID p_light,VS::LightOp p_op)=0; - virtual VS::LightOp light_get_operator(RID p_light) const=0; - - virtual void light_omni_set_shadow_mode(RID p_light,VS::LightOmniShadowMode p_mode)=0; - virtual VS::LightOmniShadowMode light_omni_get_shadow_mode(RID p_light) const=0; - - virtual void light_directional_set_shadow_mode(RID p_light,VS::LightDirectionalShadowMode p_mode)=0; - virtual VS::LightDirectionalShadowMode light_directional_get_shadow_mode(RID p_light) const=0; - virtual void light_directional_set_shadow_param(RID p_light,VS::LightDirectionalShadowParam p_param, float p_value)=0; - virtual float light_directional_get_shadow_param(RID p_light,VS::LightDirectionalShadowParam p_param) const=0; - - virtual AABB light_get_aabb(RID p_poly) const=0; - - virtual RID light_instance_create(RID p_light)=0; - virtual void light_instance_set_transform(RID p_light_instance,const Transform& p_transform)=0; - - - enum ShadowType { - SHADOW_NONE, - SHADOW_SIMPLE, - SHADOW_ORTHOGONAL, - SHADOW_DUAL_PARABOLOID, - SHADOW_CUBE, - SHADOW_PSSM, //parallel split shadow map - SHADOW_PSM //perspective shadow map - }; - - enum ShadowPass { - PASS_DUAL_PARABOLOID_FRONT=0, - PASS_DUAL_PARABOLOID_BACK=1, - PASS_CUBE_FRONT=0, - PASS_CUBE_BACK=1, - PASS_CUBE_TOP=2, - PASS_CUBE_BOTTOM=3, - PASS_CUBE_LEFT=4, - PASS_CUBE_RIGHT=5, - }; - - virtual ShadowType light_instance_get_shadow_type(RID p_light_instance,bool p_far=false) const=0; - virtual int light_instance_get_shadow_passes(RID p_light_instance) const=0; - virtual void light_instance_set_shadow_transform(RID p_light_instance, int p_index, const CameraMatrix& p_camera, const Transform& p_transform, float p_split_near=0,float p_split_far=0)=0; - virtual int light_instance_get_shadow_size(RID p_light_instance, int p_index=0) const=0; - virtual bool light_instance_get_pssm_shadow_overlap(RID p_light_instance) const=0; - - /* SHADOWS */ - - virtual void shadow_clear_near()=0; - virtual bool shadow_allocate_near(RID p_light)=0; //true on successful alloc - virtual bool shadow_allocate_far(RID p_light)=0; //true on successful alloc - - /* PARTICLES INSTANCE */ - - virtual RID particles_instance_create(RID p_particles)=0; - virtual void particles_instance_set_transform(RID p_particles_instance,const Transform& p_transform)=0; - - /* RENDER API */ - /* all calls (inside begin/end shadow) are always warranted to be in the following order: */ - - /* VIEWPORT API */ - - virtual RID viewport_data_create()=0; - - virtual RID render_target_create()=0; - virtual void render_target_set_size(RID p_render_target, int p_width, int p_height)=0; - virtual RID render_target_get_texture(RID p_render_target) const=0; - virtual bool render_target_renedered_in_frame(RID p_render_target)=0; - - virtual void begin_frame()=0; - - virtual void set_viewport(const VS::ViewportRect& p_viewport)=0; - virtual void set_render_target(RID p_render_target,bool p_transparent_bg=false,bool p_vflip=false)=0; - virtual void clear_viewport(const Color& p_color)=0; - virtual void capture_viewport(Image* r_capture)=0; - - virtual void begin_scene(RID p_viewport_data,RID p_env,VS::ScenarioDebugMode p_debug)=0; - virtual void begin_shadow_map( RID p_light_instance, int p_shadow_pass )=0; - - virtual void set_camera(const Transform& p_world,const CameraMatrix& p_projection,bool p_ortho_hint)=0; - - virtual void add_light( RID p_light_instance )=0; ///< all "add_light" calls happen before add_geometry calls - - typedef Map<StringName,Variant> ParamOverrideMap; - - struct BakedLightData { - - VS::BakedLightMode mode; - RID octree_texture; - RID light_texture; - float color_multiplier; //used for both lightmaps and octree - Transform octree_transform; - Map<int,RID> lightmaps; - //cache - - float octree_lattice_size; - float octree_lattice_divide; - float texture_multiplier; - float lightmap_multiplier; - int octree_steps; - Vector2 octree_tex_pixel_size; - Vector2 light_tex_pixel_size; - - bool realtime_color_enabled; - Color realtime_color; - float realtime_energy; - }; - - struct InstanceData { - - Transform transform; - RID skeleton; - RID material_override; - RID sampled_light; - Vector<RID> materials; - Vector<RID> light_instances; - Vector<float> morph_values; - BakedLightData *baked_light; - VS::ShadowCastingSetting cast_shadows; - Transform *baked_light_octree_xform; - int baked_lightmap_id; - bool mirror :8; - bool depth_scale :8; - bool billboard :8; - bool billboard_y :8; - bool receive_shadows : 8; - - }; - - virtual void add_mesh( const RID& p_mesh, const InstanceData *p_data)=0; - virtual void add_multimesh( const RID& p_multimesh, const InstanceData *p_data)=0; - virtual void add_immediate( const RID& p_immediate, const InstanceData *p_data)=0; - virtual void add_particles( const RID& p_particle_instance, const InstanceData *p_data)=0; - - - virtual void end_scene()=0; - virtual void end_shadow_map()=0; - - virtual void end_frame()=0; - virtual void flush_frame(); //not necessary in most cases - - /* CANVAS API */ - - enum CanvasRectFlags { - - CANVAS_RECT_REGION=1, - CANVAS_RECT_TILE=2, - CANVAS_RECT_FLIP_H=4, - CANVAS_RECT_FLIP_V=8, - CANVAS_RECT_TRANSPOSE=16 - }; - - - struct Light { - - - - bool enabled; - Color color; - Matrix32 xform; - float height; - float energy; - float scale; - int z_min; - int z_max; - int layer_min; - int layer_max; - int item_mask; - int item_shadow_mask; - VS::LightMode mode; - RID texture; - Vector2 texture_offset; - RID canvas; - RID shadow_buffer; - int shadow_buffer_size; - float shadow_esm_mult; - Color shadow_color; - - - void *texture_cache; // implementation dependent - Rect2 rect_cache; - Matrix32 xform_cache; - float radius_cache; //used for shadow far plane - CameraMatrix shadow_matrix_cache; - - Matrix32 light_shader_xform; - Vector2 light_shader_pos; - - Light *shadows_next_ptr; - Light *filter_next_ptr; - Light *next_ptr; - Light *mask_next_ptr; - - Light() { - enabled=true; - color=Color(1,1,1); - shadow_color=Color(0,0,0,0); - height=0; - z_min=-1024; - z_max=1024; - layer_min=0; - layer_max=0; - item_mask=1; - scale=1.0; - energy=1.0; - item_shadow_mask=-1; - mode=VS::CANVAS_LIGHT_MODE_ADD; - texture_cache=NULL; - next_ptr=NULL; - mask_next_ptr=NULL; - filter_next_ptr=NULL; - shadow_buffer_size=2048; - shadow_esm_mult=80; - - } - }; - - struct Item; - - struct ItemMaterial { - - RID shader; - Map<StringName,Variant> shader_param; - uint32_t shader_version; - Set<Item*> owners; - VS::ItemShadingMode shading_mode; - - ItemMaterial() {shading_mode=VS::CANVAS_ITEM_SHADING_NORMAL; shader_version=0; } - }; - - struct Item { - - struct Command { - - enum Type { - - TYPE_LINE, - TYPE_RECT, - TYPE_STYLE, - TYPE_PRIMITIVE, - TYPE_POLYGON, - TYPE_POLYGON_PTR, - TYPE_CIRCLE, - TYPE_TRANSFORM, - TYPE_BLEND_MODE, - TYPE_CLIP_IGNORE, - }; - - Type type; - virtual ~Command(){} - }; - - struct CommandLine : public Command { - - Point2 from,to; - Color color; - float width; - bool antialiased; - CommandLine() { type = TYPE_LINE; } - }; - - struct CommandRect : public Command { - - Rect2 rect; - RID texture; - Color modulate; - Rect2 source; - uint8_t flags; - - CommandRect() { flags=0; type = TYPE_RECT; } - }; - - struct CommandStyle : public Command { - - Rect2 rect; - Rect2 source; - RID texture; - float margin[4]; - bool draw_center; - Color color; - CommandStyle() { draw_center=true; type = TYPE_STYLE; } - }; - - struct CommandPrimitive : public Command { - - Vector<Point2> points; - Vector<Point2> uvs; - Vector<Color> colors; - RID texture; - float width; - - CommandPrimitive() { type = TYPE_PRIMITIVE; width=1;} - }; - - struct CommandPolygon : public Command { - - Vector<int> indices; - Vector<Point2> points; - Vector<Point2> uvs; - Vector<Color> colors; - RID texture; - int count; - - CommandPolygon() { type = TYPE_POLYGON; count = 0; } - }; - - struct CommandPolygonPtr : public Command { - - const int* indices; - const Point2* points; - const Point2* uvs; - const Color* colors; - RID texture; - int count; - - CommandPolygonPtr() { type = TYPE_POLYGON_PTR; count = 0; } - }; - - struct CommandCircle : public Command { - - Point2 pos; - float radius; - Color color; - CommandCircle() { type = TYPE_CIRCLE; } - }; - - struct CommandTransform : public Command { - - Matrix32 xform; - CommandTransform() { type = TYPE_TRANSFORM; } - }; - - struct CommandBlendMode : public Command { - - VS::MaterialBlendMode blend_mode; - CommandBlendMode() { type = TYPE_BLEND_MODE; blend_mode = VS::MATERIAL_BLEND_MODE_MIX; } - }; - struct CommandClipIgnore : public Command { - - bool ignore; - CommandClipIgnore() { type = TYPE_CLIP_IGNORE; ignore=false; } - }; - - - struct ViewportRender { - VisualServer*owner; - void* udata; - Rect2 rect; - }; - - Matrix32 xform; - bool clip; - bool visible; - bool ontop; - VS::MaterialBlendMode blend_mode; - int light_mask; - Vector<Command*> commands; - mutable bool custom_rect; - mutable bool rect_dirty; - mutable Rect2 rect; - Item*next; - ItemMaterial* material; - struct CopyBackBuffer { - Rect2 rect; - Rect2 screen_rect; - bool full; - }; - CopyBackBuffer *copy_back_buffer; - - - float final_opacity; - Matrix32 final_transform; - Rect2 final_clip_rect; - Item* final_clip_owner; - Item* material_owner; - ViewportRender *vp_render; - bool distance_field; - bool light_masked; - - Rect2 global_rect_cache; - - const Rect2& get_rect() const { - if (custom_rect || !rect_dirty) - return rect; - - //must update rect - int s=commands.size(); - if (s==0) { - - rect=Rect2(); - rect_dirty=false; - return rect; - } - - Matrix32 xf; - bool found_xform=false; - bool first=true; - - const Item::Command * const *cmd = &commands[0]; - - - for (int i=0;i<s;i++) { - - const Item::Command *c=cmd[i]; - Rect2 r; - - switch(c->type) { - case Item::Command::TYPE_LINE: { - - const Item::CommandLine* line = static_cast< const Item::CommandLine*>(c); - r.pos=line->from; - r.expand_to(line->to); - } break; - case Item::Command::TYPE_RECT: { - - const Item::CommandRect* crect = static_cast< const Item::CommandRect*>(c); - r=crect->rect; - - } break; - case Item::Command::TYPE_STYLE: { - - const Item::CommandStyle* style = static_cast< const Item::CommandStyle*>(c); - r=style->rect; - } break; - case Item::Command::TYPE_PRIMITIVE: { - - const Item::CommandPrimitive* primitive = static_cast< const Item::CommandPrimitive*>(c); - r.pos=primitive->points[0]; - for(int i=1;i<primitive->points.size();i++) { - - r.expand_to(primitive->points[i]); - - } - } break; - case Item::Command::TYPE_POLYGON: { - - const Item::CommandPolygon* polygon = static_cast< const Item::CommandPolygon*>(c); - int l = polygon->points.size(); - const Point2*pp=&polygon->points[0]; - r.pos=pp[0]; - for(int i=1;i<l;i++) { - - r.expand_to(pp[i]); - - } - } break; - - case Item::Command::TYPE_POLYGON_PTR: { - - const Item::CommandPolygonPtr* polygon = static_cast< const Item::CommandPolygonPtr*>(c); - int l = polygon->count; - if (polygon->indices != NULL) { - - r.pos=polygon->points[polygon->indices[0]]; - for (int i=1; i<l; i++) { - - r.expand_to(polygon->points[polygon->indices[i]]); - } - } else { - r.pos=polygon->points[0]; - for (int i=1; i<l; i++) { - - r.expand_to(polygon->points[i]); - } - } - } break; - case Item::Command::TYPE_CIRCLE: { - - const Item::CommandCircle* circle = static_cast< const Item::CommandCircle*>(c); - r.pos=Point2(-circle->radius,-circle->radius)+circle->pos; - r.size=Point2(circle->radius*2.0,circle->radius*2.0); - } break; - case Item::Command::TYPE_TRANSFORM: { - - const Item::CommandTransform* transform = static_cast<const Item::CommandTransform*>(c); - xf=transform->xform; - found_xform=true; - continue; - } break; - case Item::Command::TYPE_BLEND_MODE: { - - } break; - case Item::Command::TYPE_CLIP_IGNORE: { - - } break; - } - - if (found_xform) { - r = xf.xform(r); - found_xform=false; - } - - - if (first) { - rect=r; - first=false; - } else - rect=rect.merge(r); - } - - rect_dirty=false; - return rect; - } - - void clear() { for (int i=0;i<commands.size();i++) memdelete( commands[i] ); commands.clear(); clip=false; rect_dirty=true; final_clip_owner=NULL; material_owner=NULL; light_masked=false; } - Item() { light_mask=1; vp_render=NULL; next=NULL; final_clip_owner=NULL; clip=false; final_opacity=1; blend_mode=VS::MATERIAL_BLEND_MODE_MIX; visible=true; rect_dirty=true; custom_rect=false; ontop=true; material_owner=NULL; material=NULL; copy_back_buffer=NULL; distance_field=false; light_masked=false; } - virtual ~Item() { clear(); if (copy_back_buffer) memdelete(copy_back_buffer); } - }; - - - ItemDrawViewportFunc draw_viewport_func; - - - virtual void begin_canvas_bg()=0; - virtual void canvas_begin()=0; - virtual void canvas_disable_blending()=0; - virtual void canvas_set_opacity(float p_opacity)=0; - virtual void canvas_set_blend_mode(VS::MaterialBlendMode p_mode)=0; - virtual void canvas_begin_rect(const Matrix32& p_transform)=0; - virtual void canvas_set_clip(bool p_clip, const Rect2& p_rect)=0; - virtual void canvas_end_rect()=0; - virtual void canvas_draw_line(const Point2& p_from, const Point2& p_to,const Color& p_color,float p_width,bool p_antialiased)=0; - virtual void canvas_draw_rect(const Rect2& p_rect, int p_flags, const Rect2& p_source,RID p_texture,const Color& p_modulate)=0; - virtual void canvas_draw_style_box(const Rect2& p_rect, const Rect2& p_src_region, RID p_texture,const float *p_margins, bool p_draw_center=true,const Color& p_modulate=Color(1,1,1))=0; - virtual void canvas_draw_primitive(const Vector<Point2>& p_points, const Vector<Color>& p_colors,const Vector<Point2>& p_uvs, RID p_texture,float p_width)=0; - virtual void canvas_draw_polygon(int p_vertex_count, const int* p_indices, const Vector2* p_vertices, const Vector2* p_uvs, const Color* p_colors,const RID& p_texture,bool p_singlecolor)=0; - virtual void canvas_set_transform(const Matrix32& p_transform)=0; - - virtual void canvas_render_items(Item *p_item_list,int p_z,const Color& p_modulate,Light *p_light)=0; - virtual void canvas_debug_viewport_shadows(Light* p_lights_with_shadow)=0; - /* LIGHT SHADOW MAPPING */ - - virtual RID canvas_light_occluder_create()=0; - virtual void canvas_light_occluder_set_polylines(RID p_occluder, const PoolVector<Vector2>& p_lines)=0; - - - virtual RID canvas_light_shadow_buffer_create(int p_width)=0; - - struct LightOccluderInstance { - - - bool enabled; - RID canvas; - RID polygon; - RID polygon_buffer; - Rect2 aabb_cache; - Matrix32 xform; - Matrix32 xform_cache; - int light_mask; - VS::CanvasOccluderPolygonCullMode cull_cache; - - LightOccluderInstance *next; - - LightOccluderInstance() { enabled=true; next=NULL; light_mask=1; cull_cache=VS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED; } - }; - - - - virtual void canvas_light_shadow_buffer_update(RID p_buffer, const Matrix32& p_light_xform, int p_light_mask,float p_near, float p_far, LightOccluderInstance* p_occluders, CameraMatrix *p_xform_cache)=0; - - /* ENVIRONMENT */ - - - virtual RID environment_create()=0; - - virtual void environment_set_background(RID p_env,VS::EnvironmentBG p_bg)=0; - virtual VS::EnvironmentBG environment_get_background(RID p_env) const=0; - - virtual void environment_set_background_param(RID p_env,VS::EnvironmentBGParam p_param, const Variant& p_value)=0; - virtual Variant environment_get_background_param(RID p_env,VS::EnvironmentBGParam p_param) const=0; - - virtual void environment_set_enable_fx(RID p_env,VS::EnvironmentFx p_effect,bool p_enabled)=0; - virtual bool environment_is_fx_enabled(RID p_env,VS::EnvironmentFx p_effect) const=0; - - virtual void environment_fx_set_param(RID p_env,VS::EnvironmentFxParam p_param,const Variant& p_value)=0; - virtual Variant environment_fx_get_param(RID p_env,VS::EnvironmentFxParam p_param) const=0; - - /* SAMPLED LIGHT */ - virtual RID sampled_light_dp_create(int p_width,int p_height)=0; - virtual void sampled_light_dp_update(RID p_sampled_light,const Color *p_data,float p_multiplier)=0; - - - /*MISC*/ - - virtual bool is_texture(const RID& p_rid) const=0; - virtual bool is_material(const RID& p_rid) const=0; - virtual bool is_mesh(const RID& p_rid) const=0; - virtual bool is_multimesh(const RID& p_rid) const=0; - virtual bool is_immediate(const RID& p_rid) const=0; - virtual bool is_particles(const RID &p_beam) const=0; - - virtual bool is_light(const RID& p_rid) const=0; - virtual bool is_light_instance(const RID& p_rid) const=0; - virtual bool is_particles_instance(const RID& p_rid) const=0; - virtual bool is_skeleton(const RID& p_rid) const=0; - virtual bool is_environment(const RID& p_rid) const=0; - virtual bool is_shader(const RID& p_rid) const=0; - - virtual bool is_canvas_light_occluder(const RID& p_rid) const=0; - - virtual void free(const RID& p_rid)=0; - - virtual void init()=0; - virtual void finish()=0; - - virtual bool needs_to_draw_next_frame() const=0; - - virtual void reload_vram() {} - - virtual bool has_feature(VS::Features p_feature) const=0; - - virtual void restore_framebuffer()=0; - - virtual int get_render_info(VS::RenderInfo p_info)=0; - - virtual void set_force_16_bits_fbo(bool p_force) {} - - Rasterizer(); - virtual ~Rasterizer() {} -}; - -#endif -#endif +#endif // RASTERIZER_H diff --git a/servers/visual/visual_server_raster.cpp b/servers/visual/visual_server_raster.cpp index 7a00fd4557..68ca586caf 100644 --- a/servers/visual/visual_server_raster.cpp +++ b/servers/visual/visual_server_raster.cpp @@ -28,6 +28,7 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "visual_server_raster.h" + #include "default_mouse_cursor.xpm" #include "io/marshalls.h" #include "os/os.h" @@ -208,7743 +209,3 @@ VisualServerRaster::~VisualServerRaster() { memdelete(VSG::viewport); memdelete(VSG::rasterizer); } - -#if 0 - -BalloonAllocator<> *VisualServerRaster::OctreeAllocator::allocator=NULL; - -#define VS_CHANGED \ - changes++;\ - - //print_line(__FUNCTION__); - - - - -RID VisualServerRaster::texture_create() { - - return rasterizer->texture_create(); -} - - -void VisualServerRaster::texture_allocate(RID p_texture, int p_width, int p_height,Image::Format p_format,uint32_t p_flags) { - - rasterizer->texture_allocate(p_texture,p_width,p_height,p_format,p_flags); -} - -void VisualServerRaster::texture_set_flags(RID p_texture,uint32_t p_flags) { - - VS_CHANGED; - rasterizer->texture_set_flags(p_texture,p_flags); -} - - -void VisualServerRaster::texture_set_data(RID p_texture,const Image& p_image,CubeMapSide p_cube_side) { - - VS_CHANGED; - rasterizer->texture_set_data(p_texture,p_image,p_cube_side); - - -} - -Image VisualServerRaster::texture_get_data(RID p_texture,CubeMapSide p_cube_side) const { - - - return rasterizer->texture_get_data(p_texture,p_cube_side); -} - - -uint32_t VisualServerRaster::texture_get_flags(RID p_texture) const { - - return rasterizer->texture_get_flags(p_texture); - -} -Image::Format VisualServerRaster::texture_get_format(RID p_texture) const { - - return rasterizer->texture_get_format(p_texture); - -} -uint32_t VisualServerRaster::texture_get_width(RID p_texture) const { - - return rasterizer->texture_get_width(p_texture); -} - -uint32_t VisualServerRaster::texture_get_height(RID p_texture) const { - - return rasterizer->texture_get_height(p_texture); -} - -void VisualServerRaster::texture_set_size_override(RID p_texture,int p_width, int p_height) { - - rasterizer->texture_set_size_override(p_texture,p_width,p_height); -} - -bool VisualServerRaster::texture_can_stream(RID p_texture) const { - - return false; -} - -void VisualServerRaster::texture_set_reload_hook(RID p_texture,ObjectID p_owner,const StringName& p_function) const { - - rasterizer->texture_set_reload_hook(p_texture,p_owner,p_function); -} - -void VisualServerRaster::texture_set_path(RID p_texture,const String& p_path) { - - rasterizer->texture_set_path(p_texture,p_path); -} - -String VisualServerRaster::texture_get_path(RID p_texture) const{ - - return rasterizer->texture_get_path(p_texture); -} - -void VisualServerRaster::texture_debug_usage(List<TextureInfo> *r_info){ - - rasterizer->texture_debug_usage(r_info); -} - -void VisualServerRaster::texture_set_shrink_all_x2_on_set_data(bool p_enable) { - - rasterizer->texture_set_shrink_all_x2_on_set_data(p_enable); -} - -/* SHADER API */ - -RID VisualServerRaster::shader_create(ShaderMode p_mode) { - - return rasterizer->shader_create(p_mode); -} - -void VisualServerRaster::shader_set_mode(RID p_shader,ShaderMode p_mode){ - - VS_CHANGED; - rasterizer->shader_set_mode(p_shader,p_mode); -} -VisualServer::ShaderMode VisualServerRaster::shader_get_mode(RID p_shader) const{ - - return rasterizer->shader_get_mode(p_shader); -} - - -void VisualServerRaster::shader_set_code(RID p_shader, const String& p_vertex, const String& p_fragment,const String& p_light,int p_vertex_ofs,int p_fragment_ofs,int p_light_ofs) { - - VS_CHANGED; - rasterizer->shader_set_code(p_shader,p_vertex,p_fragment,p_light,p_vertex_ofs,p_fragment_ofs,p_light_ofs); -} - -String VisualServerRaster::shader_get_vertex_code(RID p_shader) const{ - - return rasterizer->shader_get_vertex_code(p_shader); -} - -String VisualServerRaster::shader_get_fragment_code(RID p_shader) const{ - - return rasterizer->shader_get_fragment_code(p_shader); -} - -String VisualServerRaster::shader_get_light_code(RID p_shader) const{ - - return rasterizer->shader_get_light_code(p_shader); -} - -void VisualServerRaster::shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const { - - return rasterizer->shader_get_param_list(p_shader,p_param_list); -} - - -void VisualServerRaster::shader_set_default_texture_param(RID p_shader, const StringName& p_name, RID p_texture) { - - rasterizer->shader_set_default_texture_param(p_shader,p_name,p_texture); -} - -RID VisualServerRaster::shader_get_default_texture_param(RID p_shader, const StringName& p_name) const{ - - return rasterizer->shader_get_default_texture_param(p_shader,p_name); -} - - -/* Material */ - -RID VisualServerRaster::material_create() { - - return rasterizer->material_create(); -} - -void VisualServerRaster::material_set_shader(RID p_material, RID p_shader) { - - VS_CHANGED; - rasterizer->material_set_shader(p_material, p_shader ); -} - -RID VisualServerRaster::material_get_shader(RID p_material) const { - - return rasterizer->material_get_shader(p_material); -} - -void VisualServerRaster::material_set_param(RID p_material, const StringName& p_param, const Variant& p_value) { - - VS_CHANGED; - rasterizer->material_set_param(p_material, p_param,p_value ); -} - -Variant VisualServerRaster::material_get_param(RID p_material, const StringName& p_param) const { - - return rasterizer->material_get_param(p_material,p_param); -} - -void VisualServerRaster::material_set_flag(RID p_material, MaterialFlag p_flag,bool p_enabled) { - VS_CHANGED; - rasterizer->material_set_flag(p_material,p_flag,p_enabled); -} - -void VisualServerRaster::material_set_depth_draw_mode(RID p_material, MaterialDepthDrawMode p_mode) { - - VS_CHANGED; - rasterizer->material_set_depth_draw_mode(p_material,p_mode); - -} - -VS::MaterialDepthDrawMode VisualServerRaster::material_get_depth_draw_mode(RID p_material) const { - - return rasterizer->material_get_depth_draw_mode(p_material); -} - - -bool VisualServerRaster::material_get_flag(RID p_material,MaterialFlag p_flag) const { - - return rasterizer->material_get_flag(p_material,p_flag); -} - -void VisualServerRaster::material_set_blend_mode(RID p_material,MaterialBlendMode p_mode) { - VS_CHANGED; - rasterizer->material_set_blend_mode(p_material,p_mode); -} -VS::MaterialBlendMode VisualServerRaster::material_get_blend_mode(RID p_material) const { - - return rasterizer->material_get_blend_mode(p_material); -} - -void VisualServerRaster::material_set_line_width(RID p_material,float p_line_width) { - VS_CHANGED; - rasterizer->material_set_line_width(p_material,p_line_width); -} -float VisualServerRaster::material_get_line_width(RID p_material) const { - - return rasterizer->material_get_line_width(p_material); -} - -/* FIXED MATERIAL */ - -RID VisualServerRaster::fixed_material_create() { - - return rasterizer->fixed_material_create(); -} - -void VisualServerRaster::fixed_material_set_flag(RID p_material, SpatialMaterialFlags p_flag, bool p_enabled) { - - rasterizer->fixed_material_set_flag(p_material,p_flag,p_enabled); -} - -bool VisualServerRaster::fixed_material_get_flag(RID p_material, SpatialMaterialFlags p_flag) const { - - return rasterizer->fixed_material_get_flag(p_material,p_flag); -} - -void VisualServerRaster::fixed_material_set_param(RID p_material, SpatialMaterialParam p_parameter, const Variant& p_value) { - VS_CHANGED; - rasterizer->fixed_material_set_parameter(p_material,p_parameter,p_value); -} - -Variant VisualServerRaster::fixed_material_get_param(RID p_material,SpatialMaterialParam p_parameter) const { - - return rasterizer->fixed_material_get_parameter(p_material,p_parameter); -} - - -void VisualServerRaster::fixed_material_set_texture(RID p_material,SpatialMaterialParam p_parameter, RID p_texture) { - VS_CHANGED; - rasterizer->fixed_material_set_texture(p_material,p_parameter,p_texture); -} - -RID VisualServerRaster::fixed_material_get_texture(RID p_material,SpatialMaterialParam p_parameter) const { - - return rasterizer->fixed_material_get_texture(p_material,p_parameter); -} - - - - -void VisualServerRaster::fixed_material_set_texcoord_mode(RID p_material,SpatialMaterialParam p_parameter, SpatialMaterialTexCoordMode p_mode) { - VS_CHANGED; - rasterizer->fixed_material_set_texcoord_mode(p_material,p_parameter,p_mode); -} - -VS::SpatialMaterialTexCoordMode VisualServerRaster::fixed_material_get_texcoord_mode(RID p_material,SpatialMaterialParam p_parameter) const { - - return rasterizer->fixed_material_get_texcoord_mode(p_material,p_parameter); -} - -void VisualServerRaster::fixed_material_set_point_size(RID p_material,float p_size) { - VS_CHANGED - - rasterizer->fixed_material_set_point_size(p_material,p_size); -} - -float VisualServerRaster::fixed_material_get_point_size(RID p_material) const{ - - return rasterizer->fixed_material_get_point_size(p_material); -} - - -void VisualServerRaster::fixed_material_set_uv_transform(RID p_material,const Transform& p_transform) { - VS_CHANGED; - rasterizer->fixed_material_set_uv_transform(p_material,p_transform); -} - -Transform VisualServerRaster::fixed_material_get_uv_transform(RID p_material) const { - - return rasterizer->fixed_material_get_uv_transform(p_material); -} - -void VisualServerRaster::fixed_material_set_light_shader(RID p_material,SpatialMaterialLightShader p_shader) { - - VS_CHANGED; - rasterizer->fixed_material_set_light_shader(p_material,p_shader); - -} - -VisualServerRaster::SpatialMaterialLightShader VisualServerRaster::fixed_material_get_light_shader(RID p_material) const{ - - return rasterizer->fixed_material_get_light_shader(p_material); -} - - - -/* MESH API */ - -RID VisualServerRaster::mesh_create() { - - return rasterizer->mesh_create(); -} - -void VisualServerRaster::mesh_set_morph_target_count(RID p_mesh,int p_amount) { - - rasterizer->mesh_set_morph_target_count(p_mesh,p_amount); - int amount = rasterizer->mesh_get_morph_target_count(p_mesh); - - - Map< RID, Set<RID> >::Element * E = instance_dependency_map.find( p_mesh ); - - if (!E) - return; - - - Set<RID>::Element *I = E->get().front(); - - while(I) { - - Instance *ins = instance_owner.get( I->get() ); - ins->data.morph_values.resize(amount); - I = I->next(); - } -} - -int VisualServerRaster::mesh_get_morph_target_count(RID p_mesh) const { - - return rasterizer->mesh_get_morph_target_count(p_mesh); -} - -void VisualServerRaster::mesh_set_morph_target_mode(RID p_mesh,MorphTargetMode p_mode) { - - rasterizer->mesh_set_morph_target_mode(p_mesh,p_mode); -} - -VisualServer::MorphTargetMode VisualServerRaster::mesh_get_morph_target_mode(RID p_mesh) const{ - - return rasterizer->mesh_get_morph_target_mode(p_mesh); -} - -void VisualServerRaster::mesh_add_custom_surface(RID p_mesh,const Variant& p_dat) { - - -} - - -void VisualServerRaster::mesh_add_surface(RID p_mesh,PrimitiveType p_primitive,const Array& p_arrays,const Array& p_blend_shapes,bool p_alpha_sort) { - - VS_CHANGED; - _dependency_queue_update(p_mesh,true,true); - rasterizer->mesh_add_surface(p_mesh,p_primitive,p_arrays,p_blend_shapes,p_alpha_sort); - -} - -Array VisualServerRaster::mesh_get_surface_arrays(RID p_mesh,int p_surface) const { - - return rasterizer->mesh_get_surface_arrays(p_mesh,p_surface); -} -Array VisualServerRaster::mesh_get_surface_morph_arrays(RID p_mesh,int p_surface) const { - - return rasterizer->mesh_get_surface_morph_arrays(p_mesh,p_surface); -} - - - - -void VisualServerRaster::mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material,bool p_owned){ - VS_CHANGED; - rasterizer->mesh_surface_set_material(p_mesh,p_surface,p_material,p_owned); -} - -RID VisualServerRaster::mesh_surface_get_material(RID p_mesh,int p_surface) const { - - return rasterizer->mesh_surface_get_material(p_mesh,p_surface); - -} - - -int VisualServerRaster::mesh_surface_get_array_len(RID p_mesh, int p_surface) const{ - - return rasterizer->mesh_surface_get_array_len(p_mesh,p_surface); -} - -int VisualServerRaster::mesh_surface_get_array_index_len(RID p_mesh, int p_surface) const{ - - - return rasterizer->mesh_surface_get_array_index_len(p_mesh,p_surface); -} - -uint32_t VisualServerRaster::mesh_surface_get_format(RID p_mesh, int p_surface) const{ - - return rasterizer->mesh_surface_get_format(p_mesh,p_surface); -} - -VisualServer::PrimitiveType VisualServerRaster::mesh_surface_get_primitive_type(RID p_mesh, int p_surface) const{ - - return rasterizer->mesh_surface_get_primitive_type(p_mesh,p_surface); -} - - -void VisualServerRaster::mesh_remove_surface(RID p_mesh,int p_surface){ - - rasterizer->mesh_remove_surface(p_mesh,p_surface); - _dependency_queue_update(p_mesh,true,true); -} - -int VisualServerRaster::mesh_get_surface_count(RID p_mesh) const{ - - return rasterizer->mesh_get_surface_count(p_mesh); - -} - - -void VisualServerRaster::mesh_set_custom_aabb(RID p_mesh,const AABB& p_aabb) { - - VS_CHANGED; - _dependency_queue_update(p_mesh,true); - rasterizer->mesh_set_custom_aabb(p_mesh,p_aabb); - -} - -AABB VisualServerRaster::mesh_get_custom_aabb(RID p_mesh) const { - - return rasterizer->mesh_get_custom_aabb(p_mesh); -} - -void VisualServerRaster::mesh_clear(RID p_mesh) { - - ERR_FAIL_COND(!rasterizer->is_mesh(p_mesh)); - while(rasterizer->mesh_get_surface_count(p_mesh)) { - rasterizer->mesh_remove_surface(p_mesh,0); - } - - _dependency_queue_update(p_mesh,true,true); -} - - -/* MULTIMESH */ - -RID VisualServerRaster::multimesh_create() { - - return rasterizer->multimesh_create(); -} - -void VisualServerRaster::multimesh_set_instance_count(RID p_multimesh,int p_count) { - VS_CHANGED; - rasterizer->multimesh_set_instance_count(p_multimesh,p_count); -} - -int VisualServerRaster::multimesh_get_instance_count(RID p_multimesh) const { - - return rasterizer->multimesh_get_instance_count(p_multimesh); -} - -void VisualServerRaster::multimesh_set_mesh(RID p_multimesh,RID p_mesh) { - VS_CHANGED; - rasterizer->multimesh_set_mesh(p_multimesh,p_mesh); - -} -void VisualServerRaster::multimesh_set_aabb(RID p_multimesh,const AABB& p_aabb) { - VS_CHANGED; - rasterizer->multimesh_set_aabb(p_multimesh,p_aabb); - _dependency_queue_update(p_multimesh,true); - -} - -void VisualServerRaster::multimesh_instance_set_transform(RID p_multimesh,int p_index,const Transform& p_transform) { - VS_CHANGED; - rasterizer->multimesh_instance_set_transform(p_multimesh,p_index,p_transform); - -} -void VisualServerRaster::multimesh_instance_set_color(RID p_multimesh,int p_index,const Color& p_color) { - VS_CHANGED; - rasterizer->multimesh_instance_set_color(p_multimesh,p_index,p_color); - -} -RID VisualServerRaster::multimesh_get_mesh(RID p_multimesh) const { - - return rasterizer->multimesh_get_mesh(p_multimesh); -} -AABB VisualServerRaster::multimesh_get_aabb(RID p_multimesh,const AABB& p_aabb) const { - - - return rasterizer->multimesh_get_aabb(p_multimesh); -} - -Transform VisualServerRaster::multimesh_instance_get_transform(RID p_multimesh,int p_index) const { - - return rasterizer->multimesh_instance_get_transform(p_multimesh,p_index); -} -Color VisualServerRaster::multimesh_instance_get_color(RID p_multimesh,int p_index) const { - - return rasterizer->multimesh_instance_get_color(p_multimesh,p_index); -} - -void VisualServerRaster::multimesh_set_visible_instances(RID p_multimesh,int p_visible) { - - rasterizer->multimesh_set_visible_instances(p_multimesh,p_visible); - -} - -int VisualServerRaster::multimesh_get_visible_instances(RID p_multimesh) const { - - return rasterizer->multimesh_get_visible_instances(p_multimesh); -} - - -/* IMMEDIATE API */ - - -RID VisualServerRaster::immediate_create() { - - return rasterizer->immediate_create(); -} - -void VisualServerRaster::immediate_begin(RID p_immediate,PrimitiveType p_primitive,RID p_texture){ - - rasterizer->immediate_begin(p_immediate,p_primitive,p_texture); -} -void VisualServerRaster::immediate_vertex(RID p_immediate,const Vector3& p_vertex){ - - rasterizer->immediate_vertex(p_immediate,p_vertex); - -} -void VisualServerRaster::immediate_normal(RID p_immediate,const Vector3& p_normal){ - - rasterizer->immediate_normal(p_immediate,p_normal); - -} -void VisualServerRaster::immediate_tangent(RID p_immediate,const Plane& p_tangent){ - - rasterizer->immediate_tangent(p_immediate,p_tangent); - -} -void VisualServerRaster::immediate_color(RID p_immediate,const Color& p_color){ - - rasterizer->immediate_color(p_immediate,p_color); - -} -void VisualServerRaster::immediate_uv(RID p_immediate,const Vector2& p_uv){ - - rasterizer->immediate_uv(p_immediate,p_uv); - -} -void VisualServerRaster::immediate_uv2(RID p_immediate,const Vector2& p_uv2){ - - rasterizer->immediate_uv2(p_immediate,p_uv2); - -} -void VisualServerRaster::immediate_end(RID p_immediate){ - - VS_CHANGED; - _dependency_queue_update(p_immediate,true); - rasterizer->immediate_end(p_immediate); - -} -void VisualServerRaster::immediate_clear(RID p_immediate){ - - VS_CHANGED; - _dependency_queue_update(p_immediate,true); - rasterizer->immediate_clear(p_immediate); - -} - -void VisualServerRaster::immediate_set_material(RID p_immediate,RID p_material) { - - rasterizer->immediate_set_material(p_immediate,p_material); -} - -RID VisualServerRaster::immediate_get_material(RID p_immediate) const { - - return rasterizer->immediate_get_material(p_immediate); -} - - -/* PARTICLES API */ - -RID VisualServerRaster::particles_create() { - - return rasterizer->particles_create(); -} - -void VisualServerRaster::particles_set_amount(RID p_particles, int p_amount) { - VS_CHANGED; - rasterizer->particles_set_amount(p_particles,p_amount); -} - -int VisualServerRaster::particles_get_amount(RID p_particles) const { - - return rasterizer->particles_get_amount(p_particles); -} - -void VisualServerRaster::particles_set_emitting(RID p_particles, bool p_emitting) { - VS_CHANGED; - rasterizer->particles_set_emitting(p_particles,p_emitting); -} -bool VisualServerRaster::particles_is_emitting(RID p_particles) const { - - return rasterizer->particles_is_emitting(p_particles); -} - -void VisualServerRaster::particles_set_visibility_aabb(RID p_particles, const AABB& p_visibility) { - VS_CHANGED; - rasterizer->particles_set_visibility_aabb(p_particles, p_visibility); -} -AABB VisualServerRaster::particles_get_visibility_aabb(RID p_particles) const { - - return rasterizer->particles_get_visibility_aabb(p_particles); -} - -void VisualServerRaster::particles_set_emission_half_extents(RID p_particles, const Vector3& p_half_extents) { - VS_CHANGED; - rasterizer->particles_set_emission_half_extents(p_particles,p_half_extents); -} -Vector3 VisualServerRaster::particles_get_emission_half_extents(RID p_particles) const { - - return rasterizer->particles_get_emission_half_extents(p_particles); -} - -void VisualServerRaster::particles_set_emission_base_velocity(RID p_particles, const Vector3& p_base_velocity) { - VS_CHANGED; - rasterizer->particles_set_emission_base_velocity(p_particles,p_base_velocity); -} -Vector3 VisualServerRaster::particles_get_emission_base_velocity(RID p_particles) const { - - return rasterizer->particles_get_emission_base_velocity(p_particles); -} - -void VisualServerRaster::particles_set_emission_points(RID p_particles, const PoolVector<Vector3>& p_points) { - - VS_CHANGED; - rasterizer->particles_set_emission_points(p_particles,p_points); -} - -PoolVector<Vector3> VisualServerRaster::particles_get_emission_points(RID p_particles) const { - - return rasterizer->particles_get_emission_points(p_particles); -} - -void VisualServerRaster::particles_set_gravity_normal(RID p_particles, const Vector3& p_normal) { - VS_CHANGED; - rasterizer->particles_set_gravity_normal(p_particles,p_normal); -} -Vector3 VisualServerRaster::particles_get_gravity_normal(RID p_particles) const { - - return rasterizer->particles_get_gravity_normal(p_particles); -} - -void VisualServerRaster::particles_set_variable(RID p_particles, ParticleVariable p_variable,float p_value) { - VS_CHANGED; - rasterizer->particles_set_variable(p_particles,p_variable,p_value); -} -float VisualServerRaster::particles_get_variable(RID p_particles, ParticleVariable p_variable) const { - - return rasterizer->particles_get_variable(p_particles,p_variable); -} - -void VisualServerRaster::particles_set_randomness(RID p_particles, ParticleVariable p_variable,float p_randomness) { - VS_CHANGED; - rasterizer->particles_set_randomness(p_particles,p_variable,p_randomness); -} -float VisualServerRaster::particles_get_randomness(RID p_particles, ParticleVariable p_variable) const { - - return rasterizer->particles_get_randomness(p_particles,p_variable); -} - -void VisualServerRaster::particles_set_color_phases(RID p_particles, int p_phases) { - VS_CHANGED; - rasterizer->particles_set_color_phases(p_particles,p_phases); -} -int VisualServerRaster::particles_get_color_phases(RID p_particles) const { - - return rasterizer->particles_get_color_phases(p_particles); -} - -void VisualServerRaster::particles_set_color_phase_pos(RID p_particles, int p_phase, float p_pos) { - VS_CHANGED; - rasterizer->particles_set_color_phase_pos(p_particles,p_phase,p_pos); -} -float VisualServerRaster::particles_get_color_phase_pos(RID p_particles, int p_phase) const { - - return rasterizer->particles_get_color_phase_pos(p_particles,p_phase); -} - -void VisualServerRaster::particles_set_attractors(RID p_particles, int p_attractors) { - VS_CHANGED; - rasterizer->particles_set_attractors(p_particles,p_attractors); -} -int VisualServerRaster::particles_get_attractors(RID p_particles) const { - - return rasterizer->particles_get_attractors(p_particles); -} - -void VisualServerRaster::particles_set_attractor_pos(RID p_particles, int p_attractor, const Vector3& p_pos) { - VS_CHANGED; - rasterizer->particles_set_attractor_pos(p_particles,p_attractor,p_pos); -} -Vector3 VisualServerRaster::particles_get_attractor_pos(RID p_particles,int p_attractor) const { - - return rasterizer->particles_get_attractor_pos(p_particles,p_attractor); -} - -void VisualServerRaster::particles_set_attractor_strength(RID p_particles, int p_attractor, float p_force) { - VS_CHANGED; - rasterizer->particles_set_attractor_strength(p_particles,p_attractor,p_force); -} - -float VisualServerRaster::particles_get_attractor_strength(RID p_particles,int p_attractor) const { - - return rasterizer->particles_get_attractor_strength(p_particles,p_attractor); -} - -void VisualServerRaster::particles_set_color_phase_color(RID p_particles, int p_phase, const Color& p_color) { - VS_CHANGED; - rasterizer->particles_set_color_phase_color(p_particles,p_phase,p_color); -} -Color VisualServerRaster::particles_get_color_phase_color(RID p_particles, int p_phase) const { - - return rasterizer->particles_get_color_phase_color(p_particles,p_phase); -} - -void VisualServerRaster::particles_set_material(RID p_particles, RID p_material,bool p_owned) { - VS_CHANGED; - rasterizer->particles_set_material(p_particles,p_material,p_owned); -} -RID VisualServerRaster::particles_get_material(RID p_particles) const { - - return rasterizer->particles_get_material(p_particles); -} - - -void VisualServerRaster::particles_set_height_from_velocity(RID p_particles, bool p_enable) { - - VS_CHANGED; - rasterizer->particles_set_height_from_velocity(p_particles,p_enable); - -} - -bool VisualServerRaster::particles_has_height_from_velocity(RID p_particles) const { - - return rasterizer->particles_has_height_from_velocity(p_particles); -} - -void VisualServerRaster::particles_set_use_local_coordinates(RID p_particles, bool p_enable) { - - rasterizer->particles_set_use_local_coordinates(p_particles,p_enable); -} - -bool VisualServerRaster::particles_is_using_local_coordinates(RID p_particles) const { - - return rasterizer->particles_is_using_local_coordinates(p_particles); -} - - -/* Light API */ - -RID VisualServerRaster::light_create(LightType p_type) { - - return rasterizer->light_create(p_type); -} -VisualServer::LightType VisualServerRaster::light_get_type(RID p_light) const { - - return rasterizer->light_get_type(p_light); -} - -void VisualServerRaster::light_set_color(RID p_light,LightColor p_type, const Color& p_color) { - VS_CHANGED; - rasterizer->light_set_color(p_light,p_type,p_color); - -} -Color VisualServerRaster::light_get_color(RID p_light,LightColor p_type) const { - - return rasterizer->light_get_color(p_light,p_type); - -} - - -void VisualServerRaster::light_set_shadow(RID p_light,bool p_enabled) { - VS_CHANGED; - rasterizer->light_set_shadow(p_light,p_enabled); -} - -bool VisualServerRaster::light_has_shadow(RID p_light) const { - - return rasterizer->light_has_shadow(p_light); -} - - - -void VisualServerRaster::light_set_volumetric(RID p_light,bool p_enabled) { - VS_CHANGED; - rasterizer->light_set_volumetric(p_light,p_enabled); -} - -bool VisualServerRaster::light_is_volumetric(RID p_light) const { - - return rasterizer->light_is_volumetric(p_light); -} - -void VisualServerRaster::light_set_projector(RID p_light,RID p_texture) { - VS_CHANGED; - rasterizer->light_set_projector(p_light,p_texture); -} - -RID VisualServerRaster::light_get_projector(RID p_light) const { - - return rasterizer->light_get_projector(p_light); -} - -void VisualServerRaster::light_set_param(RID p_light, LightParam p_var, float p_value) { - VS_CHANGED; - rasterizer->light_set_var(p_light,p_var,p_value); - _dependency_queue_update(p_light,true); - -} - -float VisualServerRaster::light_get_param(RID p_light, LightParam p_var) const { - - - return rasterizer->light_get_var(p_light,p_var); -} - -void VisualServerRaster::light_set_operator(RID p_light,LightOp p_op) { - VS_CHANGED; - rasterizer->light_set_operator(p_light,p_op); -} - -VisualServerRaster::LightOp VisualServerRaster::light_get_operator(RID p_light) const { - - return rasterizer->light_get_operator(p_light); -} - -void VisualServerRaster::light_omni_set_shadow_mode(RID p_light,LightOmniShadowMode p_mode) { - VS_CHANGED; - rasterizer->light_omni_set_shadow_mode(p_light,p_mode); -} - -VisualServerRaster::LightOmniShadowMode VisualServerRaster::light_omni_get_shadow_mode(RID p_light) const { - - return rasterizer->light_omni_get_shadow_mode(p_light); -} - -void VisualServerRaster::light_directional_set_shadow_mode(RID p_light,LightDirectionalShadowMode p_mode){ - VS_CHANGED; - rasterizer->light_directional_set_shadow_mode(p_light,p_mode); -} - -VS::LightDirectionalShadowMode VisualServerRaster::light_directional_get_shadow_mode(RID p_light) const{ - - return rasterizer->light_directional_get_shadow_mode(p_light); -} - -void VisualServerRaster::light_directional_set_shadow_param(RID p_light,LightDirectionalShadowParam p_param, float p_value) { - VS_CHANGED; - rasterizer->light_directional_set_shadow_param(p_light,p_param,p_value); -} - -float VisualServerRaster::light_directional_get_shadow_param(RID p_light,LightDirectionalShadowParam p_param) const { - - return rasterizer->light_directional_get_shadow_param(p_light,p_param); -} - - -RID VisualServerRaster::skeleton_create() { - - return rasterizer->skeleton_create(); -} - -void VisualServerRaster::skeleton_resize(RID p_skeleton,int p_bones) { - VS_CHANGED; - rasterizer->skeleton_resize(p_skeleton,p_bones); -} - -int VisualServerRaster::skeleton_get_bone_count(RID p_skeleton) const { - - return rasterizer->skeleton_get_bone_count(p_skeleton); -} - -void VisualServerRaster::skeleton_bone_set_transform(RID p_skeleton,int p_bone, const Transform& p_transform) { - VS_CHANGED; - rasterizer->skeleton_bone_set_transform(p_skeleton,p_bone,p_transform); - - Map< RID, Set<Instance*> >::Element *E=skeleton_dependency_map.find(p_skeleton); - - if (E) { - //detach skeletons - for (Set<Instance*>::Element *F=E->get().front();F;F=F->next()) { - - _instance_queue_update( F->get() , true); - } - } -} - -Transform VisualServerRaster::skeleton_bone_get_transform(RID p_skeleton,int p_bone) { - - - return rasterizer->skeleton_bone_get_transform(p_skeleton,p_bone); - -} - - -/* VISIBILITY API */ - -/* ROOM API */ - -RID VisualServerRaster::room_create() { - - Room *room = memnew( Room ); - ERR_FAIL_COND_V(!room,RID()); - return room_owner.make_rid( room ); - -} - -void VisualServerRaster::room_set_bounds(RID p_room, const BSP_Tree& p_bounds) { - VS_CHANGED; - Room *room = room_owner.get(p_room); - ERR_FAIL_COND(!room); - room->bounds=p_bounds; - _dependency_queue_update(p_room,true); - -} - -BSP_Tree VisualServerRaster::room_get_bounds(RID p_room) const { - - Room *room = room_owner.get(p_room); - ERR_FAIL_COND_V(!room, BSP_Tree()); - return room->bounds; - -} - -/* PORTAL API */ - -RID VisualServerRaster::portal_create() { - VS_CHANGED; - Portal *portal = memnew( Portal ); - ERR_FAIL_COND_V(!portal,RID()); - return portal_owner.make_rid( portal ); - - -} - - -void VisualServerRaster::portal_set_shape(RID p_portal, const Vector<Point2>& p_shape) { - VS_CHANGED; - Portal *portal = portal_owner.get(p_portal); - ERR_FAIL_COND(!portal); - portal->shape=p_shape; - - portal->bounds=Rect2(); - - for(int i=0;i<p_shape.size();i++) { - - if (i==0) - portal->bounds.pos=p_shape[i]; - else - portal->bounds.expand_to(p_shape[i]); - } - - _dependency_queue_update(p_portal,true); -} - - -Vector<Point2> VisualServerRaster::portal_get_shape(RID p_portal) const { - - Portal *portal = portal_owner.get(p_portal); - ERR_FAIL_COND_V(!portal, Vector<Point2>()); - return portal->shape; - - -} - -void VisualServerRaster::portal_set_enabled(RID p_portal, bool p_enabled) { - VS_CHANGED; - Portal *portal = portal_owner.get(p_portal); - ERR_FAIL_COND(!portal); - portal->enabled=p_enabled; - -} - -bool VisualServerRaster::portal_is_enabled(RID p_portal) const { - - Portal *portal = portal_owner.get(p_portal); - ERR_FAIL_COND_V(!portal, false); - return portal->enabled; - -} -void VisualServerRaster::portal_set_disable_distance(RID p_portal, float p_distance) { - VS_CHANGED; - Portal *portal = portal_owner.get(p_portal); - ERR_FAIL_COND(!portal); - portal->disable_distance=p_distance; - -} -float VisualServerRaster::portal_get_disable_distance(RID p_portal) const { - - Portal *portal = portal_owner.get(p_portal); - ERR_FAIL_COND_V(!portal, -1); - return portal->disable_distance; - -} -void VisualServerRaster::portal_set_disabled_color(RID p_portal, const Color& p_color) { - VS_CHANGED; - Portal *portal = portal_owner.get(p_portal); - ERR_FAIL_COND(!portal); - portal->disable_color=p_color; - -} -Color VisualServerRaster::portal_get_disabled_color(RID p_portal) const { - - Portal *portal = portal_owner.get(p_portal); - ERR_FAIL_COND_V(!portal, Color()); - return portal->disable_color; -} - -void VisualServerRaster::portal_set_connect_range(RID p_portal, float p_range) { - VS_CHANGED; - Portal *portal = portal_owner.get(p_portal); - ERR_FAIL_COND(!portal); - portal->connect_range=p_range; - _dependency_queue_update(p_portal,true); -} - -float VisualServerRaster::portal_get_connect_range(RID p_portal) const { - - Portal *portal = portal_owner.get(p_portal); - ERR_FAIL_COND_V(!portal,0); - return portal->connect_range; -} - - -RID VisualServerRaster::baked_light_create() { - - BakedLight *baked_light = memnew( BakedLight ); - ERR_FAIL_COND_V(!baked_light,RID()); - baked_light->data.mode=BAKED_LIGHT_OCTREE; - - baked_light->data.octree_lattice_size=0; - baked_light->data.octree_lattice_divide=0; - baked_light->data.octree_steps=1; - baked_light->data.lightmap_multiplier=1.0; - baked_light->data.realtime_color_enabled=false; - baked_light->data.realtime_color=Color(1.0, 1.0, 1.0); - baked_light->data.realtime_energy = 1.0; - - return baked_light_owner.make_rid( baked_light ); - -} - -void VisualServerRaster::baked_light_set_mode(RID p_baked_light,BakedLightMode p_mode){ - - VS_CHANGED; - BakedLight *baked_light = baked_light_owner.get(p_baked_light); - ERR_FAIL_COND(!baked_light); - baked_light->data.mode=p_mode; - baked_light->data.color_multiplier=1.0; - _dependency_queue_update(p_baked_light,true); - - -} - -VisualServer::BakedLightMode VisualServerRaster::baked_light_get_mode(RID p_baked_light) const{ - - const BakedLight *baked_light = baked_light_owner.get(p_baked_light); - ERR_FAIL_COND_V(!baked_light,BAKED_LIGHT_OCTREE); - return baked_light->data.mode; - -} - -void VisualServerRaster::baked_light_set_lightmap_multiplier(RID p_baked_light,float p_multiplier) { - - VS_CHANGED; - BakedLight *baked_light = baked_light_owner.get(p_baked_light); - ERR_FAIL_COND(!baked_light); - - baked_light->data.lightmap_multiplier=p_multiplier; - -} - -float VisualServerRaster::baked_light_get_lightmap_multiplier(RID p_baked_light) const{ - - const BakedLight *baked_light = baked_light_owner.get(p_baked_light); - ERR_FAIL_COND_V(!baked_light,0); - - return baked_light->data.lightmap_multiplier; - -} - - -void VisualServerRaster::baked_light_set_octree(RID p_baked_light,const PoolVector<uint8_t> p_octree){ - - VS_CHANGED; - BakedLight *baked_light = baked_light_owner.get(p_baked_light); - ERR_FAIL_COND(!baked_light); - - if (p_octree.size()==0) { - if (baked_light->data.octree_texture.is_valid()) - rasterizer->free(baked_light->data.octree_texture); - baked_light->data.octree_texture; - baked_light->octree_aabb=AABB(); - baked_light->octree_tex_size=Size2(); - } else { - - int tex_w; - int tex_h; - int light_tex_w; - int light_tex_h; - bool is16; - bool has_light_tex=false; - { - - PoolVector<uint8_t>::Read r=p_octree.read(); - tex_w = decode_uint32(&r[0]); - tex_h = decode_uint32(&r[4]); - print_line("TEX W: "+itos(tex_w)+" TEX H:"+itos(tex_h)+" LEN: "+itos(p_octree.size())); - is16=decode_uint32(&r[8]); - baked_light->data.octree_lattice_size=decode_float(&r[12]); - baked_light->data.octree_lattice_divide=tex_w/4.0; - print_line("LATTICE SIZE: "+rtos(baked_light->data.octree_lattice_size)); - print_line("LATTICE DIVIDE: "+rtos(baked_light->data.octree_lattice_divide)); - baked_light->data.octree_steps=decode_uint32(&r[16]); - baked_light->data.octree_tex_pixel_size.x=1.0/tex_w; - baked_light->data.octree_tex_pixel_size.y=1.0/tex_h; - - baked_light->data.texture_multiplier=decode_uint32(&r[20]); - light_tex_w=decode_uint16(&r[24]); - light_tex_h=decode_uint16(&r[26]); - print_line("ltexw "+itos(light_tex_w)); - print_line("ltexh "+itos(light_tex_h)); - - if (light_tex_w>0 && light_tex_h>0) { - baked_light->data.light_tex_pixel_size.x=1.0/light_tex_w; - baked_light->data.light_tex_pixel_size.y=1.0/light_tex_h; - has_light_tex=true; - } else { - baked_light->data.light_tex_pixel_size=baked_light->data.octree_tex_pixel_size; - - } - - - - baked_light->octree_aabb.pos.x=decode_float(&r[32]); - baked_light->octree_aabb.pos.y=decode_float(&r[36]); - baked_light->octree_aabb.pos.z=decode_float(&r[40]); - baked_light->octree_aabb.size.x=decode_float(&r[44]); - baked_light->octree_aabb.size.y=decode_float(&r[48]); - baked_light->octree_aabb.size.z=decode_float(&r[52]); - - - } - - if (baked_light->data.octree_texture.is_valid()) { - if (tex_w!=baked_light->octree_tex_size.x || tex_h!=baked_light->octree_tex_size.y) { - - rasterizer->free(baked_light->data.octree_texture); - baked_light->data.octree_texture; - baked_light->octree_tex_size.x=0; - baked_light->octree_tex_size.y=0; - } - } - - if (baked_light->data.light_texture.is_valid()) { - if (!has_light_tex || light_tex_w!=baked_light->light_tex_size.x || light_tex_h!=baked_light->light_tex_size.y) { - rasterizer->free(baked_light->data.light_texture); - baked_light->data.light_texture; - baked_light->light_tex_size.x=0; - baked_light->light_tex_size.y=0; - } - } - - if (!baked_light->data.octree_texture.is_valid()) { - baked_light->data.octree_texture=rasterizer->texture_create(); - rasterizer->texture_allocate(baked_light->data.octree_texture,tex_w,tex_h,Image::FORMAT_RGBA8,TEXTURE_FLAG_FILTER); - baked_light->octree_tex_size.x=tex_w; - baked_light->octree_tex_size.y=tex_h; - } - - if (!baked_light->data.light_texture.is_valid() && has_light_tex) { - baked_light->data.light_texture=rasterizer->texture_create(); - rasterizer->texture_allocate(baked_light->data.light_texture,light_tex_w,light_tex_h,Image::FORMAT_RGBA8,TEXTURE_FLAG_FILTER); - baked_light->light_tex_size.x=light_tex_w; - baked_light->light_tex_size.y=light_tex_h; - - } - - Image img(tex_w,tex_h,0,Image::FORMAT_RGBA8,p_octree); - rasterizer->texture_set_data(baked_light->data.octree_texture,img); - - } - - - _dependency_queue_update(p_baked_light,true); - -} - - -PoolVector<uint8_t> VisualServerRaster::baked_light_get_octree(RID p_baked_light) const{ - - - BakedLight *baked_light = baked_light_owner.get(p_baked_light); - ERR_FAIL_COND_V(!baked_light,PoolVector<uint8_t>()); - - if (rasterizer->is_texture(baked_light->data.octree_texture)) { - - Image img = rasterizer->texture_get_data(baked_light->data.octree_texture); - return img.get_data(); - } else { - return PoolVector<uint8_t>(); - } -} - -void VisualServerRaster::baked_light_set_light(RID p_baked_light,const PoolVector<uint8_t> p_light) { - - VS_CHANGED; - BakedLight *baked_light = baked_light_owner.get(p_baked_light); - ERR_FAIL_COND(!baked_light); - ERR_FAIL_COND(p_light.size()==0); - - int tex_w=baked_light->light_tex_size.x; - int tex_h=baked_light->light_tex_size.y; - - ERR_FAIL_COND(tex_w==0 && tex_h==0); - ERR_FAIL_COND(!baked_light->data.light_texture.is_valid()); - - - - print_line("w: "+itos(tex_w)+" h: "+itos(tex_h)+" lightsize: "+itos(p_light.size())); - - Image img(tex_w,tex_h,0,Image::FORMAT_RGBA8,p_light); - rasterizer->texture_set_data(baked_light->data.light_texture,img); - - - -} - -PoolVector<uint8_t> VisualServerRaster::baked_light_get_light(RID p_baked_light) const{ - - BakedLight *baked_light = baked_light_owner.get(p_baked_light); - ERR_FAIL_COND_V(!baked_light,PoolVector<uint8_t>()); - - if (rasterizer->is_texture(baked_light->data.light_texture)) { - - Image img = rasterizer->texture_get_data(baked_light->data.light_texture); - return img.get_data(); - } else { - return PoolVector<uint8_t>(); - } -} - - - -void VisualServerRaster::baked_light_set_sampler_octree(RID p_baked_light, const PoolVector<int> &p_sampler) { - - BakedLight *baked_light = baked_light_owner.get(p_baked_light); - ERR_FAIL_COND(!baked_light); - - baked_light->sampler=p_sampler; - - - -} - -PoolVector<int> VisualServerRaster::baked_light_get_sampler_octree(RID p_baked_light) const { - - BakedLight *baked_light = baked_light_owner.get(p_baked_light); - ERR_FAIL_COND_V(!baked_light,PoolVector<int>()); - - return baked_light->sampler; - -} - - -void VisualServerRaster::baked_light_add_lightmap(RID p_baked_light,const RID p_texture,int p_id){ - - VS_CHANGED; - BakedLight *baked_light = baked_light_owner.get(p_baked_light); - ERR_FAIL_COND(!baked_light); - baked_light->data.lightmaps.insert(p_id,p_texture); - -} -void VisualServerRaster::baked_light_clear_lightmaps(RID p_baked_light){ - - VS_CHANGED; - BakedLight *baked_light = baked_light_owner.get(p_baked_light); - ERR_FAIL_COND(!baked_light); - baked_light->data.lightmaps.clear(); - -} - -void VisualServerRaster::baked_light_set_realtime_color_enabled(RID p_baked_light, const bool p_enabled) { - - VS_CHANGED; - BakedLight *baked_light = baked_light_owner.get(p_baked_light); - ERR_FAIL_COND(!baked_light); - - baked_light->data.realtime_color_enabled = p_enabled; - -} - -bool VisualServerRaster::baked_light_get_realtime_color_enabled(RID p_baked_light) const{ - - const BakedLight *baked_light = baked_light_owner.get(p_baked_light); - ERR_FAIL_COND_V(!baked_light, false); - - return baked_light->data.realtime_color_enabled; - -} - -void VisualServerRaster::baked_light_set_realtime_color(RID p_baked_light, const Color& p_color) { - - VS_CHANGED; - BakedLight *baked_light = baked_light_owner.get(p_baked_light); - ERR_FAIL_COND(!baked_light); - - baked_light->data.realtime_color = p_color; - -} - -Color VisualServerRaster::baked_light_get_realtime_color(RID p_baked_light) const{ - - const BakedLight *baked_light = baked_light_owner.get(p_baked_light); - ERR_FAIL_COND_V(!baked_light, Color(1.0, 1.0, 1.0)); - - return baked_light->data.realtime_color; - -} - -void VisualServerRaster::baked_light_set_realtime_energy(RID p_baked_light, const float p_energy) { - - VS_CHANGED; - BakedLight *baked_light = baked_light_owner.get(p_baked_light); - ERR_FAIL_COND(!baked_light); - - baked_light->data.realtime_energy = p_energy; - -} - -float VisualServerRaster::baked_light_get_realtime_energy(RID p_baked_light) const{ - - const BakedLight *baked_light = baked_light_owner.get(p_baked_light); - ERR_FAIL_COND_V(!baked_light, 1.0f); - - return baked_light->data.realtime_energy; - -} - - -/* BAKED LIGHT SAMPLER */ - -RID VisualServerRaster::baked_light_sampler_create() { - - BakedLightSampler * blsamp = memnew( BakedLightSampler ); - RID rid = baked_light_sampler_owner.make_rid(blsamp); - _update_baked_light_sampler_dp_cache(blsamp); - return rid; -} - -void VisualServerRaster::baked_light_sampler_set_param(RID p_baked_light_sampler,BakedLightSamplerParam p_param,float p_value){ - - VS_CHANGED; - BakedLightSampler * blsamp = baked_light_sampler_owner.get(p_baked_light_sampler); - ERR_FAIL_COND(!blsamp); - ERR_FAIL_INDEX(p_param,BAKED_LIGHT_SAMPLER_MAX); - blsamp->params[p_param]=p_value; - _dependency_queue_update(p_baked_light_sampler,true); -} - -float VisualServerRaster::baked_light_sampler_get_param(RID p_baked_light_sampler,BakedLightSamplerParam p_param) const{ - - - BakedLightSampler * blsamp = baked_light_sampler_owner.get(p_baked_light_sampler); - ERR_FAIL_COND_V(!blsamp,0); - ERR_FAIL_INDEX_V(p_param,BAKED_LIGHT_SAMPLER_MAX,0); - return blsamp->params[p_param]; -} - -void VisualServerRaster::_update_baked_light_sampler_dp_cache(BakedLightSampler * blsamp) { - - int res = blsamp->resolution; - blsamp->dp_cache.resize(res*res*2); - Vector3 *dp_normals=blsamp->dp_cache.ptr(); - - for(int p=0;p<2;p++) { - float sign = p==0?1:-1; - int ofs = res*res*p; - for(int i=0;i<res;i++) { - for(int j=0;j<res;j++) { - - Vector2 v( - (i/float(res))*2.0-1.0, - (j/float(res))*2.0-1.0 - ); - - float l=v.length(); - if (l>1.0) { - v/=l; - l=1.0; //clamp to avoid imaginary - } - v*=(2*l)/(l*l+1); //inverse of the dual paraboloid function - Vector3 n = Vector3(v.x,v.y,sign*sqrtf(MAX(1 - v.dot(v),0))); //reconstruction of z - n.y*=sign; - dp_normals[j*res+i+ofs]=n; - } - } - } - -} - -void VisualServerRaster::baked_light_sampler_set_resolution(RID p_baked_light_sampler,int p_resolution){ - - ERR_FAIL_COND(p_resolution<4 || p_resolution>64); - VS_CHANGED; - BakedLightSampler * blsamp = baked_light_sampler_owner.get(p_baked_light_sampler); - ERR_FAIL_COND(!blsamp); - blsamp->resolution=p_resolution; - _update_baked_light_sampler_dp_cache(blsamp); - -} -int VisualServerRaster::baked_light_sampler_get_resolution(RID p_baked_light_sampler) const{ - - BakedLightSampler * blsamp = baked_light_sampler_owner.get(p_baked_light_sampler); - ERR_FAIL_COND_V(!blsamp,0); - return blsamp->resolution; -} - -/* CAMERA API */ - -RID VisualServerRaster::camera_create() { - - Camera * camera = memnew( Camera ); - return camera_owner.make_rid( camera ); - -} - -void VisualServerRaster::camera_set_perspective(RID p_camera,float p_fovy_degrees, float p_z_near, float p_z_far) { - VS_CHANGED - Camera *camera = camera_owner.get( p_camera ); - ERR_FAIL_COND(!camera); - camera->type=Camera::PERSPECTIVE; - camera->fov=p_fovy_degrees; - camera->znear=p_z_near; - camera->zfar=p_z_far; - -} - -void VisualServerRaster::camera_set_orthogonal(RID p_camera,float p_size, float p_z_near, float p_z_far) { - VS_CHANGED; - Camera *camera = camera_owner.get( p_camera ); - ERR_FAIL_COND(!camera); - camera->type=Camera::ORTHOGONAL; - camera->size=p_size; - camera->znear=p_z_near; - camera->zfar=p_z_far; -} - -void VisualServerRaster::camera_set_transform(RID p_camera,const Transform& p_transform) { - VS_CHANGED; - Camera *camera = camera_owner.get( p_camera ); - ERR_FAIL_COND(!camera); - camera->transform=p_transform.orthonormalized(); - - -} - -void VisualServerRaster::camera_set_visible_layers(RID p_camera,uint32_t p_layers) { - - VS_CHANGED; - Camera *camera = camera_owner.get( p_camera ); - ERR_FAIL_COND(!camera); - - camera->visible_layers=p_layers; - -} - -uint32_t VisualServerRaster::camera_get_visible_layers(RID p_camera) const{ - - const Camera *camera = camera_owner.get( p_camera ); - ERR_FAIL_COND_V(!camera,0); - - return camera->visible_layers; -} - -void VisualServerRaster::camera_set_environment(RID p_camera,RID p_env) { - - Camera *camera = camera_owner.get( p_camera ); - ERR_FAIL_COND(!camera); - camera->env=p_env; - -} - -RID VisualServerRaster::camera_get_environment(RID p_camera) const { - - const Camera *camera = camera_owner.get( p_camera ); - ERR_FAIL_COND_V(!camera,RID()); - return camera->env; - -} - -void VisualServerRaster::camera_set_use_vertical_aspect(RID p_camera,bool p_enable) { - - Camera *camera = camera_owner.get( p_camera ); - ERR_FAIL_COND(!camera); - camera->vaspect=p_enable; - -} -bool VisualServerRaster::camera_is_using_vertical_aspect(RID p_camera,bool p_enable) const{ - - const Camera *camera = camera_owner.get( p_camera ); - ERR_FAIL_COND_V(!camera,false); - return camera->vaspect; - -} - - -/* VIEWPORT API */ - - -RID VisualServerRaster::viewport_create() { - - Viewport *viewport = memnew( Viewport ); - RID rid = viewport_owner.make_rid( viewport ); - ERR_FAIL_COND_V( !rid.is_valid(), rid ); - - viewport->self=rid; - viewport->hide_scenario=false; - viewport->hide_canvas=false; - viewport->viewport_data=rasterizer->viewport_data_create(); - - return rid; -} - -void VisualServerRaster::viewport_attach_to_screen(RID p_viewport,int p_screen) { - - VS_CHANGED; - Viewport *viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND(!viewport); - - - screen_viewports[p_viewport]=p_screen; -} - -void VisualServerRaster::viewport_detach(RID p_viewport) { - - VS_CHANGED; - Viewport *viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND(!viewport); - - ERR_FAIL_COND(!screen_viewports.has(p_viewport)); - screen_viewports.erase(p_viewport); - -} - -void VisualServerRaster::viewport_set_as_render_target(RID p_viewport,bool p_enable) { - - VS_CHANGED; - Viewport *viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND(!viewport); - - if (viewport->render_target.is_valid()==p_enable) - return; - if (!p_enable) { - - rasterizer->free(viewport->render_target); - viewport->render_target; - viewport->render_target_texture; - if (viewport->update_list.in_list()) - viewport_update_list.remove(&viewport->update_list); - - } else { - - viewport->render_target=rasterizer->render_target_create(); - rasterizer->render_target_set_size(viewport->render_target,viewport->rect.width,viewport->rect.height); - viewport->render_target_texture=rasterizer->render_target_get_texture(viewport->render_target); - if (viewport->render_target_update_mode!=RENDER_TARGET_UPDATE_DISABLED) - viewport_update_list.add(&viewport->update_list); - } - -} - -void VisualServerRaster::viewport_set_render_target_update_mode(RID p_viewport,RenderTargetUpdateMode p_mode){ - - VS_CHANGED; - Viewport *viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND(!viewport); - - if (viewport->render_target.is_valid() && viewport->update_list.in_list()) - viewport_update_list.remove(&viewport->update_list); - - viewport->render_target_update_mode=p_mode; - - if (viewport->render_target.is_valid() &&viewport->render_target_update_mode!=RENDER_TARGET_UPDATE_DISABLED) - viewport_update_list.add(&viewport->update_list); - -} -VisualServer::RenderTargetUpdateMode VisualServerRaster::viewport_get_render_target_update_mode(RID p_viewport) const{ - - const Viewport *viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND_V(!viewport,RENDER_TARGET_UPDATE_DISABLED); - - return viewport->render_target_update_mode; -} -RID VisualServerRaster::viewport_get_render_target_texture(RID p_viewport) const{ - - Viewport *viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND_V(!viewport,RID()); - - return viewport->render_target_texture; - -} - -void VisualServerRaster::viewport_set_render_target_vflip(RID p_viewport,bool p_enable) { - - Viewport *viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND(!viewport); - - viewport->render_target_vflip=p_enable; - -} - -void VisualServerRaster::viewport_set_render_target_clear_on_new_frame(RID p_viewport,bool p_enable) { - - Viewport *viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND(!viewport); - - viewport->render_target_clear_on_new_frame=p_enable; - -} - -void VisualServerRaster::viewport_set_render_target_to_screen_rect(RID p_viewport,const Rect2& p_rect) { - - Viewport *viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND(!viewport); - - viewport->rt_to_screen_rect=p_rect; - -} - -bool VisualServerRaster::viewport_get_render_target_vflip(RID p_viewport) const{ - - const Viewport *viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND_V(!viewport,false); - - return viewport->render_target_vflip; - -} - -bool VisualServerRaster::viewport_get_render_target_clear_on_new_frame(RID p_viewport) const{ - - const Viewport *viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND_V(!viewport,false); - - return viewport->render_target_clear_on_new_frame; - -} - -void VisualServerRaster::viewport_render_target_clear(RID p_viewport) { - - Viewport *viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND(!viewport); - - viewport->render_target_clear=true; - -} - -void VisualServerRaster::viewport_queue_screen_capture(RID p_viewport) { - - VS_CHANGED; - Viewport *viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND(!viewport); - viewport->queue_capture=true; - -} - -Image VisualServerRaster::viewport_get_screen_capture(RID p_viewport) const { - - Viewport *viewport = (Viewport*)viewport_owner.get( p_viewport ); - ERR_FAIL_COND_V(!viewport,Image()); - - Image ret = viewport->capture; - viewport->capture=Image(); - return ret; -} - -void VisualServerRaster::viewport_set_rect(RID p_viewport,const ViewportRect& p_rect) { - VS_CHANGED; - Viewport *viewport=NULL; - - viewport = viewport_owner.get( p_viewport ); - - ERR_FAIL_COND(!viewport); - - viewport->rect=p_rect; - if (viewport->render_target.is_valid()) { - rasterizer->render_target_set_size(viewport->render_target,viewport->rect.width,viewport->rect.height); - } -} - - -VisualServer::ViewportRect VisualServerRaster::viewport_get_rect(RID p_viewport) const { - - const Viewport *viewport=NULL; - - viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND_V(!viewport, ViewportRect()); - - return viewport->rect; -} - -void VisualServerRaster::viewport_set_hide_scenario(RID p_viewport,bool p_hide) { - - VS_CHANGED; - - Viewport *viewport=NULL; - - viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND(!viewport); - - viewport->hide_scenario=p_hide; - - -} - -void VisualServerRaster::viewport_set_hide_canvas(RID p_viewport,bool p_hide) { - - VS_CHANGED; - - Viewport *viewport=NULL; - - viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND(!viewport); - - viewport->hide_canvas=p_hide; - - -} - -void VisualServerRaster::viewport_set_disable_environment(RID p_viewport,bool p_disable) { - - VS_CHANGED; - - Viewport *viewport=NULL; - viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND(!viewport); - viewport->disable_environment=p_disable; - -} - -void VisualServerRaster::viewport_attach_camera(RID p_viewport,RID p_camera) { - VS_CHANGED; - - Viewport *viewport=NULL; - viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND(!viewport); - - - - - if (p_camera.is_valid()) { - - ERR_FAIL_COND(!camera_owner.owns(p_camera)); - // a camera - viewport->camera=p_camera; - } else { - viewport->camera; - } - -} - -void VisualServerRaster::viewport_set_scenario(RID p_viewport,RID p_scenario) { - - VS_CHANGED; - - Viewport *viewport=NULL; - viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND(!viewport); - - if (p_scenario.is_valid()) { - - ERR_FAIL_COND(!scenario_owner.owns(p_scenario)); - // a camera - viewport->scenario=p_scenario; - } else { - viewport->scenario; - } - -} - -RID VisualServerRaster::viewport_get_attached_camera(RID p_viewport) const { - - const Viewport *viewport=NULL; - - viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND_V(!viewport, RID()); - - return viewport->camera; -} - -void VisualServerRaster::viewport_attach_canvas(RID p_viewport,RID p_canvas) { - VS_CHANGED; - Viewport *viewport=NULL; - - viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND(!viewport); - - Canvas *canvas = canvas_owner.get( p_canvas ); - ERR_FAIL_COND(!canvas); - - ERR_EXPLAIN("Canvas already attached."); - ERR_FAIL_COND(viewport->canvas_map.has(p_canvas)); - - - Viewport::CanvasData cd; - cd.canvas=canvas; - cd.layer=0; - - viewport->canvas_map[p_canvas]=cd; - canvas->viewports.insert(p_viewport); - -} - - -void VisualServerRaster::viewport_set_canvas_transform(RID p_viewport,RID p_canvas,const Matrix32& p_transform) { - - VS_CHANGED; - Viewport *viewport=NULL; - viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND(!viewport); - - Map<RID,Viewport::CanvasData>::Element *E=viewport->canvas_map.find(p_canvas); - if (!E) { - ERR_EXPLAIN("Viewport does not contain the canvas"); - ERR_FAIL_COND(!E); - } - - E->get().transform=p_transform; - -} - -Matrix32 VisualServerRaster::viewport_get_canvas_transform(RID p_viewport,RID p_canvas) const { - - Viewport *viewport=NULL; - viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND_V(!viewport,Matrix32()); - - Map<RID,Viewport::CanvasData>::Element *E=viewport->canvas_map.find(p_canvas); - if (!E) { - ERR_EXPLAIN("Viewport does not contain the canvas"); - ERR_FAIL_COND_V(!E,Matrix32()); - } - - - return E->get().transform; -} - - -void VisualServerRaster::viewport_set_global_canvas_transform(RID p_viewport,const Matrix32& p_transform) { - - VS_CHANGED - Viewport *viewport=NULL; - viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND(!viewport); - - viewport->global_transform=p_transform; - -} - -Matrix32 VisualServerRaster::viewport_get_global_canvas_transform(RID p_viewport) const{ - - Viewport *viewport=NULL; - viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND_V(!viewport,Matrix32()); - return viewport->global_transform; -} - -void VisualServerRaster::viewport_remove_canvas(RID p_viewport,RID p_canvas) { - - VS_CHANGED; - Viewport *viewport=NULL; - - viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND(!viewport); - - Canvas *canvas = canvas_owner.get( p_canvas ); - ERR_FAIL_COND(!canvas); - - - Map<RID,Viewport::CanvasData>::Element *E=viewport->canvas_map.find(p_canvas); - if (!E) { - ERR_EXPLAIN("Viewport does not contain the canvas"); - ERR_FAIL_COND(!E); - } - - - canvas->viewports.erase(p_viewport); - viewport->canvas_map.erase(E); - -} - - -void VisualServerRaster::viewport_set_canvas_layer(RID p_viewport,RID p_canvas,int p_layer) { - - VS_CHANGED; - Viewport *viewport=NULL; - - viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND(!viewport); - - Map<RID,Viewport::CanvasData>::Element *E=viewport->canvas_map.find(p_canvas); - if (!E) { - ERR_EXPLAIN("Viewport does not contain the canvas"); - ERR_FAIL_COND(!E); - } - - E->get().layer=p_layer; - -} - -void VisualServerRaster::viewport_set_transparent_background(RID p_viewport,bool p_enabled) { - - VS_CHANGED; - Viewport *viewport=viewport_owner.get( p_viewport ); - ERR_FAIL_COND(!viewport); - - viewport->transparent_bg=p_enabled; -} - -bool VisualServerRaster::viewport_has_transparent_background(RID p_viewport) const { - - Viewport *viewport=viewport_owner.get( p_viewport ); - ERR_FAIL_COND_V(!viewport, false); - - return viewport->transparent_bg; -} - - -RID VisualServerRaster::viewport_get_scenario(RID p_viewport) const { - - const Viewport *viewport=NULL; - - viewport = viewport_owner.get( p_viewport ); - ERR_FAIL_COND_V(!viewport, RID()); - - return viewport->scenario; -} - - -RID VisualServerRaster::environment_create() { - - return rasterizer->environment_create(); -} - -void VisualServerRaster::environment_set_background(RID p_env,EnvironmentBG p_bg){ - - rasterizer->environment_set_background(p_env,p_bg); -} -VisualServer::EnvironmentBG VisualServerRaster::environment_get_background(RID p_env) const{ - - return rasterizer->environment_get_background(p_env); -} - -void VisualServerRaster::environment_set_background_param(RID p_env,EnvironmentBGParam p_param, const Variant& p_value){ - - - rasterizer->environment_set_background_param(p_env,p_param,p_value); -} -Variant VisualServerRaster::environment_get_background_param(RID p_env,EnvironmentBGParam p_param) const{ - - return rasterizer->environment_get_background_param(p_env,p_param); -} - -void VisualServerRaster::environment_set_enable_fx(RID p_env,EnvironmentFx p_effect,bool p_enabled){ - - rasterizer->environment_set_enable_fx(p_env,p_effect,p_enabled); -} -bool VisualServerRaster::environment_is_fx_enabled(RID p_env,EnvironmentFx p_effect) const{ - - return rasterizer->environment_is_fx_enabled(p_env,p_effect); -} - - -void VisualServerRaster::environment_fx_set_param(RID p_env,EnvironmentFxParam p_param,const Variant& p_value){ - - rasterizer->environment_fx_set_param(p_env,p_param,p_value); -} -Variant VisualServerRaster::environment_fx_get_param(RID p_env,EnvironmentFxParam p_param) const { - - return environment_fx_get_param(p_env,p_param); -} - - - -/* SCENARIO API */ - -void VisualServerRaster::_dependency_queue_update(RID p_rid,bool p_update_aabb,bool p_update_materials) { - - Map< RID, Set<RID> >::Element * E = instance_dependency_map.find( p_rid ); - - if (!E) - return; - - - Set<RID>::Element *I = E->get().front(); - - while(I) { - - Instance *ins = instance_owner.get( I->get() ); - _instance_queue_update( ins , p_update_aabb, p_update_materials ); - - I = I->next(); - } - -} - -void VisualServerRaster::_instance_queue_update(Instance *p_instance,bool p_update_aabb,bool p_update_materials) { - - if (p_update_aabb) - p_instance->update_aabb=true; - if (p_update_materials) - p_instance->update_materials=true; - - if (p_instance->update) - return; - p_instance->update_next=instance_update_list; - instance_update_list=p_instance; - p_instance->update=true; - -} - -RID VisualServerRaster::scenario_create() { - - Scenario *scenario = memnew( Scenario ); - ERR_FAIL_COND_V(!scenario,RID()); - RID scenario_rid = scenario_owner.make_rid( scenario ); - scenario->self=scenario_rid; - scenario->octree.set_pair_callback(instance_pair,this); - scenario->octree.set_unpair_callback(instance_unpair,this); - - return scenario_rid; -} - -void VisualServerRaster::scenario_set_debug(RID p_scenario,ScenarioDebugMode p_debug_mode) { - VS_CHANGED; - - Scenario *scenario = scenario_owner.get(p_scenario); - ERR_FAIL_COND(!scenario); - scenario->debug=p_debug_mode; -} - -void VisualServerRaster::scenario_set_environment(RID p_scenario, RID p_environment) { - - VS_CHANGED; - - Scenario *scenario = scenario_owner.get(p_scenario); - ERR_FAIL_COND(!scenario); - scenario->environment=p_environment; - -} - -void VisualServerRaster::scenario_set_fallback_environment(RID p_scenario, RID p_environment) { - - VS_CHANGED; - - Scenario *scenario = scenario_owner.get(p_scenario); - ERR_FAIL_COND(!scenario); - scenario->fallback_environment=p_environment; - - -} - -RID VisualServerRaster::scenario_get_environment(RID p_scenario, RID p_environment) const{ - - const Scenario *scenario = scenario_owner.get(p_scenario); - ERR_FAIL_COND_V(!scenario,RID()); - return scenario->environment; - -} - - -/* INSTANCING API */ - - -RID VisualServerRaster::instance_create() { - - Instance *instance = memnew( Instance ); - ERR_FAIL_COND_V(!instance,RID()); - - RID instance_rid = instance_owner.make_rid(instance); - instance->self=instance_rid; - instance->base_type=INSTANCE_NONE; - instance->scenario=NULL; - - return instance_rid; -} - -void VisualServerRaster::instance_set_base(RID p_instance, RID p_base) { - - VS_CHANGED; - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND( !instance ); - - if (instance->base_type!=INSTANCE_NONE) { - //free anything related to that base - - Map< RID, Set<RID> >::Element * E = instance_dependency_map.find( instance->base_rid ); - - if (E) { - // wtf, no E? - E->get().erase( instance->self ); - - } else { - - ERR_PRINT("no base E? Bug?"); - } - - if ( instance->room ) { - - instance_set_room(p_instance,RID()); - /* - if((1<<instance->base_type)&INSTANCE_GEOMETRY_MASK) - instance->room->room_info->owned_geometry_instances.erase(instance->RE); - else if (instance->base_type==INSTANCE_PORTAL) { - print_line("freeing portal, is it there? "+itos(instance->room->room_info->owned_portal_instances.(instance->RE))); - instance->room->room_info->owned_portal_instances.erase(instance->RE); - } else if (instance->base_type==INSTANCE_ROOM) - instance->room->room_info->owned_room_instances.erase(instance->RE); - else if (instance->base_type==INSTANCE_LIGHT) - instance->room->room_info->owned_light_instances.erase(instance->RE); - - instance->RE=NULL;*/ - } - - - - - - if (instance->light_info) { - - if (instance->scenario && instance->light_info->D) - instance->scenario->directional_lights.erase( instance->light_info->D ); - rasterizer->free(instance->light_info->instance); - memdelete(instance->light_info); - instance->light_info=NULL; - } - - - if (instance->portal_info) { - - _portal_disconnect(instance,true); - memdelete(instance->portal_info); - instance->portal_info=NULL; - - } - - if (instance->baked_light_info) { - - while(instance->baked_light_info->owned_instances.size()) { - - Instance *owned=instance->baked_light_info->owned_instances.front()->get(); - owned->baked_light=NULL; - owned->data.baked_light=NULL; - owned->data.baked_light_octree_xform=NULL; - owned->BLE=NULL; - instance->baked_light_info->owned_instances.pop_front(); - } - - memdelete(instance->baked_light_info); - instance->baked_light_info=NULL; - - } - - if (instance->scenario && instance->octree_id) { - instance->scenario->octree.erase( instance->octree_id ); - instance->octree_id=0; - } - - - if (instance->room_info) { - - for(List<Instance*>::Element *E=instance->room_info->owned_geometry_instances.front();E;E=E->next()) { - - Instance *owned = E->get(); - owned->room=NULL; - owned->RE=NULL; - } - for(List<Instance*>::Element *E=instance->room_info->owned_portal_instances.front();E;E=E->next()) { - - _portal_disconnect(E->get(),true); - Instance *owned = E->get(); - owned->room=NULL; - owned->RE=NULL; - } - - for(List<Instance*>::Element *E=instance->room_info->owned_room_instances.front();E;E=E->next()) { - - Instance *owned = E->get(); - owned->room=NULL; - owned->RE=NULL; - } - - if (instance->room_info->disconnected_child_portals.size()) { - ERR_PRINT("BUG: Disconnected portals remain!"); - } - memdelete(instance->room_info); - instance->room_info=NULL; - - } - - if (instance->particles_info) { - - rasterizer->free( instance->particles_info->instance ); - memdelete(instance->particles_info); - instance->particles_info=NULL; - - } - - if (instance->baked_light_sampler_info) { - - while (instance->baked_light_sampler_info->owned_instances.size()) { - - instance_geometry_set_baked_light_sampler(instance->baked_light_sampler_info->owned_instances.front()->get()->self,RID()); - } - - if (instance->baked_light_sampler_info->sampled_light.is_valid()) { - rasterizer->free(instance->baked_light_sampler_info->sampled_light); - } - memdelete( instance->baked_light_sampler_info ); - instance->baked_light_sampler_info=NULL; - } - - instance->data.morph_values.clear(); - instance->data.materials.clear(); - - } - - - instance->base_type=INSTANCE_NONE; - instance->base_rid; - - - if (p_base.is_valid()) { - - if (rasterizer->is_mesh(p_base)) { - instance->base_type=INSTANCE_MESH; - instance->data.morph_values.resize( rasterizer->mesh_get_morph_target_count(p_base)); - instance->data.materials.resize( rasterizer->mesh_get_surface_count(p_base)); - } else if (rasterizer->is_multimesh(p_base)) { - instance->base_type=INSTANCE_MULTIMESH; - } else if (rasterizer->is_immediate(p_base)) { - instance->base_type=INSTANCE_IMMEDIATE; - } else if (rasterizer->is_particles(p_base)) { - instance->base_type=INSTANCE_PARTICLES; - instance->particles_info=memnew( Instance::ParticlesInfo ); - instance->particles_info->instance = rasterizer->particles_instance_create( p_base ); - } else if (rasterizer->is_light(p_base)) { - - instance->base_type=INSTANCE_LIGHT; - instance->light_info = memnew( Instance::LightInfo ); - instance->light_info->instance = rasterizer->light_instance_create(p_base); - if (instance->scenario && rasterizer->light_get_type(p_base)==LIGHT_DIRECTIONAL) { - - instance->light_info->D = instance->scenario->directional_lights.push_back(instance->self); - } - - } else if (room_owner.owns(p_base)) { - instance->base_type=INSTANCE_ROOM; - instance->room_info = memnew( Instance::RoomInfo ); - instance->room_info->room=room_owner.get(p_base); - } else if (portal_owner.owns(p_base)) { - - instance->base_type=INSTANCE_PORTAL; - instance->portal_info = memnew(Instance::PortalInfo); - instance->portal_info->portal=portal_owner.get(p_base); - } else if (baked_light_owner.owns(p_base)) { - - instance->base_type=INSTANCE_BAKED_LIGHT; - instance->baked_light_info=memnew(Instance::BakedLightInfo); - instance->baked_light_info->baked_light=baked_light_owner.get(p_base); - - //instance->portal_info = memnew(Instance::PortalInfo); - //instance->portal_info->portal=portal_owner.get(p_base); - } else if (baked_light_sampler_owner.owns(p_base)) { - - - instance->base_type=INSTANCE_BAKED_LIGHT_SAMPLER; - instance->baked_light_sampler_info=memnew( Instance::BakedLightSamplerInfo); - instance->baked_light_sampler_info->sampler=baked_light_sampler_owner.get(p_base); - - //instance->portal_info = memnew(Instance::PortalInfo); - //instance->portal_info->portal=portal_owner.get(p_base); - - } else { - ERR_EXPLAIN("Invalid base RID for instance!") - ERR_FAIL(); - } - - instance_dependency_map[ p_base ].insert( instance->self ); - - instance->base_rid=p_base; - - if (instance->scenario) - _instance_queue_update(instance,true); - } - -} - -RID VisualServerRaster::instance_get_base(RID p_instance) const { - - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND_V( !instance, RID() ); - return instance->base_rid; - -} - -void VisualServerRaster::instance_set_scenario(RID p_instance, RID p_scenario) { - - VS_CHANGED; - - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND( !instance ); - - if (instance->scenario) { - - Map< RID, Set<RID> >::Element *E = instance_dependency_map.find( instance->scenario->self ); - - if (E) { - // wtf, no E? - E->get().erase( instance->self ); - - } else { - - ERR_PRINT("no scenario E? Bug?"); - } - - if (instance->light_info) { - - if (instance->light_info->D) - instance->scenario->directional_lights.erase( instance->light_info->D ); - } - - if (instance->portal_info) { - - _portal_disconnect(instance,true); - } - - if (instance->octree_id) { - instance->scenario->octree.erase( instance->octree_id ); - instance->octree_id=0; - } - - instance->scenario=NULL; - } - - - if (p_scenario.is_valid()) { - Scenario *scenario = scenario_owner.get( p_scenario ); - ERR_FAIL_COND(!scenario); - - instance->scenario=scenario; - - instance_dependency_map[ p_scenario ].insert( instance->self ); - instance->scenario=scenario; - - if (instance->base_type==INSTANCE_LIGHT && rasterizer->light_get_type(instance->base_rid)==LIGHT_DIRECTIONAL) { - - instance->light_info->D = instance->scenario->directional_lights.push_back(instance->self); - } - - _instance_queue_update(instance,true); - } - -} -RID VisualServerRaster::instance_get_scenario(RID p_instance) const { - - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND_V( !instance, RID() ); - if (instance->scenario) - return instance->scenario->self; - else - return RID(); - - -} - - -void VisualServerRaster::instance_set_layer_mask(RID p_instance, uint32_t p_mask) { - - VS_CHANGED; - - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND( !instance ); - - instance->layer_mask=p_mask; - -} - -uint32_t VisualServerRaster::instance_get_layer_mask(RID p_instance) const{ - - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND_V( !instance, 0 ); - - return instance->layer_mask; -} - - -AABB VisualServerRaster::instance_get_base_aabb(RID p_instance) const { - - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND_V( !instance, AABB() ); - return instance->aabb; - -} - -void VisualServerRaster::instance_attach_object_instance_id(RID p_instance,uint32_t p_ID) { - VS_CHANGED; - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND( !instance ); - - instance->object_ID=p_ID; -} -uint32_t VisualServerRaster::instance_get_object_instance_id(RID p_instance) const { - - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND_V( !instance, 0 ); - return instance->object_ID; - -} - -void VisualServerRaster::instance_attach_skeleton(RID p_instance,RID p_skeleton) { - VS_CHANGED; - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND( !instance ); - - if (instance->data.skeleton.is_valid()) { - skeleton_dependency_map[instance->data.skeleton].erase(instance); - } - - instance->data.skeleton=p_skeleton; - - if (instance->data.skeleton.is_valid()) { - skeleton_dependency_map[instance->data.skeleton].insert(instance); - } - -} - -RID VisualServerRaster::instance_get_skeleton(RID p_instance) const { - - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND_V( !instance, RID() ); - return instance->data.skeleton; - -} - -void VisualServerRaster::instance_set_morph_target_weight(RID p_instance,int p_shape, float p_weight) { - - VS_CHANGED; - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND( !instance ); - ERR_FAIL_INDEX( p_shape, instance->data.morph_values.size() ); - instance->data.morph_values[p_shape]=p_weight; -} - -float VisualServerRaster::instance_get_morph_target_weight(RID p_instance,int p_shape) const { - - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND_V( !instance, 0 ); - ERR_FAIL_INDEX_V( p_shape, instance->data.morph_values.size(), 0 ); - return instance->data.morph_values[p_shape]; -} - -void VisualServerRaster::instance_set_surface_material(RID p_instance,int p_surface, RID p_material) { - - VS_CHANGED; - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND( !instance); - ERR_FAIL_INDEX( p_surface, instance->data.materials.size() ); - instance->data.materials[p_surface]=p_material; -} - - -void VisualServerRaster::instance_set_transform(RID p_instance, const Transform& p_transform) { - VS_CHANGED; - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND( !instance ); - - if (p_transform==instance->data.transform) // must improve somehow - return; - - instance->data.transform=p_transform; - if (instance->base_type==INSTANCE_LIGHT) - instance->data.transform.orthonormalize(); - _instance_queue_update(instance); - -} - -Transform VisualServerRaster::instance_get_transform(RID p_instance) const { - - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND_V( !instance, Transform() ); - - return instance->data.transform; - -} - -void VisualServerRaster::instance_set_exterior( RID p_instance, bool p_enabled ) { - VS_CHANGED; - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND( !instance ); - - ERR_EXPLAIN("Portals can't be assigned to be exterior"); - - ERR_FAIL_COND( instance->base_type == INSTANCE_PORTAL ); - if (instance->exterior==p_enabled) - return; - instance->exterior=p_enabled; - _instance_queue_update( instance ); - - -} - -bool VisualServerRaster::instance_is_exterior( RID p_instance) const { - - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND_V( !instance, false ); - - return instance->exterior; -} - - -void VisualServerRaster::instance_set_room( RID p_instance, RID p_room ) { - VS_CHANGED; - - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND( !instance ); - - if (instance->room && instance->RE) { - - //instance already havs a room, remove it from there - - if ( (1<<instance->base_type) & INSTANCE_GEOMETRY_MASK ) { - - instance->room->room_info->owned_geometry_instances.erase(instance->RE); - - if (!p_room.is_valid() && instance->octree_id) { - //remove from the octree, so it's re-added with different flags - instance->scenario->octree.erase( instance->octree_id ); - instance->octree_id=0; - _instance_queue_update( instance,true ); - } - - - } else if ( instance->base_type==INSTANCE_ROOM ) { - - instance->room->room_info->owned_room_instances.erase(instance->RE); - - for(List<Instance*>::Element *E=instance->room_info->owned_portal_instances.front();E;E=E->next()) { - _portal_disconnect(E->get()); - _instance_queue_update( E->get(),false ); - } - - } else if ( instance->base_type==INSTANCE_PORTAL ) { - - _portal_disconnect(instance,true); - bool ss = instance->room->room_info->owned_portal_instances.erase(instance->RE); - - } else if ( instance->base_type==INSTANCE_LIGHT ) { - - instance->room->room_info->owned_light_instances.erase(instance->RE); - } else { - - ERR_FAIL(); - - } - - instance->RE=NULL; - instance->room=NULL; - - - } else { - - if (p_room.is_valid() && instance->octree_id) { - //remove from the octree, so it's re-added with different flags - instance->scenario->octree.erase( instance->octree_id ); - instance->octree_id=0; - _instance_queue_update( instance,true ); - } - - } - - if (!p_room.is_valid()) - return; // just clearning the room - - Instance *room = instance_owner.get( p_room ); - - ERR_FAIL_COND( !room ); - ERR_FAIL_COND( room->base_type!=INSTANCE_ROOM ); - - - if (instance->base_type==INSTANCE_ROOM) { - - //perform cycle test - - Instance *parent = instance; - - while(parent) { - - ERR_EXPLAIN("Cycle in room assignment"); - ERR_FAIL_COND( parent == room ); - parent=parent->room; - } - } - - if ( (1<<instance->base_type) & INSTANCE_GEOMETRY_MASK ) { - - instance->RE = room->room_info->owned_geometry_instances.push_back(instance); - } else if ( instance->base_type==INSTANCE_ROOM ) { - - instance->RE = room->room_info->owned_room_instances.push_back(instance); - for(List<Instance*>::Element *E=instance->room_info->owned_portal_instances.front();E;E=E->next()) - _instance_queue_update( E->get(),false ); - - - } else if ( instance->base_type==INSTANCE_PORTAL ) { - - instance->RE = room->room_info->owned_portal_instances.push_back(instance); - } else if ( instance->base_type==INSTANCE_LIGHT ) { - - instance->RE = room->room_info->owned_light_instances.push_back(instance); - } else { - - ERR_FAIL(); - - } - - instance->room=room; -} - -RID VisualServerRaster::instance_get_room( RID p_instance ) const { - - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND_V( !instance, RID() ); - - if (instance->room) - return instance->room->self; - else - return RID(); -} - -void VisualServerRaster::instance_set_extra_visibility_margin( RID p_instance, real_t p_margin ) { - - VS_CHANGED; - - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND( !instance ); - - instance->extra_margin=p_margin; -} -real_t VisualServerRaster::instance_get_extra_visibility_margin( RID p_instance ) const{ - - - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND_V( !instance, 0 ); - - return instance->extra_margin; -} - - -Vector<RID> VisualServerRaster::instances_cull_aabb(const AABB& p_aabb, RID p_scenario) const { - - - Vector<RID> instances; - Scenario *scenario=scenario_owner.get(p_scenario); - ERR_FAIL_COND_V(!scenario,instances); - - const_cast<VisualServerRaster*>(this)->_update_instances(); // check dirty instances before culling - - int culled=0; - Instance *cull[1024]; - culled=scenario->octree.cull_aabb(p_aabb,cull,1024); - - for (int i=0;i<culled;i++) { - - Instance *instance=cull[i]; - ERR_CONTINUE(!instance); - instances.push_back(instance->self); - } - - return instances; -} -Vector<RID> VisualServerRaster::instances_cull_ray(const Vector3& p_from, const Vector3& p_to, RID p_scenario) const{ - - Vector<RID> instances; - Scenario *scenario=scenario_owner.get(p_scenario); - ERR_FAIL_COND_V(!scenario,instances); - const_cast<VisualServerRaster*>(this)->_update_instances(); // check dirty instances before culling - - int culled=0; - Instance *cull[1024]; - culled=scenario->octree.cull_segment(p_from,p_to*10000,cull,1024); - - - for (int i=0;i<culled;i++) { - - Instance *instance=cull[i]; - ERR_CONTINUE(!instance); - instances.push_back(instance->self); - } - - return instances; - -} -Vector<RID> VisualServerRaster::instances_cull_convex(const Vector<Plane>& p_convex, RID p_scenario) const{ - - Vector<RID> instances; - Scenario *scenario=scenario_owner.get(p_scenario); - ERR_FAIL_COND_V(!scenario,instances); - const_cast<VisualServerRaster*>(this)->_update_instances(); // check dirty instances before culling - - int culled=0; - Instance *cull[1024]; - - - culled=scenario->octree.cull_convex(p_convex,cull,1024); - - for (int i=0;i<culled;i++) { - - Instance *instance=cull[i]; - ERR_CONTINUE(!instance); - instances.push_back(instance->self); - } - - return instances; - -} - -void VisualServerRaster::instance_geometry_set_flag(RID p_instance,InstanceFlags p_flags,bool p_enabled) { - - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND( !instance ); - //ERR_FAIL_COND( ! ( (1<<instance->base_type) & INSTANCE_GEOMETRY_MASK) ); - - switch(p_flags) { - - case INSTANCE_FLAG_VISIBLE: { - - instance->visible=p_enabled; - - } break; - case INSTANCE_FLAG_BILLBOARD: { - - instance->data.billboard=p_enabled; - - } break; - case INSTANCE_FLAG_BILLBOARD_FIX_Y: { - - instance->data.billboard_y=p_enabled; - - } break; - case INSTANCE_FLAG_CAST_SHADOW: { - if (p_enabled == true) { - instance->data.cast_shadows = SHADOW_CASTING_SETTING_ON; - } - else { - instance->data.cast_shadows = SHADOW_CASTING_SETTING_OFF; - } - - } break; - case INSTANCE_FLAG_RECEIVE_SHADOWS: { - - instance->data.receive_shadows=p_enabled; - - } break; - case INSTANCE_FLAG_DEPH_SCALE: { - - instance->data.depth_scale=p_enabled; - - } break; - case INSTANCE_FLAG_VISIBLE_IN_ALL_ROOMS: { - - instance->visible_in_all_rooms=p_enabled; - - } break; - - } - -} - -bool VisualServerRaster::instance_geometry_get_flag(RID p_instance,InstanceFlags p_flags) const{ - - const Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND_V( !instance, false ); - //ERR_FAIL_COND_V( ! ( (1<<instance->base_type) & INSTANCE_GEOMETRY_MASK), false ); - - switch(p_flags) { - - case INSTANCE_FLAG_VISIBLE: { - - return instance->visible; - - } break; - case INSTANCE_FLAG_BILLBOARD: { - - return instance->data.billboard; - - } break; - case INSTANCE_FLAG_BILLBOARD_FIX_Y: { - - return instance->data.billboard_y; - - } break; - case INSTANCE_FLAG_CAST_SHADOW: { - if(instance->data.cast_shadows == SHADOW_CASTING_SETTING_OFF) { - return false; - } - else { - return true; - } - - } break; - case INSTANCE_FLAG_RECEIVE_SHADOWS: { - - return instance->data.receive_shadows; - - } break; - case INSTANCE_FLAG_DEPH_SCALE: { - - return instance->data.depth_scale; - - } break; - case INSTANCE_FLAG_VISIBLE_IN_ALL_ROOMS: { - - return instance->visible_in_all_rooms; - - } break; - - } - - return false; -} - -void VisualServerRaster::instance_geometry_set_cast_shadows_setting(RID p_instance, VS::ShadowCastingSetting p_shadow_casting_setting) { - - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND( !instance ); - - instance->data.cast_shadows = p_shadow_casting_setting; -} - -VS::ShadowCastingSetting VisualServerRaster::instance_geometry_get_cast_shadows_setting(RID p_instance) const{ - - const Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND_V( !instance, SHADOW_CASTING_SETTING_OFF ); - - return instance->data.cast_shadows; -} - - -void VisualServerRaster::instance_geometry_set_material_override(RID p_instance, RID p_material) { - - VS_CHANGED; - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND( !instance ); - instance->data.material_override=p_material; - -} - -RID VisualServerRaster::instance_geometry_get_material_override(RID p_instance) const{ - - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND_V( !instance, RID() ); - return instance->data.material_override; - -} - -void VisualServerRaster::instance_geometry_set_draw_range(RID p_instance,float p_min,float p_max){ - - VS_CHANGED; - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND( !instance ); - - instance->draw_range_begin=p_min; - instance->draw_range_end=p_max; - -} - -float VisualServerRaster::instance_geometry_get_draw_range_min(RID p_instance) const{ - - const Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND_V( !instance,0 ); - - return instance->draw_range_begin; - - -} - -float VisualServerRaster::instance_geometry_get_draw_range_max(RID p_instance) const{ - - const Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND_V( !instance,0 ); - - return instance->draw_range_end; - - -} - - -void VisualServerRaster::instance_geometry_set_baked_light(RID p_instance,RID p_baked_light) { - - VS_CHANGED; - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND( !instance ); - - - if (instance->baked_light) { - - - instance->baked_light->baked_light_info->owned_instances.erase(instance->BLE); - instance->BLE=NULL; - instance->baked_light=NULL; - instance->data.baked_light=NULL; - instance->data.baked_light_octree_xform=NULL; - - } - - if (!p_baked_light.is_valid()) - return; - Instance *bl_instance = instance_owner.get( p_baked_light ); - ERR_FAIL_COND( !bl_instance ); - ERR_FAIL_COND( bl_instance->base_type!=INSTANCE_BAKED_LIGHT ); - - instance->baked_light=bl_instance; - instance->BLE=bl_instance->baked_light_info->owned_instances.push_back(instance); - instance->data.baked_light=&bl_instance->baked_light_info->baked_light->data; - instance->data.baked_light_octree_xform=&bl_instance->baked_light_info->affine_inverse; - -} - -RID VisualServerRaster::instance_geometry_get_baked_light(RID p_instance) const{ - - const Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND_V( !instance,RID() ); - if (instance->baked_light) - return instance->baked_light->self; - return RID(); - -} - - -void VisualServerRaster::instance_geometry_set_baked_light_sampler(RID p_instance,RID p_baked_light_sampler) { - - VS_CHANGED; - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND( !instance ); - - if (instance->sampled_light) { - instance->sampled_light->baked_light_sampler_info->owned_instances.erase(instance); - instance->data.sampled_light; - } - - if(p_baked_light_sampler.is_valid()) { - Instance *sampler_instance = instance_owner.get( p_baked_light_sampler ); - ERR_FAIL_COND( !sampler_instance ); - ERR_FAIL_COND( sampler_instance->base_type!=INSTANCE_BAKED_LIGHT_SAMPLER ); - instance->sampled_light=sampler_instance; - instance->sampled_light->baked_light_sampler_info->owned_instances.insert(instance); - } else { - instance->sampled_light=NULL; - } - - instance->data.sampled_light; - -} - -RID VisualServerRaster::instance_geometry_get_baked_light_sampler(RID p_instance) const { - - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND_V( !instance,RID() ); - - if (instance->sampled_light) - return instance->sampled_light->self; - else - return RID(); - -} - - -void VisualServerRaster::instance_geometry_set_baked_light_texture_index(RID p_instance,int p_tex_id){ - - VS_CHANGED; - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND( !instance ); - - instance->data.baked_lightmap_id=p_tex_id; - - -} -int VisualServerRaster::instance_geometry_get_baked_light_texture_index(RID p_instance) const{ - - const Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND_V( !instance,0 ); - - return instance->data.baked_lightmap_id; - -} - - -void VisualServerRaster::_update_instance(Instance *p_instance) { - - p_instance->version++; - - if (p_instance->base_type == INSTANCE_LIGHT) { - - rasterizer->light_instance_set_transform( p_instance->light_info->instance, p_instance->data.transform ); - - } - - - if (p_instance->aabb.has_no_surface()) - return; - - - if (p_instance->base_type == INSTANCE_PARTICLES) { - - rasterizer->particles_instance_set_transform( p_instance->particles_info->instance, p_instance->data.transform ); - } - - - if ((1<<p_instance->base_type)&INSTANCE_GEOMETRY_MASK) { - - //make sure lights are updated - InstanceSet::Element *E=p_instance->lights.front(); - while(E) { - - E->get()->version++; - E=E->next(); - } - - } else if (p_instance->base_type == INSTANCE_ROOM) { - - p_instance->room_info->affine_inverse=p_instance->data.transform.affine_inverse(); - } else if (p_instance->base_type == INSTANCE_BAKED_LIGHT) { - - Transform scale; - scale.basis.scale(p_instance->baked_light_info->baked_light->octree_aabb.size); - scale.origin=p_instance->baked_light_info->baked_light->octree_aabb.pos; - //print_line("scale: "+scale); - p_instance->baked_light_info->affine_inverse=(p_instance->data.transform*scale).affine_inverse(); - } - - - - p_instance->data.mirror = p_instance->data.transform.basis.determinant() < 0.0; - - AABB new_aabb; - - if (p_instance->base_type==INSTANCE_PORTAL) { - - //portals need to be transformed in a special way, so they don't become too wide if they have scale.. - Transform portal_xform = p_instance->data.transform; - portal_xform.basis.set_axis(2,portal_xform.basis.get_axis(2).normalized()); - - p_instance->portal_info->plane_cache=Plane( p_instance->data.transform.origin, portal_xform.basis.get_axis(2)); - int point_count=p_instance->portal_info->portal->shape.size(); - p_instance->portal_info->transformed_point_cache.resize(point_count); - - AABB portal_aabb; - - for(int i=0;i<point_count;i++) { - - Point2 src = p_instance->portal_info->portal->shape[i]; - Vector3 point = portal_xform.xform(Vector3(src.x,src.y,0)); - p_instance->portal_info->transformed_point_cache[i]=point; - if (i==0) - portal_aabb.pos=point; - else - portal_aabb.expand_to(point); - } - - portal_aabb.grow_by(p_instance->portal_info->portal->connect_range); - - new_aabb = portal_aabb; - - } else { - - new_aabb = p_instance->data.transform.xform(p_instance->aabb); - } - - - for(InstanceSet::Element *E=p_instance->lights.front();E;E=E->next()) { - Instance *light = E->get(); - light->version++; - } - - - p_instance->transformed_aabb=new_aabb; - - if (!p_instance->scenario) { - - - return; - } - - - - if (p_instance->octree_id==0) { - - uint32_t base_type = 1<<p_instance->base_type; - uint32_t pairable_mask=0; - bool pairable=false; - - if (p_instance->base_type == INSTANCE_LIGHT) { - - pairable_mask=p_instance->light_info->enabled?INSTANCE_GEOMETRY_MASK:0; - pairable=true; - } - - if (p_instance->base_type == INSTANCE_PORTAL) { - - pairable_mask=(1<<INSTANCE_PORTAL); - pairable=true; - } - - if (p_instance->base_type == INSTANCE_BAKED_LIGHT_SAMPLER) { - - pairable_mask=(1<<INSTANCE_BAKED_LIGHT); - pairable=true; - } - - - if (!p_instance->room && (1<<p_instance->base_type)&INSTANCE_GEOMETRY_MASK) { - - base_type|=INSTANCE_ROOMLESS_MASK; - } - - if (p_instance->base_type == INSTANCE_ROOM) { - - pairable_mask=INSTANCE_ROOMLESS_MASK; - pairable=true; - } - - - // not inside octree - p_instance->octree_id = p_instance->scenario->octree.create(p_instance,new_aabb,0,pairable,base_type,pairable_mask); - - } else { - - /* - if (new_aabb==p_instance->data.transformed_aabb) - return; - */ - - p_instance->scenario->octree.move(p_instance->octree_id,new_aabb); - } - - if (p_instance->base_type==INSTANCE_PORTAL) { - - _portal_attempt_connect(p_instance); - } - - if (!p_instance->room && (1<<p_instance->base_type)&INSTANCE_GEOMETRY_MASK) { - - _instance_validate_autorooms(p_instance); - } - - if (p_instance->base_type == INSTANCE_ROOM) { - - for(Set<Instance*>::Element *E=p_instance->room_info->owned_autoroom_geometry.front();E;E=E->next()) - _instance_validate_autorooms(E->get()); - } - - -} - -void VisualServerRaster::_update_instance_aabb(Instance *p_instance) { - - AABB new_aabb; - - ERR_FAIL_COND(p_instance->base_type!=INSTANCE_NONE && !p_instance->base_rid.is_valid()); - - switch(p_instance->base_type) { - case VisualServer::INSTANCE_NONE: { - - // do nothing - } break; - case VisualServer::INSTANCE_MESH: { - - new_aabb = rasterizer->mesh_get_aabb(p_instance->base_rid,p_instance->data.skeleton); - - } break; - case VisualServer::INSTANCE_MULTIMESH: { - - new_aabb = rasterizer->multimesh_get_aabb(p_instance->base_rid); - - } break; - case VisualServer::INSTANCE_IMMEDIATE: { - - new_aabb = rasterizer->immediate_get_aabb(p_instance->base_rid); - - - } break; - case VisualServer::INSTANCE_PARTICLES: { - - new_aabb = rasterizer->particles_get_aabb(p_instance->base_rid); - - - } break; - case VisualServer::INSTANCE_LIGHT: { - - new_aabb = rasterizer->light_get_aabb(p_instance->base_rid); - - } break; - case VisualServer::INSTANCE_ROOM: { - - Room *room = room_owner.get( p_instance->base_rid ); - ERR_FAIL_COND(!room); - new_aabb=room->bounds.get_aabb(); - - } break; - case VisualServer::INSTANCE_PORTAL: { - - Portal *portal = portal_owner.get( p_instance->base_rid ); - ERR_FAIL_COND(!portal); - for (int i=0;i<portal->shape.size();i++) { - - Vector3 point( portal->shape[i].x, portal->shape[i].y, 0 ); - if (i==0) { - - new_aabb.pos=point; - new_aabb.size.z=0.01; // make it not flat for octree - } else { - - new_aabb.expand_to(point); - } - } - - } break; - case VisualServer::INSTANCE_BAKED_LIGHT: { - - BakedLight *baked_light = baked_light_owner.get( p_instance->base_rid ); - ERR_FAIL_COND(!baked_light); - new_aabb=baked_light->octree_aabb; - - } break; - case VisualServer::INSTANCE_BAKED_LIGHT_SAMPLER: { - - BakedLightSampler *baked_light_sampler = baked_light_sampler_owner.get( p_instance->base_rid ); - ERR_FAIL_COND(!baked_light_sampler); - float radius = baked_light_sampler->params[VS::BAKED_LIGHT_SAMPLER_RADIUS]; - - new_aabb=AABB(Vector3(-radius,-radius,-radius),Vector3(radius*2,radius*2,radius*2)); - - } break; - - default: {} - } - - if (p_instance->extra_margin) - new_aabb.grow_by(p_instance->extra_margin); - - p_instance->aabb=new_aabb; - -} - -void VisualServerRaster::_update_instances() { - - while(instance_update_list) { - - Instance *instance=instance_update_list; - - instance_update_list=instance_update_list->update_next; - - if (instance->update_aabb) - _update_instance_aabb(instance); - - if (instance->update_materials) { - if (instance->base_type==INSTANCE_MESH) { - instance->data.materials.resize(rasterizer->mesh_get_surface_count(instance->base_rid)); - } - } - - _update_instance(instance); - - instance->update=false; - instance->update_aabb=false; - instance->update_materials=false; - instance->update_next=0; - } -} - -void VisualServerRaster::instance_light_set_enabled(RID p_instance,bool p_enabled) { - - VS_CHANGED; - Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND( !instance ); - ERR_FAIL_COND( instance->base_type!=INSTANCE_LIGHT ); - - if (p_enabled==instance->light_info->enabled) - return; - - instance->light_info->enabled=p_enabled; - if (light_get_type(instance->base_rid)!=VS::LIGHT_DIRECTIONAL && instance->octree_id && instance->scenario) - instance->scenario->octree.set_pairable(instance->octree_id,p_enabled,1<<INSTANCE_LIGHT,p_enabled?INSTANCE_GEOMETRY_MASK:0); - - //_instance_queue_update( instance , true ); - -} - -bool VisualServerRaster::instance_light_is_enabled(RID p_instance) const { - - const Instance *instance = instance_owner.get( p_instance ); - ERR_FAIL_COND_V( !instance,false ); - ERR_FAIL_COND_V( instance->base_type!=INSTANCE_LIGHT,false ); - - return instance->light_info->enabled; -} - -/****** CANVAS *********/ -RID VisualServerRaster::canvas_create() { - - Canvas * canvas = memnew( Canvas ); - ERR_FAIL_COND_V(!canvas,RID()); - RID rid = canvas_owner.make_rid( canvas ); - - return rid; -} - - -void VisualServerRaster::canvas_set_item_mirroring(RID p_canvas,RID p_item,const Point2& p_mirroring) { - - Canvas * canvas = canvas_owner.get(p_canvas); - ERR_FAIL_COND(!canvas); - CanvasItem *canvas_item = canvas_item_owner.get(p_item); - ERR_FAIL_COND(!canvas_item); - - int idx = canvas->find_item(canvas_item); - ERR_FAIL_COND(idx==-1); - canvas->child_items[idx].mirror=p_mirroring; - -} - -Point2 VisualServerRaster::canvas_get_item_mirroring(RID p_canvas,RID p_item) const { - - Canvas * canvas = canvas_owner.get(p_canvas); - ERR_FAIL_COND_V(!canvas,Point2()); - CanvasItem *canvas_item = memnew( CanvasItem ); - ERR_FAIL_COND_V(!canvas_item,Point2()); - - int idx = canvas->find_item(canvas_item); - ERR_FAIL_COND_V(idx==-1,Point2()); - return canvas->child_items[idx].mirror; -} - -void VisualServerRaster::canvas_set_modulate(RID p_canvas,const Color& p_color) { - - Canvas * canvas = canvas_owner.get(p_canvas); - ERR_FAIL_COND(!canvas); - canvas->modulate=p_color; -} - - - -RID VisualServerRaster::canvas_item_create() { - - CanvasItem *canvas_item = memnew( CanvasItem ); - ERR_FAIL_COND_V(!canvas_item,RID()); - - return canvas_item_owner.make_rid( canvas_item ); -} - -void VisualServerRaster::canvas_item_set_parent(RID p_item,RID p_parent) { - - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - - if (canvas_item->parent.is_valid()) { - - if (canvas_owner.owns(canvas_item->parent)) { - - Canvas *canvas = canvas_owner.get(canvas_item->parent); - canvas->erase_item(canvas_item); - } else if (canvas_item_owner.owns(canvas_item->parent)) { - - CanvasItem *item_owner = canvas_item_owner.get(canvas_item->parent); - item_owner->child_items.erase(canvas_item); - } - - canvas_item->parent; - } - - - if (p_parent.is_valid()) { - if (canvas_owner.owns(p_parent)) { - - Canvas *canvas = canvas_owner.get(p_parent); - Canvas::ChildItem ci; - ci.item=canvas_item; - canvas->child_items.push_back(ci); - } else if (canvas_item_owner.owns(p_parent)) { - - CanvasItem *item_owner = canvas_item_owner.get(p_parent); - item_owner->child_items.push_back(canvas_item); - - } else { - - ERR_EXPLAIN("Invalid parent"); - ERR_FAIL(); - } - - - } - - canvas_item->parent=p_parent; - - -} - -RID VisualServerRaster::canvas_item_get_parent(RID p_canvas_item) const { - - CanvasItem *canvas_item = canvas_item_owner.get( p_canvas_item ); - ERR_FAIL_COND_V(!canvas_item,RID()); - - return canvas_item->parent; -} - -void VisualServerRaster::canvas_item_set_visible(RID p_item,bool p_visible) { - - VS_CHANGED; - - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - - canvas_item->visible=p_visible; -} - - -bool VisualServerRaster::canvas_item_is_visible(RID p_item) const { - - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND_V(!canvas_item,RID()); - - return canvas_item->visible; - -} - -void VisualServerRaster::canvas_item_set_light_mask(RID p_canvas_item,int p_mask) { - - VS_CHANGED; - - CanvasItem *canvas_item = canvas_item_owner.get( p_canvas_item ); - ERR_FAIL_COND(!canvas_item); - - if (canvas_item->light_mask==p_mask) - return; - VS_CHANGED; - - canvas_item->light_mask=p_mask; - -} - - -void VisualServerRaster::canvas_item_set_blend_mode(RID p_canvas_item,MaterialBlendMode p_blend) { - - VS_CHANGED; - - CanvasItem *canvas_item = canvas_item_owner.get( p_canvas_item ); - ERR_FAIL_COND(!canvas_item); - - if (canvas_item->blend_mode==p_blend) - return; - VS_CHANGED; - - canvas_item->blend_mode=p_blend; - -} - -void VisualServerRaster::canvas_item_attach_viewport(RID p_canvas_item, RID p_viewport) { - - CanvasItem *canvas_item = canvas_item_owner.get( p_canvas_item ); - ERR_FAIL_COND(!canvas_item); - - VS_CHANGED; - - canvas_item->viewport=p_viewport; - -} - - -/* -void VisualServerRaster::canvas_item_set_rect(RID p_item, const Rect2& p_rect) { - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - - canvas_item->rect=p_rect; -}*/ - -void VisualServerRaster::canvas_item_set_clip(RID p_item, bool p_clip) { - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - - canvas_item->clip=p_clip; -} - -void VisualServerRaster::canvas_item_set_distance_field_mode(RID p_item, bool p_distance_field) { - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - - canvas_item->distance_field=p_distance_field; -} - - -void VisualServerRaster::canvas_item_set_transform(RID p_item, const Matrix32& p_transform) { - - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - - canvas_item->xform=p_transform; - -} - - -void VisualServerRaster::canvas_item_set_custom_rect(RID p_item, bool p_custom_rect,const Rect2& p_rect) { - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - - canvas_item->custom_rect=p_custom_rect; - if (p_custom_rect) - canvas_item->rect=p_rect; - -} - -void VisualServerRaster::canvas_item_set_opacity(RID p_item, float p_opacity) { - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - canvas_item->opacity=p_opacity; - -} -float VisualServerRaster::canvas_item_get_opacity(RID p_item, float p_opacity) const { - - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND_V(!canvas_item,-1); - return canvas_item->opacity; - -} - -void VisualServerRaster::canvas_item_set_on_top(RID p_item, bool p_on_top) { - - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - canvas_item->ontop=p_on_top; - -} - -bool VisualServerRaster::canvas_item_is_on_top(RID p_item) const{ - const CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND_V(!canvas_item,false); - return canvas_item->ontop; - -} - - -void VisualServerRaster::canvas_item_set_self_opacity(RID p_item, float p_self_opacity) { - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - canvas_item->self_opacity=p_self_opacity; - -} -float VisualServerRaster::canvas_item_get_self_opacity(RID p_item, float p_self_opacity) const { - - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND_V(!canvas_item,-1); - return canvas_item->self_opacity; - -} - - -void VisualServerRaster::canvas_item_add_line(RID p_item, const Point2& p_from, const Point2& p_to,const Color& p_color,float p_width,bool p_antialiased) { - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - - CanvasItem::CommandLine * line = memnew( CanvasItem::CommandLine ); - ERR_FAIL_COND(!line); - line->color=p_color; - line->from=p_from; - line->to=p_to; - line->width=p_width; - line->antialiased=p_antialiased; - canvas_item->rect_dirty=true; - - - canvas_item->commands.push_back(line); -} - -void VisualServerRaster::canvas_item_add_rect(RID p_item, const Rect2& p_rect, const Color& p_color) { - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - - CanvasItem::CommandRect * rect = memnew( CanvasItem::CommandRect ); - ERR_FAIL_COND(!rect); - rect->modulate=p_color; - rect->rect=p_rect; - canvas_item->rect_dirty=true; - - canvas_item->commands.push_back(rect); -} - -void VisualServerRaster::canvas_item_add_circle(RID p_item, const Point2& p_pos, float p_radius,const Color& p_color) { - - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - - CanvasItem::CommandCircle * circle = memnew( CanvasItem::CommandCircle ); - ERR_FAIL_COND(!circle); - circle->color=p_color; - circle->pos=p_pos; - circle->radius=p_radius; - - canvas_item->commands.push_back(circle); - -} - -void VisualServerRaster::canvas_item_add_texture_rect(RID p_item, const Rect2& p_rect, RID p_texture,bool p_tile,const Color& p_modulate,bool p_transpose) { - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - - CanvasItem::CommandRect * rect = memnew( CanvasItem::CommandRect ); - ERR_FAIL_COND(!rect); - rect->modulate=p_modulate; - rect->rect=p_rect; - rect->flags=0; - if (p_tile) { - rect->flags|=Rasterizer::CANVAS_RECT_TILE; - rect->flags|=Rasterizer::CANVAS_RECT_REGION; - rect->source=Rect2(0,0,p_rect.size.width,p_rect.size.height); - } - - if (p_rect.size.x<0) { - - rect->flags|=Rasterizer::CANVAS_RECT_FLIP_H; - rect->rect.size.x = -rect->rect.size.x; - } - if (p_rect.size.y<0) { - - rect->flags|=Rasterizer::CANVAS_RECT_FLIP_V; - rect->rect.size.y = -rect->rect.size.y; - } - if (p_transpose) { - rect->flags|=Rasterizer::CANVAS_RECT_TRANSPOSE; - SWAP(rect->rect.size.x, rect->rect.size.y); - } - rect->texture=p_texture; - canvas_item->rect_dirty=true; - canvas_item->commands.push_back(rect); -} - -void VisualServerRaster::canvas_item_add_texture_rect_region(RID p_item, const Rect2& p_rect, RID p_texture,const Rect2& p_src_rect,const Color& p_modulate,bool p_transpose) { - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - - CanvasItem::CommandRect * rect = memnew( CanvasItem::CommandRect ); - ERR_FAIL_COND(!rect); - rect->modulate=p_modulate; - rect->rect=p_rect; - rect->texture=p_texture; - rect->source=p_src_rect; - rect->flags=Rasterizer::CANVAS_RECT_REGION; - - if (p_rect.size.x<0) { - - rect->flags|=Rasterizer::CANVAS_RECT_FLIP_H; - rect->rect.size.x = -rect->rect.size.x; - } - if (p_rect.size.y<0) { - - rect->flags|=Rasterizer::CANVAS_RECT_FLIP_V; - rect->rect.size.y = -rect->rect.size.y; - } - if (p_transpose) { - rect->flags|=Rasterizer::CANVAS_RECT_TRANSPOSE; - SWAP(rect->rect.size.x, rect->rect.size.y); - } - - canvas_item->rect_dirty=true; - - canvas_item->commands.push_back(rect); - -} - -void VisualServerRaster::canvas_item_add_style_box(RID p_item, const Rect2& p_rect, const Rect2& p_source, RID p_texture, const Vector2& p_topleft, const Vector2& p_bottomright, bool p_draw_center,const Color& p_modulate) { - - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - - CanvasItem::CommandStyle * style = memnew( CanvasItem::CommandStyle ); - ERR_FAIL_COND(!style); - style->texture=p_texture; - style->rect=p_rect; - style->source=p_source; - style->draw_center=p_draw_center; - style->color=p_modulate; - style->margin[MARGIN_LEFT]=p_topleft.x; - style->margin[MARGIN_TOP]=p_topleft.y; - style->margin[MARGIN_RIGHT]=p_bottomright.x; - style->margin[MARGIN_BOTTOM]=p_bottomright.y; - canvas_item->rect_dirty=true; - - canvas_item->commands.push_back(style); -} -void VisualServerRaster::canvas_item_add_primitive(RID p_item,const Vector<Point2>& p_points, const Vector<Color>& p_colors,const Vector<Point2>& p_uvs, RID p_texture,float p_width) { - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - - CanvasItem::CommandPrimitive * prim = memnew( CanvasItem::CommandPrimitive ); - ERR_FAIL_COND(!prim); - prim->texture=p_texture; - prim->points=p_points; - prim->uvs=p_uvs; - prim->colors=p_colors; - prim->width=p_width; - canvas_item->rect_dirty=true; - - canvas_item->commands.push_back(prim); -} - -void VisualServerRaster::canvas_item_add_polygon(RID p_item, const Vector<Point2>& p_points, const Vector<Color>& p_colors,const Vector<Point2>& p_uvs, RID p_texture) { - - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); -#ifdef DEBUG_ENABLED - int pointcount = p_points.size(); - ERR_FAIL_COND(pointcount<3); - int color_size=p_colors.size(); - int uv_size=p_uvs.size(); - ERR_FAIL_COND(color_size!=0 && color_size!=1 && color_size!=pointcount); - ERR_FAIL_COND(uv_size!=0 && (uv_size!=pointcount || !p_texture.is_valid())); -#endif - Vector<int> indices = Geometry::triangulate_polygon(p_points); - - if (indices.empty()) { - - ERR_EXPLAIN("Bad Polygon!"); - ERR_FAIL_V(); - } - - CanvasItem::CommandPolygon * polygon = memnew( CanvasItem::CommandPolygon ); - ERR_FAIL_COND(!polygon); - polygon->texture=p_texture; - polygon->points=p_points; - polygon->uvs=p_uvs; - polygon->colors=p_colors; - polygon->indices=indices; - polygon->count=indices.size(); - canvas_item->rect_dirty=true; - - canvas_item->commands.push_back(polygon); - -} - -void VisualServerRaster::canvas_item_add_triangle_array_ptr(RID p_item, int p_count, const int* p_indices, const Point2* p_points, const Color* p_colors,const Point2* p_uvs, RID p_texture) { - - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - - ERR_FAIL_COND(p_count <= 0); - - ERR_FAIL_COND(p_points == NULL); - - CanvasItem::CommandPolygonPtr * polygon = memnew( CanvasItem::CommandPolygonPtr ); - ERR_FAIL_COND(!polygon); - polygon->texture=p_texture; - polygon->points=p_points; - polygon->uvs=p_uvs; - polygon->colors=p_colors; - polygon->indices=p_indices; - polygon->count = p_count * 3; - canvas_item->rect_dirty=true; - - canvas_item->commands.push_back(polygon); -}; - -void VisualServerRaster::canvas_item_add_triangle_array(RID p_item, const Vector<int>& p_indices, const Vector<Point2>& p_points, const Vector<Color>& p_colors,const Vector<Point2>& p_uvs, RID p_texture, int p_count) { - - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - - int ps = p_points.size(); - ERR_FAIL_COND(!p_colors.empty() && p_colors.size()!=ps && p_colors.size()!=1); - ERR_FAIL_COND(!p_uvs.empty() && p_uvs.size()!=ps); - - Vector<int> indices = p_indices; - - int count = p_count * 3; - - if (indices.empty()) { - - ERR_FAIL_COND( ps % 3 != 0 ); - if (p_count == -1) - count = ps; - } else { - - ERR_FAIL_COND( indices.size() % 3 != 0 ); - if (p_count == -1) - count = indices.size(); - } - - CanvasItem::CommandPolygon * polygon = memnew( CanvasItem::CommandPolygon ); - ERR_FAIL_COND(!polygon); - polygon->texture=p_texture; - polygon->points=p_points; - polygon->uvs=p_uvs; - polygon->colors=p_colors; - polygon->indices=indices; - polygon->count = count; - canvas_item->rect_dirty=true; - - canvas_item->commands.push_back(polygon); -} - - -void VisualServerRaster::canvas_item_add_set_transform(RID p_item,const Matrix32& p_transform) { - - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - - CanvasItem::CommandTransform * tr = memnew( CanvasItem::CommandTransform ); - ERR_FAIL_COND(!tr); - tr->xform=p_transform; - - canvas_item->commands.push_back(tr); - -} - -void VisualServerRaster::canvas_item_add_set_blend_mode(RID p_item, MaterialBlendMode p_blend) { - - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - - CanvasItem::CommandBlendMode * bm = memnew( CanvasItem::CommandBlendMode ); - ERR_FAIL_COND(!bm); - bm->blend_mode = p_blend; - - canvas_item->commands.push_back(bm); -}; - -void VisualServerRaster::canvas_item_set_z(RID p_item, int p_z) { - - ERR_FAIL_COND(p_z<CANVAS_ITEM_Z_MIN || p_z>CANVAS_ITEM_Z_MAX); - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - canvas_item->z=p_z; - -} - -void VisualServerRaster::canvas_item_set_z_as_relative_to_parent(RID p_item, bool p_enable) { - - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - canvas_item->z_relative=p_enable; - -} - -void VisualServerRaster::canvas_item_set_copy_to_backbuffer(RID p_item, bool p_enable, const Rect2& p_rect) { - - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - if (bool(canvas_item->copy_back_buffer!=NULL) !=p_enable) { - if (p_enable) { - canvas_item->copy_back_buffer = memnew( Rasterizer::CanvasItem::CopyBackBuffer ); - } else { - memdelete(canvas_item->copy_back_buffer); - canvas_item->copy_back_buffer=NULL; - } - } - - if (p_enable) { - canvas_item->copy_back_buffer->rect=p_rect; - canvas_item->copy_back_buffer->full=p_rect==Rect2(); - } - -} - -void VisualServerRaster::canvas_item_set_use_parent_material(RID p_item, bool p_enable) { - - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - canvas_item->use_parent_material=p_enable; - -} - -void VisualServerRaster::canvas_item_set_material(RID p_item, RID p_material) { - - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - - if (canvas_item->material) - canvas_item->material->owners.erase(canvas_item); - - canvas_item->material=NULL; - - if (canvas_item_material_owner.owns(p_material)) { - canvas_item->material=canvas_item_material_owner.get(p_material); - canvas_item->material->owners.insert(canvas_item); - } -} - -void VisualServerRaster::canvas_item_set_sort_children_by_y(RID p_item, bool p_enable) { - - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - canvas_item->sort_y=p_enable; -} - - -void VisualServerRaster::canvas_item_add_clip_ignore(RID p_item, bool p_ignore) { - - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - - CanvasItem::CommandClipIgnore * ci = memnew( CanvasItem::CommandClipIgnore); - ERR_FAIL_COND(!ci); - ci->ignore=p_ignore; - - canvas_item->commands.push_back(ci); - -} - -void VisualServerRaster::canvas_item_clear(RID p_item) { - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - - - canvas_item->clear(); - -} - -void VisualServerRaster::canvas_item_raise(RID p_item) { - VS_CHANGED; - CanvasItem *canvas_item = canvas_item_owner.get( p_item ); - ERR_FAIL_COND(!canvas_item); - - if (canvas_item->parent.is_valid()) { - - if (canvas_owner.owns(canvas_item->parent)) { - - Canvas *canvas = canvas_owner.get(canvas_item->parent); - int idx = canvas->find_item(canvas_item); - ERR_FAIL_COND(idx<0); - Canvas::ChildItem ci = canvas->child_items[idx]; - canvas->child_items.remove(idx); - canvas->child_items.push_back(ci); - - } else if (canvas_item_owner.owns(canvas_item->parent)) { - - CanvasItem *item_owner = canvas_item_owner.get(canvas_item->parent); - int idx = item_owner->child_items.find(canvas_item); - ERR_FAIL_COND(idx<0); - item_owner->child_items.remove(idx); - item_owner->child_items.push_back(canvas_item); - - } - } - -} - -/***** CANVAS LIGHT *******/ - -RID VisualServerRaster::canvas_light_create() { - - Rasterizer::CanvasLight *clight = memnew( Rasterizer::CanvasLight ); - return canvas_light_owner.make_rid(clight); -} - -void VisualServerRaster::canvas_light_attach_to_canvas(RID p_light,RID p_canvas){ - - Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light); - ERR_FAIL_COND(!clight); - - if (clight->canvas.is_valid()) { - - Canvas *canvas = canvas_owner.get(clight->canvas); - canvas->lights.erase(clight); - } - - if (!canvas_owner.owns(p_canvas)) - p_canvas; - clight->canvas=p_canvas; - - if (clight->canvas.is_valid()) { - - Canvas *canvas = canvas_owner.get(clight->canvas); - canvas->lights.insert(clight); - } - - - -} -void VisualServerRaster::canvas_light_set_enabled(RID p_light, bool p_enabled){ - - Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light); - ERR_FAIL_COND(!clight); - clight->enabled=p_enabled; - -} -void VisualServerRaster::canvas_light_set_transform(RID p_light, const Matrix32& p_transform){ - - Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light); - ERR_FAIL_COND(!clight); - clight->xform=p_transform; - -} -void VisualServerRaster::canvas_light_set_scale(RID p_light, float p_scale) { - - Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light); - ERR_FAIL_COND(!clight); - clight->scale=p_scale; - -} - - -void VisualServerRaster::canvas_light_set_texture(RID p_light, RID p_texture){ - - Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light); - ERR_FAIL_COND(!clight); - clight->texture=p_texture; - -} -void VisualServerRaster::canvas_light_set_texture_offset(RID p_light, const Vector2& p_offset){ - - Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light); - ERR_FAIL_COND(!clight); - clight->texture_offset=p_offset; - -} -void VisualServerRaster::canvas_light_set_color(RID p_light, const Color& p_color){ - - Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light); - ERR_FAIL_COND(!clight); - clight->color=p_color; - - -} -void VisualServerRaster::canvas_light_set_height(RID p_light, float p_height){ - - Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light); - ERR_FAIL_COND(!clight); - clight->height=p_height; - -} - -void VisualServerRaster::canvas_light_set_energy(RID p_light, float p_energy){ - - Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light); - ERR_FAIL_COND(!clight); - clight->energy=p_energy; - -} - -void VisualServerRaster::canvas_light_set_z_range(RID p_light, int p_min_z,int p_max_z){ - - Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light); - ERR_FAIL_COND(!clight); - clight->z_min=p_min_z; - clight->z_max=p_max_z; - -} - -void VisualServerRaster::canvas_light_set_layer_range(RID p_light, int p_min_layer,int p_max_layer) { - - Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light); - ERR_FAIL_COND(!clight); - clight->layer_min=p_min_layer; - clight->layer_max=p_max_layer; - -} - -void VisualServerRaster::canvas_light_set_item_mask(RID p_light, int p_mask){ - - Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light); - ERR_FAIL_COND(!clight); - clight->item_mask=p_mask; - -} - -void VisualServerRaster::canvas_light_set_item_shadow_mask(RID p_light, int p_mask){ - - Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light); - ERR_FAIL_COND(!clight); - clight->item_shadow_mask=p_mask; - -} - - -void VisualServerRaster::canvas_light_set_mode(RID p_light, CanvasLightMode p_mode) { - - - Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light); - ERR_FAIL_COND(!clight); - clight->mode=p_mode; - -} -void VisualServerRaster::canvas_light_set_shadow_enabled(RID p_light, bool p_enabled){ - - Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light); - ERR_FAIL_COND(!clight); - - if (clight->shadow_buffer.is_valid()==p_enabled) - return; - if (p_enabled) { - clight->shadow_buffer=rasterizer->canvas_light_shadow_buffer_create(clight->shadow_buffer_size); - } else { - rasterizer->free(clight->shadow_buffer); - clight->shadow_buffer; - - } - -} - -void VisualServerRaster::canvas_light_set_shadow_buffer_size(RID p_light, int p_size){ - - Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light); - ERR_FAIL_COND(!clight); - - ERR_FAIL_COND(p_size<32 || p_size>16384); - - clight->shadow_buffer_size=next_power_of_2(p_size); - - - if (clight->shadow_buffer.is_valid()) { - rasterizer->free(clight->shadow_buffer); - clight->shadow_buffer=rasterizer->canvas_light_shadow_buffer_create(clight->shadow_buffer_size); - } - -} - -void VisualServerRaster::canvas_light_set_shadow_esm_multiplier(RID p_light, float p_multiplier) { - - Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light); - ERR_FAIL_COND(!clight); - clight->shadow_esm_mult=p_multiplier; - -} - -void VisualServerRaster::canvas_light_set_shadow_color(RID p_light, const Color& p_color) { - - Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light); - ERR_FAIL_COND(!clight); - clight->shadow_color=p_color; - -} - - -/****** CANVAS LIGHT OCCLUDER ******/ - -RID VisualServerRaster::canvas_light_occluder_create() { - - Rasterizer::CanvasLightOccluderInstance *occluder = memnew( Rasterizer::CanvasLightOccluderInstance ); - - return canvas_light_occluder_owner.make_rid( occluder ); - -} - -void VisualServerRaster::canvas_light_occluder_attach_to_canvas(RID p_occluder,RID p_canvas) { - - Rasterizer::CanvasLightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder); - ERR_FAIL_COND(!occluder); - - if (occluder->canvas.is_valid()) { - - Canvas *canvas = canvas_owner.get(occluder->canvas); - canvas->occluders.erase(occluder); - } - - if (!canvas_owner.owns(p_canvas)) - p_canvas; - - occluder->canvas=p_canvas; - - if (occluder->canvas.is_valid()) { - - Canvas *canvas = canvas_owner.get(occluder->canvas); - canvas->occluders.insert(occluder); - } -} - -void VisualServerRaster::canvas_light_occluder_set_enabled(RID p_occluder,bool p_enabled){ - - Rasterizer::CanvasLightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder); - ERR_FAIL_COND(!occluder); - - occluder->enabled=p_enabled; - -} - -void VisualServerRaster::canvas_light_occluder_set_polygon(RID p_occluder,RID p_polygon) { - - Rasterizer::CanvasLightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder); - ERR_FAIL_COND(!occluder); - - if (occluder->polygon.is_valid()) { - CanvasLightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(p_polygon); - if (occluder_poly) { - occluder_poly->owners.erase(occluder); - } - } - - occluder->polygon=p_polygon; - occluder->polygon_buffer; - - if (occluder->polygon.is_valid()) { - CanvasLightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(p_polygon); - if (!occluder_poly) - occluder->polygon; - ERR_FAIL_COND(!occluder_poly); - occluder_poly->owners.insert(occluder); - occluder->polygon_buffer=occluder_poly->occluder; - occluder->aabb_cache=occluder_poly->aabb; - occluder->cull_cache=occluder_poly->cull_mode; - } - -} - - - - -void VisualServerRaster::canvas_light_occluder_set_transform(RID p_occluder,const Matrix32& p_xform) { - - Rasterizer::CanvasLightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder); - ERR_FAIL_COND(!occluder); - - occluder->xform=p_xform; - -} - -void VisualServerRaster::canvas_light_occluder_set_light_mask(RID p_occluder,int p_mask) { - - Rasterizer::CanvasLightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder); - ERR_FAIL_COND(!occluder); - - occluder->light_mask=p_mask; - -} - - -RID VisualServerRaster::canvas_occluder_polygon_create() { - - CanvasLightOccluderPolygon * occluder_poly = memnew( CanvasLightOccluderPolygon ); - occluder_poly->occluder=rasterizer->canvas_light_occluder_create(); - return canvas_light_occluder_polygon_owner.make_rid(occluder_poly); - -} - -void VisualServerRaster::canvas_occluder_polygon_set_shape(RID p_occluder_polygon, const PoolVector<Vector2>& p_shape, bool p_close){ - - if (p_shape.size()<3) { - canvas_occluder_polygon_set_shape_as_lines(p_occluder_polygon,p_shape); - return; - } - - PoolVector<Vector2> lines; - int lc = p_shape.size()*2; - - lines.resize(lc-(p_close?0:2)); - { - PoolVector<Vector2>::Write w = lines.write(); - PoolVector<Vector2>::Read r = p_shape.read(); - - int max=lc/2; - if (!p_close) { - max--; - } - for(int i=0;i<max;i++) { - - Vector2 a = r[i]; - Vector2 b = r[(i+1)%(lc/2)]; - w[i*2+0]=a; - w[i*2+1]=b; - } - - } - - canvas_occluder_polygon_set_shape_as_lines(p_occluder_polygon,lines); -} - -void VisualServerRaster::canvas_occluder_polygon_set_shape_as_lines(RID p_occluder_polygon,const PoolVector<Vector2>& p_shape) { - - CanvasLightOccluderPolygon * occluder_poly = canvas_light_occluder_polygon_owner.get(p_occluder_polygon); - ERR_FAIL_COND(!occluder_poly); - ERR_FAIL_COND(p_shape.size()&1); - - int lc = p_shape.size(); - occluder_poly->aabb=Rect2(); - { - PoolVector<Vector2>::Read r = p_shape.read(); - for(int i=0;i<lc;i++) { - if (i==0) - occluder_poly->aabb.pos=r[i]; - else - occluder_poly->aabb.expand_to(r[i]); - } - } - - rasterizer->canvas_light_occluder_set_polylines(occluder_poly->occluder,p_shape); - for( Set<Rasterizer::CanvasLightOccluderInstance*>::Element *E=occluder_poly->owners.front();E;E=E->next()) { - E->get()->aabb_cache=occluder_poly->aabb; - } -} - -void VisualServerRaster::canvas_occluder_polygon_set_cull_mode(RID p_occluder_polygon,CanvasOccluderPolygonCullMode p_mode) { - - CanvasLightOccluderPolygon * occluder_poly = canvas_light_occluder_polygon_owner.get(p_occluder_polygon); - ERR_FAIL_COND(!occluder_poly); - occluder_poly->cull_mode=p_mode; - for( Set<Rasterizer::CanvasLightOccluderInstance*>::Element *E=occluder_poly->owners.front();E;E=E->next()) { - E->get()->cull_cache=p_mode; - } - -} - -RID VisualServerRaster::canvas_item_material_create() { - - Rasterizer::ShaderMaterial *material = memnew( Rasterizer::ShaderMaterial ); - return canvas_item_material_owner.make_rid(material); - -} - -void VisualServerRaster::canvas_item_material_set_shader(RID p_material, RID p_shader){ - - VS_CHANGED; - Rasterizer::ShaderMaterial *material = canvas_item_material_owner.get( p_material ); - ERR_FAIL_COND(!material); - material->shader=p_shader; - -} -void VisualServerRaster::canvas_item_material_set_shader_param(RID p_material, const StringName& p_param, const Variant& p_value){ - - VS_CHANGED; - Rasterizer::ShaderMaterial *material = canvas_item_material_owner.get( p_material ); - ERR_FAIL_COND(!material); - if (p_value.get_type()==Variant::NIL) - material->shader_param.erase(p_param); - else - material->shader_param[p_param]=p_value; - - -} -Variant VisualServerRaster::canvas_item_material_get_shader_param(RID p_material, const StringName& p_param) const{ - Rasterizer::ShaderMaterial *material = canvas_item_material_owner.get( p_material ); - ERR_FAIL_COND_V(!material,Variant()); - if (!material->shader_param.has(p_param)) { - ERR_FAIL_COND_V(!material->shader.is_valid(),Variant()); - return rasterizer->shader_get_default_param(material->shader,p_param); - } - - return material->shader_param[p_param]; -} - -void VisualServerRaster::canvas_item_material_set_shading_mode(RID p_material, CanvasItemShadingMode p_mode) { - - VS_CHANGED; - Rasterizer::ShaderMaterial *material = canvas_item_material_owner.get( p_material ); - ERR_FAIL_COND(!material); - material->shading_mode=p_mode; - -} - - -/******** CANVAS *********/ - - -void VisualServerRaster::cursor_set_rotation(float p_rotation, int p_cursor) { - VS_CHANGED; - ERR_FAIL_INDEX(p_cursor, MAX_CURSORS); - - cursors[p_cursor].rot = p_rotation; -}; - -void VisualServerRaster::cursor_set_texture(RID p_texture, const Point2 &p_center_offset, int p_cursor, const Rect2 &p_region) { - VS_CHANGED; - ERR_FAIL_INDEX(p_cursor, MAX_CURSORS); - - cursors[p_cursor].texture = p_texture; - cursors[p_cursor].center = p_center_offset; - cursors[p_cursor].region = p_region; -}; - -void VisualServerRaster::cursor_set_visible(bool p_visible, int p_cursor) { - VS_CHANGED; - ERR_FAIL_INDEX(p_cursor, MAX_CURSORS); - - cursors[p_cursor].visible = p_visible; -}; - -void VisualServerRaster::cursor_set_pos(const Point2& p_pos, int p_cursor) { - - ERR_FAIL_INDEX(p_cursor, MAX_CURSORS); - if (cursors[p_cursor].pos==p_pos) - return; - VS_CHANGED; - cursors[p_cursor].pos = p_pos; -}; - - -void VisualServerRaster::black_bars_set_margins(int p_left, int p_top, int p_right, int p_bottom) { - - black_margin[MARGIN_LEFT]=p_left; - black_margin[MARGIN_TOP]=p_top; - black_margin[MARGIN_RIGHT]=p_right; - black_margin[MARGIN_BOTTOM]=p_bottom; -} - -void VisualServerRaster::black_bars_set_images(RID p_left, RID p_top, RID p_right, RID p_bottom) { - - black_image[MARGIN_LEFT]=p_left; - black_image[MARGIN_TOP]=p_top; - black_image[MARGIN_RIGHT]=p_right; - black_image[MARGIN_BOTTOM]=p_bottom; -} - -void VisualServerRaster::_free_attached_instances(RID p_rid,bool p_free_scenario) { - - Map< RID, Set<RID> >::Element * E = instance_dependency_map.find( p_rid ); - - if (E) { - // has instances - while( E->get().size() ) { - // erase all attached instances - if (p_free_scenario) - instance_set_scenario( E->get().front()->get(), RID() ); - else - instance_set_base( E->get().front()->get(), RID() ); - - } - } - - instance_dependency_map.erase(p_rid); - -} - -void VisualServerRaster::custom_shade_model_set_shader(int p_model, RID p_shader) { - - VS_CHANGED; - //rasterizer->custom_shade_model_set_shader(p_model,p_shader); -} - -RID VisualServerRaster::custom_shade_model_get_shader(int p_model) const { - - //return rasterizer->custom_shade_model_get_shader(p_model); - return RID(); - -} -void VisualServerRaster::custom_shade_model_set_name(int p_model, const String& p_name) { - - //rasterizer->custom_shade_model_set_name(p_model,p_name); - -} -String VisualServerRaster::custom_shade_model_get_name(int p_model) const { - - //return rasterizer->custom_shade_model_get_name(p_model); - return ""; -} -void VisualServerRaster::custom_shade_model_set_param_info(int p_model, const List<PropertyInfo>& p_info) { - - VS_CHANGED; - //rasterizer->custom_shade_model_set_param_info(p_model,p_info); -} -void VisualServerRaster::custom_shade_model_get_param_info(int p_model, List<PropertyInfo>* p_info) const { - - //rasterizer->custom_shade_model_get_param_info(p_model,p_info); -} - -void VisualServerRaster::free( RID p_rid ) { - - VS_CHANGED; - - if (rasterizer->is_texture(p_rid) || rasterizer->is_material(p_rid) || rasterizer->is_shader(p_rid) || rasterizer->is_environment(p_rid)) { - - rasterizer->free(p_rid); - } else if (rasterizer->is_skeleton(p_rid)) { - - Map< RID, Set<Instance*> >::Element *E=skeleton_dependency_map.find(p_rid); - - if (E) { - //detach skeletons - for (Set<Instance*>::Element *F=E->get().front();F;F=F->next()) { - - F->get()->data.skeleton; - } - skeleton_dependency_map.erase(E); - } - - rasterizer->free(p_rid); - } else if (rasterizer->is_mesh(p_rid) || rasterizer->is_multimesh(p_rid) || rasterizer->is_light(p_rid) || rasterizer->is_particles(p_rid) || rasterizer->is_immediate(p_rid)) { - //delete the resource - - _free_attached_instances(p_rid); - rasterizer->free(p_rid); - } else if (room_owner.owns(p_rid)) { - - _free_attached_instances(p_rid); - Room *room = room_owner.get(p_rid); - ERR_FAIL_COND(!room); - room_owner.free(p_rid); - memdelete(room); - - - } else if (portal_owner.owns(p_rid)) { - - _free_attached_instances(p_rid); - - Portal *portal = portal_owner.get(p_rid); - ERR_FAIL_COND(!portal); - portal_owner.free(p_rid); - memdelete(portal); - - } else if (baked_light_owner.owns(p_rid)) { - - _free_attached_instances(p_rid); - - BakedLight *baked_light = baked_light_owner.get(p_rid); - ERR_FAIL_COND(!baked_light); - if (baked_light->data.octree_texture.is_valid()) - rasterizer->free(baked_light->data.octree_texture); - baked_light_owner.free(p_rid); - memdelete(baked_light); - - } else if (baked_light_sampler_owner.owns(p_rid)) { - - _free_attached_instances(p_rid); - - BakedLightSampler *baked_light_sampler = baked_light_sampler_owner.get(p_rid); - ERR_FAIL_COND(!baked_light_sampler); - /* - if (baked_light->data.octree_texture.is_valid()) - rasterizer->free(baked_light->data.octree_texture); - */ - baked_light_sampler_owner.free(p_rid); - memdelete(baked_light_sampler); - - } else if (camera_owner.owns(p_rid)) { - // delete te camera - - Camera *camera = camera_owner.get(p_rid); - ERR_FAIL_COND(!camera); - - camera_owner.free( p_rid ); - memdelete(camera); - - } else if (viewport_owner.owns(p_rid)) { - // delete the viewport - - Viewport *viewport = viewport_owner.get( p_rid ); - ERR_FAIL_COND(!viewport); - - //Viewport *parent=NULL; - - rasterizer->free(viewport->viewport_data); - if (viewport->render_target.is_valid()) { - - rasterizer->free(viewport->render_target); - } - - if (viewport->update_list.in_list()) - viewport_update_list.remove(&viewport->update_list); - if (screen_viewports.has(p_rid)) - screen_viewports.erase(p_rid); - - while(viewport->canvas_map.size()) { - - Canvas *c = viewport->canvas_map.front()->get().canvas; - c->viewports.erase(p_rid); - - viewport->canvas_map.erase(viewport->canvas_map.front()); - } - - - viewport_owner.free(p_rid); - memdelete(viewport); - - } else if (instance_owner.owns(p_rid)) { - // delete the instance - - _update_instances(); // be sure - - Instance *instance = instance_owner.get(p_rid); - ERR_FAIL_COND(!instance); - - instance_set_room(p_rid,RID()); - instance_set_scenario(p_rid,RID()); - instance_geometry_set_baked_light(p_rid,RID()); - instance_geometry_set_baked_light_sampler(p_rid,RID()); - instance_set_base(p_rid,RID()); - - if (instance->data.skeleton.is_valid()) - instance_attach_skeleton(p_rid,RID()); - - instance_owner.free(p_rid); - memdelete(instance); - - } else if (canvas_owner.owns(p_rid)) { - - Canvas *canvas = canvas_owner.get(p_rid); - ERR_FAIL_COND(!canvas); - - while(canvas->viewports.size()) { - - Viewport *vp = viewport_owner.get(canvas->viewports.front()->get()); - ERR_FAIL_COND(!vp); - - Map<RID,Viewport::CanvasData>::Element *E=vp->canvas_map.find(p_rid); - ERR_FAIL_COND(!E); - vp->canvas_map.erase(p_rid); - - canvas->viewports.erase( canvas->viewports.front() ); - } - - for (int i=0;i<canvas->child_items.size();i++) { - - canvas->child_items[i].item->parent; - } - - for (Set<Rasterizer::CanvasLight*>::Element *E=canvas->lights.front();E;E=E->next()) { - - E->get()->canvas; - } - - for (Set<Rasterizer::CanvasLightOccluderInstance*>::Element *E=canvas->occluders.front();E;E=E->next()) { - - E->get()->canvas; - } - - canvas_owner.free( p_rid ); - - memdelete( canvas ); - - } else if (canvas_item_owner.owns(p_rid)) { - - CanvasItem *canvas_item = canvas_item_owner.get(p_rid); - ERR_FAIL_COND(!canvas_item); - - if (canvas_item->parent.is_valid()) { - - if (canvas_owner.owns(canvas_item->parent)) { - - Canvas *canvas = canvas_owner.get(canvas_item->parent); - canvas->erase_item(canvas_item); - } else if (canvas_item_owner.owns(canvas_item->parent)) { - - CanvasItem *item_owner = canvas_item_owner.get(canvas_item->parent); - item_owner->child_items.erase(canvas_item); - - } - } - - for (int i=0;i<canvas_item->child_items.size();i++) { - - canvas_item->child_items[i]->parent; - } - - if (canvas_item->material) { - canvas_item->material->owners.erase(canvas_item); - } - - canvas_item_owner.free( p_rid ); - - memdelete( canvas_item ); - - } else if (canvas_item_material_owner.owns(p_rid)) { - - Rasterizer::ShaderMaterial *material = canvas_item_material_owner.get(p_rid); - ERR_FAIL_COND(!material); - for(Set<Rasterizer::CanvasItem*>::Element *E=material->owners.front();E;E=E->next()) { - - E->get()->material=NULL; - } - - canvas_item_material_owner.free(p_rid); - memdelete(material); - - } else if (canvas_light_owner.owns(p_rid)) { - - Rasterizer::CanvasLight *canvas_light = canvas_light_owner.get(p_rid); - ERR_FAIL_COND(!canvas_light); - - if (canvas_light->canvas.is_valid()) { - Canvas* canvas = canvas_owner.get(canvas_light->canvas); - if (canvas) - canvas->lights.erase(canvas_light); - } - - if (canvas_light->shadow_buffer.is_valid()) - rasterizer->free(canvas_light->shadow_buffer); - - canvas_light_owner.free( p_rid ); - memdelete( canvas_light ); - - } else if (canvas_light_occluder_owner.owns(p_rid)) { - - Rasterizer::CanvasLightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_rid); - ERR_FAIL_COND(!occluder); - - if (occluder->polygon.is_valid()) { - - CanvasLightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(occluder->polygon); - if (occluder_poly) { - occluder_poly->owners.erase(occluder); - } - - } - - if (occluder->canvas.is_valid() && canvas_owner.owns(occluder->canvas)) { - - Canvas *canvas = canvas_owner.get(occluder->canvas); - canvas->occluders.erase(occluder); - - } - - canvas_light_occluder_owner.free( p_rid ); - memdelete(occluder); - - } else if (canvas_light_occluder_polygon_owner.owns(p_rid)) { - - CanvasLightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(p_rid); - ERR_FAIL_COND(!occluder_poly); - rasterizer->free(occluder_poly->occluder); - - while(occluder_poly->owners.size()) { - - occluder_poly->owners.front()->get()->polygon; - occluder_poly->owners.erase( occluder_poly->owners.front() ); - } - - canvas_light_occluder_polygon_owner.free( p_rid ); - memdelete(occluder_poly); - - } else if (scenario_owner.owns(p_rid)) { - - Scenario *scenario=scenario_owner.get(p_rid); - ERR_FAIL_COND(!scenario); - - _update_instances(); // be sure - _free_attached_instances(p_rid,true); - - //rasterizer->free( scenario->environment ); - scenario_owner.free(p_rid); - memdelete(scenario); - - } else { - - ERR_FAIL(); - } - -} - - - -void VisualServerRaster::_instance_draw(Instance *p_instance) { - - if (p_instance->light_cache_dirty) { - int l=0; - //add positional lights - InstanceSet::Element *LE=p_instance->lights.front(); - p_instance->data.light_instances.resize(p_instance->lights.size()); - while(LE) { - - p_instance->data.light_instances[l++]=LE->get()->light_info->instance; - LE=LE->next(); - } - p_instance->light_cache_dirty=false; - } - - - switch(p_instance->base_type) { - - case INSTANCE_MESH: { - rasterizer->add_mesh(p_instance->base_rid, &p_instance->data); - } break; - case INSTANCE_MULTIMESH: { - rasterizer->add_multimesh(p_instance->base_rid, &p_instance->data); - } break; - case INSTANCE_IMMEDIATE: { - rasterizer->add_immediate(p_instance->base_rid, &p_instance->data); - } break; - case INSTANCE_PARTICLES: { - rasterizer->add_particles(p_instance->particles_info->instance, &p_instance->data); - } break; - default: {}; - } -} - - -Vector<Vector3> VisualServerRaster::_camera_generate_endpoints(Instance *p_light,Camera *p_camera,float p_range_min, float p_range_max) { - - // setup a camera matrix for that range! - CameraMatrix camera_matrix; - - switch(p_camera->type) { - - case Camera::ORTHOGONAL: { - - camera_matrix.set_orthogonal(p_camera->size,viewport_rect.width / (float)viewport_rect.height,p_range_min,p_range_max,p_camera->vaspect); - } break; - case Camera::PERSPECTIVE: { - - camera_matrix.set_perspective( - p_camera->fov, - viewport_rect.width / (float)viewport_rect.height, - p_range_min, - p_range_max, - p_camera->vaspect - ); - - } break; - } - - //obtain the frustum endpoints - - Vector<Vector3> endpoints; - endpoints.resize(8); - bool res = camera_matrix.get_endpoints(p_camera->transform,&endpoints[0]); - ERR_FAIL_COND_V(!res,Vector<Vector3>()); - - return endpoints; -} - -Vector<Plane> VisualServerRaster::_camera_generate_orthogonal_planes(Instance *p_light,Camera *p_camera,float p_range_min, float p_range_max) { - - Vector<Vector3> endpoints=_camera_generate_endpoints(p_light,p_camera,p_range_min,p_range_max); // frustum plane endpoints - ERR_FAIL_COND_V(endpoints.empty(),Vector<Plane>()); - - // obtain the light frustm ranges (given endpoints) - - Vector3 x_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_X ).normalized(); - Vector3 y_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_Y ).normalized(); - Vector3 z_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_Z ).normalized(); - - float x_min,x_max; - float y_min,y_max; - float z_min,z_max; - - for(int j=0;j<8;j++) { - - float d_x=x_vec.dot(endpoints[j]); - float d_y=y_vec.dot(endpoints[j]); - float d_z=z_vec.dot(endpoints[j]); - - if (j==0 || d_x<x_min) - x_min=d_x; - if (j==0 || d_x>x_max) - x_max=d_x; - - if (j==0 || d_y<y_min) - y_min=d_y; - if (j==0 || d_y>y_max) - y_max=d_y; - - if (j==0 || d_z<z_min) - z_min=d_z; - if (j==0 || d_z>z_max) - z_max=d_z; - - - } - //now that we now all ranges, we can proceed to make the light frustum planes, for culling octree - - Vector<Plane> light_frustum_planes; - light_frustum_planes.resize(6); - - //right/left - light_frustum_planes[0]=Plane( x_vec, x_max ); - light_frustum_planes[1]=Plane( -x_vec, -x_min ); - //top/bottom - light_frustum_planes[2]=Plane( y_vec, y_max ); - light_frustum_planes[3]=Plane( -y_vec, -y_min ); - //near/far - light_frustum_planes[4]=Plane( z_vec, z_max+1e6 ); - light_frustum_planes[5]=Plane( -z_vec, -z_min ); // z_min is ok, since casters further than far-light plane are not needed - - //TODO@ add more actual frustum planes to minimize get - - return light_frustum_planes; - -} -void VisualServerRaster::_light_instance_update_pssm_shadow(Instance *p_light,Scenario *p_scenario,Camera *p_camera,const CullRange& p_cull_range) { - - int splits = rasterizer->light_instance_get_shadow_passes( p_light->light_info->instance ); - - float split_weight=rasterizer->light_directional_get_shadow_param(p_light->base_rid,LIGHT_DIRECTIONAL_SHADOW_PARAM_PSSM_SPLIT_WEIGHT); - - - float distances[5]; - float texsize=rasterizer->light_instance_get_shadow_size( p_light->light_info->instance ); - - //float cull_min=p_cull_range.min; - //float cull_max=p_cull_range.max; - - - bool overlap = rasterizer->light_instance_get_pssm_shadow_overlap(p_light->light_info->instance); - - float cull_min=p_camera->znear; - float cull_max=p_camera->zfar; - float max_dist = rasterizer->light_directional_get_shadow_param(p_light->base_rid,VS::LIGHT_DIRECTIONAL_SHADOW_PARAM_MAX_DISTANCE); - if (max_dist>0.0) - cull_max=MIN(cull_max,max_dist); - - for(int i = 0; i < splits; i++) { - float idm = i / (float)splits; - float lg = cull_min * Math::pow(cull_max/cull_min, idm); - float uniform = cull_min + (cull_max - cull_min) * idm; - distances[i] = lg * split_weight + uniform * (1.0 - split_weight); - - } - - distances[0]=cull_min; - distances[splits]=cull_max; - - for (int i=0;i<splits;i++) { - - // setup a camera matrix for that range! - CameraMatrix camera_matrix; - - switch(p_camera->type) { - - case Camera::ORTHOGONAL: { - - camera_matrix.set_orthogonal( - p_camera->size, - viewport_rect.width / (float)viewport_rect.height, - distances[(i==0 || !overlap )?i:i-1], - distances[i+1], - p_camera->vaspect - - ); - } break; - case Camera::PERSPECTIVE: { - - - camera_matrix.set_perspective( - p_camera->fov, - viewport_rect.width / (float)viewport_rect.height, - distances[(i==0 || !overlap )?i:i-1], - distances[i+1], - p_camera->vaspect - - ); - - } break; - } - - //obtain the frustum endpoints - - Vector3 endpoints[8]; // frustum plane endpoints - bool res = camera_matrix.get_endpoints(p_camera->transform,endpoints); - ERR_CONTINUE(!res); - - // obtain the light frustm ranges (given endpoints) - - Vector3 x_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_X ).normalized(); - Vector3 y_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_Y ).normalized(); - Vector3 z_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_Z ).normalized(); - //z_vec points agsint the camera, like in default opengl - - float x_min,x_max; - float y_min,y_max; - float z_min,z_max; - - float x_min_cam,x_max_cam; - float y_min_cam,y_max_cam; - float z_min_cam,z_max_cam; - - - //used for culling - for(int j=0;j<8;j++) { - - float d_x=x_vec.dot(endpoints[j]); - float d_y=y_vec.dot(endpoints[j]); - float d_z=z_vec.dot(endpoints[j]); - - if (j==0 || d_x<x_min) - x_min=d_x; - if (j==0 || d_x>x_max) - x_max=d_x; - - if (j==0 || d_y<y_min) - y_min=d_y; - if (j==0 || d_y>y_max) - y_max=d_y; - - if (j==0 || d_z<z_min) - z_min=d_z; - if (j==0 || d_z>z_max) - z_max=d_z; - - - } - - - - - - { - //camera viewport stuff - //this trick here is what stabilizes the shadow (make potential jaggies to not move) - //at the cost of some wasted resolution. Still the quality increase is very well worth it - - - Vector3 center; - - for(int j=0;j<8;j++) { - - center+=endpoints[j]; - } - center/=8.0; - - //center=x_vec*(x_max-x_min)*0.5 + y_vec*(y_max-y_min)*0.5 + z_vec*(z_max-z_min)*0.5; - - float radius=0; - - for(int j=0;j<8;j++) { - - float d = center.distance_to(endpoints[j]); - if (d>radius) - radius=d; - } - - - radius *= texsize/(texsize-2.0); //add a texel by each side, so stepified texture will always fit - - x_max_cam=x_vec.dot(center)+radius; - x_min_cam=x_vec.dot(center)-radius; - y_max_cam=y_vec.dot(center)+radius; - y_min_cam=y_vec.dot(center)-radius; - z_max_cam=z_vec.dot(center)+radius; - z_min_cam=z_vec.dot(center)-radius; - - float unit = radius*2.0/texsize; - - x_max_cam=Math::stepify(x_max_cam,unit); - x_min_cam=Math::stepify(x_min_cam,unit); - y_max_cam=Math::stepify(y_max_cam,unit); - y_min_cam=Math::stepify(y_min_cam,unit); - - } - - //now that we now all ranges, we can proceed to make the light frustum planes, for culling octree - - Vector<Plane> light_frustum_planes; - light_frustum_planes.resize(6); - - //right/left - light_frustum_planes[0]=Plane( x_vec, x_max ); - light_frustum_planes[1]=Plane( -x_vec, -x_min ); - //top/bottom - light_frustum_planes[2]=Plane( y_vec, y_max ); - light_frustum_planes[3]=Plane( -y_vec, -y_min ); - //near/far - light_frustum_planes[4]=Plane( z_vec, z_max+1e6 ); - light_frustum_planes[5]=Plane( -z_vec, -z_min ); // z_min is ok, since casters further than far-light plane are not needed - - int caster_cull_count = p_scenario->octree.cull_convex(light_frustum_planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,INSTANCE_GEOMETRY_MASK); - - // a pre pass will need to be needed to determine the actual z-near to be used - for(int j=0;j<caster_cull_count;j++) { - - float min,max; - Instance *ins=instance_shadow_cull_result[j]; - if (!ins->visible || ins->data.cast_shadows == VS::SHADOW_CASTING_SETTING_OFF) - continue; - ins->transformed_aabb.project_range_in_plane(Plane(z_vec,0),min,max); - - if (max>z_max) - z_max=max; - } - - { - CameraMatrix ortho_camera; - real_t half_x = (x_max_cam-x_min_cam) * 0.5; - real_t half_y = (y_max_cam-y_min_cam) * 0.5; - - - ortho_camera.set_orthogonal( -half_x, half_x,-half_y,half_y, 0, (z_max-z_min_cam) ); - - Transform ortho_transform; - ortho_transform.basis=p_light->data.transform.basis; - ortho_transform.origin=x_vec*(x_min_cam+half_x)+y_vec*(y_min_cam+half_y)+z_vec*z_max; - - rasterizer->light_instance_set_shadow_transform(p_light->light_info->instance, i, ortho_camera, ortho_transform,distances[i],distances[i+1] ); - } - - rasterizer->begin_shadow_map( p_light->light_info->instance, i ); - - for (int j=0;j<caster_cull_count;j++) { - - Instance *instance = instance_shadow_cull_result[j]; - if (!instance->visible || instance->data.cast_shadows==VS::SHADOW_CASTING_SETTING_OFF) - continue; - _instance_draw(instance); - } - - rasterizer->end_shadow_map(); - - - } - - -} - - -CameraMatrix _lispm_look( const Vector3 pos, const Vector3 dir, const Vector3 up) { - - Vector3 dirN; - Vector3 upN; - Vector3 lftN; - - lftN=dir.cross(up); - lftN.normalize(); - - upN=lftN.cross(dir); - upN.normalize(); - dirN=dir.normalized(); - - CameraMatrix cmout; - float *output=&cmout.matrix[0][0]; - output[ 0] = lftN[0]; - output[ 1] = upN[0]; - output[ 2] = -dirN[0]; - output[ 3] = 0.0; - - output[ 4] = lftN[1]; - output[ 5] = upN[1]; - output[ 6] = -dirN[1]; - output[ 7] = 0.0; - - output[ 8] = lftN[2]; - output[ 9] = upN[2]; - output[10] = -dirN[2]; - output[11] = 0.0; - - output[12] = -lftN.dot(pos); - output[13] = -upN.dot(pos); - output[14] = dirN.dot(pos); - output[15] = 1.0; - - return cmout; -} - -#if 1 - -void VisualServerRaster::_light_instance_update_lispsm_shadow(Instance *p_light,Scenario *p_scenario,Camera *p_camera,const CullRange& p_cull_range) { - - Vector3 light_vec = -p_light->data.transform.basis.get_axis(2); - Vector3 view_vec = -p_camera->transform.basis.get_axis(2); - - float near_dist=1; - - Vector<Plane> light_frustum_planes = _camera_generate_orthogonal_planes(p_light,p_camera,p_cull_range.min,p_cull_range.max); - int caster_count = p_scenario->octree.cull_convex(light_frustum_planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,INSTANCE_GEOMETRY_MASK); - - // this could be faster by just getting supports from the AABBs.. - // but, safer to do as the original implementation explains for now.. - - Vector<Vector3> caster_pointcloud; - caster_pointcloud.resize(caster_count*8); - int caster_pointcloud_size=0; - - { - - //fill pointcloud - Vector3* caster_pointcloud_ptr=&caster_pointcloud[0]; - - for(int i=0;i<caster_count;i++) { - - Instance *ins = instance_shadow_cull_result[i]; - if (!ins->visible || ins->data.cast_shadows == VS::SHADOW_CASTING_SETTING_OFF) - continue; - - for(int j=0;j<8;j++) { - - Vector3 v = ins->aabb.get_endpoint(j); - v = ins->data.transform.xform(v); - caster_pointcloud_ptr[caster_pointcloud_size+j]=v; - } - - caster_pointcloud_size+=8; - - } - } - - // now generate a pointcloud that contains the maximum bound (camera extruded by light) - - Vector<Vector3> camera_pointcloud = _camera_generate_endpoints(p_light,p_camera,p_cull_range.min,p_cull_range.max); - int cpcsize=camera_pointcloud.size(); - camera_pointcloud.resize( cpcsize*2 ); - - for(int i=0;i<cpcsize;i++) { - - camera_pointcloud[i+cpcsize]=camera_pointcloud[i]-light_vec*1000; - } - - - - // Vector<Vector3> frustum_points=_camera_generate_endpoints(p_light,p_camera,p_cull_range.min,p_cull_range.max); - - - // compute the "light-space" basis, using the algorithm described in the paper - // note: since bodyB is defined in eye space, all of these vectors should also be defined in eye space - - - Vector3 eye = p_camera->transform.origin; - Vector3 up = light_vec.cross(view_vec).cross(light_vec).normalized(); - - - CameraMatrix light_space_basis = _lispm_look(eye,light_vec,up); - - AABB light_space_aabb; - - - { //create an optimal AABB from both the camera pointcloud and the objects pointcloud - AABB light_space_pointcloud_aabb; - AABB light_space_camera_aabb; - //xform pointcloud - const Vector3* caster_pointcloud_ptr=&caster_pointcloud[0]; - - for(int i=0;i<caster_pointcloud_size;i++) { - - Vector3 p = light_space_basis.xform(caster_pointcloud_ptr[i]); - if (i==0) { - light_space_pointcloud_aabb.pos=p; - } else { - light_space_pointcloud_aabb.expand_to(p); - } - } - for(int i=0;i<camera_pointcloud.size();i++) { - - Vector3 p = light_space_basis.xform(camera_pointcloud[i]); - if (i==0) { - light_space_camera_aabb.pos=p; - } else { - light_space_camera_aabb.expand_to(p); - } - } - - light_space_aabb=light_space_pointcloud_aabb.intersection(light_space_camera_aabb); - } - - float lvdp = light_vec.dot(view_vec); - - float sin_gamma = Math::sqrt(1.0-lvdp*lvdp); - //use the formulas of the paper to get n (and f) - float factor = 1.0/sin_gamma; - float z_n = factor*near_dist; //often 1 - float d = Math::abs(light_space_aabb.size.y); //perspective transform depth //light space y extents - float z_f = z_n + d*sin_gamma; - float n = (z_n+Math::sqrt(z_f*z_n))/sin_gamma; - float f = n+d; - - Vector3 pos = eye - up*(n-near_dist); - - CameraMatrix light_space_basis2 = _lispm_look(pos,light_vec,up); - //Transform light_space_basis2; - //light_space_basis2.set_look_at(pos,light_vec-pos,up); - //light_space_basis2.affine_invert(); - - //one possibility for a simple perspective transformation matrix - //with the two parameters n(near) and f(far) in y direction - - CameraMatrix lisp_matrix; - lisp_matrix.matrix[1][1]=(f+n)/(f-n); - lisp_matrix.matrix[3][1]=-2*f*n/(f-n); - lisp_matrix.matrix[1][3]=1; - lisp_matrix.matrix[3][3]=0; - - CameraMatrix projection = lisp_matrix * light_space_basis2; - //CameraMatrix projection = light_space_basis2 * lisp_matrix; - - - AABB proj_space_aabb; - - { - - AABB proj_space_pointcloud_aabb; - AABB proj_space_camera_aabb; - //xform pointcloud - Vector3* caster_pointcloud_ptr=&caster_pointcloud[0]; - for(int i=0;i<caster_pointcloud_size;i++) { - - Vector3 p = projection.xform(caster_pointcloud_ptr[i]); - if (i==0) { - proj_space_pointcloud_aabb.pos=p; - } else { - proj_space_pointcloud_aabb.expand_to(p); - } - } - - for(int i=0;i<camera_pointcloud.size();i++) { - - Vector3 p = projection.xform(camera_pointcloud[i]); - if (i==0) { - proj_space_camera_aabb.pos=p; - } else { - proj_space_camera_aabb.expand_to(p); - } - } - - //proj_space_aabb=proj_space_pointcloud_aabb.intersection_with(proj_space_camera_aabb); - proj_space_aabb=proj_space_pointcloud_aabb; - } - - projection.scale_translate_to_fit(proj_space_aabb); - projection=projection * lisp_matrix; - - CameraMatrix scale; - scale.make_scale(Vector3(1.0,1.0,-1.0)); // transform to left handed - - projection=scale * projection; - - rasterizer->light_instance_set_shadow_transform(p_light->light_info->instance,0, projection , light_space_basis2.inverse() ); - - rasterizer->begin_shadow_map( p_light->light_info->instance, 0 ); - - for(int i=0;i<caster_count;i++) { - - Instance *instance = instance_shadow_cull_result[i]; - - if (!instance->visible || instance->data.cast_shadows == VS::SHADOW_CASTING_SETTING_OFF) - continue; - _instance_draw(instance); - } - - rasterizer->end_shadow_map(); - - -} - -#else - - -void VisualServerRaster::_light_instance_update_lispsm_shadow(Instance *p_light,Scenario *p_scenario,Camera *p_camera,const CullRange& p_cull_range) { - - /* STEP 1: GENERATE LIGHT TRANSFORM */ - - - - Vector3 light_vec = -p_light->data.transform.basis.get_axis(2); - Vector3 view_vec = -p_camera->transform.basis.get_axis(2); - float viewdot = Math::absf(light_vec.dot(view_vec)); - - Vector3 up = light_vec.cross(view_vec).cross(light_vec).normalized(); - - Transform light_transform; - light_transform.set_look_at(Vector3(),light_vec,up); - - - /* STEP 2: GENERATE WORDLSPACE PLANES AND VECTORS*/ - float range_min=0.01; //p_cull_range.min - float range_max=20;//p_cull_range.max; - - Vector<Vector3> camera_endpoints=_camera_generate_endpoints(p_light,p_camera,range_min,range_max); // frustum plane endpoints - ERR_FAIL_COND(camera_endpoints.empty()); - - // obtain the light frustm ranges (given endpoints) - - - Vector3 light_x_vec=light_transform.basis.get_axis( Vector3::AXIS_X ).normalized(); - Vector3 light_y_vec=light_transform.basis.get_axis( Vector3::AXIS_Y ).normalized(); - Vector3 light_z_vec=light_transform.basis.get_axis( Vector3::AXIS_Z ).normalized(); - - Vector3 light_axis_max; - Vector3 light_axis_min; - - for(int j=0;j<8;j++) { - - float d_x=light_x_vec.dot(camera_endpoints[j]); - float d_y=light_y_vec.dot(camera_endpoints[j]); - float d_z=light_z_vec.dot(camera_endpoints[j]); - - if (j==0 || d_x<light_axis_min.x) - light_axis_min.x=d_x; - if (j==0 || d_x>light_axis_max.x) - light_axis_max.x=d_x; - - if (j==0 || d_y<light_axis_min.y) - light_axis_min.y=d_y; - if (j==0 || d_y>light_axis_max.y) - light_axis_max.y=d_y; - - if (j==0 || d_z<light_axis_min.z) - light_axis_min.z=d_z; - if (j==0 || d_z>light_axis_max.z) - light_axis_max.z=d_z; - - - } - - //now that we now all ranges, we can proceed to make the light frustum planes, for culling octree - - Vector<Plane> light_cull_planes; - light_cull_planes.resize(6); - - - //right/left - light_cull_planes[0]=Plane( light_x_vec, light_axis_max.x ); - light_cull_planes[1]=Plane( -light_x_vec, -light_axis_min.x ); - //top/bottom - light_cull_planes[2]=Plane( light_y_vec, light_axis_max.y ); - light_cull_planes[3]=Plane( -light_y_vec, -light_axis_min.y ); - //near/far - light_cull_planes[4]=Plane( light_z_vec, light_axis_max.z+1e6 ); - light_cull_planes[5]=Plane( -light_z_vec, -light_axis_min.z ); // z_min is ok, since casters further than far-light plane are not needed - - - /* STEP 3: CULL CASTERS */ - - int caster_count = p_scenario->octree.cull_convex(light_cull_planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,INSTANCE_GEOMETRY_MASK); - - /* STEP 4: ADJUST FAR Z PLANE */ - - float caster_max_z=1e-1; - for(int i=0;i<caster_count;i++) { - - Instance *ins=instance_shadow_cull_result[i]; - if (!ins->visible || ins->cast_shadows==VS::SHADOW_CASTING_SETTING_OFF) - continue; - - //@TODO optimize using support mapping - for(int j=0;j<8;j++) { - - Vector3 v=ins->data.transform.xform(ins->aabb.get_endpoint(j)); - float d = light_z_vec.dot(v); - if (d>caster_max_z) - caster_max_z=d; - - } - - } - - float expand = caster_max_z-light_axis_max.z; - if (expand<0) - expand=0; - light_axis_max.z=MAX(caster_max_z,light_axis_max.z); - - /* STEP 5: CREATE ORTHOGONAL PROJECTION */ - - CameraMatrix light_projection; - - real_t half_x = (light_axis_max.x-light_axis_min.x) * 0.5; - real_t half_y = (light_axis_max.y-light_axis_min.y) * 0.5; - light_projection.set_orthogonal( -half_x, half_x,half_y, -half_y, 0, (light_axis_max.z-light_axis_min.z) ); - light_transform.origin=light_x_vec*(light_axis_min.x+half_x)+light_y_vec*(light_axis_min.y+half_y)+light_z_vec*light_axis_max.z; - - - if (/*false &&*/ viewdot<0.96) { - - float lvdp = light_vec.dot(view_vec); - - float near_dist=1.0; - float sin_gamma = Math::sqrt(1.0-lvdp*lvdp); - //use the formulas of the paper to get n (and f) - float factor = 1.0/sin_gamma; - float z_n = factor*near_dist; //often 1 - float d = Math::abs(light_axis_max.y-light_axis_min.y); //perspective transform depth //light space y extents - float z_f = z_n + d*sin_gamma; - float n = (z_n+Math::sqrt(z_f*z_n))/sin_gamma; - float f = n+d; - - CameraMatrix lisp_matrix; - lisp_matrix.matrix[1][1]=(f+n)/(f-n); - lisp_matrix.matrix[3][1]=-2*f*n/(f-n); - lisp_matrix.matrix[1][3]=1; - lisp_matrix.matrix[3][3]=0; - - Vector3 pos = p_camera->transform.origin - up*(n-near_dist); - - CameraMatrix world2light = _lispm_look(pos,light_vec,up); - CameraMatrix projection = lisp_matrix * world2light; - - AABB projection_bounds; - for(int i=0;i<camera_endpoints.size();i++) { - - Vector3 p=camera_endpoints[i]; - if (i==0) - projection_bounds.pos=projection.xform(p); - else - projection_bounds.expand_to(projection.xform(p)); - - projection_bounds.expand_to(projection.xform(p+light_vec*-expand)); - } - - CameraMatrix scaletrans; - scaletrans.scale_translate_to_fit(projection_bounds); - projection=scaletrans * lisp_matrix; - - CameraMatrix scale; - scale.make_scale(Vector3(1.0,1.0,-1.0)); // transform to left handed - - projection=scale * projection; - - - rasterizer->light_instance_set_shadow_transform(p_light->light_info->instance,0, projection, world2light.inverse(), viewdot); - - } else { - //orthogonal - rasterizer->light_instance_set_shadow_transform(p_light->light_info->instance,0, light_projection , light_transform, viewdot); - } - - rasterizer->begin_shadow_map( p_light->light_info->instance, 0 ); - - for(int i=0;i<caster_count;i++) { - - Instance *instance = instance_shadow_cull_result[i]; - - if (!instance->visible || instance->cast_shadows==VS::SHADOW_CASTING_SETTING_OFF) - continue; - _instance_draw(instance); - } - - rasterizer->end_shadow_map(); - -} - -#endif - - -void VisualServerRaster::_light_instance_update_shadow(Instance *p_light,Scenario *p_scenario,Camera *p_camera,const CullRange& p_cull_range) { - - - - if (!rasterizer->shadow_allocate_near( p_light->light_info->instance )) - return; // shadow could not be updated - - - /* VisualServerRaster supports for many shadow techniques, using the one the rasterizer requests */ - - Rasterizer::ShadowType shadow_type = rasterizer->light_instance_get_shadow_type(p_light->light_info->instance); - - switch(shadow_type) { - - case Rasterizer::SHADOW_SIMPLE: { - /* SPOT SHADOW */ - - - rasterizer->begin_shadow_map( p_light->light_info->instance, 0 ); - - //using this one ensures that raster deferred will have it - - float far = rasterizer->light_get_var( p_light->base_rid, VS::LIGHT_PARAM_RADIUS); - - float angle = rasterizer->light_get_var( p_light->base_rid, VS::LIGHT_PARAM_SPOT_ANGLE ); - - CameraMatrix cm; - cm.set_perspective( angle*2.0, 1.0, 0.001, far ); - - Vector<Plane> planes = cm.get_projection_planes(p_light->data.transform); - int cull_count = p_scenario->octree.cull_convex(planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,INSTANCE_GEOMETRY_MASK); - - - for (int i=0;i<cull_count;i++) { - - Instance *instance = instance_shadow_cull_result[i]; - if (!instance->visible || instance->data.cast_shadows == VS::SHADOW_CASTING_SETTING_OFF) - continue; - _instance_draw(instance); - } - - rasterizer->end_shadow_map(); - - } break; - case Rasterizer::SHADOW_DUAL_PARABOLOID: { - - /* OMNI SHADOW */ - - int passes = rasterizer->light_instance_get_shadow_passes( p_light->light_info->instance ); - - if (passes==2) { - - for(int i=0;i<2;i++) { - - rasterizer->begin_shadow_map( p_light->light_info->instance, i ); - - - //using this one ensures that raster deferred will have it - - float radius = rasterizer->light_get_var( p_light->base_rid, VS::LIGHT_PARAM_RADIUS); - - float z =i==0?-1:1; - Vector<Plane> planes; - planes.resize(5); - planes[0]=p_light->data.transform.xform(Plane(Vector3(0,0,z),radius)); - planes[1]=p_light->data.transform.xform(Plane(Vector3(1,0,z).normalized(),radius)); - planes[2]=p_light->data.transform.xform(Plane(Vector3(-1,0,z).normalized(),radius)); - planes[3]=p_light->data.transform.xform(Plane(Vector3(0,1,z).normalized(),radius)); - planes[4]=p_light->data.transform.xform(Plane(Vector3(0,-1,z).normalized(),radius)); - - - int cull_count = p_scenario->octree.cull_convex(planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,INSTANCE_GEOMETRY_MASK); - - - for (int j=0;j<cull_count;j++) { - - Instance *instance = instance_shadow_cull_result[j]; - if (!instance->visible || instance->data.cast_shadows == VS::SHADOW_CASTING_SETTING_OFF) - continue; - - _instance_draw(instance); - } - - rasterizer->end_shadow_map(); - } - } else if (passes==1) { - //one go - - - - } - - } break; - case Rasterizer::SHADOW_CUBE: { - - // todo - } break; - case Rasterizer::SHADOW_ORTHOGONAL: { - - _light_instance_update_pssm_shadow(p_light,p_scenario,p_camera,p_cull_range); - } break; - case Rasterizer::SHADOW_PSSM: { - - _light_instance_update_pssm_shadow(p_light,p_scenario,p_camera,p_cull_range); - } break; - case Rasterizer::SHADOW_PSM: { - - _light_instance_update_lispsm_shadow(p_light,p_scenario,p_camera,p_cull_range); - // todo - } break; - default: {} - } - -} - -void VisualServerRaster::_portal_disconnect(Instance *p_portal,bool p_cleanup) { - - if (p_portal->portal_info->connected) { - - //disconnect first - p_portal->portal_info->connected->portal_info->connected=NULL; - p_portal->portal_info->connected=NULL; - - } - - if (p_portal->room && p_portal->room->room) { - - if (p_cleanup) { - - p_portal->room->room->room_info->disconnected_child_portals.erase(p_portal); - //p_portal->room->room->room_info->disconnected_child_portals.erase(p_portal); - } else { - p_portal->room->room->room_info->disconnected_child_portals.insert(p_portal); - } - } - -} - -void VisualServerRaster::_instance_validate_autorooms(Instance *p_geometry) { - - if (p_geometry->auto_rooms.size()==0) - return; - - p_geometry->valid_auto_rooms.clear(); - - int point_count = aabb_random_points.size(); - const Vector3 * src_points = &aabb_random_points[0]; - - for(Set<Instance*>::Element *E=p_geometry->valid_auto_rooms.front();E;E=E->next()) { - - Instance *room = E->get(); - Vector3 *dst_points=&transformed_aabb_random_points[0]; - - //generate points - for(int i=0;i<point_count;i++) { - - dst_points[i] = room->room_info->affine_inverse.xform(p_geometry->data.transform.xform((src_points[i]*p_geometry->transformed_aabb.size)+p_geometry->transformed_aabb.pos)); - } - - int pass = room->room_info->room->bounds.get_points_inside(dst_points,point_count); - - float ratio = pass; - if( point_count != 0 ) { - ratio /= (float)point_count; - } - - if (ratio>0.5) // should make some constant - p_geometry->valid_auto_rooms.insert(room); - } -} - -void VisualServerRaster::_portal_attempt_connect(Instance *p_portal) { - - - _portal_disconnect(p_portal); - - Vector3 A_norm = p_portal->data.transform.basis.get_axis(Vector3::AXIS_Z).normalized(); - Plane A_plane( p_portal->data.transform.origin, A_norm ); - float A_surface = p_portal->portal_info->portal->bounds.get_area(); - if (A_surface==0) - return; //wtf - - Instance *found=NULL; - Transform affine_inverse = p_portal->data.transform.affine_inverse(); - - for(Set<Instance*>::Element *E=p_portal->portal_info->candidate_set.front();E;E=E->next()) { - - Instance *B = E->get(); - - if (B->portal_info->connected) - continue; // in use - - Vector3 B_norm = B->data.transform.basis.get_axis(Vector3::AXIS_Z).normalized(); - - // check that they are in front of another - float dot = A_norm.dot(-B_norm); - - if (dot<0.707) // 45 degrees, TODO unharcode this - continue; - - // check the max distance to the other portal - - bool valid=true; - - Rect2 local_bounds; - - for(int i=0;i<B->portal_info->portal->shape.size();i++) { - - Point2 point2 = B->portal_info->portal->shape[i]; - - Vector3 point = B->data.transform.xform( Vector3( point2.x, point2.y, 0 ) ); - - float dist = Math::abs(A_plane.distance_to(point)); - - if ( - dist>p_portal->portal_info->portal->connect_range || - dist>B->portal_info->portal->connect_range ) { - valid=false; - break; - } - - - Vector3 point_local = affine_inverse.xform(A_plane.project(point)); - point2 = Point2(point_local.x,point_local.y); - - if (i==0) - local_bounds.pos=point2; - else - local_bounds.expand_to(point2); - - - } - - if (!valid) - continue; - - float B_surface = B->portal_info->portal->bounds.get_area(); - if (B_surface==0) - continue; //wtf - - float clip_area = p_portal->portal_info->portal->bounds.clip(local_bounds).get_area(); - - - //check that most of the area is shared - - if ( (clip_area/A_surface) < 0.5 || (clip_area/B_surface) < 0.5) // TODO change for something else - continue; - - found=B; - break; - - - - } - - - if (!found) { - - if (p_portal->room && p_portal->room->room) { - - p_portal->room->room->room_info->disconnected_child_portals.insert(p_portal); - } - - return; - } - - p_portal->portal_info->connected=found; - found->portal_info->connected=p_portal; - - -} - -void* VisualServerRaster::instance_pair(void *p_self, OctreeElementID, Instance *p_A,int, OctreeElementID, Instance *p_B,int) { - - VisualServerRaster *self = (VisualServerRaster*)p_self; - Instance *A = p_A; - Instance *B = p_B; - - if (A->base_type==INSTANCE_PORTAL) { - - ERR_FAIL_COND_V( B->base_type!=INSTANCE_PORTAL,NULL ); - - A->portal_info->candidate_set.insert(B); - B->portal_info->candidate_set.insert(A); - - self->_portal_attempt_connect(A); - //attempt to conncet portal A (will go through B anyway) - //this is a little hackish, but works fine in practice - - } else if (A->base_type==INSTANCE_BAKED_LIGHT || B->base_type==INSTANCE_BAKED_LIGHT) { - - if (B->base_type==INSTANCE_BAKED_LIGHT) { - SWAP(A,B); - } - - ERR_FAIL_COND_V(B->base_type!=INSTANCE_BAKED_LIGHT_SAMPLER,NULL); - B->baked_light_sampler_info->baked_lights.insert(A); - - } else if (A->base_type==INSTANCE_ROOM || B->base_type==INSTANCE_ROOM) { - - if (B->base_type==INSTANCE_ROOM) - SWAP(A,B); - - ERR_FAIL_COND_V(! ((1<<B->base_type)&INSTANCE_GEOMETRY_MASK ),NULL); - - B->auto_rooms.insert(A); - A->room_info->owned_autoroom_geometry.insert(B); - - self->_instance_validate_autorooms(B); - - - } else { - - if (B->base_type==INSTANCE_LIGHT) { - - SWAP(A,B); - } else if (A->base_type!=INSTANCE_LIGHT) { - return NULL; - } - - - A->light_info->affected.insert(B); - B->lights.insert(A); - B->light_cache_dirty=true; - } - - return NULL; - -} -void VisualServerRaster::instance_unpair(void *p_self, OctreeElementID, Instance *p_A,int, OctreeElementID, Instance *p_B,int,void*) { - - VisualServerRaster *self = (VisualServerRaster*)p_self; - Instance *A = p_A; - Instance *B = p_B; - - if (A->base_type==INSTANCE_PORTAL) { - - ERR_FAIL_COND( B->base_type!=INSTANCE_PORTAL ); - - - A->portal_info->candidate_set.erase(B); - B->portal_info->candidate_set.erase(A); - - //after disconnecting them, see if they can connect again - self->_portal_attempt_connect(A); - self->_portal_attempt_connect(B); - - } else if (A->base_type==INSTANCE_BAKED_LIGHT || B->base_type==INSTANCE_BAKED_LIGHT) { - - if (B->base_type==INSTANCE_BAKED_LIGHT) { - SWAP(A,B); - } - - ERR_FAIL_COND(B->base_type!=INSTANCE_BAKED_LIGHT_SAMPLER); - B->baked_light_sampler_info->baked_lights.erase(A); - - } else if (A->base_type==INSTANCE_ROOM || B->base_type==INSTANCE_ROOM) { - - if (B->base_type==INSTANCE_ROOM) - SWAP(A,B); - - ERR_FAIL_COND(! ((1<<B->base_type)&INSTANCE_GEOMETRY_MASK )); - - B->auto_rooms.erase(A); - B->valid_auto_rooms.erase(A); - A->room_info->owned_autoroom_geometry.erase(B); - - }else { - - - if (B->base_type==INSTANCE_LIGHT) { - - SWAP(A,B); - } else if (A->base_type!=INSTANCE_LIGHT) { - return; - } - - - A->light_info->affected.erase(B); - B->lights.erase(A); - B->light_cache_dirty=true; - } -} - -bool VisualServerRaster::_test_portal_cull(Camera *p_camera, Instance *p_from_portal, Instance *p_to_portal) { - - - int src_point_count=p_from_portal->portal_info->transformed_point_cache.size(); - int dst_point_count=p_to_portal->portal_info->transformed_point_cache.size(); - - if (src_point_count<2 || dst_point_count<2) - return false; - - const Vector3 *src_points=&p_from_portal->portal_info->transformed_point_cache[0]; - const Vector3 *dst_points=&p_to_portal->portal_info->transformed_point_cache[0]; - - bool outside=false; - - bool clockwise = !p_from_portal->portal_info->plane_cache.is_point_over(p_camera->transform.origin); - - for(int i=0;i<src_point_count;i++) { - - const Vector3& point_prev = src_points[i?(i-1):(src_point_count-1)]; - const Vector3& point = src_points[i]; - - Plane p = clockwise?Plane(p_camera->transform.origin,point,point_prev):Plane(p_camera->transform.origin,point_prev,point); - - bool all_over=true; - - for(int j=0;j<dst_point_count;j++) { - - if (!p.is_point_over(dst_points[j])) { - - all_over=false; - break; - } - - } - - if (all_over) { - outside=true; - break; - } - - } - - return !outside; - -} - -void VisualServerRaster::_cull_portal(Camera *p_camera, Instance *p_portal,Instance *p_from_portal) { - - ERR_FAIL_COND(!p_portal->scenario); //scenario outside - - Instance *portal = p_portal; - - if (!portal->room) { - - return; //portals need all to belong to a room, it may be unconfigured yet - } else if (portal->last_render_pass!=render_pass) { - - return; //invalid portal, ignore - } else if (portal->portal_info->last_visited_pass==render_pass) { - - return; //portal already visited - } else if (portal==p_from_portal) { - - return; // came from this portal, don't even bother testing - } - - /* TEST DISABLE DISTANCE */ - - float disable_distance = p_portal->portal_info->portal->disable_distance; - if (disable_distance) { - //has disable distance.. - float distance = p_camera->transform.origin.distance_to(portal->data.transform.origin); - if (disable_distance < distance) { - - return; - } - } - - /* TEST PORTAL NOT FACING OPTIMIZATION */ - - - if (p_portal->portal_info->connected) { - //connected portal means, it must face against the camera to be seen - if (p_portal->portal_info->plane_cache.is_point_over(p_camera->transform.origin)) { //portal facing against camera (exterior) - - return; - } - } else { - //disconencted portals (go from room to parent room or exterior) must face towards the canera - if (!p_portal->portal_info->plane_cache.is_point_over(p_camera->transform.origin)) { //portal facing against camera (exterior) - - return; - } - } - - if (p_from_portal && !_test_portal_cull(p_camera, p_from_portal, portal)) { - return; // portal not visible (culled) - } - - portal->portal_info->last_visited_pass=render_pass; - - if (portal->portal_info->connected) { - - //interior<->interior portal - Instance *to_room = portal->portal_info->connected->room; - if (!to_room) { - return; //wtf.. oh well, connected to a roomless (invalid) portal - } - - _cull_room(p_camera, to_room, portal->portal_info->connected); - - } else { - //to exterior/to parent roomportal - - Instance *parent_room = portal->room->room; - - _cull_room(p_camera, parent_room, portal); - } - -} - -void VisualServerRaster::_cull_room(Camera *p_camera, Instance *p_room,Instance *p_from_portal) { - - if (p_room==NULL) { - //exterior - exterior_visited=true; - - for(int i=0;i<exterior_portal_cull_count;i++) { - - _cull_portal(p_camera, exterior_portal_cull_result[i],p_from_portal); - } - - } else { - - ERR_FAIL_COND(!p_room->scenario); - - if (p_room->last_render_pass!=render_pass) - return; //this room is invalid - - //interior - //first of all, validate the room - p_room->room_info->last_visited_pass=render_pass; - //see about going around portals - if (!p_room->room_info->room->occlude_exterior) - exterior_visited=true; - - for(List<Instance*>::Element * E=p_room->room_info->owned_portal_instances.front();E;E=E->next()) { - - _cull_portal(p_camera, E->get(),p_from_portal); - - } - - for(Set<Instance*>::Element * E=p_room->room_info->disconnected_child_portals.front();E;E=E->next()) { - - _cull_portal(p_camera, E->get(),p_from_portal); - - } - - - } - -} - -void VisualServerRaster::_process_sampled_light(const Transform& p_camera,Instance *p_sampled_light,bool p_linear_colorspace) { - - - BakedLightSampler *sampler_opts = p_sampled_light->baked_light_sampler_info->sampler; - int res = sampler_opts->resolution; - int dp_size = res*res*2; - Color * dp_map = (Color*)alloca( sizeof(Color)*dp_size); //allocate the dual parabolloid colors - Vector3 * dp_normals = (Vector3*)alloca( sizeof(Vector3)*dp_size); //allocate the dual parabolloid normals - const Vector3 * dp_src_normals = p_sampled_light->baked_light_sampler_info->sampler->dp_cache.ptr(); - - - if (!p_sampled_light->baked_light_sampler_info->sampled_light.is_valid() || p_sampled_light->baked_light_sampler_info->resolution!=sampler_opts->resolution) { - if (p_sampled_light->baked_light_sampler_info->sampled_light.is_valid()) { - rasterizer->free(p_sampled_light->baked_light_sampler_info->sampled_light); - } - - p_sampled_light->baked_light_sampler_info->resolution=sampler_opts->resolution; - p_sampled_light->baked_light_sampler_info->sampled_light=rasterizer->sampled_light_dp_create(sampler_opts->resolution,sampler_opts->resolution*2); - - - } - - - zeromem(dp_map,sizeof(Color)*dp_size); - bool valid=false; - int samples=0; - - - for(Set<Instance*>::Element *E=p_sampled_light->baked_light_sampler_info->baked_lights.front();E;E=E->next()) { - - Instance *bl = E->get(); - if (bl->baked_light_info->baked_light->sampler.size()==0) - continue; //not usable - - - Matrix3 norm_xform = bl->baked_light_info->affine_inverse.basis;//.inverse(); - for(int i=0;i<dp_size;i++) { - dp_normals[i]=norm_xform.xform(dp_src_normals[i]).normalized(); - } - - //normals in place - - - //sample octree - - float r = sampler_opts->params[VS::BAKED_LIGHT_SAMPLER_RADIUS]; - float att = sampler_opts->params[VS::BAKED_LIGHT_SAMPLER_ATTENUATION]; - float str = sampler_opts->params[VS::BAKED_LIGHT_SAMPLER_STRENGTH]; - Vector3 s = p_sampled_light->data.transform.basis.get_scale(); - - r*=MAX(MAX(s.x,s.y),s.z); - AABB sample_aabb= bl->data.transform.affine_inverse().xform(AABB(Vector3(-r,-r,-r)+p_sampled_light->data.transform.origin,Vector3(r*2,r*2,r*2))); - //ok got octree local AABB - - PoolVector<int>::Read rp = bl->baked_light_info->baked_light->sampler.read(); - const int *rptr = rp.ptr(); - - int first = rptr[1]; - int depth = rptr[2]; - bool islinear = rptr[3]&1; - depth+=1; - - AABB aabb; - aabb.pos.x=decode_float((const uint8_t*)&rptr[4]); - aabb.pos.y=decode_float((const uint8_t*)&rptr[5]); - aabb.pos.z=decode_float((const uint8_t*)&rptr[6]); - aabb.size.x=decode_float((const uint8_t*)&rptr[7]); - aabb.size.y=decode_float((const uint8_t*)&rptr[8]); - aabb.size.z=decode_float((const uint8_t*)&rptr[9]); - - uint32_t *stack=(uint32_t*)alloca(depth*sizeof(uint32_t)); - int *stack_ptr=(int*)alloca(depth*sizeof(int)); - AABB *aabb_stack=(AABB*)alloca(depth*sizeof(AABB)); - - stack[0]=0; - stack_ptr[0]=first; - aabb_stack[0]=aabb; - Vector3 center = sample_aabb.pos + sample_aabb.size * 0.5; - - - int stack_pos=0; - Color max_col; - - //int reso = sampler_opts->params[VS::BAKED_LIGHT_SAMPLER_DETAIL_RATIO]; - - int lalimit = sample_aabb.get_longest_axis_index(); - float limit = sampler_opts->params[VS::BAKED_LIGHT_SAMPLER_DETAIL_RATIO]*sample_aabb.size[lalimit]; - - - while(true) { - - - bool leaf = (rptr[ stack_ptr[stack_pos] ]>>16)==0; - - if (aabb_stack[stack_pos].size[lalimit]<limit) { - leaf=true; - } - - - if (leaf) { - - Vector3 from = aabb_stack[stack_pos].pos + aabb_stack[stack_pos].size * 0.5; - Vector3 norm = (from-center).normalized(); - - - Color col; - col.r = ((rptr[ stack_ptr[stack_pos] ]&0xFFFF)/256.0); - col.g = ((rptr[ stack_ptr[stack_pos]+1 ]>>16)/256.0); - col.b = ((rptr[ stack_ptr[stack_pos]+1 ]&0xFFFF)/256.0); - - - max_col.r = MAX(max_col.r,col.r); - max_col.g = MAX(max_col.g,col.g); - max_col.b = MAX(max_col.b,col.b); - - if (!islinear && p_linear_colorspace) { - col=col.to_linear(); - } - - float distance; - - if (aabb_stack[stack_pos].has_point(center)) { - distance=0; - } else { - - Vector3 support = aabb_stack[stack_pos].get_support(norm); - distance = Math::absf(norm.dot(support)-norm.dot(center)); - - } - - if (distance>r) - distance=r; - - float mult = Math::pow(1.0-distance/r,att)*str; - if (mult>0) { - col.r*=mult; - col.g*=mult; - col.b*=mult; - - - - for(int i=0;i<dp_size;i++) { - float mult2 = norm.dot(dp_normals[i]); - if (mult2<0) - mult2=0; - Color col2(col.r*mult2,col.g*mult2,col.b*mult2,1.0); - dp_map[i].r=MAX(dp_map[i].r,col2.r); - dp_map[i].g=MAX(dp_map[i].g,col2.g); - dp_map[i].b=MAX(dp_map[i].b,col2.b); - } - - } - - samples++; - //nothing is valid unless you hit a leaf - valid=true; - stack_pos--; - } else if ((stack[stack_pos]&0xFF)<8) { - - int i = stack[stack_pos]&0xFF; - int base = (stack[stack_pos]>>8); - - if (!((rptr[ stack_ptr[stack_pos] ]>>16)&(1<<i))) { - //no bit, no test - stack[stack_pos]=(base<<8)+(i+1); - continue; - } - - stack[stack_pos]=((base+1)<<8)+(i+1); - - AABB child_aabb = aabb_stack[stack_pos]; - child_aabb.size*=0.5; - if (i&1) - child_aabb.pos.x+=child_aabb.size.x; - if (i&2) - child_aabb.pos.y+=child_aabb.size.y; - if (i&4) - child_aabb.pos.z+=child_aabb.size.z; - - if (!child_aabb.intersects(sample_aabb)) { - continue; - } - - if (child_aabb.encloses(sample_aabb)) { - stack[stack_pos]=(base<<8)|8; //don't test the rest - } - - stack_pos++; - ERR_FAIL_COND(stack_pos>=depth); - - stack[stack_pos]=0; - stack_ptr[stack_pos]=rptr[ stack_ptr[stack_pos-1]+2+base ]; - aabb_stack[stack_pos]=child_aabb; - } else { - stack_pos--; - if (stack_pos<0) - break; - } - } - - - } - - //print_line("samples "+itos(samples) ); - - if (valid) { - - for(int i=0;i<res;i++) { - //average seams to avoid aliasing - { - //top - int ofs1 = i; - int ofs2 = dp_size-res+i; - Color avg( - (dp_map[ofs1].r+dp_map[ofs2].r)*0.5, - (dp_map[ofs1].g+dp_map[ofs2].g)*0.5, - (dp_map[ofs1].b+dp_map[ofs2].b)*0.5, - 1.0 - ); - dp_map[ofs1]=avg; - dp_map[ofs2]=avg; - } - { - //bottom - int ofs1 = res*res-res+i; - int ofs2 = res*res+i; - Color avg( - (dp_map[ofs1].r+dp_map[ofs2].r)*0.5, - (dp_map[ofs1].g+dp_map[ofs2].g)*0.5, - (dp_map[ofs1].b+dp_map[ofs2].b)*0.5, - 1.0 - ); - dp_map[ofs1]=avg; - dp_map[ofs2]=avg; - } - { - //left - int ofs1 = i*res; - int ofs2 = res*res+(res-i-1)*res; - Color avg( - (dp_map[ofs1].r+dp_map[ofs2].r)*0.5, - (dp_map[ofs1].g+dp_map[ofs2].g)*0.5, - (dp_map[ofs1].b+dp_map[ofs2].b)*0.5, - 1.0 - ); - dp_map[ofs1]=avg; - dp_map[ofs2]=avg; - } - { - //right - int ofs1 = i*res+(res-1); - int ofs2 = res*res+(res-i-1)*res+(res-1); - Color avg( - (dp_map[ofs1].r+dp_map[ofs2].r)*0.5, - (dp_map[ofs1].g+dp_map[ofs2].g)*0.5, - (dp_map[ofs1].b+dp_map[ofs2].b)*0.5, - 1.0 - ); - dp_map[ofs1]=avg; - dp_map[ofs2]=avg; - } - - } - - rasterizer->sampled_light_dp_update(p_sampled_light->baked_light_sampler_info->sampled_light,dp_map,1.0); - for(Set<Instance*>::Element *F=p_sampled_light->baked_light_sampler_info->owned_instances.front();F;F=F->next()) { - - F->get()->data.sampled_light=p_sampled_light->baked_light_sampler_info->sampled_light; - } - - - } else { - - for(Set<Instance*>::Element *F=p_sampled_light->baked_light_sampler_info->owned_instances.front();F;F=F->next()) { - - F->get()->data.sampled_light; //do not use because nothing close - } - } - - - - -/* - highp vec3 vtx = vertex_interp; - vtx.z*=dual_paraboloid.y; //side to affect - vtx.z+=0.01; - dp_clip=vtx.z; - highp float len=length( vtx ); - vtx=normalize(vtx); - vtx.xy/=1.0+vtx.z; - vtx.z = len*dual_paraboloid.x; // it's a reciprocal(len - z_near) / (z_far - z_near); - vtx+=normalize(vtx)*0.025; - vtx.z = vtx.z * 2.0 - 1.0; // fit to clipspace - vertex_interp=vtx; -*/ - - - - -} - - -void VisualServerRaster::_render_no_camera(Viewport *p_viewport,Camera *p_camera, Scenario *p_scenario) { - RID environment; - if (p_scenario->environment.is_valid()) - environment=p_scenario->environment; - else - environment=p_scenario->fallback_environment; - - rasterizer->set_camera(Transform(),CameraMatrix(),false); - rasterizer->begin_scene(p_viewport->viewport_data,environment,p_scenario->debug); - rasterizer->set_viewport(viewport_rect); - rasterizer->end_scene(); -} - - -void VisualServerRaster::_render_camera(Viewport *p_viewport,Camera *p_camera, Scenario *p_scenario) { - - - render_pass++; - uint32_t camera_layer_mask=p_camera->visible_layers; - - /* STEP 1 - SETUP CAMERA */ - CameraMatrix camera_matrix; - bool ortho=false; - - switch(p_camera->type) { - case Camera::ORTHOGONAL: { - - camera_matrix.set_orthogonal( - p_camera->size, - viewport_rect.width / (float)viewport_rect.height, - p_camera->znear, - p_camera->zfar, - p_camera->vaspect - - ); - ortho=true; - } break; - case Camera::PERSPECTIVE: { - - camera_matrix.set_perspective( - p_camera->fov, - viewport_rect.width / (float)viewport_rect.height, - p_camera->znear, - p_camera->zfar, - p_camera->vaspect - - ); - ortho=false; - - } break; - } - - - rasterizer->set_camera(p_camera->transform, camera_matrix,ortho); - - Vector<Plane> planes = camera_matrix.get_projection_planes(p_camera->transform); - - CullRange cull_range; // cull range is used for PSSM, and having an idea of the rendering depth - cull_range.nearp=Plane(p_camera->transform.origin,-p_camera->transform.basis.get_axis(2).normalized()); - cull_range.z_near=camera_matrix.get_z_near(); - cull_range.z_far=camera_matrix.get_z_far(); - cull_range.min=cull_range.z_far; - cull_range.max=cull_range.z_near; - - /* STEP 2 - CULL */ - int cull_count = p_scenario->octree.cull_convex(planes,instance_cull_result,MAX_INSTANCE_CULL); - light_cull_count=0; - light_samplers_culled=0; - -/* print_line("OT: "+rtos( (OS::get_singleton()->get_ticks_usec()-t)/1000.0)); - print_line("OTO: "+itos(p_scenario->octree.get_octant_count())); - //print_line("OTE: "+itos(p_scenario->octree.get_elem_count())); - print_line("OTP: "+itos(p_scenario->octree.get_pair_count())); -*/ - - /* STEP 3 - PROCESS PORTALS, VALIDATE ROOMS */ - - - // compute portals - - exterior_visited=false; - exterior_portal_cull_count=0; - - if (room_cull_enabled) { - for(int i=0;i<cull_count;i++) { - - Instance *ins = instance_cull_result[i]; - ins->last_render_pass=render_pass; - - if (ins->base_type!=INSTANCE_PORTAL) - continue; - - if (ins->room) - continue; - - ERR_CONTINUE(exterior_portal_cull_count>=MAX_EXTERIOR_PORTALS); - exterior_portal_cull_result[exterior_portal_cull_count++]=ins; - - } - - room_cull_count = p_scenario->octree.cull_point(p_camera->transform.origin,room_cull_result,MAX_ROOM_CULL,NULL,(1<<INSTANCE_ROOM)|(1<<INSTANCE_PORTAL)); - - - Set<Instance*> current_rooms; - Set<Instance*> portal_rooms; - //add to set - for(int i=0;i<room_cull_count;i++) { - - if (room_cull_result[i]->base_type==INSTANCE_ROOM) { - current_rooms.insert(room_cull_result[i]); - } - if (room_cull_result[i]->base_type==INSTANCE_PORTAL) { - //assume inside that room if also inside the portal.. - if (room_cull_result[i]->room) { - portal_rooms.insert(room_cull_result[i]->room); - } - - SWAP(room_cull_result[i],room_cull_result[room_cull_count-1]); - room_cull_count--; - i--; - } - } - - //remove from set if it has a parent room or BSP doesn't contain - for(int i=0;i<room_cull_count;i++) { - Instance *r = room_cull_result[i]; - - //check inside BSP - Vector3 room_local_point = r->room_info->affine_inverse.xform( p_camera->transform.origin ); - - if (!portal_rooms.has(r) && !r->room_info->room->bounds.point_is_inside(room_local_point)) { - - current_rooms.erase(r); - continue; - } - - //check parent - while (r->room) {// has parent room - - current_rooms.erase(r); - r=r->room; - } - - } - - if (current_rooms.size()) { - //camera is inside a room - // go through rooms - for(Set<Instance*>::Element *E=current_rooms.front();E;E=E->next()) { - _cull_room(p_camera,E->get()); - } - - } else { - //start from exterior - _cull_room(p_camera,NULL); - - } - } - - /* STEP 4 - REMOVE FURTHER CULLED OBJECTS, ADD LIGHTS */ - - for(int i=0;i<cull_count;i++) { - - Instance *ins = instance_cull_result[i]; - - bool keep=false; - - - if ((camera_layer_mask&ins->layer_mask)==0) { - - //failure - } else if (ins->base_type==INSTANCE_LIGHT) { - - if (light_cull_count<MAX_LIGHTS_CULLED) { - light_cull_result[light_cull_count++]=ins; - //rasterizer->light_instance_set_active_hint(ins->light_info->instance); - { - //compute distance to camera using aabb support - Vector3 n = ins->data.transform.basis.xform_inv(cull_range.nearp.normal).normalized(); - Vector3 s = ins->data.transform.xform(ins->aabb.get_support(n)); - ins->light_info->dtc=cull_range.nearp.distance_to(s); - } - } - - } else if ((1<<ins->base_type)&INSTANCE_GEOMETRY_MASK && ins->visible && ins->data.cast_shadows!=VS::SHADOW_CASTING_SETTING_SHADOWS_ONLY) { - - - bool discarded=false; - - if (ins->draw_range_end>0) { - - float d = cull_range.nearp.distance_to(ins->data.transform.origin); - if (d<0) - d=0; - discarded=(d<ins->draw_range_begin || d>=ins->draw_range_end); - - - } - - if (!discarded) { - - // test if this geometry should be visible - - if (room_cull_enabled) { - - - if (ins->visible_in_all_rooms) { - keep=true; - } else if (ins->room) { - - if (ins->room->room_info->last_visited_pass==render_pass) - keep=true; - } else if (ins->auto_rooms.size()) { - - - for(Set<Instance*>::Element *E=ins->auto_rooms.front();E;E=E->next()) { - - if (E->get()->room_info->last_visited_pass==render_pass) { - keep=true; - break; - } - } - } else if(exterior_visited) - keep=true; - } else { - - keep=true; - } - - - } - - - if (keep) { - // update cull range - float min,max; - ins->transformed_aabb.project_range_in_plane(cull_range.nearp,min,max); - - if (min<cull_range.min) - cull_range.min=min; - if (max>cull_range.max) - cull_range.max=max; - - if (ins->sampled_light && ins->sampled_light->baked_light_sampler_info->last_pass!=render_pass) { - if (light_samplers_culled<MAX_LIGHT_SAMPLERS) { - light_sampler_cull_result[light_samplers_culled++]=ins->sampled_light; - ins->sampled_light->baked_light_sampler_info->last_pass=render_pass; - } - } - } - - } - - if (!keep) { - // remove, no reason to keep - cull_count--; - SWAP( instance_cull_result[i], instance_cull_result[ cull_count ] ); - i--; - ins->last_render_pass=0; // make invalid - } else { - - ins->last_render_pass=render_pass; - } - } - - if (cull_range.max > cull_range.z_far ) - cull_range.max=cull_range.z_far; - if (cull_range.min < cull_range.z_near ) - cull_range.min=cull_range.z_near; - - /* STEP 5 - PROCESS LIGHTS */ - - rasterizer->shadow_clear_near(); //clear near shadows, will be recreated - - // directional lights - { - List<RID>::Element *E=p_scenario->directional_lights.front(); - - - while(E) { - - Instance *light = E->get().is_valid()?instance_owner.get(E->get()):NULL; - - if (light && light->light_info->enabled && rasterizer->light_has_shadow(light->base_rid)) { - //rasterizer->light_instance_set_active_hint(light->light_info->instance); - _light_instance_update_shadow(light,p_scenario,p_camera,cull_range); - } - - E=E->next(); - } - } - - - //discard lights not affecting anything (useful for deferred rendering, shadowmaps, etc) - - for (int i=0;i<light_cull_count;i++) { - - Instance *ins = light_cull_result[i]; - - if (light_discard_enabled) { - - //see if the light should be pre discarded because no one is seeing it - //this test may seem expensive, but in reality, it shouldn't be - //because of early out condition. It will only go through everything - //if it's being discarded. - - bool valid=false; - InstanceSet::Element *E =ins->light_info->affected.front(); - while(E) { - - if (E->get()->last_render_pass==render_pass) { - - valid=true; // early out. - break; - } - E=E->next(); - } - if (!valid) { - - light_cull_count--; - SWAP( light_cull_result[i], light_cull_result[ light_cull_count ] ); - i--; - - } - } - - } - - { //this should eventually change to - //assign shadows by distance to camera - SortArray<Instance*,_InstanceLightsort> sorter; - sorter.sort(light_cull_result,light_cull_count); - for (int i=0;i<light_cull_count;i++) { - - Instance *ins = light_cull_result[i]; - - if (!rasterizer->light_has_shadow(ins->base_rid) || !shadows_enabled) - continue; - - /* for far shadows? - if (ins->version == ins->light_info->last_version && rasterizer->light_instance_has_far_shadow(ins->light_info->instance)) - continue; // didn't change - */ - - _light_instance_update_shadow(ins,p_scenario,p_camera,cull_range); - ins->light_info->last_version=ins->version; - } - } - - /* ENVIRONMENT */ - - RID environment; - if (p_camera->env.is_valid()) //camera has more environment priority - environment=p_camera->env; - else if (p_scenario->environment.is_valid()) - environment=p_scenario->environment; - else - environment=p_scenario->fallback_environment; - - - /* STEP 6 - SAMPLE BAKED LIGHT */ - - bool islinear =false; - if (environment.is_valid()) { - islinear = rasterizer->environment_is_fx_enabled(environment,VS::ENV_FX_SRGB); - } - - for(int i=0;i<light_samplers_culled;i++) { - - _process_sampled_light(p_camera->transform,light_sampler_cull_result[i],islinear); - } - - /* STEP 7 - PROCESS GEOMETRY AND DRAW SCENE*/ - - - rasterizer->begin_scene(p_viewport->viewport_data,environment,p_scenario->debug); - rasterizer->set_viewport(viewport_rect); - - // add lights - - { - List<RID>::Element *E=p_scenario->directional_lights.front(); - - - for(;E;E=E->next()) { - Instance *light = E->get().is_valid()?instance_owner.get(E->get()):NULL; - - ERR_CONTINUE(!light); - if (!light->light_info->enabled) - continue; - - rasterizer->add_light(light->light_info->instance); - light->light_info->last_add_pass=render_pass; - } - - for (int i=0;i<light_cull_count;i++) { - - Instance *ins = light_cull_result[i]; - rasterizer->add_light(ins->light_info->instance); - ins->light_info->last_add_pass=render_pass; - } - } - // add geometry - - for(int i=0;i<cull_count;i++) { - - Instance *ins = instance_cull_result[i]; - - ERR_CONTINUE(!((1<<ins->base_type)&INSTANCE_GEOMETRY_MASK)); - - _instance_draw(ins); - } - - rasterizer->end_scene(); -} - - -void VisualServerRaster::_render_canvas_item_tree(CanvasItem *p_canvas_item, const Matrix32& p_transform, const Rect2& p_clip_rect, const Color& p_modulate, Rasterizer::CanvasLight *p_lights) { - - - static const int z_range = CANVAS_ITEM_Z_MAX-CANVAS_ITEM_Z_MIN+1; - Rasterizer::CanvasItem *z_list[z_range]; - Rasterizer::CanvasItem *z_last_list[z_range]; - - for(int i=0;i<z_range;i++) { - z_list[i]=NULL; - z_last_list[i]=NULL; - } - - - _render_canvas_item(p_canvas_item,p_transform,p_clip_rect,1.0,0,z_list,z_last_list,NULL,NULL); - - for(int i=0;i<z_range;i++) { - if (!z_list[i]) - continue; - rasterizer->canvas_render_items(z_list[i],CANVAS_ITEM_Z_MIN+i,p_modulate,p_lights); - } - -} - - -void VisualServerRaster::_render_canvas_item_viewport(VisualServer* p_self,void *p_vp,const Rect2& p_rect) { - - VisualServerRaster *self=(VisualServerRaster*)(p_self); - Viewport *vp=(Viewport*)p_vp; - self->_draw_viewport(vp,p_rect.pos.x,p_rect.pos.y,p_rect.size.x,p_rect.size.y); - self->rasterizer->canvas_begin(); - -} - - -void VisualServerRaster::_render_canvas_item(CanvasItem *p_canvas_item,const Matrix32& p_transform,const Rect2& p_clip_rect, float p_opacity,int p_z,Rasterizer::CanvasItem **z_list,Rasterizer::CanvasItem **z_last_list,CanvasItem *p_canvas_clip,CanvasItem *p_material_owner) { - - CanvasItem *ci = p_canvas_item; - - if (!ci->visible) - return; - - if (p_opacity<0.007) - return; - - - Rect2 rect = ci->get_rect(); - Matrix32 xform = p_transform * ci->xform; - Rect2 global_rect = xform.xform(rect); - global_rect.pos+=p_clip_rect.pos; - - - if (global_rect.intersects(p_clip_rect) && ci->viewport.is_valid() && viewport_owner.owns(ci->viewport)) { - - Viewport *vp = viewport_owner.get(ci->viewport); - - Point2i from = xform.get_origin() + Point2(viewport_rect.x,viewport_rect.y); - Point2i size = rect.size; - size.x *= xform[0].length(); - size.y *= xform[1].length(); - - ci->vp_render = memnew( Rasterizer::CanvasItem::ViewportRender ); - ci->vp_render->owner=this; - ci->vp_render->udata=vp; - ci->vp_render->rect=Rect2(from.x, - from.y, - size.x, - size.y); -/* - _draw_viewport(vp, - from.x, - from.y, - size.x, - size.y); -*/ - //rasterizer->canvas_begin(); - } else { - ci->vp_render=NULL; - } - - if (ci->use_parent_material && p_material_owner) - ci->material_owner=p_material_owner; - else { - p_material_owner=ci; - ci->material_owner=NULL; - } - - - float opacity = ci->opacity * p_opacity; - - - int child_item_count=ci->child_items.size(); - CanvasItem **child_items=(CanvasItem**)alloca(child_item_count*sizeof(CanvasItem*)); - copymem(child_items,ci->child_items.ptr(),child_item_count*sizeof(CanvasItem*)); - - if (ci->clip) { - if (p_canvas_clip != NULL) { - ci->final_clip_rect=p_canvas_clip->final_clip_rect.clip(global_rect); - } else { - ci->final_clip_rect=global_rect; - } - ci->final_clip_owner=ci; - - } else { - ci->final_clip_owner=p_canvas_clip; - } - - if (ci->sort_y) { - - SortArray<CanvasItem*,CanvasItemPtrSort> sorter; - sorter.sort(child_items,child_item_count); - } - - if (ci->z_relative) - p_z=CLAMP(p_z+ci->z,CANVAS_ITEM_Z_MIN,CANVAS_ITEM_Z_MAX); - else - p_z=ci->z; - - for(int i=0;i<child_item_count;i++) { - - if (child_items[i]->ontop) - continue; - _render_canvas_item(child_items[i],xform,p_clip_rect,opacity,p_z,z_list,z_last_list,(CanvasItem*)ci->final_clip_owner,p_material_owner); - } - - if (ci->copy_back_buffer) { - - ci->copy_back_buffer->screen_rect = xform.xform(ci->copy_back_buffer->rect).clip(p_clip_rect); - } - - if ((!ci->commands.empty() && p_clip_rect.intersects(global_rect)) || ci->vp_render || ci->copy_back_buffer) { - //something to draw? - ci->final_transform=xform; - ci->final_opacity=opacity * ci->self_opacity; - ci->global_rect_cache=global_rect; - ci->global_rect_cache.pos-=p_clip_rect.pos; - ci->light_masked=false; - - int zidx = p_z-CANVAS_ITEM_Z_MIN; - - if (z_last_list[zidx]) { - z_last_list[zidx]->next=ci; - z_last_list[zidx]=ci; - - } else { - z_list[zidx]=ci; - z_last_list[zidx]=ci; - } - - - - ci->next=NULL; - - } - - for(int i=0;i<child_item_count;i++) { - - if (!child_items[i]->ontop) - continue; - _render_canvas_item(child_items[i],xform,p_clip_rect,opacity,p_z,z_list,z_last_list,(CanvasItem*)ci->final_clip_owner,p_material_owner); - } - -} - -void VisualServerRaster::_light_mask_canvas_items(int p_z,Rasterizer::CanvasItem *p_canvas_item,Rasterizer::CanvasLight *p_masked_lights) { - - if (!p_masked_lights) - return; - - Rasterizer::CanvasItem *ci=p_canvas_item; - - while(ci) { - - Rasterizer::CanvasLight *light=p_masked_lights; - while(light) { - - if (ci->light_mask&light->item_mask && p_z>=light->z_min && p_z<=light->z_max && ci->global_rect_cache.intersects_transformed(light->xform_cache,light->rect_cache)) { - ci->light_masked=true; - } - - light=light->mask_next_ptr; - } - - ci=ci->next; - } - - - - -} - -void VisualServerRaster::_render_canvas(Canvas *p_canvas,const Matrix32 &p_transform,Rasterizer::CanvasLight *p_lights,Rasterizer::CanvasLight *p_masked_lights) { - - rasterizer->canvas_begin(); - - int l = p_canvas->child_items.size(); - Canvas::ChildItem *ci=p_canvas->child_items.ptr(); - - bool has_mirror=false; - for(int i=0;i<l;i++) { - if (ci[i].mirror.x || ci[i].mirror.y) { - has_mirror=true; - break; - } - } - - Rect2 clip_rect(viewport_rect.x,viewport_rect.y,viewport_rect.width,viewport_rect.height); - if (!has_mirror) { - - static const int z_range = CANVAS_ITEM_Z_MAX-CANVAS_ITEM_Z_MIN+1; - Rasterizer::CanvasItem *z_list[z_range]; - Rasterizer::CanvasItem *z_last_list[z_range]; - - for(int i=0;i<z_range;i++) { - z_list[i]=NULL; - z_last_list[i]=NULL; - } - for(int i=0;i<l;i++) { - _render_canvas_item(ci[i].item,p_transform,clip_rect,1.0,0,z_list,z_last_list,NULL,NULL); - } - - for(int i=0;i<z_range;i++) { - if (!z_list[i]) - continue; - - if (p_masked_lights) { - _light_mask_canvas_items(CANVAS_ITEM_Z_MIN+i,z_list[i],p_masked_lights); - } - - rasterizer->canvas_render_items(z_list[i],CANVAS_ITEM_Z_MIN+i,p_canvas->modulate,p_lights); - } - } else { - - for(int i=0;i<l;i++) { - - Canvas::ChildItem& ci=p_canvas->child_items[i]; - _render_canvas_item_tree(ci.item,p_transform,clip_rect,p_canvas->modulate,p_lights); - - //mirroring (useful for scrolling backgrounds) - if (ci.mirror.x!=0) { - - Matrix32 xform2 = p_transform * Matrix32(0,Vector2(ci.mirror.x,0)); - _render_canvas_item_tree(ci.item,xform2,clip_rect,p_canvas->modulate,p_lights); - } - if (ci.mirror.y!=0) { - - Matrix32 xform2 = p_transform * Matrix32(0,Vector2(0,ci.mirror.y)); - _render_canvas_item_tree(ci.item,xform2,clip_rect,p_canvas->modulate,p_lights); - } - if (ci.mirror.y!=0 && ci.mirror.x!=0) { - - Matrix32 xform2 = p_transform * Matrix32(0,ci.mirror); - _render_canvas_item_tree(ci.item,xform2,clip_rect,p_canvas->modulate,p_lights); - } - - } - } - -} - - -void VisualServerRaster::_draw_viewport_camera(Viewport *p_viewport,bool p_ignore_camera) { - - - Camera *camera=NULL; - if (camera_owner.owns( p_viewport->camera )) - camera=camera_owner.get( p_viewport->camera ); - Scenario *scenario = scenario_owner.get( p_viewport->scenario ); - - _update_instances(); // check dirty instances before rendering - - if (p_ignore_camera) - _render_no_camera(p_viewport, camera,scenario ); - else - _render_camera(p_viewport, camera,scenario ); - -} - -void VisualServerRaster::_draw_viewport(Viewport *p_viewport,int p_ofs_x, int p_ofs_y,int p_parent_w,int p_parent_h) { - - ViewportRect desired_rect=p_viewport->rect; - ViewportRect old_rect = viewport_rect; - //bool vpchanged=false; - // convert default expanding viewports to actual size - //if (desired_rect.x==0 && desired_rect.y==0 && desired_rect.width==0 && desired_rect.height==0) { - if (p_parent_w != 0 && p_parent_h != 0) { - - desired_rect.width=p_parent_w; - desired_rect.height=p_parent_h; - } - - ERR_FAIL_COND(desired_rect.width<=0 || desired_rect.height<=0); - - desired_rect.x+=p_ofs_x; - desired_rect.y+=p_ofs_y; - - - // if the viewport is different than the actual one, change it - - if ( p_viewport->render_target.is_valid() || viewport_rect.x != desired_rect.x || - viewport_rect.y != desired_rect.y || - viewport_rect.width != desired_rect.width || - viewport_rect.height != desired_rect.height ) { - - - viewport_rect=desired_rect; - rasterizer->set_viewport(viewport_rect); - - } - - /* Camera should always be BEFORE any other 3D */ - - bool scenario_draw_canvas_bg=false; - int scenario_canvas_max_layer=0; - - if (!p_viewport->hide_canvas && !p_viewport->disable_environment && scenario_owner.owns(p_viewport->scenario)) { - - Scenario *scenario=scenario_owner.get(p_viewport->scenario); - if (scenario->environment.is_valid()) { - if (rasterizer->is_environment(scenario->environment)) { - scenario_draw_canvas_bg=rasterizer->environment_get_background(scenario->environment)==VS::ENV_BG_CANVAS; - scenario_canvas_max_layer=rasterizer->environment_get_background_param(scenario->environment,VS::ENV_BG_PARAM_CANVAS_MAX_LAYER); - } - } - } - - bool can_draw_3d=!p_viewport->hide_scenario && camera_owner.owns(p_viewport->camera) && scenario_owner.owns(p_viewport->scenario); - - - if (scenario_draw_canvas_bg) { - - rasterizer->begin_canvas_bg(); - } - - if (!scenario_draw_canvas_bg && can_draw_3d) { - - _draw_viewport_camera(p_viewport,false); - - } else if (true /*|| !p_viewport->canvas_list.empty()*/){ - - //clear the viewport black because of no camera? i seriously should.. - if (p_viewport->render_target_clear_on_new_frame || p_viewport->render_target_clear) { - if (p_viewport->transparent_bg) { - rasterizer->clear_viewport(Color(0,0,0,0)); - } - else { - Color cc=clear_color; - if (scenario_draw_canvas_bg) - cc.a=0; - rasterizer->clear_viewport(cc); - } - p_viewport->render_target_clear=false; - } - } - - if (!p_viewport->hide_canvas) { - int i=0; - - Map<Viewport::CanvasKey,Viewport::CanvasData*> canvas_map; - - Rect2 clip_rect(0,0,viewport_rect.width,viewport_rect.height); - Rasterizer::CanvasLight *lights=NULL; - Rasterizer::CanvasLight *lights_with_shadow=NULL; - Rasterizer::CanvasLight *lights_with_mask=NULL; - Rect2 shadow_rect; - - int light_count=0; - - for (Map<RID,Viewport::CanvasData>::Element *E=p_viewport->canvas_map.front();E;E=E->next()) { - - Matrix32 xf = p_viewport->global_transform * E->get().transform; - - //find lights in canvas - - - for(Set<Rasterizer::CanvasLight*>::Element *F=E->get().canvas->lights.front();F;F=F->next()) { - - - Rasterizer::CanvasLight* cl=F->get(); - if (cl->enabled && cl->texture.is_valid()) { - //not super efficient.. - Size2 tsize(rasterizer->texture_get_width(cl->texture),rasterizer->texture_get_height(cl->texture)); - tsize*=cl->scale; - - Vector2 offset=tsize/2.0; - cl->rect_cache=Rect2(-offset+cl->texture_offset,tsize); - cl->xform_cache=xf * cl->xform; - - - if (clip_rect.intersects_transformed(cl->xform_cache,cl->rect_cache)) { - - cl->filter_next_ptr=lights; - lights=cl; - cl->texture_cache=NULL; - Matrix32 scale; - scale.scale(cl->rect_cache.size); - scale.elements[2]=cl->rect_cache.pos; - cl->light_shader_xform = (cl->xform_cache * scale).affine_inverse(); - cl->light_shader_pos=cl->xform_cache[2]; - if (cl->shadow_buffer.is_valid()) { - - cl->shadows_next_ptr=lights_with_shadow; - if (lights_with_shadow==NULL) { - shadow_rect = cl->xform_cache.xform(cl->rect_cache); - } else { - shadow_rect=shadow_rect.merge( cl->xform_cache.xform(cl->rect_cache) ); - } - lights_with_shadow=cl; - cl->radius_cache=cl->rect_cache.size.length(); - - } - if (cl->mode==CANVAS_LIGHT_MODE_MASK) { - cl->mask_next_ptr=lights_with_mask; - lights_with_mask=cl; - } - - light_count++; - } - - } - } - - //print_line("lights: "+itos(light_count)); - canvas_map[ Viewport::CanvasKey( E->key(), E->get().layer) ]=&E->get(); - - } - - if (lights_with_shadow) { - //update shadows if any - - Rasterizer::CanvasLightOccluderInstance * occluders=NULL; - - //make list of occluders - for (Map<RID,Viewport::CanvasData>::Element *E=p_viewport->canvas_map.front();E;E=E->next()) { - - Matrix32 xf = p_viewport->global_transform * E->get().transform; - - for(Set<Rasterizer::CanvasLightOccluderInstance*>::Element *F=E->get().canvas->occluders.front();F;F=F->next()) { - - if (!F->get()->enabled) - continue; - F->get()->xform_cache = xf * F->get()->xform; - if (shadow_rect.intersects_transformed(F->get()->xform_cache,F->get()->aabb_cache)) { - - F->get()->next=occluders; - occluders=F->get(); - - } - } - } - //update the light shadowmaps with them - Rasterizer::CanvasLight *light=lights_with_shadow; - while(light) { - - rasterizer->canvas_light_shadow_buffer_update(light->shadow_buffer,light->xform_cache.affine_inverse(),light->item_mask,light->radius_cache/1000.0,light->radius_cache*1.1,occluders,&light->shadow_matrix_cache); - light=light->shadows_next_ptr; - } - - rasterizer->set_viewport(viewport_rect); //must reset viewport afterwards - } - - - - - if (scenario_draw_canvas_bg && canvas_map.front() && canvas_map.front()->key().layer>scenario_canvas_max_layer) { - - _draw_viewport_camera(p_viewport,!can_draw_3d); - scenario_draw_canvas_bg=false; - - } - - for (Map<Viewport::CanvasKey,Viewport::CanvasData*>::Element *E=canvas_map.front();E;E=E->next()) { - - - //print_line("canvas "+itos(i)+" size: "+itos(I->get()->canvas->child_items.size())); - //print_line("GT "+p_viewport->global_transform+". CT: "+E->get()->transform); - Matrix32 xform = p_viewport->global_transform * E->get()->transform; - - Rasterizer::CanvasLight *canvas_lights=NULL; - - Rasterizer::CanvasLight *ptr=lights; - while(ptr) { - if (E->get()->layer>=ptr->layer_min && E->get()->layer<=ptr->layer_max) { - ptr->next_ptr=canvas_lights; - canvas_lights=ptr; - } - ptr=ptr->filter_next_ptr; - } - - _render_canvas( E->get()->canvas,xform,canvas_lights,lights_with_mask ); - i++; - - if (scenario_draw_canvas_bg && E->key().layer>=scenario_canvas_max_layer) { - _draw_viewport_camera(p_viewport,!can_draw_3d); - scenario_draw_canvas_bg=false; - } - - - } - - if (scenario_draw_canvas_bg) { - _draw_viewport_camera(p_viewport,!can_draw_3d); - scenario_draw_canvas_bg=false; - } - - - //rasterizer->canvas_debug_viewport_shadows(lights_with_shadow); - } - - //capture - - if (p_viewport->queue_capture) { - - rasterizer->capture_viewport(&p_viewport->capture); - p_viewport->queue_capture = false; - } - - //restore - if ( viewport_rect.x != old_rect.x || - viewport_rect.y != old_rect.y || - viewport_rect.width != old_rect.width || - viewport_rect.height != old_rect.height ) { - - viewport_rect=old_rect; - - rasterizer->set_viewport(viewport_rect); - } - - -} - -void VisualServerRaster::_draw_viewports() { - - //draw viewports for render targets - - List<Viewport*> to_blit; - List<Viewport*> to_disable; - for(SelfList<Viewport> *E=viewport_update_list.first();E;E=E->next()) { - - Viewport *vp = E->self(); - ERR_CONTINUE(!vp); - if ( - vp->render_target_update_mode==RENDER_TARGET_UPDATE_WHEN_VISIBLE && - !vp->rendered_in_prev_frame && - !vp->queue_capture - ) { - - continue; - } - - if (vp->rt_to_screen_rect!=Rect2()) - to_blit.push_back(vp); - - rasterizer->set_render_target(vp->render_target,vp->transparent_bg,vp->render_target_vflip); - _draw_viewport(vp,0,0,vp->rect.width,vp->rect.height); - - if ( (vp->queue_capture && vp->render_target_update_mode==RENDER_TARGET_UPDATE_DISABLED) || vp->render_target_update_mode==RENDER_TARGET_UPDATE_ONCE) { - //was only enabled for capture - to_disable.push_back(vp); - vp->render_target_update_mode=RENDER_TARGET_UPDATE_DISABLED; - } - - } - - rasterizer->set_render_target(RID()); - - while(to_disable.size()) { - //disable again because it was only for capture - viewport_update_list.remove(&to_disable.front()->get()->update_list); - to_disable.pop_front(); - } - - - //draw RTs directly to screen when requested - - for (List<Viewport*>::Element *E=to_blit.front();E;E=E->next()) { - - int window_w = OS::get_singleton()->get_video_mode().width; - int window_h = OS::get_singleton()->get_video_mode().height; - - ViewportRect desired_rect; - desired_rect.x = desired_rect.y = 0; - desired_rect.width = window_w; - desired_rect.height = window_h; - - if ( viewport_rect.x != desired_rect.x || - viewport_rect.y != desired_rect.y || - viewport_rect.width != desired_rect.width || - viewport_rect.height != desired_rect.height ) { - - viewport_rect=desired_rect; - - rasterizer->set_viewport(viewport_rect); - } - - rasterizer->canvas_begin(); - rasterizer->canvas_disable_blending(); - rasterizer->canvas_begin_rect(Matrix32()); - rasterizer->canvas_draw_rect(E->get()->rt_to_screen_rect,0,Rect2(Point2(),E->get()->rt_to_screen_rect.size),E->get()->render_target_texture,Color(1,1,1)); - - } - - - - //draw viewports attached to screen - - for(Map<RID,int>::Element *E=screen_viewports.front();E;E=E->next()) { - - Viewport *vp = viewport_owner.get(E->key()); - ERR_CONTINUE(!vp); - - int window_w = OS::get_singleton()->get_video_mode(E->get()).width; - int window_h = OS::get_singleton()->get_video_mode(E->get()).height; - - Rect2 r(0,0,vp->rect.width,vp->rect.height); - if (r.size.width==0) - r.size.width=window_w; - if (r.size.height==0) - r.size.height=window_h; - - - _draw_viewport(vp,r.pos.x,r.pos.y,r.size.width,r.size.height); - } - - - //check when a viewport associated to a render target was drawn - - for(SelfList<Viewport> *E=viewport_update_list.first();E;E=E->next()) { - - Viewport *vp = E->self(); - ERR_CONTINUE(!vp); - if (vp->render_target_update_mode!=RENDER_TARGET_UPDATE_WHEN_VISIBLE) - continue; - vp->rendered_in_prev_frame=rasterizer->render_target_renedered_in_frame(vp->render_target); - } - -} - - - -void VisualServerRaster::_draw_cursors_and_margins() { - - int window_w = OS::get_singleton()->get_video_mode().width; - int window_h = OS::get_singleton()->get_video_mode().height; - - ViewportRect desired_rect; - desired_rect.x = desired_rect.y = 0; - desired_rect.width = window_w; - desired_rect.height = window_h; - - if ( viewport_rect.x != desired_rect.x || - viewport_rect.y != desired_rect.y || - viewport_rect.width != desired_rect.width || - viewport_rect.height != desired_rect.height ) { - - viewport_rect=desired_rect; - - rasterizer->set_viewport(viewport_rect); - } - - rasterizer->canvas_begin(); - rasterizer->canvas_begin_rect(Matrix32()); - - for (int i=0; i<MAX_CURSORS; i++) { - - if (!cursors[i].visible) { - - continue; - }; - - RID tex = cursors[i].texture?cursors[i].texture:default_cursor_texture; - ERR_CONTINUE( !tex ); - if (cursors[i].region.has_no_area()) { - Point2 size(texture_get_width(tex), texture_get_height(tex)); - rasterizer->canvas_draw_rect(Rect2(cursors[i].pos-cursors[i].center, size), 0, Rect2(), tex, Color(1, 1, 1, 1)); - } else { - Point2 size = cursors[i].region.size; - rasterizer->canvas_draw_rect(Rect2(cursors[i].pos-cursors[i].center, size), Rasterizer::CANVAS_RECT_REGION, cursors[i].region, tex, Color(1, 1, 1, 1)); - } - }; - - - - if (black_image[MARGIN_LEFT].is_valid()) { - Size2 sz(rasterizer->texture_get_width(black_image[MARGIN_LEFT]),rasterizer->texture_get_height(black_image[MARGIN_LEFT])); - rasterizer->canvas_draw_rect(Rect2(0,0,black_margin[MARGIN_LEFT],window_h),0,Rect2(0,0,sz.x,sz.y),black_image[MARGIN_LEFT],Color(1,1,1)); - } else if (black_margin[MARGIN_LEFT]) - rasterizer->canvas_draw_rect(Rect2(0,0,black_margin[MARGIN_LEFT],window_h),0,Rect2(0,0,1,1),RID(),Color(0,0,0)); - - if (black_image[MARGIN_RIGHT].is_valid()) { - Size2 sz(rasterizer->texture_get_width(black_image[MARGIN_RIGHT]),rasterizer->texture_get_height(black_image[MARGIN_RIGHT])); - rasterizer->canvas_draw_rect(Rect2(window_w-black_margin[MARGIN_RIGHT],0,black_margin[MARGIN_RIGHT],window_h),0,Rect2(0,0,sz.x,sz.y),black_image[MARGIN_RIGHT],Color(1,1,1)); - } else if (black_margin[MARGIN_RIGHT]) - rasterizer->canvas_draw_rect(Rect2(window_w-black_margin[MARGIN_RIGHT],0,black_margin[MARGIN_RIGHT],window_h),0,Rect2(0,0,1,1),RID(),Color(0,0,0)); - - if (black_image[MARGIN_TOP].is_valid()) { - Size2 sz(rasterizer->texture_get_width(black_image[MARGIN_TOP]),rasterizer->texture_get_height(black_image[MARGIN_TOP])); - rasterizer->canvas_draw_rect(Rect2(0,0,window_w,black_margin[MARGIN_TOP]),0,Rect2(0,0,sz.x,sz.y),black_image[MARGIN_TOP],Color(1,1,1)); - - } else if (black_margin[MARGIN_TOP]) { - rasterizer->canvas_draw_rect(Rect2(0,0,window_w,black_margin[MARGIN_TOP]),0,Rect2(0,0,1,1),RID(),Color(0,0,0)); - } - - if (black_image[MARGIN_BOTTOM].is_valid()) { - - Size2 sz(rasterizer->texture_get_width(black_image[MARGIN_BOTTOM]),rasterizer->texture_get_height(black_image[MARGIN_BOTTOM])); - rasterizer->canvas_draw_rect(Rect2(0,window_h-black_margin[MARGIN_BOTTOM],window_w,black_margin[MARGIN_BOTTOM]),0,Rect2(0,0,sz.x,sz.y),black_image[MARGIN_BOTTOM],Color(1,1,1)); - } else if (black_margin[MARGIN_BOTTOM]) { - rasterizer->canvas_draw_rect(Rect2(0,window_h-black_margin[MARGIN_BOTTOM],window_w,black_margin[MARGIN_BOTTOM]),0,Rect2(0,0,1,1),RID(),Color(0,0,0)); - } - - rasterizer->canvas_end_rect(); -}; - -void VisualServerRaster::sync() { - //do none -} - -void VisualServerRaster::draw() { - /* - if (changes) - print_line("changes: "+itos(changes)); - */ - changes=0; - shadows_enabled=GLOBAL_DEF("render/shadows_enabled",true); - room_cull_enabled = GLOBAL_DEF("render/room_cull_enabled",true); - light_discard_enabled = GLOBAL_DEF("render/light_discard_enabled",true); - rasterizer->begin_frame(); - _draw_viewports(); - _draw_cursors_and_margins(); - rasterizer->end_frame(); - draw_extra_frame=rasterizer->needs_to_draw_next_frame(); -} - -bool VisualServerRaster::has_changed() const { - - return changes>0 || draw_extra_frame; -} - -int VisualServerRaster::get_render_info(RenderInfo p_info) { - - return rasterizer->get_render_info(p_info); -} - -bool VisualServerRaster::has_feature(Features p_feature) const { - - return rasterizer->has_feature(p_feature); // lies for now -} - -void VisualServerRaster::set_default_clear_color(const Color& p_color) { - - clear_color=p_color; -} - -void VisualServerRaster::set_boot_image(const Image& p_image, const Color& p_color,bool p_scale) { - - if (p_image.empty()) - return; - - rasterizer->restore_framebuffer(); - - rasterizer->begin_frame(); - - int window_w = OS::get_singleton()->get_video_mode(0).width; - int window_h = OS::get_singleton()->get_video_mode(0).height; - ViewportRect vr; - vr.x=0; - vr.y=0; - vr.width=OS::get_singleton()->get_video_mode(0).width; - vr.height=OS::get_singleton()->get_video_mode(0).height; - rasterizer->set_viewport(vr); - rasterizer->clear_viewport(p_color); - rasterizer->canvas_begin(); - RID texture = texture_create(); - texture_allocate(texture,p_image.get_width(),p_image.get_height(),p_image.get_format(),TEXTURE_FLAG_FILTER); - texture_set_data(texture,p_image); - rasterizer->canvas_begin_rect(Matrix32()); - Rect2 imgrect(0,0,p_image.get_width(),p_image.get_height()); - Rect2 screenrect; - if (p_scale) { - - if (window_w > window_h) { - //scale horizontally - screenrect.size.y = window_h; - screenrect.size.x = imgrect.size.x * window_h / imgrect.size.y; - screenrect.pos.x = (window_w - screenrect.size.x)/2; - - } else { - //scale vertically - screenrect.size.x = window_w; - screenrect.size.y = imgrect.size.y * window_w / imgrect.size.x; - screenrect.pos.y = (window_h - screenrect.size.y)/2; - } - } else { - - screenrect=imgrect; - screenrect.pos+=((Size2(vr.width,vr.height)-screenrect.size)/2.0).floor(); - - } - rasterizer->canvas_draw_rect(screenrect,0,imgrect,texture,Color(1,1,1,1)); - rasterizer->canvas_end_rect(); - - rasterizer->end_frame(); - rasterizer->flush_frame(); - - free(texture); // free since it's only one frame that stays there - -} - -void VisualServerRaster::init() { - - rasterizer->init(); - - shadows_enabled=GLOBAL_DEF("render/shadows_enabled",true); - //default_scenario = scenario_create(); - //default_viewport = viewport_create(); - for(int i=0;i<4;i++) - black_margin[i]=0; - - Image img; - img.create(default_mouse_cursor_xpm); - //img.convert(Image::FORMAT_RGB8); - default_cursor_texture = texture_create_from_image(img, 0); - - aabb_random_points.resize( GLOBAL_DEF("render/aabb_random_points",16) ); - for(int i=0;i<aabb_random_points.size();i++) - aabb_random_points[i]=Vector3(Math::random(0,1),Math::random(0,1),Math::random(0,1)); - transformed_aabb_random_points.resize(aabb_random_points.size()); - changes=0; -} - -void VisualServerRaster::_clean_up_owner(RID_OwnerBase *p_owner,String p_type) { - - List<RID> rids; - p_owner->get_owned_list(&rids); - - int lost=0; - for(List<RID>::Element *I=rids.front();I;I=I->next()) { - if (OS::get_singleton()->is_stdout_verbose()) { - lost++; - } - free(I->get()); - } - - if (lost) - print_line("VisualServerRaster: WARNING: Lost "+itos(lost)+" RIDs of type "+p_type); - -} - -void VisualServerRaster::finish() { - - - free(default_cursor_texture); - if (test_cube.is_valid()) - free(test_cube); - - _free_internal_rids(); - - _clean_up_owner( &room_owner,"Room" ); - _clean_up_owner( &portal_owner,"Portal" ); - - _clean_up_owner( &camera_owner,"Camera" ); - _clean_up_owner( &viewport_owner,"Viewport" ); - - _clean_up_owner( &scenario_owner,"Scenario" ); - _clean_up_owner( &instance_owner,"Instance" ); - - _clean_up_owner( &canvas_owner,"Canvas" ); - _clean_up_owner( &canvas_item_owner,"CanvasItem" ); - - rasterizer->finish(); - octree_allocator.clear(); - - if (instance_dependency_map.size()) { - print_line("Base resources missing amount: "+itos(instance_dependency_map.size())); - } - ERR_FAIL_COND( instance_dependency_map.size() ); -} - - -RID VisualServerRaster::get_test_cube() { - - if (test_cube.is_valid()) - return test_cube; - - test_cube=_make_test_cube(); - return test_cube; - -} - - - -VisualServerRaster::VisualServerRaster(Rasterizer *p_rasterizer) { - - rasterizer=p_rasterizer; - rasterizer->draw_viewport_func=_render_canvas_item_viewport; - instance_update_list=NULL; - render_pass=0; - clear_color=Color(0.3,0.3,0.3,1.0); - OctreeAllocator::allocator=&octree_allocator; - draw_extra_frame=false; - -} - - -VisualServerRaster::~VisualServerRaster() -{ -} -#endif diff --git a/servers/visual/visual_server_raster.h b/servers/visual/visual_server_raster.h index b5caa91575..774b692a22 100644 --- a/servers/visual/visual_server_raster.h +++ b/servers/visual/visual_server_raster.h @@ -73,6 +73,7 @@ class VisualServerRaster : public VisualServer { List<FrameDrawnCallbacks> frame_drawn_callbacks; +// FIXME: Kept as reference for future implementation #if 0 struct Room { diff --git a/servers/visual/visual_server_scene.cpp b/servers/visual/visual_server_scene.cpp index eea8c05589..95ac876ec0 100644 --- a/servers/visual/visual_server_scene.cpp +++ b/servers/visual/visual_server_scene.cpp @@ -761,13 +761,6 @@ void VisualServerScene::_update_instance(Instance *p_instance) { return; } -#if 0 - if (p_instance->base_type == VS::INSTANCE_PARTICLES) { - - rasterizer->particles_instance_set_transform( p_instance->particles_info->instance, p_instance->data.transform ); - } - -#endif if ((1 << p_instance->base_type) & VS::INSTANCE_GEOMETRY_MASK) { InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(p_instance->base_data); @@ -1606,20 +1599,7 @@ void VisualServerScene::_render_scene(const Transform p_cam_transform, const Cam else environment = scenario->fallback_environment; -#if 0 - /* STEP 6 - SAMPLE BAKED LIGHT */ - - bool islinear =false; - if (environment.is_valid()) { - islinear = rasterizer->environment_is_fx_enabled(environment,VS::ENV_FX_SRGB); - } - - for(int i=0;i<light_samplers_culled;i++) { - - _process_sampled_light(camera->transform,light_sampler_cull_result[i],islinear); - } -#endif - /* STEP 7 - PROCESS GEOMETRY AND DRAW SCENE*/ + /* STEP 6 - PROCESS GEOMETRY AND DRAW SCENE*/ VSG::scene_render->render_scene(p_cam_transform, p_cam_projection, p_cam_orthogonal, (RasterizerScene::InstanceBase **)instance_cull_result, cull_count, light_instance_cull_result, light_cull_count + directional_light_count, reflection_probe_instance_cull_result, reflection_probe_cull_count, environment, p_shadow_atlas, scenario->reflection_atlas, p_reflection_probe, p_reflection_probe_pass); } diff --git a/servers/visual/visual_server_scene.h b/servers/visual/visual_server_scene.h index f31b2f79e3..ac771030cf 100644 --- a/servers/visual/visual_server_scene.h +++ b/servers/visual/visual_server_scene.h @@ -54,6 +54,8 @@ public: uint64_t render_pass; static VisualServerScene *singleton; + +// FIXME: Kept as reference for future implementation #if 0 struct Portal { diff --git a/servers/visual/visual_server_viewport.cpp b/servers/visual/visual_server_viewport.cpp index c68d5b3d2b..0dca09a5bf 100644 --- a/servers/visual/visual_server_viewport.cpp +++ b/servers/visual/visual_server_viewport.cpp @@ -28,6 +28,7 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "visual_server_viewport.h" + #include "project_settings.h" #include "visual_server_canvas.h" #include "visual_server_global.h" @@ -51,36 +52,6 @@ void VisualServerViewport::_draw_viewport(Viewport *p_viewport, ARVRInterface::E } bool can_draw_3d = !p_viewport->disable_3d && !p_viewport->disable_3d_by_usage && VSG::scene->camera_owner.owns(p_viewport->camera); -#if 0 - - - - if (scenario_draw_canvas_bg) { - - rasterizer->begin_canvas_bg(); - } - - if (!scenario_draw_canvas_bg && can_draw_3d) { - - _draw_viewport_camera(p_viewport,false); - - } else if (true /*|| !p_viewport->canvas_list.empty()*/){ - - //clear the viewport black because of no camera? i seriously should.. - if (p_viewport->render_target_clear_on_new_frame || p_viewport->render_target_clear) { - if (p_viewport->transparent_bg) { - rasterizer->clear_viewport(Color(0,0,0,0)); - } - else { - Color cc=clear_color; - if (scenario_draw_canvas_bg) - cc.a=0; - rasterizer->clear_viewport(cc); - } - p_viewport->render_target_clear=false; - } - } -#endif if (p_viewport->clear_mode != VS::VIEWPORT_CLEAR_NEVER) { VSG::rasterizer->clear_render_target(clear_color); diff --git a/servers/visual_server.cpp b/servers/visual_server.cpp index e2266efd3c..777feffb06 100644 --- a/servers/visual_server.cpp +++ b/servers/visual_server.cpp @@ -28,6 +28,7 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "visual_server.h" + #include "method_bind_ext.gen.inc" #include "project_settings.h" @@ -39,25 +40,6 @@ VisualServer *VisualServer::get_singleton() { return singleton; } -PoolVector<String> VisualServer::_shader_get_param_list(RID p_shader) const { - - //remove at some point - - PoolVector<String> pl; - -#if 0 - List<StringName> params; - shader_get_param_list(p_shader,¶ms); - - - for(List<StringName>::Element *E=params.front();E;E=E->next()) { - - pl.push_back(E->get()); - } -#endif - return pl; -} - VisualServer *VisualServer::create() { ERR_FAIL_COND_V(singleton, NULL); @@ -1466,7 +1448,6 @@ void VisualServer::_camera_set_orthogonal(RID p_camera, float p_size, float p_z_ void VisualServer::mesh_add_surface_from_mesh_data(RID p_mesh, const Geometry::MeshData &p_mesh_data) { -#if 1 PoolVector<Vector3> vertices; PoolVector<Vector3> normals; @@ -1491,24 +1472,6 @@ void VisualServer::mesh_add_surface_from_mesh_data(RID p_mesh, const Geometry::M d[ARRAY_VERTEX] = vertices; d[ARRAY_NORMAL] = normals; mesh_add_surface_from_arrays(p_mesh, PRIMITIVE_TRIANGLES, d); - -#else - - PoolVector<Vector3> vertices; - - for (int i = 0; i < p_mesh_data.edges.size(); i++) { - - const Geometry::MeshData::Edge &f = p_mesh_data.edges[i]; - vertices.push_back(p_mesh_data.vertices[f.a]); - vertices.push_back(p_mesh_data.vertices[f.b]); - } - - Array d; - d.resize(VS::ARRAY_MAX); - d[ARRAY_VERTEX] = vertices; - mesh_add_surface(p_mesh, PRIMITIVE_LINES, d); - -#endif } void VisualServer::mesh_add_surface_from_planes(RID p_mesh, const PoolVector<Plane> &p_planes) { diff --git a/servers/visual_server.h b/servers/visual_server.h index 12f2532019..9f2d685531 100644 --- a/servers/visual_server.h +++ b/servers/visual_server.h @@ -49,7 +49,6 @@ class VisualServer : public Object { int mm_policy; - PoolVector<String> _shader_get_param_list(RID p_shader) const; void _camera_set_orthogonal(RID p_camera, float p_size, float p_z_near, float p_z_far); void _canvas_item_add_style_box(RID p_item, const Rect2 &p_rect, const Rect2 &p_source, RID p_texture, const Vector<float> &p_margins, const Color &p_modulate = Color(1, 1, 1)); Array _get_array_from_surface(uint32_t p_format, PoolVector<uint8_t> p_vertex_data, int p_vertex_len, PoolVector<uint8_t> p_index_data, int p_index_len) const; |