Beispiel #1
0
        private void CreateBolt(LineRenderer line)
        {
            //lineObject.material.SetTextureScale("_MainTex", new Vector2(distance * zigZagPerMeter, 1.0f));
            //lineObject.numPositions = vertexCount;

            float totalDistance = _pathComp.TotalDistance;
            int   numPositions  = Mathf.CeilToInt(totalDistance * zigZagPerMeter);

            Vector3[] points = new Vector3[numPositions];

            line.positionCount = numPositions;
            line.material.SetTextureScale("_MainTex", new Vector2(totalDistance * zigZagPerMeter, 1.0f));

            // set the ends
            points[0] = _pathComp.GetPathPoint(0.0f).point;
            points[numPositions - 1] = _pathComp.GetPathPoint(totalDistance).point;


            Vector2 previousOffset = Vector2.zero;

            for (int i = 1; i < numPositions - 1; i++)
            {
                Path_Point pathPoint = _pathComp.GetPathPoint(Math_Functions.Value_from_another_Scope(i, 0, numPositions - 1, 0, totalDistance));


                Vector2 offset = new Vector2(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f));

                offset        *= zigZagIntensity;
                previousOffset = offset;

                points[i] = pathPoint.point + (pathPoint.right * offset.x) + (pathPoint.up * offset.y);
            }

            line.SetPositions(points);
        }
        void LateUpdate()
        {
            if (_particle_array == null)
            {
                Start();
            }

            if (isRectangleSizeUpdating)
            {
                Calculate_The_Four_Corners();
            }

            _numParticles = _particle_system.GetParticles(_particle_array);

            if (_numParticles > 0)
            {
                for (int i = 0; i < _numParticles; i++)
                {
                    ParticleSystem.Particle obj = _particle_array[i];

                    // This made it based on the particle lifetime
//					float normalizedLifetime = (1.0f - obj.remainingLifetime / obj.startLifetime);
//
//					if(hasRandomStartingPoints){
//						normalizedLifetime += Get_Value_From_Random_Seed_0t1(obj.randomSeed, 100.0f);
//						normalizedLifetime = normalizedLifetime % 1.0f;
//					}
//
//					Path_Point axis = _path.GetPathPoint(_path.TotalDistance * normalizedLifetime, isSmooth);


                    // This made it based on the paritcle speed
                    float dist = (obj.startLifetime - obj.remainingLifetime) * obj.velocity.magnitude;
                    if (hasRandomStartingPoints)
                    {
                        dist += Get_Value_From_Random_Seed_0t1(obj.randomSeed, 100.0f) * _path.TotalDistance;
                    }
                    dist = dist % _path.TotalDistance;

                    Path_Point axis = _path.GetPathPoint(dist, isSmooth);


                    Vector2 offset = Vector2.zero;

                    if (pathWidth > 0)
                    {
                        offset  = Math_Functions.AngleToVector2D(obj.randomSeed % 360.0f);
                        offset *= Get_Value_From_Random_Seed_0t1(obj.randomSeed, 150.0f) * pathWidth;
                    }

                    _particle_array[i].position = axis.point +
                                                  (isFlat ? Vector3.zero :  axis.right * offset.x) +
                                                  (axis.up * offset.y);

                    _particle_array[i].velocity = axis.forward * _particle_array[i].velocity.magnitude;
                }

                _particle_system.SetParticles(_particle_array, _numParticles);
            }
        }
