Esempio n. 1
0
        public void SetParameters(uint seed)
        {
            var hash = new XXHash(seed);

            _props.SetFloat(_seed1Key, hash.Float(0));
            _props.SetFloat(_seed2Key, hash.Float(1));
        }
        void Start()
        {
            var hash = new XXHash(_randomSeed);
            var mat  = new MaterialOverride(_randomSeed);

            var parent = transform;
            var rot    = quaternion.identity;

            // Column - Row - Depth
            for (var col = 0u; col < _extent.x; col++)
            {
                for (var row = 0u; row < _extent.y; row++)
                {
                    // Per-strip parameters
                    var seed = hash.UInt(col) + hash.UInt(row);
                    mat.SetParameters(seed + 500);

                    for (var depth = 0u; depth < _extent.z; depth++)
                    {
                        // Random removal
                        if (hash.Float(seed + depth) < _removalRate)
                        {
                            continue;
                        }

                        // Instance position
                        var p = math.float3(col, row, depth);
                        p.xy = p.xy - _extent.xy / 2 + math.float2(0, 0.5f);

                        // Instantiation and material overriding
                        mat.Apply(Instantiate(_prefab, p, rot, parent));
                    }
                }
            }
        }
Esempio n. 3
0
        void UpdateAnimationParameters()
        {
            var dt = Time.deltaTime;

            // Update the noise parameter.
            _noise.x += _noiseFrequency * dt;

            // Step parameter delta
            var delta = _stepFrequency * dt;

            // Check if the current step still continues.
            if (StepCount == (int)(_step + delta))
            {
                // The current step is still going:

                // Recalculate the non-pivot foot position.
                _feet[(int)NonPivotSide] =
                    _feet[(int)PivotSide] + CurrentStepDirection * _footDistance;

                // Update the step parameter.
                _step += delta;
            }
            else
            {
                // The current step ends in this frame:

                // Put the foot at the destination.
                _feet[(int)NonPivotSide] = CurrentStepDestination;

                // Update the step parameter.
                _step += delta;

                // Randomly flip the direction.
                if (_hash.Float(StepSeed) > 0.5f)
                {
                    _stepFlip *= -1;
                }

                // Check if the next destination is in the maxDistance range.
                var origin = transform.position;
                var dist   = math.distance(CurrentStepDestination, origin);
                if (dist > _maxDistance)
                {
                    // Try flipping the turn direction. Revert it if it makes the
                    // destination further from the origin.
                    _stepFlip *= -1;
                    if (dist < math.distance(CurrentStepDestination, origin))
                    {
                        _stepFlip *= -1;
                    }
                }
            }
        }
        void Start()
        {
            var hash = new XXHash(_randomSeed);
            var mat  = new MaterialOverride(_randomSeed);

            var parent = transform;

            // Column - Row - Edge x3 - Depth
            for (var col = 0u; col < _extent.x; col++)
            {
                for (var row = 0u; row < _extent.y; row++)
                {
                    // Tube origin
                    var org = math.float2(col, row) - _extent.xy / 2;
                    org.x += (row & 1) * 0.5f;
                    org   *= math.float2(3, 0.87f);

                    for (var i = 0u; i < 3u; i++)
                    {
                        // Per-strip random seed
                        var seed = hash.UInt(col) + hash.UInt(row) + i;

                        // Per-strip random material properties
                        mat.SetParameters(seed + 500);

                        // Rotation
                        var phi = (i - 1.0f) * math.PI / 3;
                        var rot = quaternion.RotateZ(phi);

                        // Position
                        var pos = math.mul(rot, math.float3(0, 0.87f, 0));
                        pos.xy += org;

                        for (var depth = 0u; depth < _extent.z; depth++)
                        {
                            // Random removal
                            if (hash.Float(seed + depth) < _removalRate)
                            {
                                continue;
                            }

                            // Instantiation and material overriding
                            pos.z = depth;
                            mat.Apply(Instantiate(_prefab, pos, rot, parent));
                        }
                    }
                }
            }
        }
Esempio n. 5
0
            public void Execute(int i)
            {
                var e    = Elements[i];
                var hash = new XXHash((uint)i);

                // Source triangle vertices
                var v1 = e.Vertex1;
                var v2 = e.Vertex2;
                var v3 = e.Vertex3;

                // Source position (triangle centroid)
                var p = (v1 + v2 + v3) / 3;

                // Effect parameter
                var eff = -math.mul(Effector, math.float4(p, 1)).z;

                eff -= hash.Float(0.4f, 4773); // Random distribution

                // Triangle shrink
                var tmod = math.saturate(eff);

                v1 = math.lerp(v1, p, tmod);
                v2 = math.lerp(v2, p, tmod);
                v3 = math.lerp(v3, p, tmod);

                // Motion by noise
                var offs = MathUtil.DFNoise(p * 3.3f) * 0.02f;

                offs *= math.smoothstep(0, 1, eff);

                v1 += offs;
                v2 += offs;
                v3 += offs;

                // Normal/tangent
                var nrm = MathUtil.UnitOrtho(v2 - v1, v3 - v1);
                var tan = MathUtil.AdHocTangent(nrm);

                // UV and material parameters
                var em  = math.saturate(eff * 10);
                var uv1 = math.float4(e.UV1, em, em);
                var uv2 = math.float4(e.UV2, em, em);
                var uv3 = math.float4(e.UV3, em, em);

                // Output
                Output[i] = new Triangle(new Vertex(v1, nrm, tan, 0, uv1),
                                         new Vertex(v2, nrm, tan, 0, uv2),
                                         new Vertex(v3, nrm, tan, 0, uv3));
            }
