Beispiel #1
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, 0, material, matrices, 1023, block,
         ShadowCastingMode.On, true, 0, null,
         lightProbeVolume ?
         LightProbeUsage.UseProxyVolume : LightProbeUsage.CustomProvided,
         lightProbeVolume
         );
 }
Beispiel #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
            );
    }
        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);
            }
        }