Beispiel #3
0
        private void AddTriangle(int[] indices, int submesh)
        {
            // vertices
            Vector3[] verts = new Vector3[3] {
                segmentSourceMesh.vertices[indices[0]],
                segmentSourceMesh.vertices[indices[1]],
                segmentSourceMesh.vertices[indices[2]]
            };
            // normals
            Vector3[] norms = new Vector3[3] {
                segmentSourceMesh.normals[indices[0]],
                segmentSourceMesh.normals[indices[1]],
                segmentSourceMesh.normals[indices[2]]
            };
            // uvs
            Vector2[] uvs = new Vector2[3] {
                segmentSourceMesh.uv[indices[0]],
                segmentSourceMesh.uv[indices[1]],
                segmentSourceMesh.uv[indices[2]]
            };
            // tangent
            Vector4[] tangents = new Vector4[3] {
                segmentSourceMesh.tangents[indices[0]],
                segmentSourceMesh.tangents[indices[1]],
                segmentSourceMesh.tangents[indices[2]]
            };

            // apply offset
            float   lerpValue = 0.0f;
            Vector3 pointA, pointB;
            Vector3 normA, normB;
            Vector4 tangentA, tangentB;
            //Matrix4x4 4*4 矩阵;Transform.localToWorldMatrix 局部转世界矩阵
            Matrix4x4 localToWorld_A = _helpTransform1.localToWorldMatrix;
            Matrix4x4 localToWorld_B = _helpTransform2.localToWorldMatrix;
            Matrix4x4 worldToLocal   = transform.worldToLocalMatrix;

            for (int i = 0; i < 3; i++)
            {
                lerpValue  = Math_Functions.Value_from_another_Scope(verts[i].z, _segment_MinZ, _segment_MaxZ, 0.0f, 1.0f);
                verts[i].z = 0.0f;

                pointA = localToWorld_A.MultiplyPoint(verts[i]); // to world
                pointB = localToWorld_B.MultiplyPoint(verts[i]);

                verts[i] = worldToLocal.MultiplyPoint(Vector3.Lerp(pointA, pointB, lerpValue)); // to local

                normA = localToWorld_A.MultiplyVector(norms[i]);
                normB = localToWorld_B.MultiplyVector(norms[i]);

                norms[i] = worldToLocal.MultiplyVector(Vector3.Lerp(normA, normB, lerpValue));

                tangentA = localToWorld_A.MultiplyVector(tangents[i]);
                tangentB = localToWorld_B.MultiplyVector(tangents[i]);

                tangents[i] = worldToLocal.MultiplyVector(Vector3.Lerp(tangentA, tangentB, lerpValue));
            }

            _maker.AddTriangle(verts, norms, uvs, tangents, submesh);
        }
Beispiel #4
0
        public Vector3 GetRoutePosition(float dist)
        {
            dist = Mathf.Clamp(dist, 0.0f, TotalDistance);

            if (dist <= 0)
            {
                return(points_positions[0]);
            }
            else if (dist >= TotalDistance)
            {
                return(points_positions[points_num - 1]);
            }

            int point = 0;

            while (points_distances[point] < dist)
            {
                point++;
            }

            // get nearest two points, ensuring points wrap-around start & end of circuit
            p1n = point - 1;
            p2n = point;

            // found point numbers, now find interpolation value between the two middle points

            i = Mathf.InverseLerp(points_distances[p1n], points_distances[p2n], dist);

            if (smoothRoute)
            {
                p0n = Mathf.Clamp(point - 2, 0, points_num - 1);
                p3n = Mathf.Clamp(point + 1, 0, points_num - 1);


                P0 = points_positions[p0n];
                P1 = points_positions[p1n];
                P2 = points_positions[p2n];
                P3 = points_positions[p3n];

                return(Math_Functions.CatmullRom(P0, P1, P2, P3, i));
            }
            else
            {
                return(Vector3.Lerp(points_positions[p1n], points_positions[p2n], i));
            }
        }
        public void AddTriangle(int[] indices, int submesh)
        {
            // vertices
            Vector3[] verts = new Vector3[3] {
                segment_sourceMesh.vertices[indices[0]],
                segment_sourceMesh.vertices[indices[1]],
                segment_sourceMesh.vertices[indices[2]]
            };
            // normals
            Vector3[] norms = new Vector3[3] {
                segment_sourceMesh.normals[indices[0]],
                segment_sourceMesh.normals[indices[1]],
                segment_sourceMesh.normals[indices[2]]
            };

            // uvs
            Vector2[] uvs = new Vector2[3] {
                segment_sourceMesh.uv[indices[0]],
                segment_sourceMesh.uv[indices[1]],
                segment_sourceMesh.uv[indices[2]]
            };

            // apply offset
            float   lerpValue = 0.0f;
            Vector3 pointA, pointB;
            Vector3 normA, normB;

            for (int i = 0; i < 3; i++)
            {
                lerpValue  = Math_Functions.Value_from_another_Scope(verts[i].z, _segment_MinZ, _segment_MaxZ, 0.0f, 1.0f);
                verts[i].z = 0.0f;

                pointA = _helpTransform1.TransformPoint(verts[i]);                 // to world
                pointB = _helpTransform2.TransformPoint(verts[i]);

                verts[i] = transform.InverseTransformPoint(Vector3.Lerp(pointA, pointB, lerpValue));                // to local

                normA = _helpTransform1.TransformDirection(norms[i]);
                normB = _helpTransform2.TransformDirection(norms[i]);

                norms[i] = transform.InverseTransformDirection(Vector3.Lerp(normA, normB, lerpValue));
            }

            _maker.AddTriangle(verts, norms, uvs, submesh);
        }
