示例#1
0
    /// <summary>
    /// Start this instance.
    /// Initializes all buffers and other resources needed by tressfx simulation and rendering.
    /// </summary>
    public void Start()
    {
        // Vertex buffers
        this.g_HairVertexPositions = this.InitializeBuffer (this.hairData.m_pVertices, 16);
        this.g_HairVertexPositionsPrev = this.InitializeBuffer (this.hairData.m_pVertices, 16);
        this.g_InitialHairPositions = this.InitializeBuffer (this.hairData.m_pVertices, 16);

        // Tangents and rotations
        this.g_HairVertexTangents = this.InitializeBuffer (this.hairData.m_pTangents, 16);
        this.g_GlobalRotations = this.InitializeBuffer (this.hairData.m_pGlobalRotations, 16);
        this.g_LocalRotations = this.InitializeBuffer (this.hairData.m_pLocalRotations, 16);

        // Others
        this.g_HairRestLengthSRV = this.InitializeBuffer (this.hairData.m_pRestLengths, 4);
        this.g_HairStrandType = this.InitializeBuffer (this.hairData.m_pHairStrandType, 4);
        this.g_HairRefVecsInLocalFrame = this.InitializeBuffer (this.hairData.m_pRefVectors, 16);
        this.g_FollowHairRootOffset = this.InitializeBuffer (this.hairData.m_pFollowRootOffset, 16);
        this.g_HairThicknessCoeffs = this.InitializeBuffer (this.hairData.m_pThicknessCoeffs, 4);

        // Get other parts
        this.simulation = this.GetComponent<TressFXSimulation> ();
    }
示例#2
0
    /// <summary>
    /// This initializes tressfx and all of it's components.
    /// This function gets called from the TressFX Loader.
    /// </summary>
    /// <param name="vertices">Vertices.</param>
    /// <param name="strandIndices">Strand indices.</param>
    public void Initialize(TressFXStrand[] strands, int numVertices, int hairCount)
    {
        // Initialize data
        this.vertexCount = numVertices;
        this.strandCount = strands.Length;
        this.strands     = strands;
        this.hairCount   = hairCount;
        this.globalToLocalVertexIndexMappings = new Dictionary <int, int> ();
        this.hairData = this.GetComponent <TressFXLoader> ().hairs;

        // Buffer resources
        positionVectors = new Vector4[numVertices];
        Vector4[] initialPositionVectors = new Vector4[numVertices];
        referenceVectors = new Vector3[numVertices];
        vectorTangents   = new Vector3[numVertices];
        hairRestLengths  = new float[numVertices];
        strandIndices    = new int[numVertices];
        offsets          = new int[strands.Length];
        hairIndices      = new int[strands.Length];

        // Rotations
        localRotations  = new Quaternion[numVertices];
        globalRotations = new Quaternion[numVertices];

        // Transforms
        localTransforms  = new TressFXTransform[numVertices];
        globalTransforms = new TressFXTransform[numVertices];

        List <int> triangleIndicesList = new List <int> ();

        thicknessCoeffs = new float[numVertices];

        // Initialize transforms and fill hair and strand indices, hair rest lengths and position vectors
        int index = 0;

        this.ComputeDistanceToRoot();

        for (int i = 0; i < strands.Length; i++)
        {
            hairIndices[i] = strands[i].hairId;

            for (int j = 0; j < strands[i].vertices.Length; j++)
            {
                this.globalToLocalVertexIndexMappings.Add(index, j);

                Vector4 pos  = strands[i].GetTressFXVector(j);
                Vector3 pos3 = new Vector3(pos.x, pos.y, pos.z);
                pos3 = this.transform.position + pos3;
                Vector4 position = new Vector4(pos3.x, pos3.y, pos3.z, pos.w);

                // Load position of the strand
                positionVectors[index]        = position;
                initialPositionVectors[index] = strands[i].GetTressFXVector(j);

                // Get rest length
                if (j < strands[i].vertices.Length - 1)
                {
                    hairRestLengths[index] = (strands[i].vertices[j].pos - strands[i].vertices[j + 1].pos).magnitude;
                }
                else
                {
                    hairRestLengths[index] = 0;
                }

                // Init transforms
                globalTransforms[index]        = new TressFXTransform();
                localTransforms[index]         = new TressFXTransform();
                strands[i].localTransforms[j]  = localTransforms[index];
                strands[i].globalTransforms[j] = globalTransforms[index];

                // Set triangle indices
                if (j < strands[i].vertices.Length - 1)
                {
                    triangleIndicesList.Add(2 * index);
                    triangleIndicesList.Add(2 * index + 1);
                    triangleIndicesList.Add(2 * index + 2);
                    triangleIndicesList.Add(2 * index + 2);
                    triangleIndicesList.Add(2 * index + 1);
                    triangleIndicesList.Add(2 * index + 3);
                }

                float tVal = strands[i].vertices[j].texcoords.z;
                thicknessCoeffs[index] = Mathf.Sqrt(1.0f - tVal * tVal);

                // Set strand indices currently used for cutting linestrips by a geometry shader
                strandIndices[index] = j;
                index++;
            }

            // Set strand offsets
            offsets[i] = index;
        }

        this.ThicknessCoeffsBuffer = new ComputeBuffer(numVertices, 4);
        this.ThicknessCoeffsBuffer.SetData(thicknessCoeffs);

        this.triangleIndices       = triangleIndicesList.ToArray();
        this.TriangleIndicesBuffer = new ComputeBuffer(this.triangleIndices.Length, 4);
        this.TriangleIndicesBuffer.SetData(this.triangleIndices);

        // Initialize frames
        this.InitializeLocalGlobalFrame();

        // Compute strand tangents
        this.ComputeStrandTangents();
        this.TangentsBuffer = new ComputeBuffer(this.vectorTangents.Length, 12);
        this.TangentsBuffer.SetData(this.vectorTangents);

        // Initialize compute buffers
        this.InitialVertexPositionBuffer = new ComputeBuffer(numVertices, 16);
        this.LastVertexPositionBuffer    = new ComputeBuffer(numVertices, 16);
        this.VertexPositionBuffer        = new ComputeBuffer(numVertices, 16);
        this.StrandIndicesBuffer         = new ComputeBuffer(numVertices, 4);
        this.HairIndicesBuffer           = new ComputeBuffer(strands.Length, 4);

        this.InitialVertexPositionBuffer.SetData(initialPositionVectors);
        this.StrandIndicesBuffer.SetData(strandIndices);
        this.HairIndicesBuffer.SetData(hairIndices);

        // Initialize simulation if existing
        TressFXSimulation simulation = this.gameObject.GetComponent <TressFXSimulation>();

        if (simulation != null)
        {
            simulation.Initialize(hairRestLengths, referenceVectors, offsets, localRotations, globalRotations);
        }

        // Initialize Rendering if existing
        ATressFXRender render = this.gameObject.GetComponent <ATressFXRender>();

        if (render != null)
        {
            render.Initialize();
        }

        this.LastVertexPositionBuffer.SetData(positionVectors);
        this.VertexPositionBuffer.SetData(positionVectors);

        Debug.Log("TressFX Loaded! Hair vertices: " + this.vertexCount);
    }