碧血红天的HomePage

U3d杂谈 — 顶点纹理动画精度问题

最近再使用顶点纹理动画制作动画效果,目的是为了节省资源的消耗。但是当使用一个颜色通道存储一个Mesh顶点位置坐标的时候,发现精度是不够的,毕竟一个RGBA32的每个通道的分量是0-255。导致精度是有限的。所以这里使用一个RGBA 4个通道存储一个顶点的一个方向的坐标值。

一个float是32位的,一个RGBA32也是32位的,刚刚好能够高精度的存储下来。

基本思想,我们把32位的浮点数当做4个byte数据,存储再RGBA的4个通道中,然后在Shader中通过RGBA的4个通道的数据还原成一个float数据。

首先,float转RGBA的方法如下:

    public Color FloatToColor(float f)
    {
        byte[] buffer = BitConverter.GetBytes(f);
        int r = buffer[0];
        int g = buffer[1];
        int b = buffer[2];
        int a = buffer[3];

        return new Color((float)r / 255, (float)g / 255, (float)b / 255, (float)a / 255);
    }

在C#中把4个Byte转成Float的方法如下:

    public float ColorToFloat(Color c)
    {
        byte r = (byte)(c.r * 255);
        byte g = (byte)(c.g * 255);
        byte b = (byte)(c.b * 255);
        byte a = (byte)(c.a * 255);

        List<byte> tempList = new List<byte>();
        tempList.Add(r);
        tempList.Add(g);
        tempList.Add(b);
        tempList.Add(a);

        byte[] buffer = tempList.ToArray();
        float f = (float)BitConverter.ToSingle(buffer, 0);
        return f;
    }

但是,我们是在Shader中提取颜色,然后把一个颜色转换成一个Float数据类型,Shader中可没有方便的BitConverter方法使用,那就的我们自己通过Float的实现标准来换算出来。

//m0 是最高位  m3是最低位
float b2f(float m0, float m1, float m2, float m3)
    {
        // 求符号位
        float sig = 1;
        if (m3 >= 128.0f)
            sig = -1.0f;

        //求阶码
        float jie = 0f;
        if (m3 >= 128.0f)
        {
            jie = m3 - 128.0f;
        }
        else
        {
            jie = m3;
        }
        jie = jie * 2.0f;
        if (m2 >= 128.0f)
            jie += 1.0f;
        jie -= 127.0f;

        //求尾码

        float tail = 0.0f;
        if (m2 >= 128.0f)
            m2 -= 128.0f;
        tail = m0 + (m1 + m2 * 256.0f) * 256.0f;
        tail = (tail) / 8388608;   //   8388608 = 2^23

        float f = sig * Mathf.Pow(2.0f, jie) * (1 + tail);

        return f;
    }

然后,我们在采样的时候,3个像素值来表示一个坐标,方法如下:

v2f vert (appdata v,uint vid:SV_VertexID)
            {
                float animMap_xx = vid*3 * _AnimMap_TexelSize.x + _AnimMap_TexelSize.x / 2;
                float animMap_xy = (vid*3+1) * _AnimMap_TexelSize.x + _AnimMap_TexelSize.x / 2;
                float animMap_xz = (vid*3+2) * _AnimMap_TexelSize.x + _AnimMap_TexelSize.x / 2;
                int curFrame = int(_FrameTime %_AnimFrameCount);
                float animMap_y = (curFrame + 0.5) * _AnimMap_TexelSize.y;
                float4 pos_x = tex2Dlod(_AnimMap, float4(animMap_xx, animMap_y, 0, 0));
                float4 pos_y = tex2Dlod(_AnimMap, float4(animMap_xy, animMap_y, 0, 0));
                float4 pos_z = tex2Dlod(_AnimMap, float4(animMap_xz, animMap_y, 0, 0));
                float4 pos;
                pos.x = b2f(pos_x.x *255 , pos_x.y * 255, pos_x.z * 255, pos_x.w * 255) ;
                pos.y = b2f(pos_y.x *255 , pos_y.y * 255, pos_y.z * 255, pos_y.w * 255) ;
                pos.z = b2f(pos_z.x*255 , pos_z.y * 255, pos_z.z * 255, pos_z.w * 255) ;
                pos.w =1;
                
                v2f o;
                o.vertex = UnityObjectToClipPos(pos);

                o.uv = v.uv;
                return o;
            }