Beispiel #6
0
        void LateUpdate()
        {
            if (_particle_array == null)
            {
                Start();
                _path_comp.Update_Path();
            }
            else if (isPathUpdating)
            {
                _path_comp.Update_Path();
            }



            _numParticles = _particle_system.GetParticles(_particle_array);

            if (_numParticles > 0)
            {
                for (int i = 0; i < _numParticles; i++)
                {
                    ParticleSystem.Particle obj = _particle_array[i];
                    Path_Point axis             = _path_comp.GetPathPoint(_path_comp.TotalDistance * (1.0f - obj.remainingLifetime / obj.startLifetime));
                    Vector2    offset           = Math_Functions.AngleToVector2D(obj.randomSeed % 360.0f);

                    offset *= (((float)obj.randomSeed % 100.0f) / 100.0f) * pathWidth;

                    _particle_array[i].position = axis.point +
                                                  (axis.right * offset.x) +
                                                  (axis.up * offset.y);

                    _particle_array[i].velocity = axis.forward * _particle_array[i].velocity.magnitude;
                }

                _particle_system.SetParticles(_particle_array, _numParticles);
            }
        }
Beispiel #7
0
        public Path_Point GetPathPoint(float dist, bool isSmooth)
        {
            if (_isCircuit)
            {
                dist = (dist + TotalDistance) % TotalDistance;
            }
            else
            {
                dist = Mathf.Clamp(dist, 0.0f, TotalDistance);
            }

            // find segment index
            int index = 1;

            while (_distances[index] < dist)
            {
                index++;
            }

            // the segment in the middle
            _interpolation = Mathf.InverseLerp(
                _distances[index - 1],
                _distances[index],
                dist);

            index = index % _numPoints;

            if (_isCircuit)
            {
                _four_indices[0] = ((index - 2) + _numPoints) % _numPoints;
                _four_indices[1] = ((index - 1) + _numPoints) % _numPoints;
                _four_indices[2] = index % _numPoints;
                _four_indices[3] = (index + 1) % _numPoints;
            }
            else
            {
                _four_indices[0] = Mathf.Clamp(index - 2, 0, _numPoints - 1);
                _four_indices[1] = ((index - 1) + _numPoints) % _numPoints;
                _four_indices[2] = index % _numPoints;
                _four_indices[3] = Mathf.Clamp(index + 1, 0, _numPoints - 1);
            }

            if (isSmooth)
            {
                // assign the four points with the segment in the middle
                _four_points[0] = _points[_four_indices[0]];
                _four_points[1] = _points[_four_indices[1]];
                _four_points[2] = _points[_four_indices[2]];
                _four_points[3] = _points[_four_indices[3]];

                // you need two points to get a forward direction
                _pathPoint.point = Math_Functions.CatmullRom(
                    _four_points[0],
                    _four_points[1],
                    _four_points[2],
                    _four_points[3],
                    _interpolation);
                _pathPoint.forward = Math_Functions.CatmullRom(
                    _four_points[0],
                    _four_points[1],
                    _four_points[2],
                    _four_points[3],
                    _interpolation + 0.01f) - _pathPoint.point;

                _pathPoint.forward.Normalize();
            }
            else             // strait shooting
            {
                _pathPoint.point = Vector3.Lerp(
                    _points[_four_indices[1]],
                    _points[_four_indices[2]],
                    _interpolation);

                _pathPoint.forward = _points[_four_indices[2]] - _points[_four_indices[1]];
                _pathPoint.forward.Normalize();
            }

            // 90 degree turn to right
            _pathPoint.right = Vector3.Cross(
                Vector3.Lerp(
                    _upDirections[_four_indices[1]],
                    _upDirections[_four_indices[2]],
                    _interpolation),                     // lerp
                _pathPoint.forward).normalized;          // cross

            // 90 degree turn to up
            _pathPoint.up = Vector3.Cross(_pathPoint.forward, _pathPoint.right).normalized;

            // now all directions are 90 degrees from each other

            return(_pathPoint);
        }
        void LateUpdate()
        {
        #if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                _editorTimeDelta   = EditorApplication.timeSinceStartup - _editorTimetracker;
                _editorTimetracker = EditorApplication.timeSinceStartup;
            }
        #endif

            if (transform.childCount <= 1)
            {
                return;
            }

            // emision
            if (_emissionRateTracker <= 0.0f)
            {
                _emissionRateTracker += 1.0f / emissionRate;

                RenewOneDeadParticle();
            }
            _emissionRateTracker -= (Application.isPlaying ? Time.deltaTime : (float)_editorTimeDelta);

            // age them
            foreach (PathParticleTracker tracker in _particle_trackerArray)
            {
                if (tracker.particle.remainingLifetime > 0.0f)
                {
                    tracker.particle.remainingLifetime = Mathf.Max(tracker.particle.remainingLifetime - (Application.isPlaying ? Time.deltaTime : (float)_editorTimeDelta), 0.0f);
                }
            }


            float      normLifetime = 0.0f;
            Path_Point Rpoint;

            // move them
            foreach (PathParticleTracker tracker in _particle_trackerArray)
            {
                if (tracker.particle.remainingLifetime > 0.0f)
                {
                    normLifetime = tracker.particle.remainingLifetime / tracker.particle.startLifetime;
                    normLifetime = 1.0f - normLifetime;

                    Rpoint = _path_comp.GetPathPoint(normLifetime * _path_comp.TotalDistance);

                    // rotate around Rpoint.direction

                    Rpoint.point += (pathWidth * tracker.distance) * Math_Functions.Rotate_Vector(Rpoint.up, Rpoint.forward, tracker.rotation);

                    tracker.particle.position = Rpoint.point;
                    tracker.particle.velocity = Rpoint.forward;
                }
            }

            _particle_count = 0;

            // set the given array
            foreach (PathParticleTracker tracker in _particle_trackerArray)
            {
                if (tracker.particle.remainingLifetime > 0.0f)         // it's alive
                {
                    _particle_array[_particle_count] = tracker.particle;
                    _particle_count++;
                }
            }

            _particle_system.SetParticles(_particle_array, _particle_count);
        }
