Exemplo n.º 1
0
    void DrawChildCells(float2 frequency)
    {
        WorleyNoise childWorley = worley;

        childWorley.frequency = frequency;

        float3 meanPointWorld = parentCell.data.position + vectorUtil.MeanPoint(parentCell.vertices);

        WorleyNoise.CellData startChild = childWorley.GetCellData(meanPointWorld);

        var checkNext      = new NativeQueue <WorleyNoise.CellData>(Allocator.Temp);
        var alreadyChecked = new NativeList <int2>(Allocator.Temp);

        checkNext.Enqueue(startChild);
        alreadyChecked.Add(startChild.index);

        var children = new NativeList <WorleyNoise.CellProfile>(Allocator.Temp);

        while (checkNext.Count > 0)
        {
            WorleyNoise.CellData childData = checkNext.Dequeue();

            WorleyNoise.CellData dataFromParent = worley.GetCellData(childData.position);
            bool childIsInParent = dataFromParent.index.Equals(parentCell.data.index);

            if (!childIsInParent)
            {
                continue;
            }

            WorleyNoise.CellProfile childProfile = childWorley.GetCellProfile(childData);
            float3 positionInParent = childProfile.data.position - parentCell.data.position;
            positionInParent.y += baseHeight;

            leaves.Draw(childProfile, positionInParent);

            children.Add(childProfile);

            for (int i = 0; i < childProfile.vertices.Length; i++)
            {
                WorleyNoise.CellData adjacent = childProfile.adjacentCells[i].c0;
                if (!alreadyChecked.Contains(adjacent.index))
                {
                    checkNext.Enqueue(adjacent);
                    alreadyChecked.Add(adjacent.index);
                }
            }
        }

        checkNext.Dispose();
        alreadyChecked.Dispose();
    }
        /// <summary>
        /// Draws the leaves using the specified world, view, and projection matrices.
        /// </summary>
        /// <param name="world">World matrix.</param>
        /// <param name="view">View matrix.</param>
        /// <param name="projection">Projection matrix.</param>
        /// <exception cref="InvalidOperationException">If no leaf effect is set.</exception>
        /// <exception cref="InvalidOperationException">If no Skeleton is set.</exception>
        /// <remarks>
        /// This method sets all the appropriate effect parameters.
        /// </remarks>
        public void DrawLeaves(Matrix world, Matrix view, Matrix projection, Vector3 cameraPos)
        {
            if (Skeleton == null)
            {
                throw new InvalidOperationException("A Skeleton must be set before leaves can be rendered.");
            }

            if (LeafEffect == null)
            {
                throw new InvalidOperationException("LeafEffect must be set before leaves can be rendered.");
            }

            LeafEffect.Parameters["WorldView"].SetValue(world * view);
            //leafEffect.Parameters["View"].SetValue(view);
            LeafEffect.Parameters["Projection"].SetValue(projection);
            LeafEffect.Parameters["LeafScale"].SetValue(world.Right.Length());


            Effect effect = LeafEffect;

            effect.Parameters["World"].SetValue(world);
            effect.Parameters["WorldIT"].SetValue(Matrix.Transpose(Matrix.Invert(world)));
            effect.Parameters["WorldViewProj"].SetValue(world * view * projection);
            effect.Parameters["ViewInv"].SetValue(Matrix.Invert(view));

            effect.Parameters["isSkydome"].SetValue(false);

            effect.Parameters["LightDirection"].SetValue(Lighting.LightDirection);
            effect.Parameters["LightColor"].SetValue(Lighting.LightColor);
            effect.Parameters["LightColorAmbient"].SetValue(Lighting.LightColorAmbient);
            effect.Parameters["FogColor"].SetValue(Lighting.FogColor);
            effect.Parameters["fDensity"].SetValue(Lighting.FogDensity);
            effect.Parameters["SunLightness"].SetValue(Lighting.SunLightness);
            effect.Parameters["sunRadiusAttenuation"].SetValue(Lighting.SunRadiusAttenuation);
            effect.Parameters["largeSunLightness"].SetValue(Lighting.LargeSunLightness);
            effect.Parameters["largeSunRadiusAttenuation"].SetValue(Lighting.LargeSunRadiusAttenuation);
            effect.Parameters["dayToSunsetSharpness"].SetValue(Lighting.DayToSunsetSharpness);
            effect.Parameters["hazeTopAltitude"].SetValue(Lighting.HazeTopAltitude);

            if (Skeleton.LeafAxis == null)
            {
                LeafEffect.Parameters["BillboardRight"].SetValue(Vector3.Right);
                LeafEffect.Parameters["BillboardUp"].SetValue(Vector3.Up);
            }
            else
            {
                Vector3 axis    = Skeleton.LeafAxis.Value;
                var     forward = new Vector3(view.M13, view.M23, view.M33);

                Vector3 right = Vector3.Cross(forward, axis);
                right.Normalize();
                Vector3 up = axis;

                Vector3.TransformNormal(ref right, ref view, out right);
                Vector3.TransformNormal(ref up, ref view, out up);

                LeafEffect.Parameters["BillboardRight"].SetValue(right);
                LeafEffect.Parameters["BillboardUp"].SetValue(up);
            }

            Skeleton.CopyBoneBindingMatricesTo(BindingMatrices, AnimationState.BoneRotations);
            LeafEffect.Parameters["Bones"].SetValue(BindingMatrices);

            // Used for normals of billboard leaves
            Vector3 treePos = Vector3.Transform(Vector3.Zero, BindingMatrices[0] * world);

            effect.Parameters["BillboardNormal"].SetValue(cameraPos - treePos);

            LeafEffect.Parameters["Texture"].SetValue(LeafTexture);

            Leaves.Draw(LeafEffect);
        }