GetTextureOffset() public method

Gets the placement offset of texture propertyName.

public GetTextureOffset ( string propertyName ) : Vector2
propertyName string The name of the property.
return Vector2
Ejemplo n.º 1
0
        protected override void Init()
        {
            if (null == m_target)
            {
                return;
            }
            // end if
            var renderer = m_target.GetComponent <Renderer>();

            if (null != renderer)
            {
                m_Material = renderer.material;
            }
            // end if
            if (null == m_Material)
            {
                return;
            }
            // end if
            if (!string.IsNullOrEmpty(m_property))
            {
                m_beginOffset = m_Material.GetTextureOffset(m_property);
            }
            else if (-1 != m_propertyID)
            {
                m_beginOffset = m_Material.GetTextureOffset(m_propertyID);
            } // end if
        }
Ejemplo n.º 2
0
	// Use this for initialization
	void Start () {
        material = GetComponent<Renderer>().material;

        offset = material.GetTextureOffset("_MainTex");


	}
Ejemplo n.º 3
0
	public List<StoredValue> GetValues(Material m)
	{
		var list = GetShaderProperties(m);
		var output = new List<StoredValue>();
		foreach(var p in list)
		{
			var o = new StoredValue { Property = p };
			output.Add(o);
			switch(p.type)
			{
			case MaterialProperty.PropertyType.color:
				o.Value = m.GetColor(p.name);
				break;
			case MaterialProperty.PropertyType.real:
				o.Value = m.GetFloat(p.name);
				break;
			case MaterialProperty.PropertyType.texture:
				o.Value = m.GetTexture(p.name);
				break;
			case MaterialProperty.PropertyType.vector:
				o.Value = m.GetVector(p.name);
				break;
			case MaterialProperty.PropertyType.textureOffset:
				o.Value = m.GetTextureOffset(p.name);
				break;
			case MaterialProperty.PropertyType.textureScale:
				o.Value = m.GetTextureScale(p.name);
				break;
			case MaterialProperty.PropertyType.matrix:
				o.Value = m.GetMatrix(p.name);
				break;
			}
		}
		return output;
	}
 public override void OnRecord()
 {
     if (_material = material)
     {
         _original = _material.GetTextureOffset(propertyName);
     }
 }
Ejemplo n.º 5
0
    // Use this for initialization
    void Start()
    {
        //get reference to the material tied to the background quad
        material = GetComponent<Renderer> ().material;

        offset = material.GetTextureOffset("_MainTex");
    }
 public void SetFromToCurrent()
 {
     if (_material = material)
     {
         from = _material.GetTextureOffset(propertyName);
     }
 }
        public static SMaterial FromMaterial(Material mat)
        {
            if (mat == null)
                return null;

            Shader shader = mat.shader;
            if (shader == null)
                return null;

            SMaterial result = new SMaterial();
            result.instanceID = mat.GetInstanceID();
            result.materialName = mat.name;
            result.shaderName = shader.name;

            ShaderProperties.Info info = ShaderPropertyHelper.GetShaderInfo(shader.name);

            if (info != null){
                ComposedByteStream rawData = new ComposedByteStream();

                int iMax = info.propertyNames.Length;
                for (int i = 0; i < iMax; i++)
                {
                    string propName = info.propertyNames[i];
                    switch (info.propertyTypes[i])
                    {
                        case ShaderProperties.PropType.Color:
                            Color color = mat.GetColor(propName);
                            rawData.AddStream(new float[] { color.r, color.g, color.b, color.a });
                            break;

                        case ShaderProperties.PropType.Range:
                        case ShaderProperties.PropType.Float:
                            rawData.AddStream(new float[] { mat.GetFloat(propName) });
                            break;

                        case ShaderProperties.PropType.TexEnv:
                            Texture texture = mat.GetTexture(propName);
                            Vector2 offset = mat.GetTextureOffset(propName);
                            Vector2 scale = mat.GetTextureScale(propName);

                            rawData.AddStream(new int[] { texture != null ? texture.GetInstanceID() : -1 });
                            rawData.AddStream(texture != null ? texture.name : "" );
                            rawData.AddStream(new float[] { offset.x, offset.y });
                            rawData.AddStream(new float[] { scale.x, scale.y });
                            break;

                        case ShaderProperties.PropType.Vector:
                            Vector4 vector = mat.GetVector(propName);
                            rawData.AddStream(new float[] { vector.x, vector.y, vector.z, vector.w });
                            break;
                    }
                }
                result.rawData = rawData.Compose();
                return result;
            } else {
                if (vBug.settings.general.debugMode)
                    Debug.LogError("No shader-info found @" + shader.name);
                return null;
            }
        }
        private void ExportTextureTransform(TextureInfo def, UnityEngine.Material mat, string texName)
        {
            Vector2 offset = mat.GetTextureOffset(texName);
            Vector2 scale  = mat.GetTextureScale(texName);

            if (offset == Vector2.zero && scale == Vector2.one)
            {
                return;
            }

            if (_root.ExtensionsUsed == null)
            {
                _root.ExtensionsUsed = new List <string>(
                    new string[] { ExtTextureTransformExtensionFactory.EXTENSION_NAME }
                    );
            }
            else if (!_root.ExtensionsUsed.Contains(ExtTextureTransformExtensionFactory.EXTENSION_NAME))
            {
                _root.ExtensionsUsed.Add(ExtTextureTransformExtensionFactory.EXTENSION_NAME);
            }

            if (def.Extensions == null)
            {
                def.Extensions = new Dictionary <string, IExtension>();
            }

            def.Extensions[ExtTextureTransformExtensionFactory.EXTENSION_NAME] = new ExtTextureTransformExtension(
                new GLTF.Math.Vector2(offset.x, -offset.y),
                new GLTF.Math.Vector2(scale.x, scale.y),
                0                 // TODO: support UV channels
                );
        }