Beispiel #9
0
        //Gizmos是用于在场景视图可视化调试或辅助设置。==================game视图中不可用,需要对点做模型,并且提供平移
        private void DrawGizmos(bool selected)
        {
            Update_Path();

            if (transform.childCount <= 1)
            {
                return;
            }

            Path_Point prev = GetPathPoint(0.0f);
            //gizmoLineSize=1.0f; [Range(0.01f, 1.0f)],用来变形的常量
            float dist = -gizmoLineSize;

            //print (dist);
            //print (gizmoLineSize);
            //int i = 0;
            do
            {
                //i += 1;

                //gizmoLineSize / 10 : 控制曲线上点与点的距离
                dist = Mathf.Clamp(dist + gizmoLineSize / 10, 0, _path.TotalDistance);

                //=======!!!此处不断依据CatmullRom算法,找到一个个点
                Path_Point next = GetPathPoint(dist);
                //print("前点:"+prev.point.x+","+prev.point.y+","+prev.point.z);
                //print("前点方向:"+prev.forward);

                //print("后点:"+next.point.x +","+next.point.y+","+next.point.z);
                float distance = Vector3.Distance(prev.point, next.point);
                distance = distance / 2;
                //print("点间距/2:"+distance);
                Vector3 V = next.point - prev.point;
                V = V.normalized;
                //print("两点之间的向量:"+V);//后点-前点
                float angle = Vector3.Angle(prev.forward, V);
                //print("两向量之间的夹角:"+angle);
                //print(Mathf.Sin(angle));
                //print(Mathf.Sin(90));

                //r=(length/2)/sina
                float radius = distance / Mathf.Sin(Math_Functions.DegreesToRadians(angle));
                //print("第" + i + "个点" + "前点:" + prev.point.x + "," + prev.point.y + "," + prev.point.z + "后点:" + next.point.x + "," + next.point.y + "," + next.point.z + "两向量之间的夹角:" + angle + "前点处的转弯半径:" + radius);

                if (radius < 2.7 && radius != 0)
                {
                    Gizmos.color = selected ? Color.red : new Color(1, 0, 0, 0.5f);
                    Gizmos.DrawLine(transform.TransformPoint(prev.point), transform.TransformPoint(next.point));
                    Gizmos.color = selected ? Color.green : new Color(0, 1, 0, 0.5f);
                    //Gizmos.DrawLine(transform.TransformPoint(next.point), transform.TransformPoint(next.point) + transform.TransformDirection(next.up * gizmoLineSize));
                    Gizmos.color = selected ? new Color(0, 1, 1, 1) : new Color(0, 1, 1, 0.5f);
                    //Gizmos.DrawLine(transform.TransformPoint(next.point), transform.TransformPoint(next.point) + transform.TransformDirection(next.right * gizmoLineSize));
                }
                else
                {
                    Gizmos.color = selected ? new Color(0, 1, 1, 1) : new Color(0, 1, 1, 0.5f);
                    Gizmos.DrawLine(transform.TransformPoint(prev.point), transform.TransformPoint(next.point));
                    Gizmos.color = selected ? Color.green : new Color(0, 1, 0, 0.5f);
                    //Gizmos.DrawLine(transform.TransformPoint(next.point), transform.TransformPoint(next.point) + transform.TransformDirection(next.up * gizmoLineSize));
                    Gizmos.color = selected ? Color.red : new Color(1, 0, 0, 0.5f);
                    // Gizmos.DrawLine(transform.TransformPoint(next.point), transform.TransformPoint(next.point) + transform.TransformDirection(next.right * gizmoLineSize));
                }
                prev = next;
            } while (dist < _path.TotalDistance);
        }
