Esempio n. 1
0
        private void ApplyToBlock(ref MaterialPropertyBlock block, ShaderIDs bids)
        {
                        #if USE_PROPERTY_BLOCKS
            block.AddVector(bids.exposureIBL, exposures);
            block.AddVector(bids.exposureLM, exposuresLM);

            block.AddMatrix(bids.skyMatrix, skyMatrix);
            block.AddMatrix(bids.invSkyMatrix, invMatrix);

            block.AddVector(bids.skyMin, skyMin);
            block.AddVector(bids.skyMax, skyMax);

            if (specularCube)
            {
                block.AddTexture(bids.specCubeIBL, specularCube);
            }
            else
            {
                block.AddTexture(bids.specCubeIBL, blackCube);
            }

            block.AddVector(bids.SH[0], SH.cBuffer[0]);
            block.AddVector(bids.SH[1], SH.cBuffer[1]);
            block.AddVector(bids.SH[2], SH.cBuffer[2]);
            block.AddVector(bids.SH[3], SH.cBuffer[3]);
            block.AddVector(bids.SH[4], SH.cBuffer[4]);
            block.AddVector(bids.SH[5], SH.cBuffer[5]);
            block.AddVector(bids.SH[6], SH.cBuffer[6]);
            block.AddVector(bids.SH[7], SH.cBuffer[7]);
            block.AddVector(bids.SH[8], SH.cBuffer[8]);
                        #endif
        }
Esempio n. 2
0
        void Update()
        {
            if (_needsReset)
            {
                ResetResources();
            }

            // Advance the time variables.
            _rotationNoiseTime += _rotationNoiseSpeed * Time.deltaTime;

            // Call the kernels.
            UpdateKernelShader();

            if (_needsReset || _positionUpdateFlag)
            {
                Graphics.Blit(null, _positionBuffer, _kernelMaterial, 0);
            }

            Graphics.Blit(null, _rotationBuffer, _kernelMaterial, 1);

            if (_needsReset || _scaleUpdateFlag)
            {
                Graphics.Blit(null, _scaleBuffer, _kernelMaterial, 2);
            }

            // Make a material property block for the following drawcalls.
            var props = new MaterialPropertyBlock();

            props.AddTexture("_PositionTex", _positionBuffer);
            props.AddTexture("_RotationTex", _rotationBuffer);
            props.AddTexture("_ScaleTex", _scaleBuffer);

            // Temporary variables.
            var mesh     = _bulkMesh.mesh;
            var position = transform.position;
            var rotation = transform.rotation;
            var material = _material ? _material : _defaultMaterial;
            var uv       = new Vector2(0.5f / _positionBuffer.width, 0);

            // Draw mesh segments.
            for (var i = 0; i < _positionBuffer.height; i++)
            {
                uv.y = (0.5f + i) / _positionBuffer.height;
                props.AddVector("_BufferOffset", uv);
                Graphics.DrawMesh(
                    mesh, position, rotation,
                    material, 0, null, 0, props,
                    _castShadows, _receiveShadows);
            }

            // Clear flag variables.
            _positionUpdateFlag = true;
            _scaleUpdateFlag    = true;
            _needsReset         = false;
        }
Esempio n. 3
0
    public void InitializeVectorField()
    {
        vectorFieldTexture          = new Texture2D((int)particleWidth, (int)particleWidth);
        vectorFieldTexture.wrapMode = TextureWrapMode.Clamp;
        velocityField    = new Color[particleCount];
        velocityFieldNew = new Color[particleCount];
        pressureField    = new Color[particleCount];
        pressureFieldNew = new Color[particleCount];

        for (uint i = 0; i < particleCount; ++i)
        {
            velocityField[i]    = new Color(0.0f, 0.0f, 0.0f);
            velocityFieldNew[i] = new Color(0.0f, 0.0f, 0.0f);
            pressureField[i]    = new Color(0.0f, 0.0f, 0.0f);
            pressureFieldNew[i] = new Color(0.0f, 0.0f, 0.0f);
        }

        ApplyTextureData(ref vectorFieldTexture, velocityField);
        MaterialPropertyBlock mp = new MaterialPropertyBlock();

        mp.AddTexture(0, vectorFieldTexture);
        container.MySprite.SetPropertyBlock(mp);

        vfInitialized = true;
    }
