public void SampleFromField()
        {
            m_objPosition = transform.position;
            m_objRotation = transform.rotation;

            if (ReactorField == null)
            {
                return;
            }

            var comp = ReactorField.GetComponent <BoingReactorField>();

            if (comp == null)
            {
        #if UNITY_EDITOR
                if (!s_warnedComponent)
                {
                    Debug.LogWarning("The assigned ReactorField game object must have a BoingReactorField component for BoingReactorFieldCpuSampler components to sample from.");
                    s_warnedComponent = true;
                }
        #endif

                return;
            }

            if (comp.HardwareMode != BoingReactorField.HardwareModeEnum.CPU)
            {
        #if UNITY_EDITOR
                if (!s_warnedHardwareMode)
                {
                    Debug.LogWarning("The BoingReactorField component needs to be set to CPU hardware mode for BoingReactorFieldCpuSampler components to sample from.");
                    s_warnedHardwareMode = true;
                }
        #endif

                return;
            }

            Vector3 positionOffset;
            Vector4 rotationOffset;
            if (!comp.SampleCpuGrid(transform.position, out positionOffset, out rotationOffset))
            {
                return;
            }

            transform.position = m_objPosition + positionOffset * PositionSampleMultiplier;
            transform.rotation = QuaternionUtil.Pow(QuaternionUtil.FromVector4(rotationOffset), RotationSampleMultiplier) * m_objRotation;
        }
示例#2
0
    public void Start()
    {
        Random.InitState(0);

        var bushGpuSampler = Bush.GetComponent <BoingReactorFieldGPUSampler>();

        if (bushGpuSampler == null)
        {
            for (int i = 0; i < NumBushes; ++i)
            {
                var bush = Instantiate(Bush);

                float scale = Random.Range(BushScaleRange.x, BushScaleRange.y);

                bush.transform.position =
                    new Vector3
                    (
                        Random.Range(-0.5f * FieldBounds.x, 0.5f * FieldBounds.x),
                        0.2f * scale,
                        Random.Range(-0.5f * FieldBounds.y, 0.5f * FieldBounds.y)
                    );

                bush.transform.rotation   = Quaternion.Euler(0.0f, Random.Range(0.0f, 360.0f), 0.0f);
                bush.transform.localScale = scale * Vector3.one;

                var cpuSampler = bush.GetComponent <BoingReactorFieldCPUSampler>();
                if (cpuSampler != null)
                {
                    cpuSampler.ReactorField = ReactorField;
                }

                var gpuSampler = bush.GetComponent <BoingReactorFieldGPUSampler>();
                if (gpuSampler != null)
                {
                    gpuSampler.ReactorField = ReactorField;
                }
            }
        }
        else
        {
            m_aaInstancedBushMatrix = new Matrix4x4[(NumBushes + kNumInstancedBushesPerDrawCall - 1) / kNumInstancedBushesPerDrawCall][];
            for (int i = 0; i < m_aaInstancedBushMatrix.Length; ++i)
            {
                m_aaInstancedBushMatrix[i] = new Matrix4x4[kNumInstancedBushesPerDrawCall];
            }
            for (int i = 0; i < NumBushes; ++i)
            {
                float scale = Random.Range(BushScaleRange.x, BushScaleRange.y);

                Vector3 position =
                    new Vector3
                    (
                        Random.Range(-0.5f * FieldBounds.x, 0.5f * FieldBounds.x),
                        0.2f * scale,
                        Random.Range(-0.5f * FieldBounds.y, 0.5f * FieldBounds.y)
                    );

                Quaternion rotation = Quaternion.Euler(0.0f, Random.Range(0.0f, 360.0f), 0.0f);

                m_aaInstancedBushMatrix[i / kNumInstancedBushesPerDrawCall][i % kNumInstancedBushesPerDrawCall].SetTRS(position, rotation, scale * Vector3.one);
            }
        }

        for (int i = 0; i < NumBlossoms; ++i)
        {
            var blossom = Instantiate(Blossom);

            float scale = Random.Range(BlossomScaleRange.x, BlossomScaleRange.y);

            blossom.transform.position =
                new Vector3
                (
                    Random.Range(-0.5f * FieldBounds.x, 0.5f * FieldBounds.y),
                    0.2f * scale,
                    Random.Range(-0.5f * FieldBounds.y, 0.5f * FieldBounds.y)
                );

            blossom.transform.rotation   = Quaternion.Euler(0.0f, Random.Range(0.0f, 360.0f), 0.0f);
            blossom.transform.localScale = scale * Vector3.one;

            blossom.GetComponent <BoingReactorFieldCPUSampler>().ReactorField = ReactorField;
        }

        m_aSphere = new List <BoingEffector>(NumSpheresPerCircle * NumCircles);
        for (int c = 0; c < NumCircles; ++c)
        {
            for (int s = 0; s < NumSpheresPerCircle; ++s)
            {
                var sphere = Instantiate(Sphere);
                m_aSphere.Add(sphere.GetComponent <BoingEffector>());
            }
        }

        var field = ReactorField.GetComponent <BoingReactorField>();

        field.Effectors =
            field.Effectors != null
        ? field.Effectors.Concat(m_aSphere.ToArray()).ToArray()
        : m_aSphere.ToArray();

        m_basePhase = 0.0f;
    }
示例#3
0
        public void Update()
        {
            if (ReactorField == null)
            {
                return;
            }

            var comp = ReactorField.GetComponent <BoingReactorField>();

            if (comp == null)
            {
        #if UNITY_EDITOR
                if (!s_warnedComponent)
                {
                    Debug.LogWarning("The assigned ReactorField game object must have a BoingReactorField component for BoingReactorFieldCpuSampler components to sample from.");
                    s_warnedComponent = true;
                }
        #endif

                return;
            }

            if (comp.HardwareMode != BoingReactorField.HardwareModeEnum.GPU)
            {
        #if UNITY_EDITOR
                if (!s_warnedHardwareMode)
                {
                    Debug.LogWarning("The BoingReactorField component needs to be set to GPU hardware mode for BoingReactorFieldCpuSampler components to sample from.");
                    s_warnedHardwareMode = true;
                }
        #endif

                return;
            }

            //-----------------------------------------------------------------------

            if (m_fieldResourceSetId != comp.GpuResourceSetId)
            {
                if (m_matProps == null)
                {
                    m_matProps = new MaterialPropertyBlock();
                }

                if (comp.UpdateShaderConstants(m_matProps, PositionSampleMultiplier, RotationSampleMultiplier))
                {
                    m_fieldResourceSetId = comp.GpuResourceSetId;

                    var aRenderer =
                        new Renderer[]
                    {
                        GetComponent <MeshRenderer>(),
                        GetComponent <SkinnedMeshRenderer>(),
                    };

                    foreach (var renderer in aRenderer)
                    {
                        if (renderer == null)
                        {
                            continue;
                        }

                        renderer.SetPropertyBlock(m_matProps);
                    }
                }
            }
        }