Beispiel #10
0
        /*
         *@dist: distance 点距离起始点的距离(两点之间直线段距离累加)
         * return:返回一个Path_Point 依据传入的distance
         */
        public Path_Point GetPathPoint(float dist, bool isSmooth)
        {
            if (_isCircuit)
            {
                dist = (dist + TotalDistance) % TotalDistance;
            }
            else
            {
                dist = Mathf.Clamp(dist, 0.0f, TotalDistance);//限制value的值在min和max之间, 如果value小于min,返回min。 如果value大于max,返回max,否则返回value
            }
            // find segment index
            int index = 1;

            while (_distances[index] < dist)
            {
                index++;
            }

            // the segment in the middle
            // static float InverseLerp(float from, float to, float value);  eg: (5,10,8)==3/5
            _interpolation = Mathf.InverseLerp(
                _distances[index - 1],
                _distances[index],
                dist);//计算两个值之间的Lerp参数。也就是value在from和to之间的比例值。

            //防止是圆的情况
            index = index % _numPoints;

            // 获取插值两边各两个点的index,总共4个点;
            if (_isCircuit)
            {
                _four_indices[0] = ((index - 2) + _numPoints) % _numPoints;
                _four_indices[1] = ((index - 1) + _numPoints) % _numPoints;
                _four_indices[2] = index % _numPoints;
                _four_indices[3] = (index + 1) % _numPoints;
            }
            else
            {
                _four_indices[0] = Mathf.Clamp(index - 2, 0, _numPoints - 1);
                _four_indices[1] = ((index - 1) + _numPoints) % _numPoints;
                _four_indices[2] = index % _numPoints;
                _four_indices[3] = Mathf.Clamp(index + 1, 0, _numPoints - 1);
            }

            if (isSmooth)
            {
                // assign the four points with the segment in the middle在中间分配四个点与段
                _four_points[0] = _points[_four_indices[0]];
                _four_points[1] = _points[_four_indices[1]];
                _four_points[2] = _points[_four_indices[2]];
                _four_points[3] = _points[_four_indices[3]];

                // you need two points to get a forward direction你需要两点来获得前进的方向
                _pathPoint.point = Math_Functions.CatmullRom(
                    _four_points[0],
                    _four_points[1],
                    _four_points[2],
                    _four_points[3],
                    _interpolation);
                // 获取方向增加点 interpolation值:0.001f
                _pathPoint.forward = Math_Functions.CatmullRom(
                    _four_points[0],
                    _four_points[1],
                    _four_points[2],
                    _four_points[3],
                    _interpolation + interpolationPara) - _pathPoint.point;

                _pathPoint.forward.Normalize();
            }
            else // strait shooting
            {
                _pathPoint.point = Vector3.Lerp(
                    _points[_four_indices[1]],
                    _points[_four_indices[2]],
                    _interpolation);

                _pathPoint.forward = _points[_four_indices[2]] - _points[_four_indices[1]];
                _pathPoint.forward.Normalize();
            }

            // 90 degree turn to right90度右转
            // Vector3.Lerp p=form+(to-form)*t
            //_interpolation = Mathf.InverseLerp
            // 计算点在up方向上的Vector3
            Vector3 tempUpDirection = Vector3.Lerp(_upDirections[_four_indices[1]], _upDirections[_four_indices[2]], _interpolation);

            _pathPoint.right = Vector3.Cross(tempUpDirection, _pathPoint.forward).normalized; // cross

            // 90 degree turn to up90度上转
            _pathPoint.up = Vector3.Cross(_pathPoint.forward, _pathPoint.right).normalized;

            //tempUpDirection.normalized 与 _pathPoint.up不相同

            // now all directions are 90 degrees from each other

            return(_pathPoint);
        }
        void LateUpdate()
        {
        #if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                editorTimeDelta   = EditorApplication.timeSinceStartup - editorTimetracker;
                editorTimetracker = EditorApplication.timeSinceStartup;
            }
        #endif

            if (transform.childCount <= 1)
            {
                return;
            }

            // emision
            if (emissionRateTracker <= 0.0f)
            {
                emissionRateTracker += 1.0f / emissionRate;

                RenewOneDeadParticle();
            }
            emissionRateTracker -= (Application.isPlaying ? Time.deltaTime : (float)editorTimeDelta);

            // age them
            foreach (PathParticleTracker tracker in particle_trackerArray)
            {
                if (tracker.particle.lifetime > 0.0f)
                {
                    tracker.particle.lifetime = Mathf.Max(tracker.particle.lifetime - (Application.isPlaying ? Time.deltaTime : (float)editorTimeDelta), 0.0f);
                }
            }


            float      normLifetime = 0.0f;
            RoutePoint Rpoint;

            // move them
            foreach (PathParticleTracker tracker in particle_trackerArray)
            {
                if (tracker.particle.lifetime > 0.0f)
                {
                    normLifetime = tracker.particle.lifetime / tracker.particle.startLifetime;
                    normLifetime = 1.0f - normLifetime;

                    Rpoint = path.GetRoutePoint(normLifetime * path.TotalDistance);

                    // 90 degree turn
                    perpendicularDir.x = Rpoint.direction.y;
                    perpendicularDir.y = -Rpoint.direction.x;
                    perpendicularDir.z = Rpoint.direction.z;

                    // rotate around Rpoint.direction
                    perpendicularDir = Math_Functions.Rotate_Direction(perpendicularDir, Rpoint.direction, tracker.rotation);

                    // targetPos
                    Rpoint.position += (pathWidth * tracker.distance) * perpendicularDir;

                    tracker.particle.position = Rpoint.position;
                    tracker.particle.velocity = Rpoint.direction;
                }
            }

            particle_count = 0;

            // set the given array
            foreach (PathParticleTracker tracker in particle_trackerArray)
            {
                if (tracker.particle.lifetime > 0.0f)         // it's alive
                {
                    particle_array[particle_count] = tracker.particle;
                    particle_count++;
                }
            }

            particle_system.SetParticles(particle_array, particle_count);
        }