Esempio n. 4
0
    static int AddTexture(IntPtr L)
    {
        int count = LuaDLL.lua_gettop(L);

        Type[] types0 = { typeof(MaterialPropertyBlock), typeof(int), typeof(Texture) };
        Type[] types1 = { typeof(MaterialPropertyBlock), typeof(string), typeof(Texture) };

        if (count == 3 && LuaScriptMgr.CheckTypes(L, types0, 1))
        {
            MaterialPropertyBlock obj = LuaScriptMgr.GetNetObject <MaterialPropertyBlock>(L, 1);
            int     arg0 = (int)LuaScriptMgr.GetNumber(L, 2);
            Texture arg1 = LuaScriptMgr.GetNetObject <Texture>(L, 3);
            obj.AddTexture(arg0, arg1);
            return(0);
        }
        else if (count == 3 && LuaScriptMgr.CheckTypes(L, types1, 1))
        {
            MaterialPropertyBlock obj = LuaScriptMgr.GetNetObject <MaterialPropertyBlock>(L, 1);
            string  arg0 = LuaScriptMgr.GetString(L, 2);
            Texture arg1 = LuaScriptMgr.GetNetObject <Texture>(L, 3);
            obj.AddTexture(arg0, arg1);
            return(0);
        }
        else
        {
            LuaDLL.luaL_error(L, "invalid arguments to method: MaterialPropertyBlock.AddTexture");
        }

        return(0);
    }
Esempio n. 5
0
    // Use this for initialization
    void Start()
    {
#if !USE_TEX
        MaterialPropertyBlock block = new MaterialPropertyBlock();
        block.AddTexture("_MainTex", tex);
        renderer.SetPropertyBlock(block);
#endif
        updateImage();
    }
Esempio n. 6
0
        void Update()
        {
            if (_needsReset)
            {
                ResetResources();
            }

            // Call the kernels.
            UpdateKernelShader();
            Graphics.Blit(null, _positionBuffer, _kernelMaterial, 0);
            Graphics.Blit(null, _rotationBuffer, _kernelMaterial, 1);
            Graphics.Blit(null, _scaleBuffer, _kernelMaterial, 2);

            // Make a material property block for the following drawcalls.
            var props = new MaterialPropertyBlock();

            props.AddTexture("_PositionTex", _positionBuffer);
            props.AddTexture("_RotationTex", _rotationBuffer);
            props.AddTexture("_ScaleTex", _scaleBuffer);
            props.SetVector("_ColumnRow", new Vector2(_columns, _rows));
            props.SetVector("_UVOffset", UVOffset);

            // Temporary variables.
            var mesh     = _bulkMesh.mesh;
            var position = transform.position;
            var rotation = transform.rotation;
            var material = _material ? _material : _defaultMaterial;
            var uv       = new Vector2(0.5f / _positionBuffer.width, 0);

            position += transform.right * XOffset;
            position += transform.up * YOffset;

            // Draw mesh segments.
            for (var i = 0; i < _positionBuffer.height; i++)
            {
                uv.y = (0.5f + i) / _positionBuffer.height;
                props.AddVector("_BufferOffset", uv);
                Graphics.DrawMesh(
                    mesh, position, rotation,
                    material, 0, null, 0, props,
                    _castShadows, _receiveShadows);
            }
        }
    public override void SetTexture(Texture2D texture)
    {
        base.SetTexture(texture);
        MaterialPropertyBlock props = new MaterialPropertyBlock();

#if UNITY_4_6 || UNITY_4_7 || UNITY_5_0 || UNITY_5_1 || UNITY_5_2
        props.AddTexture("_MainTex", texture);
#else
        props.SetTexture("_MainTex", texture);
#endif
        spriteRenderer.SetPropertyBlock(props);
    }