Ejemplo n.º 9
0
    private Vector2 offset = Vector2.zero; //Starting Offset

    #endregion Fields

    #region Methods

    void Start()
    {
        material = GetComponent<Renderer>().material;

        //"_MainTex" is the main diffuse texture. This is specified in the Unity Docs.
        offset = material.GetTextureOffset("_MainTex");
    }
Ejemplo n.º 10
0
        public override void OnTween(float factor)
        {
            if (_material = material)
            {
                _temp = _material.GetTextureOffset(propertyName);

                if (mask.GetBit(0)) _temp.x = from.x + (to.x - from.x) * factor;
                if (mask.GetBit(1)) _temp.y = from.y + (to.y - from.y) * factor;

                _material.SetTextureOffset(propertyName, _temp);
            }
        }
Ejemplo n.º 11
0
 static public int GetTextureOffset(IntPtr l)
 {
     try{
         UnityEngine.Material self = (UnityEngine.Material)checkSelf(l);
         System.String        a1;
         checkType(l, 2, out a1);
         UnityEngine.Vector2 ret = self.GetTextureOffset(a1);
         pushValue(l, ret);
         return(1);
     }
     catch (Exception e) {
         LuaDLL.luaL_error(l, e.ToString());
         return(0);
     }
 }
Ejemplo n.º 12
0
 static public int GetTextureOffset(IntPtr l)
 {
     try {
         UnityEngine.Material self = (UnityEngine.Material)checkSelf(l);
         System.String        a1;
         checkType(l, 2, out a1);
         var ret = self.GetTextureOffset(a1);
         pushValue(l, true);
         pushValue(l, ret);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Ejemplo n.º 13
0
 static int GetTextureOffset(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 2);
         UnityEngine.Material obj = (UnityEngine.Material)ToLua.CheckObject(L, 1, typeof(UnityEngine.Material));
         string arg0           = ToLua.CheckString(L, 2);
         UnityEngine.Vector2 o = obj.GetTextureOffset(arg0);
         ToLua.Push(L, o);
         return(1);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
Ejemplo n.º 14
0
        static void ExportTextureTransform(TextureInfo def, UnityEngine.Material mat, int texPropertyId, IGltfWritable gltf)
        {
            var offset = mat.GetTextureOffset(texPropertyId);
            var scale  = mat.GetTextureScale(texPropertyId);

            // Counter measure for Unity/glTF texture coordinate difference
            // TODO: Offer UV conversion as alternative
            offset.y = 1 - offset.x;
            scale.y *= -1;

            if (offset != Vector2.zero || scale != Vector2.one)
            {
                gltf.RegisterExtensionUsage(Extension.TextureTransform);
                def.extensions = def.extensions ?? new TextureInfoExtension();
                def.extensions.KHR_texture_transform = new TextureTransform {
                    scale  = new[] { scale.x, scale.y },
                    offset = new[] { offset.x, offset.y }
                };
            }
        }
        public ACCTexture(Texture tex, Material mate, ShaderPropTex texProp, ShaderType type) :this(texProp.key) {
            this.tex = tex;
            this.type = type;
            this.prop = texProp;

            this.editname = tex.name;
            if (tex is Texture2D) {
               texOffset = mate.GetTextureOffset(propName);
               texScale  = mate.GetTextureScale(propName);
            } else {
                LogUtil.DebugF("propName({0}): texture type:{1}", propName, tex.GetType());
            }
            
//            } else {
//                // シェーダ切り替えなどで、元々存在しないテクスチャの場合
//                LogUtil.DebugF("texture not found. propname={0}, material={1}", propName, mate.name);
//                // 空のテクスチャは作成しない
////                this.tex = new Texture2D(2, 2);
////                this.tex.name = string.Empty;
////                // テクスチャを追加セット
////                mate.SetTexture(propName, this.tex);
//            }
        }
Ejemplo n.º 16
0
        public override void StartLevel()
        {
            var camera = Camera.main;
            var cameraWidth = camera.GetOrthographicWidth();
            var cameraHeight = camera.GetOrthographicHeight();
            bounds = new Rect( -cameraWidth / 2, -cameraHeight / 2, cameraWidth, cameraHeight );

            background.transform.localScale = new Vector3( cameraWidth, cameraHeight, 1 );
            background.sharedMaterial = Instantiate( background.sharedMaterial );

            enemyShips = new LinkedList<EnemyShip>();
            var shipConfig = new ShipConfig( 0.1f, 0.1f );
            var shipModel = new ShipModel() { Lives = 3 };
            var weaponConfig = new WeaponConfig( 5f, 1, 0.5f );

            var weapon = Locator.ResLoader.LoadAndInstantiateAs< ProjectileWeapon >( "Weapons/Cannon" );
            weapon.Init( IsRunning, TimeProvider, weaponConfig, bounds, 1 );

            var enemyShipPrefab = Locator.ResLoader.Load< GameObject >( "Ships/Enemies/LaserCorvette" );
            enemyShipPrefabLink = enemyShipPrefab.transform;
            PrefabPoolManager.Prepare( enemyShipPrefabLink, 8 );

            Player = Locator.ResLoader.LoadAndInstantiateAs< PlayerShip >( "Ships/PlayerShip" );
            Player.transform.SetParent( transform, true );
            Player.OnShipDestroyed += OnGameOver;
            Player.Init( IsRunning, TimeProvider, shipConfig, shipModel, bounds);
            Player.Weapon = weapon;

            backgroundMaterial = background.GetComponent< Renderer >().sharedMaterial;
            initialTextureOffset = backgroundMaterial.GetTextureOffset( "_MainTex" );

            if ( musicTrack != null )
                Locator.Sound.PlayMusic( musicTrack );

            base.StartLevel();
        }
Ejemplo n.º 17
0
        public void AddColorChannel(string channelName, string propertyName, string texturePropertyName, Material material)
        {
            Color value = material.GetColor(propertyName);
            Texture texture = material.GetTexture(texturePropertyName);
            Vector2 offset = material.GetTextureOffset(texturePropertyName);
            Vector2 scale = material.GetTextureScale(texturePropertyName);

            ColorChannel channel = new ColorChannel();
            channel.value = value;
            channel.texture = texture is Texture2D ? (texture as Texture2D) : null;
            channel.offset = offset;
            channel.scale = scale;

            this.colorChannels[channelName] = channel;
        }
Ejemplo n.º 18
0
        public void EqualSource()
        {
            UnityEngine.Material realMaterial = resultMaterial.Unity3dObject as UnityEngine.Material;

            Assert.AreEqual(material.Name, realMaterial.name);
            Assert.AreEqual(material.Shader, realMaterial.shader.name);
            for (int i = 0; i < material.PropertiesLength; i++)
            {
                Schema.ShaderProperty p = material.GetProperties(i);

                Assert.IsTrue(realMaterial.HasProperty(p.Names));

                switch (p.Type)
                {
                case ShaderPropertyType.Float:
                case ShaderPropertyType.Range:
                {
                    Assert.AreEqual(p.ValueType, ShaderPropertyValue.ShaderPropertyFloat);

                    float originValue     = realMaterial.GetFloat(p.Names);
                    ShaderPropertyFloat f = p.GetValue <ShaderPropertyFloat>(new ShaderPropertyFloat());
                    Assert.AreEqual(f.Value, originValue);
                }
                break;

                case ShaderPropertyType.Color:
                {
                    Assert.AreEqual(p.ValueType, ShaderPropertyValue.ShaderPropertyColor);

                    UnityEngine.Color   originValue = realMaterial.GetColor(p.Names);
                    ShaderPropertyColor c           = p.GetValue <ShaderPropertyColor>(new ShaderPropertyColor());
                    Assert.AreEqual(originValue.a, c.Color.A);
                    Assert.AreEqual(originValue.g, c.Color.G);
                    Assert.AreEqual(originValue.b, c.Color.B);
                    Assert.AreEqual(originValue.r, c.Color.R);
                }
                break;

                case ShaderPropertyType.Vector:
                {
                    Assert.AreEqual(p.ValueType, ShaderPropertyValue.ShaderPropertyVector);

                    UnityEngine.Vector4  originValue = realMaterial.GetVector(p.Names);
                    ShaderPropertyVector v           = p.GetValue <ShaderPropertyVector>(new ShaderPropertyVector());
                    Assert.AreEqual(originValue.x, v.Vector.X);
                    Assert.AreEqual(originValue.y, v.Vector.Y);
                    Assert.AreEqual(originValue.z, v.Vector.Z);
                    Assert.AreEqual(originValue.w, v.Vector.W);
                }
                break;

                case ShaderPropertyType.TexEnv:
                {
                    Assert.AreEqual(p.ValueType, ShaderPropertyValue.ShaderPropertyTexture);
                    //UnityEngine.Texture texture = realMaterial.GetTexture(p.Names);
                    Vector2 offset = realMaterial.GetTextureOffset(p.Names);
                    Vector2 scale  = realMaterial.GetTextureScale(p.Names);

                    //这个测试用例不真正装载 texture.
                    //Assert.IsFalse(texture == null);
                    ShaderPropertyTexture t = p.GetValue <ShaderPropertyTexture>(new ShaderPropertyTexture());
                    string texturePath      = resultMaterial.GetTexturePath(t.Name);

                    Assert.IsTrue(realMaterial.HasProperty(p.Names));
                    Assert.IsTrue(dictTextures.ContainsKey(texturePath));
                    Assert.IsNotNull(realMaterial.GetTexture(p.Names));
                    Assert.AreEqual(dictTextures[texturePath].resourceObject.Unity3dObject.GetInstanceID(), realMaterial.GetTexture(p.Names).GetInstanceID());

                    Assert.AreEqual(offset.x, t.Offset.X);
                    Assert.AreEqual(offset.y, t.Offset.Y);
                    Assert.AreEqual(scale.x, t.Scale.X);
                    Assert.AreEqual(scale.y, t.Scale.Y);
                }
                break;
                }
            }
        }
Ejemplo n.º 19
0
        public void AddSingleChannel(string channelName, string propertyName, string texturePropertyName, Material material)
        {
            float value = material.GetFloat(propertyName);
            Texture texture = material.GetTexture(texturePropertyName);
            Vector2 offset = material.GetTextureOffset(texturePropertyName);
            Vector2 scale = material.GetTextureScale(texturePropertyName);

            SingleChannel channel = new SingleChannel();
            channel.value = value;
            channel.texture = texture is Texture2D ? (texture as Texture2D) : null;
            channel.offset = offset;
            channel.scale = scale;

            this.singleChannels[channelName] = channel;
        }
Ejemplo n.º 20
0
        public void AddTextureChannel(string channelName, string texturePropertyName, Material material)
        {
            Texture texture = material.GetTexture(texturePropertyName);
            Vector2 offset = material.GetTextureOffset(texturePropertyName);
            Vector2 scale = material.GetTextureScale(texturePropertyName);

            TextureChannel channel = new TextureChannel();
            channel.texture = texture is Texture2D ? (texture as Texture2D) : null;
            channel.offset = offset;
            channel.scale = scale;

            this.textureChannels[channelName] = channel;
        }
Ejemplo n.º 21
0
        public static ByteBuffer Save(UnityEngine.Material material)
        {
            FlatBufferBuilder builder = new FlatBufferBuilder(InitBufferSize);

            int count = ShaderUtil.GetPropertyCount(material.shader);
            List <Offset <ShaderProperty> > listOffShaderProperties = new List <Offset <ShaderProperty> >();

            for (int i = 0; i < count; i++)
            {
                string name = ShaderUtil.GetPropertyName(material.shader, i);
                ShaderUtil.ShaderPropertyType type      = ShaderUtil.GetPropertyType(material.shader, i);
                Schema.ShaderPropertyValue    valueType = ShaderPropertyValue.NONE;
                int valueOffset = -1;
                switch (type)
                {
                case ShaderUtil.ShaderPropertyType.Color:
                {
                    UnityEngine.Color c = material.GetColor(name);
                    ShaderPropertyColor.StartShaderPropertyColor(builder);
                    ShaderPropertyColor.AddColor(builder, Schema.Color.CreateColor(builder, c.a, c.b, c.g, c.r));
                    Offset <ShaderPropertyColor> offset = ShaderPropertyColor.EndShaderPropertyColor(builder);
                    valueType   = ShaderPropertyValue.ShaderPropertyColor;
                    valueOffset = offset.Value;
                }
                break;

                case ShaderUtil.ShaderPropertyType.Vector:
                {
                    Vector4 v = material.GetVector(name);
                    ShaderPropertyVector.StartShaderPropertyVector(builder);
                    ShaderPropertyVector.AddVector(builder, Schema.Vec4.CreateVec4(builder, v.x, v.y, v.z, v.w));
                    Offset <ShaderPropertyVector> offset = ShaderPropertyVector.EndShaderPropertyVector(builder);
                    valueType   = ShaderPropertyValue.ShaderPropertyVector;
                    valueOffset = offset.Value;
                }
                break;

                case ShaderUtil.ShaderPropertyType.Range:
                case ShaderUtil.ShaderPropertyType.Float:
                {
                    float f = material.GetFloat(name);
                    ShaderPropertyFloat.StartShaderPropertyFloat(builder);
                    ShaderPropertyFloat.AddValue(builder, f);
                    Offset <ShaderPropertyFloat> offset = ShaderPropertyFloat.EndShaderPropertyFloat(builder);
                    valueType   = ShaderPropertyValue.ShaderPropertyFloat;
                    valueOffset = offset.Value;
                }
                break;

                case ShaderUtil.ShaderPropertyType.TexEnv:
                {
                    UnityEngine.Texture t = material.GetTexture(name);
                    string textureName    = "$NULL_TEXTURE";
                    if (t != null)
                    {
                        textureName = AssetDatabase.GetAssetPath(t.GetInstanceID());
                        if (string.IsNullOrEmpty(textureName))
                        {
                            textureName = t.name;
                        }
                        else
                        {
                            textureName = textureName.Substring(ArtWork.path.Length);
                            textureName = System.IO.Path.GetDirectoryName(textureName) + "/" + System.IO.Path.GetFileNameWithoutExtension(textureName);
                        }
                    }
                    Vector2 toffset = material.GetTextureOffset(name);
                    Vector2 tscale  = material.GetTextureScale(name);

                    StringOffset pathOffset = builder.CreateString(textureName);
                    ShaderPropertyTexture.StartShaderPropertyTexture(builder);
                    ShaderPropertyTexture.AddName(builder, pathOffset);
                    ShaderPropertyTexture.AddOffset(builder, Vec2.CreateVec2(builder, toffset.x, toffset.y));
                    ShaderPropertyTexture.AddScale(builder, Vec2.CreateVec2(builder, tscale.x, tscale.y));
                    Offset <ShaderPropertyTexture> offset = ShaderPropertyTexture.EndShaderPropertyTexture(builder);
                    valueType   = ShaderPropertyValue.ShaderPropertyTexture;
                    valueOffset = offset.Value;
                }
                break;
                }

                if (valueOffset >= 0)
                {
                    listOffShaderProperties.Add(
                        ShaderProperty.CreateShaderProperty(
                            builder, builder.CreateString(name), (Schema.ShaderPropertyType)type, valueType, valueOffset
                            ));
                }
            }
            StringOffset             offMaterialName = builder.CreateString(material.name);
            StringOffset             offShader       = builder.CreateString(material.shader.name);
            Offset <Schema.Material> offMaterial     = Schema.Material.CreateMaterial(
                builder, offMaterialName, offShader, Schema.Material.CreatePropertiesVector(builder, listOffShaderProperties.ToArray()));

            builder.Finish(offMaterial.Value);
            return(builder.DataBuffer);
        }
        private BabylonTexture DumpTexture(Texture texture, Material material = null, string name = "")
        {
            if (texture == null)
            {
                return null;
            }
            var texturePath = AssetDatabase.GetAssetPath(texture);
            var textureName = Path.GetFileName(texturePath);
            var babylonTexture = new BabylonTexture { name = textureName };

            if (material != null)
            {
                var textureScale = material.GetTextureScale(name);
                babylonTexture.uScale = textureScale.x;
                babylonTexture.vScale = textureScale.y;

                var textureOffset = material.GetTextureOffset(name);
                babylonTexture.uOffset = textureOffset.x;
                babylonTexture.vOffset = textureOffset.y;
            }

            var texture2D = texture as Texture2D;

            if (texture2D)
            {
                babylonTexture.hasAlpha = texture2D.alphaIsTransparency;

                CopyTexture(texturePath, texture2D, babylonTexture);
            }
            else
            {
                var cubemap = texture as Cubemap;
                if (cubemap != null)
                {
                    CopyTextureCube(texturePath, cubemap, babylonTexture);
                }
            }

            return babylonTexture;
        }
Ejemplo n.º 23
0
 /// <summary>
 /// Gets the values given a material
 /// </summary>
 /// <param name="m">The material</param>
 /// <returns>A StoredValue containing value and type information</returns>
 public List<StoredValue> GetValues(Material m)
 {
     var list = GetShaderProperties(m);
     var output = new List<StoredValue>();
     foreach (var p in list) {
         var o = new StoredValue {
             property = p
         };
         output.Add(o);
         switch (p.type) {
             case MaterialProperty.PropertyType.Color:
                 o.value = new object[1];
                 o.value[0] = m.GetColor(p.name);
                 break;
             case MaterialProperty.PropertyType.Float:
                 o.value = new object[1];
                 o.value[0] = m.GetFloat(p.name);
                 break;
             case MaterialProperty.PropertyType.Range:
                 o.value = new object[1];
                 o.value[0] = m.GetFloat(p.name);
                 break;
             case MaterialProperty.PropertyType.TexEnv:
                 o.value = new object[3];
                 o.value[0] = m.GetTexture(p.name);
                 o.value[1] = m.GetTextureOffset(p.name);
                 o.value[2] = m.GetTextureScale(p.name);
                 break;
             case MaterialProperty.PropertyType.Vector:
                 o.value = new object[1];
                 o.value[0] = m.GetVector(p.name);
                 break;
             default:
                 Debug.LogError("Unsupported type: " + p.type.ToString());
                 break;
         }
     }
     return output;
 }
 // Use this for initialization
 void Start()
 {
     material = GetComponent<Renderer> ().material;
     offset = material.GetTextureOffset (_MAIN_TEXTURE_NAME);
 }
Ejemplo n.º 25
0
    private static KSerializeMaterialProperty _GetShaderTexProp(Material mm, string texProp, float scaleTexture = 1f)
    {
        if (mm.HasProperty(texProp))
        {
            Texture tex = mm.GetTexture(texProp);
            if (tex != null)
            {
                KSerializeMaterialProperty shaderProp = new KSerializeMaterialProperty();

                shaderProp.PropName = texProp;
                if (tex is Texture2D)
                {
                    var texTiling = mm.GetTextureScale(texProp); // 纹理+tiling+offset
                    var texOffset = mm.GetTextureOffset(texProp);
                    var texPath = KDependencyBuild.BuildDepTexture(tex, scaleTexture);
                    shaderProp.Type = KSerializeMaterialProperty.ShaderType.Texture;

                    shaderProp.PropValue = string.Format("{0}|{1}|{2}|{3}|{4}", texPath, texTiling.x, texTiling.y,
                        texOffset.x, texOffset.y);
                }
                else
                {
                    Log.Warning("找到一个非Texture2D, Type:{0} Mat:{1} PropName:{2}", tex.GetType(), mm.name, texProp);
                    shaderProp.Type = KSerializeMaterialProperty.ShaderType.RenderTexture;
                        // Shader的RenderTexture不打包,一般由脚本动态生成
                    shaderProp.PropValue = null;
                }
                return shaderProp;
            }
            else
            {
                Log.Info("[_GetShaderTexProp]处理纹理时发现获取不到纹理, 材质{0}  Shader属性{1}", mm.name, texProp);
                return null;
            }
        }
        return null;
    }
Ejemplo n.º 26
0
	public List<MaterialProperty> GetShaderProperties(Material material)
	{
		if(cache.ContainsKey(material.shader.name))
			return cache[material.shader.name];
		
		var list = new List<MaterialProperty>();
		foreach(var m in MaterialProperties)
		{
			if(material.HasProperty(m.name))
			{
				if(m.type == MaterialProperty.PropertyType.unknown)
				{
					try
					{
						var p = material.GetColor(m.name);
						if(p != transparent)
						    list.Add( new MaterialProperty { name = m.name, type = MaterialProperty.PropertyType.color });
					}
					catch
					{
					}
					try
					{
						var p = material.GetFloat(m.name);
						if(p != 0)
						    list.Add( new MaterialProperty { name = m.name, type = MaterialProperty.PropertyType.real });
					}
					catch
					{
					}
					try
					{
						var p = material.GetTexture(m.name);
						if(p!=null)
						    list.Add( new MaterialProperty { name = m.name, type = MaterialProperty.PropertyType.texture });
					}
					catch
					{
					}
					try
					{
						var p = material.GetVector(m.name);
						if(p != Vector4.zero)
						    list.Add( new MaterialProperty { name = m.name, type = MaterialProperty.PropertyType.vector });
					}
					catch
					{
						
					}
					try
					{
						var p = material.GetMatrix(m.name);
						if(p != Matrix4x4.identity)
						     list.Add( new MaterialProperty { name = m.name, type = MaterialProperty.PropertyType.matrix });
					}
					catch
					{
					}
					try
					{
						var p = material.GetTextureOffset(m.name);
						if(p != Vector2.zero)
						     list.Add( new MaterialProperty { name = m.name, type = MaterialProperty.PropertyType.textureOffset });
					}
					catch
					{
					}
					try
					{
						var p = material.GetTextureScale(m.name);
						if(p != Vector2.zero)
						     list.Add( new MaterialProperty { name = m.name, type = MaterialProperty.PropertyType.textureScale });
					}
					catch
					{
					}
				}
				else
				{
					list.Add(m);
				}

					
			}
			
		}
		cache[material.shader.name] = list;
		return list;
	}
Ejemplo n.º 27
0
    private Vector4 GetTextureTransformation(Material material) {
        Vector2 offset;
        Vector2 scale;

        if (!m_hasParentTexture) {
            offset = m_offset.vector2Value;
            scale = m_scale.vector2Value;
        }
        else {
            offset = material.GetTextureOffset(m_parentTexture);
            scale = material.GetTextureScale(m_parentTexture);
        }

        return new Vector4(offset.x, offset.y, scale.x, scale.y);
    }
Ejemplo n.º 28
0
        public void EqualSource()
        {
            Assert.AreEqual(material.Name, originMaterial.name);
            Assert.AreEqual(material.Shader, originMaterial.shader.name);
            for (int i = 0; i < material.PropertiesLength; i++)
            {
                Schema.ShaderProperty p = material.GetProperties(i);

                Assert.IsTrue(originMaterial.HasProperty(p.Names));

                switch (p.Type)
                {
                case ShaderPropertyType.Float:
                case ShaderPropertyType.Range:
                {
                    Assert.AreEqual(p.ValueType, ShaderPropertyValue.ShaderPropertyFloat);

                    float originValue     = originMaterial.GetFloat(p.Names);
                    ShaderPropertyFloat f = p.GetValue <ShaderPropertyFloat>(new ShaderPropertyFloat());
                    Assert.AreEqual(f.Value, originValue);
                }
                break;

                case ShaderPropertyType.Color:
                {
                    Assert.AreEqual(p.ValueType, ShaderPropertyValue.ShaderPropertyColor);

                    UnityEngine.Color   originValue = originMaterial.GetColor(p.Names);
                    ShaderPropertyColor c           = p.GetValue <ShaderPropertyColor>(new ShaderPropertyColor());
                    Assert.AreEqual(originValue.a, c.Color.A);
                    Assert.AreEqual(originValue.g, c.Color.G);
                    Assert.AreEqual(originValue.b, c.Color.B);
                    Assert.AreEqual(originValue.r, c.Color.R);
                }
                break;

                case ShaderPropertyType.Vector:
                {
                    Assert.AreEqual(p.ValueType, ShaderPropertyValue.ShaderPropertyVector);

                    UnityEngine.Vector4  originValue = originMaterial.GetVector(p.Names);
                    ShaderPropertyVector v           = p.GetValue <ShaderPropertyVector>(new ShaderPropertyVector());
                    Assert.AreEqual(originValue.x, v.Vector.X);
                    Assert.AreEqual(originValue.y, v.Vector.Y);
                    Assert.AreEqual(originValue.z, v.Vector.Z);
                    Assert.AreEqual(originValue.w, v.Vector.W);
                }
                break;

                case ShaderPropertyType.TexEnv:
                {
                    Assert.AreEqual(p.ValueType, ShaderPropertyValue.ShaderPropertyTexture);
                    UnityEngine.Texture texture = originMaterial.GetTexture(p.Names);
                    Vector2             offset  = originMaterial.GetTextureOffset(p.Names);
                    Vector2             scale   = originMaterial.GetTextureScale(p.Names);

                    Assert.IsFalse(texture == null);
                    ShaderPropertyTexture t = p.GetValue <ShaderPropertyTexture>(new ShaderPropertyTexture());

                    Assert.AreEqual(texture.name, t.Name);
                    Assert.AreEqual(offset.x, t.Offset.X);
                    Assert.AreEqual(offset.y, t.Offset.Y);
                    Assert.AreEqual(scale.x, t.Scale.X);
                    Assert.AreEqual(scale.y, t.Scale.Y);
                }
                break;
                }
            }
        }
        private void DumpGlossinessReflectivity(Material material, bool metallic, BabylonPBRMaterial babylonPbrMaterial)
        {
            if (material.HasProperty("_Glossiness"))
            {
                babylonPbrMaterial.microSurface = material.GetFloat("_Glossiness");
            }

            if (metallic)
            {
                if (material.HasProperty("_Metallic"))
                {
                    var metalness = material.GetFloat("_Metallic");
                    babylonPbrMaterial.reflectivity = new float[] { metalness * babylonPbrMaterial.albedo[0],
                        metalness * babylonPbrMaterial.albedo[1],
                        metalness * babylonPbrMaterial.albedo[2] };

                    if (babylonPbrMaterial.albedoTexture != null)
                    {
                        var albedoTexture = material.GetTexture("_MainTex") as Texture2D;
                        if (albedoTexture != null)
                        {
                            var albedoPixels = GetPixels(albedoTexture);
                            var reflectivityTexture = new Texture2D(albedoTexture.width, albedoTexture.height, TextureFormat.RGBA32, false);
                            reflectivityTexture.alphaIsTransparency = true;
                            babylonPbrMaterial.useMicroSurfaceFromReflectivityMapAlpha = true;

                            var metallicTexture = material.GetTexture("_MetallicGlossMap") as Texture2D;
                            if (metallicTexture == null)
                            {
                                for (var i = 0; i < albedoTexture.width; i++)
                                {
                                    for (var j = 0; j < albedoTexture.height; j++)
                                    {
                                        albedoPixels[j * albedoTexture.width + i].r *= metalness;
                                        albedoPixels[j * albedoTexture.width + i].g *= metalness;
                                        albedoPixels[j * albedoTexture.width + i].b *= metalness;
                                        albedoPixels[j * albedoTexture.width + i].a = babylonPbrMaterial.microSurface;
                                    }
                                }
                            }
                            else
                            {
                                var metallicPixels = GetPixels(metallicTexture);
                                for (var i = 0; i < albedoTexture.width; i++)
                                {
                                    for (var j = 0; j < albedoTexture.height; j++)
                                    {
                                        var pixel = albedoPixels[j * albedoTexture.width + i];
                                        var metallicPixel = metallicPixels[j * albedoTexture.width + i];
                                        albedoPixels[j * albedoTexture.width + i].r *= metallicPixel.r;
                                        albedoPixels[j * albedoTexture.width + i].g *= metallicPixel.r;
                                        albedoPixels[j * albedoTexture.width + i].b *= metallicPixel.r;
                                        albedoPixels[j * albedoTexture.width + i].a = metallicPixel.a;
                                    }
                                }
                            }

                            reflectivityTexture.SetPixels(albedoPixels);
                            reflectivityTexture.Apply();

                            var textureName = albedoTexture.name + "_MetallicGlossMap.png";
                            var babylonTexture = new BabylonTexture { name = textureName };
                            var textureScale = material.GetTextureScale("_MainTex");
                            babylonTexture.uScale = textureScale.x;
                            babylonTexture.vScale = textureScale.y;

                            var textureOffset = material.GetTextureOffset("_MainTex");
                            babylonTexture.uOffset = textureOffset.x;
                            babylonTexture.vOffset = textureOffset.y;

                            var reflectivityTexturePath = Path.Combine(Path.GetTempPath(), textureName);
                            File.WriteAllBytes(reflectivityTexturePath, reflectivityTexture.EncodeToPNG());
                            babylonScene.AddTexture(reflectivityTexturePath);
                            if (File.Exists(reflectivityTexturePath))
                            {
                                File.Delete(reflectivityTexturePath);
                            }

                            babylonPbrMaterial.reflectivityTexture = babylonTexture;
                        }
                    }
                    //else
                    //{
                    //      TODO. Manage Albedo Cube Texture.
                    //}
                }
            }
            else
            {

                if (material.HasProperty("_SpecColor"))
                {
                    babylonPbrMaterial.reflectivity = material.GetColor("_SpecColor").ToFloat();
                }
                babylonPbrMaterial.reflectivityTexture = DumpTextureFromMaterial(material, "_SpecGlossMap");
                if (babylonPbrMaterial.reflectivityTexture != null && babylonPbrMaterial.reflectivityTexture.hasAlpha)
                {
                    babylonPbrMaterial.useMicroSurfaceFromReflectivityMapAlpha = true;
                }
            }
        }
Ejemplo n.º 30
0
		void CopyTextureTilingOffset(string propName, string targetPropName, Material sourceMaterial, Material targetMaterial) {
			if (sourceMaterial.HasProperty(propName) && targetMaterial.HasProperty(targetPropName)) {
				targetMaterial.SetTextureScale(targetPropName, sourceMaterial.GetTextureScale(propName));
				targetMaterial.SetTextureOffset(targetPropName, sourceMaterial.GetTextureOffset(propName));
			}
		}
Ejemplo n.º 31
0
 // Use this for initialization
 void Start()
 {
     myMeshRenderer = GetComponent<MeshRenderer> ();
     myMat = myMeshRenderer.material;
     origOffset = myMat.GetTextureOffset ("_MainTex");
 }