示例#1
0
        public unsafe void LateUpdate()
        {
            var vertices = this.instance.vertices.cpuReference;

            fixed(Vector3 *pVertices = vertices)
            {
                UnsafeUtility.MemCpy(this.splineVertices.GetUnsafePtr(), (void *)pVertices, UnsafeUtility.SizeOf <Vector3>() * vertices.Length);
            }

            handle = new PositionUpdateJob()
            {
                normals      = this.normals,
                segments     = this.segments,
                hairVertices = this.splineVertices,
                vertices     = this.triVertices,
                facing       = this.facing.position,
                hairWidth    = this.hairWidth,
                worldToLocal = this.transform.worldToLocalMatrix
            }.Schedule(this.segments.Length, 16);

            JobHandle.ScheduleBatchedJobs();
            handle.Complete();
            NoAllocHelpers.SetMesh(this.mesh, this.triVertices, null, this.normals);
            this.mesh.UploadMeshData(false);
        }
示例#2
0
        public void Start()
        {
            List <FollowHairStrand> followHairs = new List <FollowHairStrand>();



            List <TriangulatedSegment> segments = new List <TriangulatedSegment>();

            // Triangulate
            var strands = this.instance.strands.cpuReference;

            {
                int vCtr = 0;
                for (int sIndex = 0; sIndex < strands.Length; sIndex++)
                {
                    HairStrand strand = strands[sIndex];
                    for (int vIndex = strand.firstVertex; vIndex < strand.lastVertex - 1; vIndex++)
                    {
                        // Build segment
                        TriangulatedSegment segment = new TriangulatedSegment()
                        {
                            hairVertexIndex1 = vIndex,
                            hairVertexIndex2 = vIndex + 1,
                            v1                 = vCtr++,
                            v2                 = vCtr++,
                            v3                 = vCtr++,
                            v4                 = vCtr++,
                            strandIndex        = sIndex,
                            strandVertexIndex1 = vIndex - strand.firstVertex,
                            strandVertexIndex2 = (vIndex + 1) - strand.firstVertex
                        };

                        segments.Add(segment);
                    }
                }
            }
            this.segments = new NativeArray <TriangulatedSegment>(segments.Count, Allocator.Persistent);
            this.segments.CopyFrom(segments.ToArray());

            // Distribute uv and indices
            this.uvs         = new NativeArray <float3>(segments.Count * 4, Allocator.Persistent);
            this.normals     = new NativeArray <float3>(segments.Count * 4, Allocator.Persistent);
            this.triVertices = new NativeArray <float3>(segments.Count * 4, Allocator.Persistent);
            this.indices     = new NativeArray <int>(segments.Count * 6, Allocator.Persistent);

            for (int i = 0; i < segments.Count; i++)
            {
                var segment = segments[i];

                // Calc uv
                float3 uv1, uv2, uv3, uv4;
                uv1 = uv2 = uv3 = uv4 = float3.zero;
                switch (this.uvDistStrat)
                {
                case UVDistributionStrategy.ALONG_STRAND_Y:
                {
                    HairStrand hs = strands[segment.strandIndex];
                    float      y  = segment.strandVertexIndex1 / (hs.lastVertex - hs.firstVertex);
                    float      y2 = segment.strandVertexIndex2 / (hs.lastVertex - hs.firstVertex);

                    uv1 = new float3(0, y, 0);
                    uv2 = new float3(1, y, 0);
                    uv3 = new float3(0, y2, 0);
                    uv4 = new float3(1, y2, 0);
                }
                break;
                }

                this.uvs[segment.v1] = uv1;
                this.uvs[segment.v2] = uv2;
                this.uvs[segment.v3] = uv3;
                this.uvs[segment.v4] = uv4;

                // Write indices
                this.indices[(i * 6)]     = segment.v1;
                this.indices[(i * 6) + 1] = segment.v2;
                this.indices[(i * 6) + 2] = segment.v4;
                this.indices[(i * 6) + 3] = segment.v4;
                this.indices[(i * 6) + 4] = segment.v3;
                this.indices[(i * 6) + 5] = segment.v1;
            }

            this.uvs.CopyFrom(uvs.ToArray());
            this.indices.CopyFrom(indices.ToArray());

            this.splineVertices = new NativeArray <float3>(this.instance.asset.vertexCount, Allocator.Persistent);
            NoAllocHelpers.SetMesh(this.mesh, this.triVertices, this.uvs, this.normals, this.indices);
        }