Esempio n. 8
0
    void updateImage()
    {
#if USE_TEX
        int       BALL_SIZE = tex.width / 2;
        int       TEX_SIZE  = tex.width;
        Texture2D texture   = new Texture2D(BALL_SIZE, BALL_SIZE);
        texture.filterMode = FilterMode.Point;
        texture.wrapMode   = TextureWrapMode.Clamp;

        float[] v = new float[3];
        for (int ix = 0; ix < BALL_SIZE; ix++)
        {
            for (int iy = 0; iy < BALL_SIZE; iy++)
            {
                float x  = (2 * ix - BALL_SIZE) / (float)BALL_SIZE;
                float y  = (2 * iy - BALL_SIZE) / (float)BALL_SIZE;
                float z2 = (x * x + y * y);
                if (z2 < 1.0f)
                {
                    float z = (float)Mathf.Sqrt(1.0f - z2);
                    v[0] = x;
                    v[1] = y;
                    v[2] = z;

                    rotate(rot, v);
                    float theta = (float)Mathf.Acos(v[2]);
                    float phi   = (float)(Mathf.Atan2(v[1], v[0]) + Mathf.PI);
                    int   ty    = (int)((theta / Mathf.PI) * TEX_SIZE);
                    int   dty   = (int)(0.5f * (phi / Mathf.PI) * TEX_SIZE);
                    int   tx    = TEX_SIZE - dty;
                    tx &= TEX_SIZE - 1;
                    ty &= TEX_SIZE - 1;
                    texture.SetPixel(ix, iy, tex.GetPixel(tx, ty));
                }
                else
                {
                    texture.SetPixel(ix, iy, trans);
                }
            }
        }
        texture.Apply();
        MaterialPropertyBlock block = new MaterialPropertyBlock();
        block.AddTexture("_MainTex", texture);
        renderer.SetPropertyBlock(block);
#else
        renderer.material.SetVector("_Rotation", new Vector4(rot[0], rot[1], rot[2], rot[3]));
#endif
    }
Esempio n. 9
0
        public void SetTile(MaterialPropertyBlock matPropertyBlock, int level, int tx, int ty)
        {
            if (!m_producer.IsGPUProducer())
            {
                return;
            }

            RenderTexture tex = null;
            Vector3       coords = Vector3.zero, size = Vector3.zero;

            SetTile(ref tex, ref coords, ref size, level, tx, ty);

            matPropertyBlock.AddTexture(m_uniforms.tile, tex);
            matPropertyBlock.AddVector(m_uniforms.tileCoords, coords);
            matPropertyBlock.AddVector(m_uniforms.tileSize, size);
        }
Esempio n. 10
0
    void Awake()
    {
        mb = new MaterialPropertyBlock();
        mb.AddTexture("_MainTex", texture);

        foreach (Transform child in transform)
        {
            defaultColor = child.renderer.material.color;


            Color newColor = defaultColor;
            //newColor.a = (1-child.localPosition.y*8)/2f;
            mb.AddColor("_Color", newColor);
            mb.AddColor("_TintColor", newColor);

            child.renderer.SetPropertyBlock(mb);
            child.renderer.material.SetTexture("_MainTex", texture);
        }
    }