Beispiel #12
0
        private void IsNotCircuit(float dist)
        {
            dist = Mathf.Clamp(dist, 0.0f, TotalDistance);

            // find segment index
            int index = 1;

            while (_distances[index] < dist)
            {
                index++;
            }

            // the segment in the middle
            _four_indices[1] = index - 1;
            _four_indices[2] = index;

            _interpolation = Mathf.InverseLerp(
                _distances[_four_indices[1]],
                _distances[_four_indices[2]],
                dist);


            if (_isSmooth)
            {
                _four_indices[0] = Mathf.Clamp(index - 2, 0, _points.Length - 1);
                _four_indices[3] = Mathf.Clamp(index + 1, 0, _points.Length - 1);

                // assign the four points with the segment in the middle
                _four_points[0] = _points[_four_indices[0]];
                _four_points[1] = _points[_four_indices[1]];
                _four_points[2] = _points[_four_indices[2]];
                _four_points[3] = _points[_four_indices[3]];

                // you need two points to get a forward direction
                pathPoint.point = Math_Functions.CatmullRom(
                    _four_points[0],
                    _four_points[1],
                    _four_points[2],
                    _four_points[3],
                    _interpolation);
                pathPoint.forward = Math_Functions.CatmullRom(
                    _four_points[0],
                    _four_points[1],
                    _four_points[2],
                    _four_points[3],
                    _interpolation + 0.01f) - pathPoint.point;

                pathPoint.forward.Normalize();
            }
            else             // strait shooting
            {
                pathPoint.point = Vector3.Lerp(
                    _points[_four_indices[1]],
                    _points[_four_indices[2]],
                    _interpolation);

                pathPoint.forward = _points[_four_indices[2]] - _points[_four_indices[1]];
                pathPoint.forward.Normalize();
            }

            // 90 degree turn to right
            pathPoint.right = Vector3.Cross(
                Vector3.Lerp(
                    transform.InverseTransformDirection(_children_transforms[_four_indices[1]].up),
                    transform.InverseTransformDirection(_children_transforms[_four_indices[2]].up),
                    _interpolation),                     // lerp
                pathPoint.forward).normalized;           // cross

            // 90 degree turn to up
            pathPoint.up = Vector3.Cross(pathPoint.forward, pathPoint.right).normalized;


            // now all points are 90 degrees from each other
        }
