void Update()
    {
        if (block == null)
        {
            block = new MaterialPropertyBlock();
            block.SetVectorArray(baseColorId, baseColors);
            block.SetFloatArray(metallicId, metallic);
            block.SetFloatArray(smoothnessId, smoothness);

            if (!lightProbeVolume)
            {
                var positions = new Vector3[1023];
                for (int i = 0; i < matrices.Length; i++)
                {
                    positions[i] = matrices[i].GetColumn(3);
                }
                var lightProbes     = new SphericalHarmonicsL2[1023];
                var occlusionProbes = new Vector4[1023];
                LightProbes.CalculateInterpolatedLightAndOcclusionProbes(
                    positions, lightProbes, occlusionProbes
                    );
                block.CopySHCoefficientArraysFrom(lightProbes);
                block.CopyProbeOcclusionArrayFrom(occlusionProbes);
            }
        }
        Graphics.DrawMeshInstanced(
            mesh, 0, material, matrices, 1023, block,
            ShadowCastingMode.On, true, 0, null,
            lightProbeVolume ?
            LightProbeUsage.UseProxyVolume : LightProbeUsage.CustomProvided,
            lightProbeVolume
            );
    }
Exemplo n.º 2
0
    void Update()
    {
        if (_block == null)
        {
            _block = new MaterialPropertyBlock();
            _block.SetVectorArray(_baseColorId, _baseColors);
            _block.SetFloatArray(_metallicId, _metallic);
            _block.SetFloatArray(_smoothnessId, _smoothness);


            if (!_lightProbeVolume)
            {
                var positions = new Vector3[1023];
                for (int i = 0; i < _matrices.Length; i++)
                {
                    positions[i] = _matrices[i].GetColumn(3);
                }

                var lightProbes     = new SphericalHarmonicsL2[1023];
                var occlusionProbes = new Vector4[1023];
                LightProbes.CalculateInterpolatedLightAndOcclusionProbes(
                    positions, lightProbes, occlusionProbes
                    );
                _block.CopySHCoefficientArraysFrom(lightProbes);
                _block.CopyProbeOcclusionArrayFrom(occlusionProbes);
            }
        }

        Graphics.DrawMeshInstanced(
            _mesh,
            submeshIndex: 0,
            _material,
            _matrices,
            count: 1023,
            _block,
            ShadowCastingMode.On,
            receiveShadows: true,
            layer: 0,
            camera: null,
            _lightProbeVolume ? LightProbeUsage.UseProxyVolume : LightProbeUsage.CustomProvided,
            _lightProbeVolume
            );
    }