Esempio n. 6
0
        void Update()
        {
            // Noise update
            _noise.x += _noiseFrequency * Time.deltaTime;

            // Step time delta
            var delta = _stepFrequency * Time.deltaTime;

            // Right vector (a vector indicating from left foot to right foot)
            // Renormalized every frame to apply _footDistance changes.
            var right = (_feet[1] - _feet[0]).normalized * _footDistance;

            // Check if the current step ends in this frame.
            if (StepCount == Mathf.FloorToInt(_step + delta))
            {
                // The current step keeps going:
                //  Recalculate the non-pivot foot position.
                //  (To apply _footDistance changes)
                if (PivotIsLeft)
                {
                    _feet[1] = _feet[0] + right;
                }
                else
                {
                    _feet[0] = _feet[1] - right;
                }

                // Update the step time
                _step += delta;
            }
            else
            {
                // The current step is going to end:
                //  Update the next pivot point;
                _feet[PivotIsLeft ? 1 : 0] = CurrentStepDestination;

                // Update the step time
                _step += delta;

                // Flip the turn direction randomly.
                if (_hash.Float(StepSeed) > 0.5f)
                {
                    _stepSign *= -1;
                }

                // Check if the next destination is in the range.
                var dist = (CurrentStepDestination - transform.position).magnitude;
                if (dist > _maxDistance)
                {
                    // The next destination is out of the range:
                    //  Try flipping the turn direction. Use it if it makes
                    //  the destination closer to the origin. Revert it if not.
                    _stepSign *= -1;
                    if (dist < (CurrentStepDestination - transform.position).magnitude)
                    {
                        _stepSign *= -1;
                    }
                }
            }

            // Update the chest matrices for use in OnAnimatorIK.
            var chest = _animator.GetBoneTransform(HumanBodyBones.Chest);

            _chestMatrix    = chest.localToWorldMatrix;
            _chestMatrixInv = chest.worldToLocalMatrix;
        }
Esempio n. 7
0
            public void Execute(int i)
            {
                var e    = Elements[i];
                var hash = new XXHash((uint)i);

                // Source triangle vertices
                var v1 = e.Vertex1;
                var v2 = e.Vertex2;
                var v3 = e.Vertex3;

                // Source position (triangle centroid)
                var p = (v1 + v2 + v3) / 3;

                // Effect parameter
                var eff = -MathUtil.Transform(Effector, p).z;

                eff -= hash.Float(0.4f, 3244); // Random distribution

                // Triangle deformation (expand very fast)
                var tmod = 1 + math.smoothstep(0, 0.3f, eff) * 30;

                v1 = math.lerp(p, v1, tmod);
                v2 = math.lerp(p, v2, tmod);
                v3 = math.lerp(p, v3, tmod);

                // Voxel motion by noise (stops before travelling)
                var nmod = 1 - math.smoothstep(0.6f, 0.7f, eff);
                var nfp  = p * 3.3f + math.float3(0, eff * 0.4f, 0);

                p += MathUtil.DFNoise(nfp) * 0.004f * nmod;

                // Voxel moving out
                var mout = math.smoothstep(0.7f, 1.3f, eff);

                p.z -= mout * 2;

                // Voxel scaling
                var vs = 0.03f * math.float2(1 - mout, 1 + mout * 30).xxy;

                // Voxel vertices
                var vv1 = p + math.float3(-1, -1, -1) * vs;
                var vv2 = p + math.float3(+1, -1, -1) * vs;
                var vv3 = p + math.float3(-1, +1, -1) * vs;
                var vv4 = p + math.float3(+1, +1, -1) * vs;
                var vv5 = p + math.float3(-1, -1, +1) * vs;
                var vv6 = p + math.float3(+1, -1, +1) * vs;
                var vv7 = p + math.float3(-1, +1, +1) * vs;
                var vv8 = p + math.float3(+1, +1, +1) * vs;

                // Triangle to voxel deformation
                var vmod = math.smoothstep(0.1f, 0.3f, eff);

                vv1 = math.lerp(v1, vv1, vmod);
                vv2 = math.lerp(v2, vv2, vmod);
                vv3 = math.lerp(v3, vv3, vmod);
                vv4 = math.lerp(v3, vv4, vmod);
                vv5 = math.lerp(v1, vv5, vmod);
                vv6 = math.lerp(v2, vv6, vmod);
                vv7 = math.lerp(v3, vv7, vmod);
                vv8 = math.lerp(v3, vv8, vmod);

                // UV and material parameters
                var mat = math.saturate(eff * 10);
                var emm = mat - vmod +
                          math.smoothstep(0.6f, 0.7f, eff) * hash.Float(1.1f, 2.1f, 1893);
                var uv1 = math.float4(e.UV1, mat, emm);
                var uv2 = math.float4(e.UV2, mat, emm);
                var uv3 = math.float4(e.UV3, mat, emm);

                // Output
                Output[i] = new VoxelOutput
                                (vv1, uv1, vv2, uv2, vv3, uv3, vv4, uv3,
                                vv5, uv1, vv6, uv2, vv7, uv3, vv8, uv3);
            }