174 评论

  1. ledipasvir sofosbuvir generic cost free harvoni treatment generic harvoni

  2. hi opp ggeis 2022 ert go fi

  3. Good day! This post couldn’t be written any
    better! Reading this post reminds me of my previous room
    mate! He always kept talking about this. I will forward this article to
    him. Fairly certain he will have a good read.
    Thanks foor sharing!

    Here is my homepage; diagram maker free download – Jonah

  4. asunaprevir daclatasvir daklinza genotype 3 daclatasvir side effects

  5. help paying for lyrica lyrica canada lyrica 50mg

  6. Appreciaqte it for this post, I am a big big fan of this website would like to proceed updated.

    Feell free to surf to my web blog answer cppc exam
    (https://aaasjournal.net/)

  7. I regard somethuing genuinely special iin this web site.

    Feel free to surf to my website exasm materials preloaded (Sherman)

  8. Thank youu a lot for sharing this with all people you actually recognize what you aare
    talking approximately! Bookmarked. Kindly also discuss with my web sitge =).

    We may have a hyperlink trade contract between us!

    Also viskt my webpage :: police wrritten exam (sens-lab.org)

  9. Hmm is anyone elpse experiencing probems with the pictures on this blog loading?
    I’m trying too find out if its a problem on my end or if it’s thhe blog.
    Any feedback would be greatly appreciated.

    Feel free to visit my homepage: cpc exam questions, Amado,

  10. We are a gaggle of volunteers and opening a brand new scheme in oour community.
    Your sie offered us with valuable info to work on. You have perfrmed an impressive task and our entire community wilol likely be grateful to you.

    Visit my webpage pmp software (Joy)

  11. I have recently started a web site, the info you provide on this
    website has helped me greatly. Thanks for all off
    your time & work.

    Revie my website cisco certified (akuraschools.org)

  12. Lovingg the info oon this site, you have done
    great job on tthe articles.

    My web blog – police exam (Flossie)

  13. Undeniably consider that which you stated. Your favorite justification seemed to be on the internet the easiest thing to take into accout of. I say to you, I certainly get irked even as folks consider issues that they plainly do not realize about. You controlled to hit the nail upon the top as smartly as defined out the whole thing with no need side-effects, other people can take a signal. Will probably be back to get more. Thank you

    https://www.deviantart.com/ruddle-digital/art/SEO-Services-UK-922584056 – deviantart.com

  14. Thanks for this post, I am a big big fan of this
    internet site would like to keep updated.

    Review my webpage :: cisco ccna exam, Luz,

  15. I have been absent for a while, but nnow I remember
    why I used to love this blog. Thank you, I’ll try and
    check back more frequently. Hoow frequently you update your website?

    Feel free to surf to my wesite :: bar exam preparation (https://aaasjournal.net/)

  16. I have recently started a site, the info you provide on this site has helped me greatly.

    Thanks for all of your time & work.

    My page: module exams (https://sens-lab.org)

  17. We’re a group of volunteers and opening a brand new scheme in our
    community. Yourr website provided us with valuable info
    to work on. You have done an impressive task and our entire
    group will likely be thankful tto you.

    Here is my blog – exazm questions (Kristan)

  18. Super-Duper website! I am loving it!! Willl be back later to read some more.
    I am bookmarking your feedxs also

    Feel free to surf to my web blog … exzam material typically – stphelps.org

  19. I am sure this piece of writing has touched alll the internet visitors, its really really pleasant post on building upp new weblog.

    Also visit my web blog; pmp simulator – Trent

  20. Loving the info on this website, you have
    done great job on tthe bllg posts.

    Feel free to visit my web site :: police jobs [aaasjournal.net]

  21. Thank you, I have recently been searching for info approximately this subject for a while and yours is the greatest I have came upon so far.
    However, what concerning the conclusion? Are you sure about the
    source?

    Look at my web blog – pmp 35 pdu (Margarito)

  22. tadalafil sublingual tablet 20mg cialis daily online viagra levitra cialis which works better?

  23. tadalafil 20 mg dosage cialis side effects forum canadian drugstore cialis

  24. cheap generic viagra from india viagra 50 mg tablet online purchase cheap viagra mastercard

  25. where to buy viagra over the counter sildenafil 50 mg online uk cheap viagra uk

  26. tadalafil without a prescription cialis canada pharmacy tadalafil and blood pressure

  27. people’s pharmacy synthroid real viagra online canadian pharmacy popular prescription drugs

  28. giant pharmacy reliable rx pharmacy walmart pharmacy enalapril

  29. walgreens pharmacy near me canadian pharmacy east bay largo fl cedarwood pharmacy canada

  30. canadian pharmacy viagra cialis troy pharmacy tramadol cod compounding pharmacy viagra

  31. optum rx pharmacy help desk canadian pharmacy locations most reputable canadian pharmacy

  32. fry’s pharmacy la canada and lambert prometrium canadian pharmacy cost of levitra at walmart pharmacy

  33. pharmacy viagra sans ordonnance canadian pharmacy azithromycin effexor xr pharmacy

  34. I am impressed with this site, real I am a big fan.

    Here is my wweb page; certificatiin questions (Inge)

  35. purchase viagra over the counter best prices for viagra prescription coupon sildenafil 20mg

  36. discount online viagra generic viagra no prescription viagra otc in canada

  37. cialis viagra levitra canada get cialis online cialis samples free

  38. generic viagra pills for sale cheap viagra free shipping viagra tablet

  39. generic viagra online fast delivery viagra pills online canada where can i get female viagra uk

  40. genuine viagra canada purchase genuine viagra generic viagra – mastercard

  41. what is cialis black 800mg cialis 36 hour online super tadalafil with dapoxetine

  42. tadalafil generico tadalafil order buy cialis no prescription

  43. cialis 5mg no prescription online cialis canada cialis canada over the counter

  44. brands of generic tadalafil cialis contraindications generic tadalafil canada

  45. cialis otc usa when will generic cialis be available cialis australia express shipping

  46. sublingual tadalafil buy cialis 20 mg tadalafil alternative

  47. generic sildenafil online sildenafil 100mg buy online viagra tablet

  48. I’m no longer positive where you’re getting
    your info, but good topic. I must spend some time studying much more or understanding more.
    Thank yyou for great info I used to be searching for
    this info for my mission.

    Also visit my web-site – written driving exam (Reyes)

  49. how much cialis should i take cialis daily vs regular cialis walmart price for cialis

  50. Hmm is anyone else encountering problems with the pictures on this blog loading?
    I’m trying to figure out if its a problem on my end or if it’s
    the blog.Any suggestions would be greatly appreciated.

    My webpage: ppmp software (Nannette)

  51. furosemide 40 mg para que sirve what is furosemide what is lasix

  52. metformin melasma metformin analysis dauerdurchfall metformin

  53. lisinopril lexapro lisinopril omeprazole lisinopril breastfeeding

  54. utilisation flagyl metrogyl flagyl metronidazole image

  55. tenormin ivz atenolol respiratory atenolol spironolactone

  56. furosemide journals lasix insert lasix rash

  57. atenolol mood atenolol thirsty atenolol therabloc

  58. tamoxifen behandlingstid tamoxifen innemen nolvadex laws

  59. lasix medicines lasix cad adjusting lasix

  60. synthroid pronunciation synthroid medicinenet administering synthroid

  61. pregabalin withdrawal reviews what does lyrica do to you lyrica dose

  62. trazodone haldol trazodone white trazodone venezuela

  63. furosemide werkingsduur lasix shape lasix msds

  64. synthroid sudafed warfarin synthroid aspirin synthroid

  65. pregabalin good rx how much lyrica to get high pregabalin for seizures

  66. bactrim valtrex malarone valtrex valtrex hepatitis

  67. metformin liraglutide metformin nierenversagen metformin gynaecology

  68. bnf lisinopril lisinopril laryngitis lisinopril vs avalide

  69. tenormin and aspirin tenormin safe dose atenolol adderall

  70. trazodone pharmacodynamics trazodone barr trazodone agoraphobia

  71. nejm atorvastatin lipitor obat atorvastatin potenz

  72. atenolol pheochromocytoma atenolol concentration atenolol exhaustion

  73. lisinopril mental health lisinopril swallowing sinopren lisinopril

  74. metformin hwz metformin cream metformin koloskopie

  75. synthroid mania organic synthroid synthroid overdoses

  76. pregabalin 50mg pregabalin 25 mg price pregabalin 150 mg cost

  77. neurontin and citalopram neurontin and hypertension gabapentin powder

  78. I’ll right away snatch your rsss ass I can’t
    find your email subscription hyperlink or newsletter service.
    Do you’ve any? Please permit me understand in order
    that I may just subscribe. Thanks.

    Also visit my web site exam material (Ferne)

  79. metronidazole pgd flagyl lepistes flagyl apresentacoes

  80. ivermectin dog dosage pyrantel ivermectin stromectol 3 mg tablet

  81. pregabalin 100mg capsules best dose of pregabalin for anxiety side effects of lyrica

  82. occipital neuralgia neurontin gabapentin greyhound neurontin causes neuropathy

  83. I think the admin of this site is truly working hard in favor of his website, for the reason that here every data is quality based stuff.
    then stake it in Syrup Pools to earn more tokens! Win millions in prizes

  84. comparative essay example write my essay for cheap the best way to ensure parallel construction in your essay is to use similar

  85. essay proposal example college admission essay help character analysis essay example

  86. thesis statement in an essay sat essay tips respond essay

  87. essay writer software citations in an essay essay title page

  88. essay writing website essay help online essay writers

  89. En iyi takipç isitesinden takipçi satın al ucuza ;https://rebrand.ly/2d8aee

  90. best college essay service editing essay services college essay help nyc

  91. auto essay writer cyber bullying essay buy cheap essays

  92. where to buy college research papers writing a research paper in apa format professional paper writing services

  93. academic essay help what is a claim in an essay custom essay writing canada

  94. decorative writing paper printable horizontal writing paper custom written paper

  95. which of the following are best practices when writing the first draft of a research paper? writing a college paper help with writing a paper for college

  96. printing writing paper writing paper for kindergarten imagine you are writing a research paper on social networking sites which of the following thesis

  97. chinese writing paper lined paper for second grade writing write my paper for money

  98. primary writing paper with picture box free writing paper to print writing paper 1st grade

  99. online paper writing service paper writing sites printable writing paper with border

  100. thesis paper writing service designed paper for writing printable free writing paper with picture box pdf

  101. literature review for dissertation dissertation proposal digital dissertations

  102. ivermectin tractor supply pyrantel ivermectin ivermectin side effects in dogs

  103. best dissertation services cornell dissertation how many pages should a dissertation be

  104. rackham dissertation deadlines dissertation proposal writing service dissertation examples in education

  105. do my papers bee writing paper professional paper writer

  106. higher education dissertation topics how long is a dissertation for phd dissertation literature review

  107. college paper writers lined writing paper templates buying papers online

  108. citing a dissertation mla doctoral dissertation writing help acls dissertation completion fellowship

  109. dissertation help reviews dissertation topic how to ask someone to be on your dissertation committee

  110. dissertation reviewer how long to write dissertation e thesis and dissertation

  111. help writing dissertation proposal dissertation topics writing your dissertation in 15 minutes a day

  112. stromectol ivermectin for dogs ivermectin toxicity in dogs

  113. admission essay writing services cheap essay service custom essays no plagiarism

  114. assignment essay help custom essay writing reviews write my essay today

  115. help me write an essay buy essays for college college admission essay help

  116. online essay writing help help essay 123 help for essay writing

  117. best essay writers online recommended essay writing service will someone write my essay for me

  118. top rated essay writing services websites for essay writing essay introduction help

  119. can someone write my essay online essay writing help essay help websites

  120. essay revision help online what are good essay writing services help write my essay

  121. stromectol over the counter ivermectin pour on oral ivermectin for rosacea

  122. personal essay help best essay for you cheap essay writing service usa

发表评论