Beispiel #13
0
        public void AddTriangle(int[] indices, int submesh)
        {
            // vertices
            Vector3[] verts = new Vector3[3] {
                segment_sourceMesh.vertices[indices[0]],
                segment_sourceMesh.vertices[indices[1]],
                segment_sourceMesh.vertices[indices[2]]
            };
            // normals
            Vector3[] norms = new Vector3[3] {
                segment_sourceMesh.normals[indices[0]],
                segment_sourceMesh.normals[indices[1]],
                segment_sourceMesh.normals[indices[2]]
            };

            // apply offset
            float   lerpValue = 0.0f;
            Vector3 pointA, pointB;
            Vector3 normA, normB;

            for (int i = 0; i < 3; i++)
            {
                lerpValue  = Math_Functions.Value_from_another_Scope(verts[i].z, _segment_MinZ, _segment_MaxZ, 0.0f, 1.0f);
                verts[i].z = 0.0f;

                pointA = _helpTransform1.TransformPoint(verts[i]);                 // to world
                pointB = _helpTransform2.TransformPoint(verts[i]);

                verts[i] = transform.InverseTransformPoint(Vector3.Lerp(pointA, pointB, lerpValue));                // to local

                normA = _helpTransform1.TransformDirection(norms[i]);
                normB = _helpTransform2.TransformDirection(norms[i]);

                norms[i] = transform.InverseTransformDirection(Vector3.Lerp(normA, normB, lerpValue));


                // is this new?
                int index = -1;

                if (shouldOptimizeMesh)
                {
                    for (int iterator = 0; iterator < _vertices.Count; iterator++)
                    {
                        if (_vertices[iterator] == verts[i] &&
                            _normals[iterator] == norms[i] &&
                            _uvs[iterator] == segment_sourceMesh.uv[indices[i]])
                        {
                            index = iterator;
                            break;
                        }
                    }
                }

                // it is
                if (index == -1)
                {
                    _subIndices[submesh].Add(_vertices.Count);
                    _triangles.Add(_vertices.Count);
                    _vertices.Add(verts[i]);
                    _normals.Add(norms[i]);
                    _uvs.Add(segment_sourceMesh.uv[indices[i]]);
                }
                else                   // it is not
                {
                    numDoubles++;
                    _subIndices[submesh].Add(index);
                    _triangles.Add(index);
                }
            }
        }