Exemplo n.º 3
0
        private void Init()
        {
            matrices     = new Matrix4x4[meshCount];
            baseColors   = new Vector4[meshCount];
            metallics    = new float[meshCount];
            smoothnesses = new float[meshCount];

            for (int i = 0; i < matrices.Length; i++)
            {
                matrices[i] = Matrix4x4.TRS(Random.insideUnitSphere * radius,
                                            Quaternion.Euler(Random.value * 360f, Random.value * 360f, Random.value * 360f),
                                            Vector3.one * Random.Range(0.5f, 1.5f));
                baseColors[i]   = new Vector4(Random.value, Random.value, Random.value, Random.Range(0.5f, 1f));
                metallics[i]    = Random.value < 0.25f ? 1f : 0f;
                smoothnesses[i] = Random.Range(0.05f, 0.95f);
            }

            materialPropertyBlock = new MaterialPropertyBlock();
            materialPropertyBlock.SetVectorArray(baseColorPropertyID, baseColors);
            materialPropertyBlock.SetFloatArray(metallicPropertyID, metallics);
            materialPropertyBlock.SetFloatArray(smoothnessPropertyID, smoothnesses);

            if (!llpv)
            {
                Vector3[] positions = new Vector3[meshCount];

                for (int i = 0; i < matrices.Length; i++)
                {
                    positions[i] = matrices[i].GetColumn(3);
                }

                SphericalHarmonicsL2[] lightProbes = new SphericalHarmonicsL2[meshCount];
                LightProbes.CalculateInterpolatedLightAndOcclusionProbes(positions, lightProbes, null); // consider the environment light
                materialPropertyBlock.CopySHCoefficientArraysFrom(lightProbes);
            }
        }
        private void UpdateEntitiesFromGrid()
        {
            Profiler.BeginSample("UpdateEntitiesFromGrid");

            var SHArType         = GetComponentTypeHandle <BuiltinMaterialPropertyUnity_SHAr>();
            var SHAgType         = GetComponentTypeHandle <BuiltinMaterialPropertyUnity_SHAg>();
            var SHAbType         = GetComponentTypeHandle <BuiltinMaterialPropertyUnity_SHAb>();
            var SHBrType         = GetComponentTypeHandle <BuiltinMaterialPropertyUnity_SHBr>();
            var SHBgType         = GetComponentTypeHandle <BuiltinMaterialPropertyUnity_SHBg>();
            var SHBbType         = GetComponentTypeHandle <BuiltinMaterialPropertyUnity_SHBb>();
            var SHCType          = GetComponentTypeHandle <BuiltinMaterialPropertyUnity_SHC>();
            var localToWorldType = GetComponentTypeHandle <LocalToWorld>();

            var chunks = m_ProbeGridQuery.CreateArchetypeChunkArray(Allocator.Temp);

            if (chunks.Length == 0)
            {
                Profiler.EndSample();
                return;
            }

            //TODO: Bring this off the main thread when we have new c++ API
            Dependency.Complete();

            foreach (var chunk in chunks)
            {
                var chunkSHAr         = chunk.GetNativeArray(SHArType);
                var chunkSHAg         = chunk.GetNativeArray(SHAgType);
                var chunkSHAb         = chunk.GetNativeArray(SHAbType);
                var chunkSHBr         = chunk.GetNativeArray(SHBrType);
                var chunkSHBg         = chunk.GetNativeArray(SHBgType);
                var chunkSHBb         = chunk.GetNativeArray(SHBbType);
                var chunkSHC          = chunk.GetNativeArray(SHCType);
                var chunkLocalToWorld = chunk.GetNativeArray(localToWorldType);

                m_Positions.Clear();
                m_LightProbes.Clear();
                m_OcclusionProbes.Clear();

                for (int i = 0; i != chunkLocalToWorld.Length; i++)
                {
                    m_Positions.Add(chunkLocalToWorld[i].Position);
                }

                LightProbes.CalculateInterpolatedLightAndOcclusionProbes(m_Positions, m_LightProbes, m_OcclusionProbes);

                for (int i = 0; i < m_Positions.Count; ++i)
                {
                    var properties = new SHProperties(m_LightProbes[i]);
                    chunkSHAr[i] = new BuiltinMaterialPropertyUnity_SHAr {
                        Value = properties.SHAr
                    };
                    chunkSHAg[i] = new BuiltinMaterialPropertyUnity_SHAg {
                        Value = properties.SHAg
                    };
                    chunkSHAb[i] = new BuiltinMaterialPropertyUnity_SHAb {
                        Value = properties.SHAb
                    };
                    chunkSHBr[i] = new BuiltinMaterialPropertyUnity_SHBr {
                        Value = properties.SHBr
                    };
                    chunkSHBg[i] = new BuiltinMaterialPropertyUnity_SHBg {
                        Value = properties.SHBg
                    };
                    chunkSHBb[i] = new BuiltinMaterialPropertyUnity_SHBb {
                        Value = properties.SHBb
                    };
                    chunkSHC[i] = new BuiltinMaterialPropertyUnity_SHC {
                        Value = properties.SHC
                    };
                }
            }
            Profiler.EndSample();
        }