TreeEditor.TreeGroup.ComputeAmbientOcclusion C# (CSharp) Method

ComputeAmbientOcclusion() protected static method

protected static ComputeAmbientOcclusion ( Vector3 pos, Vector3 nor, List aoSpheres, float aoDensity ) : float
pos Vector3
nor Vector3
aoSpheres List
aoDensity float
return float
        protected static float ComputeAmbientOcclusion(Vector3 pos, Vector3 nor, List<TreeAOSphere> aoSpheres, float aoDensity)
        {
            if (aoSpheres.Count == 0)
            {
                return 1f;
            }
            float num2 = 0f;
            for (int i = 0; i < aoSpheres.Count; i++)
            {
                num2 += (aoSpheres[i].PointOcclusion(pos, nor) * aoSpheres[i].density) * 0.25f;
            }
            return (1f - (Mathf.Clamp01(num2) * aoDensity));
        }

Usage Example

Beispiel #1
0
        private void UpdateNodeMesh(TreeNode node, List <TreeMaterial> materials, List <TreeVertex> verts, List <TreeTriangle> tris, List <TreeAOSphere> aoSpheres, int buildFlags, float adaptiveQuality, float aoDensity)
        {
            node.triStart  = tris.Count;
            node.triEnd    = tris.Count;
            node.vertStart = verts.Count;
            node.vertEnd   = verts.Count;
            if (node.visible && base.visible)
            {
                Vector2 vector = base.ComputeWindFactor(node, node.offset);
                if (this.geometryMode == 4)
                {
                    if ((((cloneMesh == null) || (cloneVerts == null)) || ((cloneNormals == null) || (cloneTangents == null))) || (cloneUVs == null))
                    {
                        return;
                    }
                    Matrix4x4 localToWorldMatrix = this.instanceMesh.transform.localToWorldMatrix;
                    Matrix4x4 matrixx2           = node.matrix * localToWorldMatrix;
                    int       count = verts.Count;
                    float     num2  = 5f;
                    for (int i = 0; i < cloneVerts.Length; i++)
                    {
                        TreeVertex item = new TreeVertex {
                            pos = matrixx2.MultiplyPoint(cloneVerts[i]),
                            nor = matrixx2.MultiplyVector(cloneNormals[i]).normalized,
                            uv0 = new Vector2(cloneUVs[i].x, cloneUVs[i].y)
                        };
                        Vector3 normalized = matrixx2.MultiplyVector(new Vector3(cloneTangents[i].x, cloneTangents[i].y, cloneTangents[i].z)).normalized;
                        item.tangent = new Vector4(normalized.x, normalized.y, normalized.z, cloneTangents[i].w);
                        float edgeFactor = (cloneVerts[i].magnitude / num2) * base.animationEdge;
                        item.SetAnimationProperties(vector.x, vector.y, edgeFactor, node.animSeed);
                        if ((buildFlags & 1) != 0)
                        {
                            item.SetAmbientOcclusion(TreeGroup.ComputeAmbientOcclusion(item.pos, item.nor, aoSpheres, aoDensity));
                        }
                        verts.Add(item);
                    }
                    for (int j = 0; j < cloneMesh.subMeshCount; j++)
                    {
                        int   num6;
                        int[] triangles = cloneMesh.GetTriangles(j);
                        if ((this.instanceMesh.GetComponent <Renderer>() != null) && (j < this.instanceMesh.GetComponent <Renderer>().sharedMaterials.Length))
                        {
                            num6 = TreeGroup.GetMaterialIndex(this.instanceMesh.GetComponent <Renderer>().sharedMaterials[j], materials, false);
                        }
                        else
                        {
                            num6 = TreeGroup.GetMaterialIndex(null, materials, false);
                        }
                        for (int k = 0; k < triangles.Length; k += 3)
                        {
                            TreeTriangle triangle = new TreeTriangle(num6, triangles[k] + count, triangles[k + 1] + count, triangles[k + 2] + count);
                            tris.Add(triangle);
                        }
                    }
                }
                else if (this.geometryMode == 3)
                {
                    Vector3 eulerAngles = node.rotation.eulerAngles;
                    eulerAngles.z = eulerAngles.x * 2f;
                    eulerAngles.x = 0f;
                    eulerAngles.y = 0f;
                    Quaternion billboardRotation = Quaternion.Euler(eulerAngles);
                    Vector3    normalBase        = new Vector3(TreeGroup.GenerateBendBillboardNormalFactor, TreeGroup.GenerateBendBillboardNormalFactor, 1f);
                    Vector3    tangentBase       = (Vector3)(billboardRotation * new Vector3(1f, 0f, 0f));
                    float      normalFix         = node.scale / (TreeGroup.GenerateBendBillboardNormalFactor * TreeGroup.GenerateBendBillboardNormalFactor);
                    TreeVertex vertex2           = CreateBillboardVertex(node, billboardRotation, normalBase, normalFix, tangentBase, new Vector2(0f, 1f));
                    TreeVertex vertex3           = CreateBillboardVertex(node, billboardRotation, normalBase, normalFix, tangentBase, new Vector2(0f, 0f));
                    TreeVertex vertex4           = CreateBillboardVertex(node, billboardRotation, normalBase, normalFix, tangentBase, new Vector2(1f, 0f));
                    TreeVertex vertex5           = CreateBillboardVertex(node, billboardRotation, normalBase, normalFix, tangentBase, new Vector2(1f, 1f));
                    vertex2.SetAnimationProperties(vector.x, vector.y, base.animationEdge, node.animSeed);
                    vertex3.SetAnimationProperties(vector.x, vector.y, base.animationEdge, node.animSeed);
                    vertex4.SetAnimationProperties(vector.x, vector.y, base.animationEdge, node.animSeed);
                    vertex5.SetAnimationProperties(vector.x, vector.y, base.animationEdge, node.animSeed);
                    if ((buildFlags & 1) != 0)
                    {
                        Vector3 vector8 = (Vector3)(Vector3.right * node.scale);
                        Vector3 vector9 = (Vector3)(Vector3.forward * node.scale);
                        float   ao      = 0f;
                        ao  = TreeGroup.ComputeAmbientOcclusion(vertex2.pos + vector8, Vector3.right, aoSpheres, aoDensity) + TreeGroup.ComputeAmbientOcclusion(vertex2.pos - vector8, -Vector3.right, aoSpheres, aoDensity);
                        ao += TreeGroup.ComputeAmbientOcclusion(vertex2.pos + vector9, Vector3.forward, aoSpheres, aoDensity);
                        ao += TreeGroup.ComputeAmbientOcclusion(vertex2.pos - vector9, -Vector3.forward, aoSpheres, aoDensity);
                        ao /= 4f;
                        vertex2.SetAmbientOcclusion(ao);
                        vertex3.SetAmbientOcclusion(ao);
                        vertex4.SetAmbientOcclusion(ao);
                        vertex5.SetAmbientOcclusion(ao);
                    }
                    int num10 = verts.Count;
                    verts.Add(vertex2);
                    verts.Add(vertex3);
                    verts.Add(vertex4);
                    verts.Add(vertex5);
                    int material = TreeGroup.GetMaterialIndex(this.materialLeaf, materials, false);
                    tris.Add(new TreeTriangle(material, num10, num10 + 2, num10 + 1, true));
                    tris.Add(new TreeTriangle(material, num10, num10 + 3, num10 + 2, true));
                }
                else
                {
                    int num12 = 0;
                    switch (((GeometryMode)this.geometryMode))
                    {
                    case GeometryMode.PLANE:
                        num12 = 1;
                        break;

                    case GeometryMode.CROSS:
                        num12 = 2;
                        break;

                    case GeometryMode.TRI_CROSS:
                        num12 = 3;
                        break;
                    }
                    int       num13             = TreeGroup.GetMaterialIndex(this.materialLeaf, materials, false);
                    Vector2[] vectorArray       = new Vector2[] { new Vector2(0f, 1f), new Vector2(0f, 0f), new Vector2(1f, 0f), new Vector2(1f, 1f) };
                    Vector2[] planeHullVertices = this.GetPlaneHullVertices(this.materialLeaf);
                    if (planeHullVertices == null)
                    {
                        planeHullVertices = vectorArray;
                    }
                    float     scale        = node.scale;
                    Vector3[] vectorArray3 = new Vector3[] { new Vector3(-scale, 0f, -scale), new Vector3(-scale, 0f, scale), new Vector3(scale, 0f, scale), new Vector3(scale, 0f, -scale) };
                    Vector3   vector10     = new Vector3(TreeGroup.GenerateBendNormalFactor, 1f - TreeGroup.GenerateBendNormalFactor, TreeGroup.GenerateBendNormalFactor);
                    Vector3[] vectorArray6 = new Vector3[4];
                    Vector3   vector11     = new Vector3(-vector10.x, vector10.y, -vector10.z);
                    vectorArray6[0] = vector11.normalized;
                    Vector3 vector12 = new Vector3(-vector10.x, vector10.y, 0f);
                    vectorArray6[1] = vector12.normalized;
                    Vector3 vector13 = new Vector3(vector10.x, vector10.y, 0f);
                    vectorArray6[2] = vector13.normalized;
                    Vector3 vector14 = new Vector3(vector10.x, vector10.y, -vector10.z);
                    vectorArray6[3] = vector14.normalized;
                    Vector3[] vectorArray4 = vectorArray6;
                    for (int m = 0; m < num12; m++)
                    {
                        Quaternion rot = Quaternion.Euler(new Vector3(90f, 0f, 0f));
                        switch (m)
                        {
                        case 1:
                            rot = Quaternion.Euler(new Vector3(90f, 90f, 0f));
                            break;

                        case 2:
                            rot = Quaternion.Euler(new Vector3(0f, 90f, 0f));
                            break;
                        }
                        TreeVertex[] tv = new TreeVertex[] { new TreeVertex(), new TreeVertex(), new TreeVertex(), new TreeVertex(), new TreeVertex(), new TreeVertex(), new TreeVertex(), new TreeVertex() };
                        for (int n = 0; n < 4; n++)
                        {
                            tv[n].pos     = node.matrix.MultiplyPoint((Vector3)(rot * vectorArray3[n]));
                            tv[n].nor     = node.matrix.MultiplyVector((Vector3)(rot * vectorArray4[n]));
                            tv[n].tangent = TreeGroup.CreateTangent(node, rot, tv[n].nor);
                            tv[n].uv0     = planeHullVertices[n];
                            tv[n].SetAnimationProperties(vector.x, vector.y, base.animationEdge, node.animSeed);
                            if ((buildFlags & 1) != 0)
                            {
                                tv[n].SetAmbientOcclusion(TreeGroup.ComputeAmbientOcclusion(tv[n].pos, tv[n].nor, aoSpheres, aoDensity));
                            }
                        }
                        for (int num17 = 0; num17 < 4; num17++)
                        {
                            tv[num17 + 4].Lerp4(tv, planeHullVertices[num17]);
                            tv[num17 + 4].uv0  = tv[num17].uv0;
                            tv[num17 + 4].uv1  = tv[num17].uv1;
                            tv[num17 + 4].flag = tv[num17].flag;
                        }
                        int num18 = verts.Count;
                        for (int num19 = 0; num19 < 4; num19++)
                        {
                            verts.Add(tv[num19 + 4]);
                        }
                        tris.Add(new TreeTriangle(num13, num18, num18 + 1, num18 + 2));
                        tris.Add(new TreeTriangle(num13, num18, num18 + 2, num18 + 3));
                        Vector3 inNormal = node.matrix.MultiplyVector((Vector3)(rot * new Vector3(0f, 1f, 0f)));
                        if (TreeGroup.GenerateDoubleSidedGeometry)
                        {
                            TreeVertex[] vertexArray2 = new TreeVertex[] { new TreeVertex(), new TreeVertex(), new TreeVertex(), new TreeVertex(), new TreeVertex(), new TreeVertex(), new TreeVertex(), new TreeVertex() };
                            for (int num20 = 0; num20 < 4; num20++)
                            {
                                vertexArray2[num20].pos       = tv[num20].pos;
                                vertexArray2[num20].nor       = Vector3.Reflect(tv[num20].nor, inNormal);
                                vertexArray2[num20].tangent   = Vector3.Reflect((Vector3)tv[num20].tangent, inNormal);
                                vertexArray2[num20].tangent.w = -1f;
                                vertexArray2[num20].uv0       = tv[num20].uv0;
                                vertexArray2[num20].SetAnimationProperties(vector.x, vector.y, base.animationEdge, node.animSeed);
                                if ((buildFlags & 1) != 0)
                                {
                                    vertexArray2[num20].SetAmbientOcclusion(TreeGroup.ComputeAmbientOcclusion(vertexArray2[num20].pos, vertexArray2[num20].nor, aoSpheres, aoDensity));
                                }
                            }
                            for (int num21 = 0; num21 < 4; num21++)
                            {
                                vertexArray2[num21 + 4].Lerp4(vertexArray2, planeHullVertices[num21]);
                                vertexArray2[num21 + 4].uv0  = vertexArray2[num21].uv0;
                                vertexArray2[num21 + 4].uv1  = vertexArray2[num21].uv1;
                                vertexArray2[num21 + 4].flag = vertexArray2[num21].flag;
                            }
                            int num22 = verts.Count;
                            for (int num23 = 0; num23 < 4; num23++)
                            {
                                verts.Add(vertexArray2[num23 + 4]);
                            }
                            tris.Add(new TreeTriangle(num13, num22, num22 + 2, num22 + 1));
                            tris.Add(new TreeTriangle(num13, num22, num22 + 3, num22 + 2));
                        }
                    }
                }
                node.triEnd  = tris.Count;
                node.vertEnd = verts.Count;
            }
        }
All Usage Examples Of TreeEditor.TreeGroup::ComputeAmbientOcclusion