Esempio n. 11
0
        private GameObject GenerateBlocksJoined(int xOffest, int yOffest)
        {
            var rand = new SimpleRNG(Util.Hash(Seed, xOffest, yOffest));

            _vertices.Clear();
            _uvs.Clear();
            _triangles.Clear();
            _lightAbsorptionColors.Clear();
            _lightEmissionColors.Clear();

            var meshObj = (GameObject)Instantiate(MeshObjectPrefab);

            meshObj.name = "Block Mesh { X = " + xOffest / ChunkSize + "; Y = " + yOffest / ChunkSize + " }";
            var meshObjTransform = meshObj.transform;

            meshObjTransform.position = meshObjTransform.position.WithXY(xOffest, yOffest);
            meshObjTransform.parent   = Container;

            var collMap = new CollMapPoint[ChunkSize, ChunkSize];

            for (int x = 0; x < ChunkSize; x++)
            {
                for (int y = 0; y < ChunkSize; y++)
                {
                    collMap[x, y] = GetCollMapPoint(x + xOffest, y + yOffest);
                }
            }

            for (int x = 0; x < ChunkSize; x++)
            {
                for (int y = 0; y < ChunkSize; y++)
                {
                    var blockInfo = collMap[x, y].BlockInfo;

                    if (blockInfo.AditionalObjectPrefab != null && blockInfo.AditionalObjectProbability >= rand.value)
                    {
                        var addObj = (GameObject)Instantiate(blockInfo.AditionalObjectPrefab);

                        addObj.transform.parent        = meshObjTransform;
                        addObj.transform.localPosition = blockInfo.AditionalObjectPrefab
                                                         .transform.position.WithXY(x + 0.5f, y + 0.5f);
                    }

                    if (blockInfo.SpriteInfo.Length == 0)
                    {
                        Debug.LogError("Sprite Info is broken");
                        continue;
                    }

                    var compactInfo =
                        (SafeIndex(collMap, x, y + 1, ChunkSize, ChunkSize,
                                   () => GetCollMapPoint(x + xOffest, y + yOffest))
                         .BlockType == BlockSetProfile.BlockType.CollidingWall
                            ? 1
                            : 0) +
                        (SafeIndex(collMap, x + 1, y, ChunkSize, ChunkSize,
                                   () => GetCollMapPoint(x + xOffest, y + yOffest))
                         .BlockType == BlockSetProfile.BlockType.CollidingWall
                            ? 2
                            : 0) +
                        (SafeIndex(collMap, x, y - 1, ChunkSize, ChunkSize,
                                   () => GetCollMapPoint(x + xOffest, y + yOffest))
                         .BlockType == BlockSetProfile.BlockType.CollidingWall
                            ? 4
                            : 0) +
                        (SafeIndex(collMap, x - 1, y, ChunkSize, ChunkSize,
                                   () => GetCollMapPoint(x + xOffest, y + yOffest))
                         .BlockType == BlockSetProfile.BlockType.CollidingWall
                            ? 8
                            : 0);

                    CreatePoint(x, y, blockInfo, compactInfo, false, rand);
                }
            }

            var blockMesh = new Mesh();

            blockMesh.vertices  = _vertices.ToArray();
            blockMesh.uv        = _uvs.ToArray();
            blockMesh.triangles = _triangles.ToArray();
            blockMesh.RecalculateBounds();
            blockMesh.Optimize();

            var meshFilter = meshObj.GetComponent <MeshFilter>();

            meshFilter.mesh = blockMesh;

            var meshRenderer = meshObj.GetComponent <MeshRenderer>();
            var texture      = BlockSet.BlockInfos
                               .First(bi => bi.SpriteInfo.Any(si => si != null))
                               .SpriteInfo.First(ti => ti != null)
                               .texture;
            var mpb = new MaterialPropertyBlock();

            mpb.AddTexture("_MainTex", texture);
            meshRenderer.SetPropertyBlock(mpb);

            for (int x = 0; x < ChunkSize; x++)
            {
                var yStart = 0;
                for (int y = 0; y < ChunkSize; y++)
                {
                    if (collMap[x, y].BlockInfo.BlockType != BlockSetProfile.BlockType.CollidingWall)
                    {
                        if (y - yStart > 0)
                        {
                            var obj = new GameObject();
                            obj.layer                   = meshObj.layer;
                            obj.transform.parent        = meshObjTransform;
                            obj.transform.localPosition = new Vector3(x, 0);
                            obj.name = "Collider x = " + x;
                            var coll = obj.AddComponent <BoxCollider2D>();
                            coll.size   = new Vector2(1, (y - yStart));
                            coll.offset = new Vector2(0.5f, yStart + coll.size.y / 2f);
                        }
                        yStart = y + 1;
                    }
                }
                if (ChunkSize - yStart > 0)
                {
                    var obj = new GameObject();
                    obj.layer                   = meshObj.layer;
                    obj.transform.parent        = meshObjTransform;
                    obj.transform.localPosition = new Vector3(x, 0);
                    obj.name = "Collider x = " + x;
                    var coll = obj.AddComponent <BoxCollider2D>();
                    coll.size   = new Vector2(1, (ChunkSize - yStart));
                    coll.offset = new Vector2(0.5f, yStart + coll.size.y / 2f);
                }
            }

            var lightObstaclesObject = (GameObject)Instantiate(LightObstaclesPrefab);

            lightObstaclesObject.transform.parent        = meshObjTransform;
            lightObstaclesObject.transform.localPosition = Vector3.zero;
            //lightObstaclesObject.transform.localPosition += new Vector3(0, 0, -10);
            var lightObstaclesMeshFilter = lightObstaclesObject.GetComponent <MeshFilter>();

            lightObstaclesMeshFilter.mesh = ChunkMeshFromColors(_lightAbsorptionColors);

            var ambientLightObject = (GameObject)Instantiate(AmbientLightPrefab);

            ambientLightObject.transform.parent        = meshObjTransform;
            ambientLightObject.transform.localPosition = Vector3.zero;
            //ambientLightObject.transform.localPosition += new Vector3(0, 0, -5);
            var ambientLightMeshFilter = ambientLightObject.GetComponent <MeshFilter>();

            ambientLightMeshFilter.mesh = ChunkMeshFromColors(_lightEmissionColors);

            return(meshObj);
        }
    public void InitializeVectorField()
    {
        velocityField    = new ComputeBuffer((int)particleCount, 2 * sizeof(float));
        velocityFieldNew = new ComputeBuffer((int)particleCount, 2 * sizeof(float));
        pressureField    = new ComputeBuffer((int)particleCount, sizeof(float));
        pressureFieldNew = new ComputeBuffer((int)particleCount, sizeof(float));
        jacobiHelper     = new ComputeBuffer((int)particleCount, 2 * sizeof(float));
        particleData     = new ComputeBuffer((int)particleCount, 4 * sizeof(float));
        dyeField         = new ComputeBuffer((int)particleCount, sizeof(float));
        cbArray[0]       = velocityField;
        cbArray[1]       = velocityFieldNew;
        cbArray[2]       = pressureField;
        cbArray[3]       = pressureFieldNew;
        cbArray[4]       = jacobiHelper;
        cbArray[5]       = particleData;
        cbArray[6]       = dyeField;

        for (int i = 0; i < CB_COUNT; ++i)
        {
        }

        velocityField.SetData(new Vector2[particleCount]);
        velocityFieldNew.SetData(new Vector2[particleCount]);
        pressureField.SetData(new float[particleCount]);
        pressureFieldNew.SetData(new float[particleCount]);
        jacobiHelper.SetData(new Vector2[particleCount]);
        particleData.SetData(new Vector2[particleCount]);
        dyeField.SetData(new Vector2[particleCount]);

        velocityFieldBuffer = new Vector2[particleCount];
        particleDataBuffer  = new Vector4[particleCount];
        pressureFieldBuffer = new float[particleCount];
        dyeFieldBuffer      = new float[particleCount];

        finalTexture          = new Texture2D((int)particleWidth, (int)particleWidth, TextureFormat.RGBAFloat, false);
        finalTexture.wrapMode = TextureWrapMode.Clamp;

        _DfinalTexture          = new Texture2D((int)particleWidth, (int)particleWidth, TextureFormat.RGBAFloat, false);
        _DfinalTexture.wrapMode = TextureWrapMode.Clamp;

        MaterialPropertyBlock mp = new MaterialPropertyBlock();

        mp.AddTexture(0, finalTexture);
        container.MySprite.SetPropertyBlock(mp);

        kernelIDs[0] = cShader.FindKernel("Advect");
        kernelIDs[1] = cShader.FindKernel("Diffuse");
        kernelIDs[2] = cShader.FindKernel("ApplyForces");
        kernelIDs[3] = cShader.FindKernel("Pressure");
        kernelIDs[4] = cShader.FindKernel("SubtractPressure");
        kernelIDs[5] = cShader.FindKernel("SwapOldToNew");
        kernelIDs[6] = cShader.FindKernel("SwapNewToOld");
        kernelIDs[7] = cShader.FindKernel("Clear");

        cShader.SetBuffer(kernelIDs[(int)KernelIDs.ADVECT], "VelocityField", velocityField);
        cShader.SetBuffer(kernelIDs[(int)KernelIDs.ADVECT], "VelocityFieldNew", velocityFieldNew);
        cShader.SetBuffer(kernelIDs[(int)KernelIDs.ADVECT], "DyeField", dyeField);

        cShader.SetBuffer(kernelIDs[(int)KernelIDs.DIFFUSE], "VelocityField", velocityFieldNew);
        cShader.SetBuffer(kernelIDs[(int)KernelIDs.DIFFUSE], "VelocityFieldNew", velocityField);

        cShader.SetBuffer(kernelIDs[(int)KernelIDs.APPLY_FORCE], "VelocityField", velocityField);
        cShader.SetBuffer(kernelIDs[(int)KernelIDs.APPLY_FORCE], "VelocityFieldNew", velocityFieldNew);
        cShader.SetBuffer(kernelIDs[(int)KernelIDs.APPLY_FORCE], "DyeField", dyeField);

        cShader.SetBuffer(kernelIDs[(int)KernelIDs.PRESSURE], "VelocityField", velocityFieldNew);
        cShader.SetBuffer(kernelIDs[(int)KernelIDs.PRESSURE], "VelocityFieldNew", velocityField);
        cShader.SetBuffer(kernelIDs[(int)KernelIDs.PRESSURE], "PressureField", pressureField);
        cShader.SetBuffer(kernelIDs[(int)KernelIDs.PRESSURE], "PressureFieldNew", pressureFieldNew);

        cShader.SetBuffer(kernelIDs[(int)KernelIDs.SWAP_TO_NEW], "VelocityField", velocityField);
        cShader.SetBuffer(kernelIDs[(int)KernelIDs.SWAP_TO_NEW], "VelocityFieldNew", velocityFieldNew);

        cShader.SetBuffer(kernelIDs[(int)KernelIDs.SWAP_TO_OLD], "VelocityField", velocityFieldNew);
        cShader.SetBuffer(kernelIDs[(int)KernelIDs.SWAP_TO_OLD], "VelocityFieldNew", velocityField);

        cShader.SetBuffer(kernelIDs[(int)KernelIDs.SUBTRACT_PRESSURE], "VelocityField", velocityField);
        cShader.SetBuffer(kernelIDs[(int)KernelIDs.SUBTRACT_PRESSURE], "VelocityFieldNew", velocityFieldNew);
        cShader.SetBuffer(kernelIDs[(int)KernelIDs.SUBTRACT_PRESSURE], "PressureField", pressureField);

        cShader.SetBuffer(kernelIDs[(int)KernelIDs.CLEAR], "PressureField", pressureField);
        cShader.SetBuffer(kernelIDs[(int)KernelIDs.CLEAR], "PressureFieldNew", pressureFieldNew);

        cShader.SetFloat("DeltaTime", Time.fixedDeltaTime);
        float dx = container.containerBase / (float)particleWidth;

        cShader.SetFloat("Dx", dx);
        cShader.SetInt("Width", (int)particleWidth);
        cShader.SetFloat("ContainerElasticity", (float)container.elasticity);
        cShader.SetFloat("DropperInsertedDensity", dropper.InsertedDensity);
        cShader.SetFloat("Dissipation", DISSIPATION);
        // setup particle data to texture
        for (uint i = 0; i < particleWidth; ++i)
        {
            for (uint j = 0; j < particleWidth; ++j)
            {
                uint flatCoord = i * particleWidth + j;

                particleDataBuffer[flatCoord].x = particles[flatCoord].transform.position.x;
                particleDataBuffer[flatCoord].y = particles[flatCoord].transform.position.y;
                particleDataBuffer[flatCoord].z = (float)particles[flatCoord].viscosity;
                particleDataBuffer[flatCoord].w = (float)particles[flatCoord].mass;
            }
        }
        particleData.SetData(particleDataBuffer);

        cShader.SetBuffer(kernelIDs[(int)KernelIDs.ADVECT], "ParticleData", particleData);
        cShader.SetBuffer(kernelIDs[(int)KernelIDs.DIFFUSE], "ParticleData", particleData);
        cShader.SetBuffer(kernelIDs[(int)KernelIDs.APPLY_FORCE], "ParticleData", particleData);
        vfInitialized = true;
    }