Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.4 kB
2
Indexable
Never
        public void Execute(int index) {
            //Check if voxel is inside wall, if so flip it to black.
            Color32 GIAOColor = voxelGIAO[index];
            if (GIAOColor.r == whiteColor.r && GIAOColor.g == whiteColor.g && GIAOColor.b == whiteColor.b) {
                voxelGIAO[index] = emptyColor;
                voxelNormal[index] = greyColor;
            } else {
                Vector3 GIEmissive = zero;
                Vector3 GILighting = zero;
                float AO = 1f;
                Vector3 normal = zero;
                float directShadow = 1f;
                float belowObstacle = 1f;

                int startIndex = index * directionsLength;
                for (int i = 0; i < directionsLength; i++) {
                    int resultIndex = startIndex + i;

                    RaycastHit hit = hitResults[resultIndex];
                    if (hit.colliderInstanceID != 0) {
                        float distanceToHit = hit.distance;

                        if (i == 0) {
                            directShadow = 0f;
                        } else if (i == 1) {
                            belowObstacle = 0f;
                        }

                        //float clampedDistance;
                        float a;
                        float AOResult;

                        a = Mathf.InverseLerp(AOSpread, 0f, distanceToHit) - hitDistanceReduction;
                        AOResult = AOAdd * a;
                        AO -= AOResult;
                        
                        //Add normal if hit is close enough
                        if (distanceToHit <= normalLength) {
                            a = Mathf.InverseLerp(normalLength, 0f, distanceToHit) - hitDistanceReduction;
                            normal += hit.normal * AOAdd * a;
                        }

                        //Add GI
                        Vector2 uv = hit.textureCoord;
                        if (GIIntensity > 0 && uv.sqrMagnitude > 0f) {
                            Color hitColor = atlasColors[(Mathf.FloorToInt(uv.y * atlasHeight) * atlasWidth) + Mathf.FloorToInt(uv.x * atlasWidth)];
                            Vector3 hitColorRGB = new Vector3(hitColor.r, hitColor.g, hitColor.b);

                            a = Mathf.InverseLerp(GISpread, 0, distanceToHit) - hitDistanceReduction;
                            GIEmissive += hitColorRGB * a * hitColor.a * GIReduce;

                            float h;
                            float s;
                            float v;
                            Color.RGBToHSV(hitColor, out h, out s, out v);
                            if (s > 0.5f) {
                                GILighting += ((hitColorRGB * a * directShadow * GIDirect * (1f - hitColor.a)) + (hitColorRGB * GIAmbient)) * GIReduce;
                            }
                        }
                    }
                }

                normal = (normal * 0.5f) + new Vector3(0.5f, 0.5f, 0.5f);
                Vector3 GITotal = (GILighting + GIEmissive) * GIIntensity;

                voxelGIAO[index] = new Color(GITotal.x, GITotal.y, GITotal.z, AO + GITotal.magnitude);
                voxelNormal[index] = new Color(normal.x, normal.y, normal.z, 0f);
                voxelControl[index] = new Color(directShadow, belowObstacle, 0f, 0f);
            }
        }